Java Program Basic Codes
Java Program Basic Codes
Java Program Basic Codes
1 & 2
• Program Code:
class pr1 {
public static void main(String args[]) {
System.out.println("Java test run");
}
}
• Output:
Practical No. 3
• Program Code:
class pr2a {
}
•
else if(y > x && y > z) {
•
System.out.print(y+" is the largest nmuber.");
•
}
•
else {
•
System.out.print(z+" is the largest number.");
•
}
• Output:
Practical No. 3
• Program Code:
class pr2 {
public static void main(String args[]) {
int x = 17;
if(x % 2 == 0) {
System.out.print(x +" is an even number.");
}
else {
System.out.print(x +" is an odd number.");
}
}
}
• Output:
Practical No. 4
Program Code:
class pr4 {
switch(input) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
System.out.println(input+" is a vowel.");
break;
default:
• Output:
Practical No. 4 • Output:
• Program Code:
class pr4 {
public static void main(String args[])
{
int input = 4;
switch(input) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid Input");
}
}
}
Practical No. 5
• Program Code:
public class pr5a {
public static void main(String[] args) {
int numRows = 5;
System.out.println();
}
}
}
• Output:
Practical No. 5 • Output:
• Program Code:
class pr5 {
public static void main(String[]
args) {
// Initialize an array with
predefined values
int[] numbers = {5, 8, -3, 10, 2};
• Program Code:
import java.util.Scanner;
• Output:
class pr6 {
public static void main(String[]
args) {
Scanner scanner = new
Scanner(System.in);
do {
System.out.print("Enter an
even number: ");
num1 = scanner.nextInt();
System.out.print("Enter an
odd number: ");
num2 = scanner.nextInt();
if (!isValid) {
System.out.println("Invalid
input! Please enter one even
number and one odd number.");
}
} while (!isValid);
System.out.println("You
entered a valid pair of numbers: " +
num1 + " and " + num2);
scanner.close();
}
Practical No. 6
• Program Code:
class pr6a {
public static void main(String args[])
{
int x = 1;
do {
System.out.println(x);
x++;
}while(x<=50);
}
}
• Output:
Practical No. 10 System.out.println();
System.out.println("Details of
object created using default
constructor:");
defaultConstructorObj.displayDetail
s();
Practical No. 10 class Pr10a {
public static void main(String[]
• Program Code: args) {
ComplexNumber complex1 =
class ComplexNumber {
new ComplexNumber();
private double real;
ComplexNumber complex2 =
private double imaginary;
new ComplexNumber(2.5, 3.7);
public ComplexNumber() {
ComplexNumber complex3 =
this.real = 0.0;
new ComplexNumber(complex2);
this.imaginary = 0.0;
System.out.println("Complex
}
number 1:");
public ComplexNumber(double
complex1.display();
real, double imaginary) {
System.out.println("Complex
this.real = real;
number 2:");
this.imaginary = imaginary;
complex2.display();
}
System.out.println("Complex
public
number 3 (copy of complex number
ComplexNumber(ComplexNumber
2):");
other) {
complex3.display();
this.real = other.real;
ComplexNumber sum =
this.imaginary =
complex1.add(complex2);
other.imaginary;
System.out.println("Sum of
}
complex number 1 and complex
public ComplexNumber
number 2:");
add(ComplexNumber other) {
sum.display();
double sumReal = this.real +
}
other.real;
}
double sumImaginary =
this.imaginary + other.imaginary; • Output:
return new
ComplexNumber(sumReal,
sumImaginary);
}
public void display() {
System.out.println(real + " + " +
imaginary + "i");
}
}
Practical No. 28
• Program Code:
import java.applet.Applet;
import java.awt.Graphics;
• Output:
Practical No. 28 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Graphics;
• Program Code:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Polygon;
g.drawPolygon(polygon);
}
}
/*<applet code="pr29.class"
width="300" height="300">
</applet>*/
Practical No. 29 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
g.setColor(Color.YELLOW);
g.fillOval(100, 100, 200, 200);
g.setColor(Color.BLACK);
g.fillOval(150, 150, 30, 30);
g.fillOval(220, 150, 30, 30);
g.setColor(Color.RED);
g.fillOval(190, 200, 20, 20);
g.setColor(Color.BLACK);
g.drawArc(150, 220, 100, 80,
180, 180);
}
}
/*<applet code="pr29a.class"
width="400" height="400">
</applet>*/
Practical No. 30 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
g.setColor(Color.BLUE);
g.drawRect(600,100,200,200);
g.drawRect(700,200,200,200);
g.drawLine(600,100,700,200);
g.drawLine(800,100,900,200);
g.drawLine(600,300,700,400);
g.drawLine(800,300,900,400);
g.setColor(Color.RED);
g.drawOval(300,60,280,60);
g.drawLine(300,90,430,280);
g.drawLine(430,280,580,90);
}
}
/*<applet code="pr30.class"
width="400" height="400">
</applet>*/
Practical No. 30 • Output:
• Program Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
g.setColor(Color.BLUE);
g.fillRect(50, 50, 200, 200);
g.setColor(Color.RED);
g.fillOval(50, 50, 200, 200);
}
}
/*<applet code="pr30a.class"
width="300" height="300">
</applet>*/
Practical No. 31 & 32
• Program Code: • Output:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
int character;
System.out.println("Characters
copied from " + inputFile + " to " +
outputFile + " successfully.");
} catch (IOException e) {
System.err.println("Error: " +
e.getMessage());
}
}
}
Practical No. 31 & 32 • Output:
• Program Code:
import java.io.FileOutputStream;
import java.io.IOException;
try (FileOutputStream
outputStream = new
FileOutputStream(fileName)) {
// Data to be written to the
file (example: byte array)
byte[] data = {72, 101, 108,
108, 111, 32, 87, 111, 114, 108,
100};
System.out.println("Bytes
written to " + fileName + "
successfully.");
} catch (IOException e) {
System.err.println("Error: " +
e.getMessage());
}
}
}
PR 13 . Develop a program for System.out.println("Length of the array:
implementation of Array in Java. " + length);
• Program Code : }
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
System.out.println("Element at index 0:
" + numbers[0]);
System.out.println("Element at index 1:
" + numbers[1]);
System.out.println("Element at index 2:
" + numbers[2]);
System.out.println("Element at index 3:
" + numbers[3]);
System.out.println("Element at index 4:
" + numbers[4]);
• Program Code :
System.out.println("Array elements:");
System.out.println(number);
}
PR 14 . Develop a program for // Display elements of the Vector after
implementation of Vector in Java. removal
System.out.println(number);
public class VectorExample {
}
public static void main(String[] args) {
}
// Create a Vector of integers
}
Vector<Integer> numbers = new
Vector<>(); • Output :
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
System.out.println("Vector elements:");
System.out.println(number);
numbers.removeElement(30);
Exercise PR 14. Write a program to System.out.println("Does the Vector
use different methods of vector class. contain 'Banana'? " +
vector.contains("Banana"));
• Program Code :
import java.util.Vector;
// Remove an element by index
System.out.println("Removed element
public class VectorMethodsExample { at index 2: " + vector.remove(2));
public static void main(String[] args) {
System.out.println("Vector elements
vector.add("Apple"); after removal: " + vector);
vector.add("Banana");
vector.clear();
System.out.println("Element at index 1:
" + vector.get(1));
• Program Code :
// Parent class
class Animal {
System.out.println("Animal makes a
sound");
@Override
System.out.println("Dog barks");
myDog.makeSound();
}
Exercise PR 17. Develop a program to • Output :
extend ‘dog’ from ‘animal’ to override
‘move()’ method using super keyword.
• Program Code :
// Parent class
class Animal {
// Method to move
System.out.println("Animal moves");
super.move();
myDog.move();
}
PR 18. Develop a program for public class InheritanceExample {
implementation of Single and
public static void main(String[] args) {
Multilevel inheritance.
// Single Inheritance
• Program Code :
Dog myDog = new Dog();
myLabrador.bark(); // Accessing
// Subclass inheriting from Animal class method of parent class
(Single Inheritance) myLabrador.color(); // Accessing
class Dog extends Animal { method of child class
System.out.println("Labrador is black in
color");
}
Exercise PR 18. Develop a program to }
calculate the room area and volume to
public class RoomExample {
illustrate the concept of single
inheritance . public static void main(String[] args) {
this.width = width;
• Output :
this.height = height;
}
PR 19. Develop a program for public class MultipleInheritanceExample {
implementation of multipe
public static void main(String[] args) {
inheritance.
// Create an object of Dog
• Program Code :
Dog myDog = new Dog();
// Interface for LivingBeing
interface LivingBeing {
// Call methods from interfaces
void breathe();
myDog.breathe();
}
myDog.eat();
@Override
System.out.println("Dog breathes");
@Override
System.out.println("Dog eats");
System.out.println("Dog barks");
}
Exercise PR 19. Develop a program to }
find area of rectangle and circle using
}
interfaces.
• Program code :
public class AreaCalculationExample {
// Interface for Shape
public static void main(String[] args) {
interface Shape {
// Create objects of Rectangle and
double calculateArea(); Circle
}
Rectangle rectangle = new Rectangle(5,
// Class representing Rectangle 4);
double length;
this.radius = radius;
// Implementation of calculateArea
method for Circle
@Override
System.out.println("Thread: " +
Thread.currentThread().getId() + " Value: " +
i);
thread1.start();
thread2.start();
}
Exercise PR 21& 22. Implement
multithreading to perform
public class MultithreadingExample {
simultaneous processes.
public static void main(String[] args) {
• Program :
// Create and start two threads for each
class Process1 extends Thread { process
public void run() {
Process1 process1 = new Process1();
for (int i = 0; i < 5; i++) { Process2 process2 = new Process2();
System.out.println("Process 1 is
process1.start();
running...");
process2.start();
try {
}
Thread.sleep(1000); // Sleep for 1
second }
} catch (InterruptedException e) {
e.printStackTrace(); • Output :
}
System.out.println("Process 2 is
running...");
try {
} catch (InterruptedException e) {
e.printStackTrace();