Java Programs
Java Programs
Lab File
OOPS BY JAVA LAB(KCA-251)
Question 01: - WAP to find the greatest among three using abstract method.
Solution: -
abstract class Number{
abstract void Check();
int a=10,b=8,c=25;
int Display(){
if (a > b && a > c)
System.out.println("Greatest Number is: " + a); Output: -
else if (b > a && b > c) Greatest Number is:
System.out.println("Greatest Number is: " + b); 25
else
System.out.println("Greatest Number is: " + c);
return 0;
}
}
public class GreatestNo{
public static void main(String[] args){
Number G = new Number(){
void Check(){}
};
G.Display();
}
}
Question 02: - WAP to check whether any rectangle is square or not using parameterized constructor.
Solution: -
class MyRectangle {
private int l;
private int w;
public MyRectangle(int l, int w) { Output: -
this.l = l; The rectangle is not a square.
this.w = w;
}
public boolean isSquare() {
return l == w;
}
}
class ArrayUtils {
public int[] removeDuplicates(int[] array) {
Set<Integer> uniqueElements = new LinkedHashSet<>();
for (int element : array) {
uniqueElements.add(element);
}
int[] resultArray = new int[uniqueElements.size()]; Output: -
int i = 0; Array without duplicates: [1, 2, 3, 4, 5, 6]
for (Integer element : uniqueElements) {
resultArray[i++] = element;
}
return resultArray;
}
}
public class RemoveDuplicate {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 2, 3, 1, 5, 6, 4};
ArrayUtils arrayUtils = new ArrayUtils();
int[] uniqueArray = arrayUtils.removeDuplicates(array);
System.out.println("Array without duplicates: " + Arrays.toString(uniqueArray));
}
}
Question 08: - WAP to show the concept of method local inner class.
Solution: -
class Demo{
public void display() {
class Inner {
private int number;
Inner(int number) {
this.number = number;
}
void showNumber() {
System.out.println("Number: " + number); Output: -
} Number: 42
}
Inner inner = new Inner(42);
inner.showNumber();
}
}
public class MethodLocalInnerClassDemo {
public static void main(String[] args) {
Demo outer = new Demo();
outer.display();
}
}
Question 09: - WAP to display n terms of natural numbers and their sum using interface concept.
Solution: -
interface NaturalNumberOperations {
void displayNaturalNumbers(int n);
int calculateSum(int n);
}
class NaturalNumberOperationsImpl implements NaturalNumberOperations {
@Override
public void displayNaturalNumbers(int n) {
System.out.println("The first " + n + " natural numbers are:");
for (int i = 1; i <= n; i++) {
System.out.print(i + " ");
}
System.out.println(); Output: -
} The first 10 natural numbers are:
@Override 1 2 3 4 5 6 7 8 9 10
public int calculateSum(int n) { The sum of the first 10 natural numbers is: 55
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
}
public class NaturalInterface {
public static void main(String[] args) {
int n = 10;
NaturalNumberOperations operations = new NaturalNumberOperationsImpl();
operations.displayNaturalNumbers(n);
int sum = operations.calculateSum(n);
System.out.println("The sum of the first " + n + " natural numbers is: " + sum);
}
}
OOP File Code Questions Part-2
Question 01: - WAP to find the percentage of student who contains 5 subjects and check the following
Condition (Take the input from user) using interface and super keyword.
Calculate the percentage of any student who have 5 subjects.
a. If percentage is greater than, equal to 90 but less than 100 then print “You are an outstanding student”.
b. If percentage is greater than, equal to 80 but less than 90 then print “Your performance is good”.
c. If percentage is greater than, equal to 70 but less than 80 then print “Your performance is better”.
d. If percentage is greater than, equal to 60 but less than 70 then print “Your performance is average”.
e. If percentage is greater than, equal to 50 but less than 60 then print “Work hard, you can do better”.
f. Otherwise print “You have to work hard”.
Solution: -
import java.util.*;
interface Marks {
void Perc();
}
class Student implements Marks {
private int[] subjects;
private double percentage;
public Student(int[] subjects) {
this.subjects = subjects;
}
@Override
public void Perc() {
int totalMarks = 0;
for (int marks : subjects) {
totalMarks += marks;
} Output: -
percentage = (totalMarks / 5.0);
Enter marks for subject 1: 99
Enter marks for subject 2: 100
if (percentage >= 90 && percentage < 100) {
Enter marks for subject 3: 96
System.out.println("You are an outstanding student");
Enter marks for subject 4: 98
} else if (percentage >= 80 && percentage < 90) {
Enter marks for subject 5: 99
System.out.println("Your performance is good");
You are an outstanding student
} else if (percentage >= 70 && percentage < 80) {
System.out.println("Your performance is better");
} else if (percentage >= 60 && percentage < 70) {
System.out.println("Your performance is average");
} else if (percentage >= 50 && percentage < 60) {
System.out.println("Work hard, you can do better");
} else {
System.out.println("You have to work hard");
}
}
}
public class MarksRemark {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] subjects = new int[5];
for (int i = 0; i < 5; i++) {
System.out.print("Enter marks for subject " + (i + 1) + ": ");
subjects[i] = scanner.nextInt();
}
Student s = new Student(subjects);
s.Perc();
scanner.close();
}
}
Question 02: - WAP to show the matrix. (Take input from user).
Solution: -
import java.util.Scanner;
class Matrixx{
void Display(){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int r = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int c = scanner.nextInt();
int[][] matrix = new int[r][c]; Output: -
System.out.println("Enter the matrix elements:"); Enter the number of rows: 3
for (int i = 0; i < r; i++) { Enter the number of columns: 3
for (int j = 0; j < c; j++) { Enter the matrix elements:
matrix[i][j] = scanner.nextInt(); 123
} 456
} 789
System.out.println("Matrix:"); Matrix:
for (int i = 0; i < r; i++) { 123
for (int j = 0; j < c; j++) { 456
System.out.print(matrix[i][j] + " "); 789
}
System.out.println();
}
scanner.close();
}
}
public class MatrixDisplay {
public static void main(String[] args) {
Matrixx a=new Matrixx();
a.Display();
}
}
Question 03: - WAP to show the concept of Method overloading.(use 2 ways of same and take input from user).
Solution: -
import java.util.Scanner;
class Demo{
void Display(){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num1 = scanner.nextInt();
System.out.print("Enter a double: ");
double num2 = scanner.nextDouble();
display(num1); Output: -
display(num2); Enter an integer: 10
scanner.close(); Enter a double: 10.5
} Integer value: 10
public static void display(int value) { Double value: 10.5
System.out.println("Integer value: " + value);
}
public static void display(double value) {
System.out.println("Double value: " + value);
}
}
public class MethodOverloading {
public static void main(String[] args) {
Demo d=new Demo();
d.Display();
}
}
Question 04: - WAP to show the concept of method overriding (Take the input from user).
Solution: -
import java.util.Scanner;
class Animal {
String name;
Animal(String name) {
this.name = name; Output: -
} Enter the type of animal (dog/cat): cat
String sound() { Enter the name of the animal: cat
return "Some generic sound"; The cat named cat says Meow
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
@Override
String sound() {
return "Bark";
}
}
class Cat extends Animal {
Cat(String name) {
super(name);
}
@Override
String sound() {
return "Meow";
}
}
public class OverRiding {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the type of animal (dog/cat): ");
String animalType = sc.nextLine().trim().toLowerCase();
System.out.print("Enter the name of the animal: ");
String animalName = sc.nextLine().trim();
Animal animal;
switch (animalType) {
case "dog":
animal = new Dog(animalName);
break;
case "cat":
animal = new Cat(animalName);
break;
default:
animal = new Animal(animalName);
break;
}
System.out.println("The " + animalType + " named " + animal.name + " says " + animal.sound());
}
}
Question 05: - WAP to show the concept of for each loop. (Take the input from user).
Solution: -
import java.util.ArrayList;
import java.util.Scanner;
class Demo{
void D(){
Scanner sc = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
System.out.println("Enter numbers (type 'done' to finish):");
while (true) {
String input = sc.nextLine().trim();
if (input.equalsIgnoreCase("done")) {
break;
}
try {
int number = Integer.parseInt(input);
numbers.add(number);
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number or 'done' to finish.");
}
}
System.out.println("You entered:"); Output: -
for (int number : numbers) { Enter numbers (type 'done' to finish):
System.out.println(number); 10
} done
} You entered:
} 10
public class ForEach {
public static void main(String[] args) {
Demo d=new Demo();
d.D();
}
}
Question 06: - WAP to show the concept of copy constructor. (Take the input from user).
Solution: -
import java.util.Scanner;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
Person(Person p) { Output: -
this.name = p.name; Enter the name: Shivam
this.age = p.age; Enter the age: 20
}
void display() { Original Person details:
System.out.println("Name: " + name); Name: Shivam
System.out.println("Age: " + age); Age: 20
}
} Copied Person details:
public class CopyConstructer { Name: Shivam
public static void main(String[] args) { Age: 20
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name: ");
String name = sc.nextLine();
System.out.print("Enter the age: ");
int age = sc.nextInt();
Person original = new Person(name, age);
Person copy = new Person(original);
System.out.println("\nOriginal Person details:");
original.display();
System.out.println("\nCopied Person details:");
copy.display();
}
}
Question 07: - WAP to show whether the rectangle become the square or not on the basis of user input.
Solution: -
import java.util.Scanner;
class Rectangle{
void check(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of the rectangle: ");
double length = sc.nextDouble();
Question 08: - WAP to show the concept of hierarchical inheritance by importing user defined package
and split a file into more than one files using the same.
Solution: -
Question 09: - WAP to show the find whether number is divisible by 5,10,15,20 or not using
parameterized constructor and import user defined package and split a file into more than one files.
(Take input from user).
Solution: -
Question 10: - WAP to show the month name using on the basis of entered number using switch
statement. (Take input from user).
Solution: -
Question 05: - WAP to show the concept of Multithreading by using extending a thread class.
Solution: -
Question 06: - WAP to show the concept of Multithreading by using implementing a run able interface.
Solution: -
Question 07: - WAP to display the user registration form uses AWT.
Solution: -