Java Practical Programs
Java Practical Programs
Practical No.3 :
int x = 10;
int y = 5;
if (x > y) {
if (x < 15) {
if (y != 0) {
System.out.println("y is not equal to zero");
if (x % 2 == 0) {
System.out.println("x is even");
OUTPUT :
x is greater than y
x is less than 15
x is even
Practical No.4 :
String dayName;
switch (dayNumber) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
break;
scanner.close();
}
OUTPUT :
Practical No.5 :
OUTPUT :
Argument 1: arg1
Argument 2: arg2
Argument 3: arg3
PRACTICAL NO.6 :
char choice;
do {
double result;
switch (operator) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
if (num2 != 0) {
} else {
break;
default:
System.out.println("Invalid operator!");
break;
}
// Prompt the user to continue or not
choice = scanner.next().charAt(0);
scanner.close();
OUTPUT :
Result: 8.0
10
Result: 5.0
OUTPUT :
int value: 10
}
OUTPUT :
int value: 10
PRACTICAL NO.10 :
public Car() {
make = "Unknown";
model = "Unknown";
year = 0;
// Parameterized constructor
this.make = make;
this.model = model;
this.year = year;
}
public static void main(String[] args) {
car1.displayDetails();
System.out.println();
car2.displayDetails();
OUTPUT :
Details of Car 1:
Make: Unknown
Model: Unknown
Year: 0
Details of Car 2:
Make: Toyota
Model: Camry
Year: 2022
// Declaring a string
// 10. indexOf(int ch): returns the index of the first occurrence of the
specified character
// 13. split(String regex): splits the string using the specified regular
expression
System.out.println(part.trim());
/* Output:
Hello
World!
*/
// 14. startsWith(String prefix): checks if the string starts with the specified
prefix
// 15. endsWith(String suffix): checks if the string ends with the specified
suffix
OUTPUT :
//Output is provided in the code itself in the form of comments.
PRACTICAL NO.13 :
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
OUTPUT :
123
456
789
PRACTICAL NO.14 :
Write a program to insert different elements in the Vector and display
them.
import java.util.Vector;
// Creating a Vector
vector.add("Hello");
vector.add(123);
vector.add(3.14);
vector.add(true);
System.out.println(element);
}
}
OUTPUT :
Hello
123
3.14
true
// hashCode(): returns the hash code value for this Integer object
}
}
OUTPUT :
Value of num1: 10
Comparison result: -1
Max value: 20
Min value: 10
PRACTICAL NO.17 :
class SuperClass {
@Override
super.display();
subObj.display();
OUTPUT :
PRACTICAL NO.18 :
class Animal {
System.out.println("Animal is eating");
System.out.println("Dog is barking");
// Single Inheritance
System.out.println();
// Multilevel Inheritance
}
OUTPUT :
Animal is eating
Dog is barking
Animal is eating
Dog is barking
PRACTICAL NO.19 :
// First interface
interface Animal {
// Second interface
interface Vehicle {
@Override
System.out.println("AnimalVehicle is eating");
@Override
System.out.println("AnimalVehicle is driving");
}
public class MultipleInheritanceExample {
animalVehicle.eat();
animalVehicle.drive();
OUTPUT :
AnimalVehicle is eating
AnimalVehicle is driving
PRACTICAL NO.20 :
Write a program to implement user defined packages in terms of
creating a new package and importing the same.
package mypackage;
/*Now, let's create another Java file outside the mypackage directory to
demonstrate how to use this package.*/
import mypackage.MyClass;
obj.display();
javac mypackage/MyClass.java
javac PackageDemo.java
java PackageDemo
OUTPUT :
This is MyClass in mypackage
import java.util.concurrent.*;
class Order {
this.itemName = itemName;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
this.order = order;
@Override
executor.submit(staff1);
executor.submit(staff2);
executor.submit(staff3);
executor.shutdown();
OUTPUT :
import java.util.Scanner;
try {
} catch (ArithmeticException e) {
} catch (Exception e) {
} finally {
scanner.close();
System.out.println("Scanner closed.");
OUTPUT :
Result of division: 5
Scanner closed.
PRACTICAL NO.26 & 27 :
USING THROW :
super(message);
class Voter {
try {
voter.validateAge(17);
} catch (AgeValidationException e) {
OUTPUT :
USING THROWS :
import java.io.FileNotFoundException;
import java.io.FileReader;
try {
readFile("example.txt");
} catch (FileNotFoundException e) {
}
OUTPUT :
PRACTICAL NO.28 :
import java.applet.Applet;
import java.awt.Graphics;
OUTPUT :
import java.applet.Applet;
import java.awt.Graphics;
// Initialization code
// Stop code
// Cleanup code
OUTPUT :
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
setBackground(Color.white);
}
public void start() {
try {
while (true) {
x += 5;
repaint();
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
public void paint(Graphics g) {
g.setColor(Color.blue);
OUTPUT :
PRACTICAL NO.30 :
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
// Draw cone
g.setColor(Color.red);
// Draw cube
g.setColor(Color.green);
// Draw cylinder
g.setColor(Color.blue);
g.fillOval(400, 50, 100, 50);
g.setColor(Color.white);
OUTPUT :
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
try {
outputStream.write("Hello, World!".getBytes());
outputStream.close();
int data;
while ((data = inputStream.read()) != -1) {
System.out.print((char) data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
OUTPUT :
Hello, World!
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
try {
writer.write("Hello, World!");
writer.close();
int data;
System.out.print((char) data);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
OUTPUT :
Hello, World!