0% found this document useful (0 votes)
18 views

Java Programs

Uploaded by

2k23.mca2314118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java Programs

Uploaded by

2k23.mca2314118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Pranveer Singh Institute of Technology, Kanpur

Even Semester 2023-2024

Master of Computer Application 1st Year


Semester-II

Lab File
OOPS BY JAVA LAB(KCA-251)

SUBMITTED TO: - Submitted by: -

Name of Faculty: - Deepali Nigam Name of Student: -Shivam Nishad


Designation: - Asst. Professor Roll No.: - 2301640140169
Department: - MCA Branch: - MCA
OOP File Code Questions Part-1

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;
}
}

public class Rectangle {


public static void main(String[] args) {
MyRectangle a = new MyRectangle(10, 9);
if (a.isSquare()) {
System.out.println("The rectangle is a square.");
} else {
System.out.println("The rectangle is not a square.");
}
}
}
Question 03: - Write a Program to Print Pyramid Number Pattern in Java.
Solution: -
class Print{
void Display(){
int rows = 5;
for (int i = 0; i < rows; i++) {
Output: -
for (int j = 0; j < rows - i - 1; j++) {
System.out.print(" "); *
}
for (int k = 0; k <= i; k++) { * *
System.out.print("* ");
***
}
System.out.println(); ****
}
} *****
}
public class Pattern {
public static void main(String[] args) {
Print a=new Print();
a.Display();
}
}

Question 04: - Write Java Program to Find the Transpose of Matrix.


Solution: -
class Matrix{
void Display(){
int[][] matrix = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};
int rows = matrix.length; Output: -
int columns = matrix[0].length;
1 4 7
int[][] transpose = new int[columns][rows];
2 5 8
for (int i = 0; i < columns; i++) {
3 6 9
for (int j = 0; j < rows; j++) {
transpose[i][j] = matrix[j][i];
}
}
System.out.println("Transpose of the matrix:");
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
}
}
public class Transpose {
public static void main(String[] args) {
Matrix a=new Matrix();
a.Display();
}
}
Question 05: - WAP to Remove Duplicate Elements From an Array.
Solution: -
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

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 06: - Java Program for Hexadecimal to Decimal Conversion.


Solution: -
class Hexa {
public int convertHexToDecimal(String hex) {
return Integer.parseInt(hex, 16);
}
}
Output: -
public class HexaToDeci { Hexadecimal number 1A3F is equal to decimal number 6719
public static void main(String[] args) {
String hexNumber = "1A3F";
Hexa converter = new Hexa();
int decimalNumber = converter.convertHexToDecimal(hexNumber);
System.out.println("Hexadecimal number " + hexNumber + " is equal to decimal number " +
decimalNumber);
}
}

Question 07: - WAP to show the concept of java array method.


Solution: -
import java.util.Arrays;
public class ArrayMethod { Output: -
public static void main(String[] args) { Original array: [5, 3, 8, 2, 1, 9]
int[] array = {5, 3, 8, 2, 1, 9}; Sorted array: [1, 2, 3, 5, 8, 9]
System.out.println("Original array: " + Arrays.toString(array)); Index of element 8: 4
Arrays.sort(array); Array filled with 7: [7, 7, 7, 7, 7, 7]
System.out.println("Sorted array: " + Arrays.toString(array)); Are array and array2 equal? true
int index = Arrays.binarySearch(array, 8); Copy of array: [7, 7, 7, 7, 7, 7]
System.out.println("Index of element 8: " + index); Comparison result of array and array2: 0
Arrays.fill(array, 7); Mismatch index between array and array2: -1
System.out.println("Array filled with 7: " + Arrays.toString(array));
int[] array2 = {7, 7, 7, 7, 7, 7};
boolean areEqual = Arrays.equals(array, array2);
System.out.println("Are array and array2 equal? " + areEqual);
int[] arrayCopy = Arrays.copyOf(array, array.length);
System.out.println("Copy of array: " + Arrays.toString(arrayCopy));
int comparisonResult = Arrays.compare(array, array2);
System.out.println("Comparison result of array and array2: " + comparisonResult);
int mismatchIndex = Arrays.mismatch(array, array2);
System.out.println("Mismatch index between array and array2: " + mismatchIndex);
}
}

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();

System.out.print("Enter the width of the rectangle: ");


double width = sc.nextDouble();
if (length == width) { Output: -
System.out.println("The rectangle is a square."); Enter the length of the rectangle: 10
} else { Enter the width of the rectangle: 10
System.out.println("The rectangle is not a square."); The rectangle is a square.
}
}
}
public class RectangleSquare {
public static void main(String[] args) {
Rectangle a=new Rectangle();
a.check();
}
}

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 11: - WAP to show the concept of break statement.


Solution: -
import java.util.Scanner;
class Break{
void statement(){
int[] numbers = {1, 3, 7, 8, 12, 14, 17, 20, 25};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a divisor to find the first multiple of: ");
int divisor = scanner.nextInt();
boolean found = false;
for (int number : numbers) {
if (number % divisor == 0) {
System.out.println("Found a multiple of " + divisor + ": " + number);
found = true;
break;
}
}
if (!found) {
System.out.println("No multiple of " + divisor + " found in the list.");
}
scanner.close();
}
}
public class BreakStatement { Output: -
Enter a divisor to find the first multiple of: 25
Found a multiple of 25: 25
public static void main(String[] args) {
Break b=new Break();
b.statement();
}
}

Question 12: - WAP to show the concept of continue statement.


Solution: -
class Loop{
void conti(){ Output: -
for (int i = 1; i <= 10; i++) { 1
if (i % 3 == 0) { 2
continue; 4
} 5
System.out.println(i); 7
} 8
} 10
}
public class Continue {
public static void main(String[] args) {
Loop l=new Loop();
l.conti();
}
}

Question 13: - WAP to print the table of user entered number.


Solution: -
import java.util.Scanner;
class Print{ Output: -
void table(){ Enter a number: 25
Scanner scanner = new Scanner(System.in); Multiplication table of 25:
System.out.print("Enter a number: "); 25 * 1 = 25
int number = scanner.nextInt(); 25 * 2 = 50
System.out.println("Multiplication table of " + number + ":"); 25 * 3 = 75
for (int i = 1; i <= 10; i++) { 25 * 4 = 100
int result = number * i; 25 * 5 = 125
System.out.println(number + " * " + i + " = " + result); 25 * 6 = 150
} 25 * 7 = 175
scanner.close(); 25 * 8 = 200
} 25 * 9 = 225
} 25 * 10 = 250
public class TablePrint {
public static void main(String[] args) {
Print p=new Print();
p.table();
}
}
OOP File Code Questions Part-3
Question 01: - WAP to show the concept of try…..catch block in Exception handling.
Solution: -

Question 02: - WAP to show the concept of finally in Exception handling.


Solution: -

Question 03: - WAP to show the concept of throw in Exception handling.


Solution: -

Question 04: - WAP to show the concept of throws in Exception handling.


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: -

You might also like