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

yash_java

The document contains multiple Java files authored by Yash Pandey, focusing on various programming exercises including menu-driven arithmetic operations, pattern printing using loops, and object-oriented programming concepts like class creation for account management. Each file demonstrates different programming techniques such as method overloading, constructor overloading, and matrix operations. The code snippets are structured to provide practical examples of these concepts in Java.

Uploaded by

yashpandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

yash_java

The document contains multiple Java files authored by Yash Pandey, focusing on various programming exercises including menu-driven arithmetic operations, pattern printing using loops, and object-oriented programming concepts like class creation for account management. Each file demonstrates different programming techniques such as method overloading, constructor overloading, and matrix operations. The code snippets are structured to provide practical examples of these concepts in Java.

Uploaded by

yashpandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

File: Exp1.

java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP1 : Write a program to perform Menu Driven Arithmetic Operation.
*/

import java.util.Scanner;

public class Exp1 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter two numbers: ");


double num1 = sc.nextDouble();
double num2 = sc.nextDouble();

System.out.println("Choose an operation:");
System.out.println("1. Addition (+)");
System.out.println("2. Subtraction (-)");
System.out.println("3. Multiplication (*)");
System.out.println("4. Division (/)");
System.out.println("5. Modulus (%)");
int choice = sc.nextInt();

switch (choice) {
case 1:
System.out.println("Result: " + (num1 + num2));
break;
case 2:
System.out.println("Result: " + (num1 - num2));
break;
case 3:
System.out.println("Result: " + (num1 * num2));
break;
case 4:
if (num2 != 0) {
System.out.println("Result: " + (num1 / num2));
} else {
System.out.println("Division by zero is not allowed.");
}
break;
case 5:
if (num2 != 0) {
System.out.println("Result: " + (num1 % num2));
} else {
System.out.println("Modulus by zero is not allowed.");
}
break;
default:
System.out.println("Invalid choice.");
}
}
}
File: Exp1PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP1 : Menu Driven Arithmetic Operations
*/
import java.util.Scanner;

public class Exp1PostLab1 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char choice;
double num1, num2, result;

System.out.print("Enter first number: ");


num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
num2 = scanner.nextDouble();
System.out.print("Choose operation (+, -, *, /): ");
choice = scanner.next().charAt(0);

switch (choice) {
case '+':
result = num1 + num2;
System.out.println("Result: " + result);
break;
case '-':
result = num1 - num2;
System.out.println("Result: " + result);
break;
case '*':
result = num1 * num2;
System.out.println("Result: " + result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Division by zero is not allowed.");
}
break;
default:
System.out.println("Invalid operation.");
}
}
}
File: Exp1PostLab2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP1 : Menu Driven Arithmetic Operations
*/
import java.util.Scanner;

public class Exp1PostLab2 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter month number (1-12): ");
int month = scanner.nextInt();
String monthName;

switch (month) {
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: monthName = "Invalid month number."; break;
}

System.out.println("Month: " + monthName);


}
}
File: Exp2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP2 : Write a program to print following pattern using
labelled break and continue statement.
*/
import java.util.*;

public class Exp2 {


public static void main(String[] args) {
outer:
for (int row = 1; row <= 6; row++) {
if (row == 6) {
break outer;
}

for (int column = 1; column <= 6; column++) {


if (column > row) {
continue;
}
System.out.print("* ");
}
System.out.println();
}
}
}
File: Exp2PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP2 : Pattern Using Labelled Break and Continue Statement
*/
public class Exp2PostLab1 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
if (i == 5) break;
}
}
}
File: Exp2PostLab2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP2 : Pattern Using Labelled Break and Continue Statement
*/
public class Exp2PostLab2 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) continue;
System.out.println(i);
}
}
}
File: Exp2PostLab3.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23

EXP2 : Pattern Using Labelled Break and Continue Statement


*/
public class Exp2PostLab3 {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(col + " ");
}
System.out.println();
}
}
}
File: Exp3.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP3 : Write a program to create a class Account to perform the
operation of insert, deposit and withdrawal of single
employee and make use of object.
*/
import java.util.Scanner;

class Account {
int accountNumber;
String accountHolderName;
double balance;

void insert(int accNo, String name, double initialBalance) {


accountNumber = accNo;
accountHolderName = name;
balance = initialBalance;
}

void deposit(double amount) {


balance += amount;
}

void withdraw(double amount) {


if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance.");
}
}

void checkBalance() {
System.out.println("Balance: " + balance);
}

void display() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Holder Name: " + accountHolderName);
System.out.println("Balance: " + balance);
}
}

public class Exp3 {


public static void main(String[] args) {
Account acc = new Account();
Scanner sc = new Scanner(System.in);

System.out.println("Enter account number:");


int accNo = sc.nextInt();
sc.nextLine();
System.out.println("Enter account holder name:");
String name = sc.nextLine();
System.out.println("Enter initial balance:");
double balance = sc.nextDouble();

acc.insert(accNo, name, balance);


acc.display();

System.out.println("Enter amount to deposit:");


double depositAmount = sc.nextDouble();
acc.deposit(depositAmount);
acc.checkBalance();

System.out.println("Enter amount to withdraw:");


double withdrawAmount = sc.nextDouble();
acc.withdraw(withdrawAmount);
acc.checkBalance();
}
}
File: Exp3PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP3 : Create a Class Account to Perform Insert, Deposit, and Withdrawal Operations
*/
public class Exp3PostLab1 {
public static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

public static void main(String[] args) {


int num1 = 56, num2 = 98;
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd(num1, num2));
}
}
File: Exp3PostLab2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP3 : Create a Class Account to Perform Insert, Deposit, and Withdrawal Operations
*/
import java.util.Scanner;

public class Exp3PostLab2 {


static class Circle {
private double radius;

public void acceptRadius() {


Scanner sc = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = sc.nextDouble();
}

public double calculateArea() {


return Math.PI * radius * radius;
}

public void displayArea() {


System.out.println("The area of the circle is: " + calculateArea());
}
}

public static void main(String[] args) {


Circle circle = new Circle();
circle.acceptRadius();
circle.displayArea();
}
}
File: Exp4PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP4 : Find Area of Circle Using Method Overloading and Constructor Overloading
*/
import java.util.Scanner;

public class Exp4PostLab1 {


public double area(double radius) {
return Math.PI * radius * radius;
}

public double area(double length, double width) {


return length * width;
}

public double area(double base, double height, boolean isTriangle) {


return 0.5 * base * height;
}

public static void main(String[] args) {


Exp4PostLab1 shape = new Exp4PostLab1();
Scanner sc = new Scanner(System.in);

System.out.print("Enter radius of circle: ");


double radius = sc.nextDouble();
System.out.println("Area of Circle: " + shape.area(radius));

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


double length = sc.nextDouble();
double width = sc.nextDouble();
System.out.println("Area of Rectangle: " + shape.area(length, width));

System.out.print("Enter base and height of triangle: ");


double base = sc.nextDouble();
double height = sc.nextDouble();
System.out.println("Area of Triangle: " + shape.area(base, height, true));
}
}
File: Exp4PostLab2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP4 : Find Area of Circle Using Method and Constructor Overloading
*/
public class Exp4PostLab2 {

public int add(int a, int b) {


return a + b;
}

public String add(String s1, String s2) {


return s1 + s2;
}

public static void main(String[] args) {


Exp4PostLab2 obj = new Exp4PostLab2();
System.out.println("Sum of integers: " + obj.add(10, 20));
System.out.println("Concatenation of strings: " + obj.add("Hello, ", "World!"));
}
}
File: Exp4PostLab3.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP4 : Find Area of Circle Using Method and Constructor Overloading
*/
public class Exp4PostLab3 {

public Exp4PostLab3(double radius) {


System.out.println("Area of Circle: " + (Math.PI * radius * radius));
}

public Exp4PostLab3(double length, double breadth) {


System.out.println("Area of Rectangle: " + (length * breadth));
}

public Exp4PostLab3(double base, double height, boolean isTriangle) {


System.out.println("Area of Triangle: " + (0.5 * base * height));
}

public static void main(String[] args) {


new Exp4PostLab3(4); // Circle
new Exp4PostLab3(5, 7); // Rectangle
new Exp4PostLab3(6, 8, true); // Triangle
}
}
File: Exp4PostLab4.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP4 : Find Area of Circle Using Method and Constructor Overloading
*/
public class Exp4PostLab4 {
private int num1;
private int num2;

public Exp4PostLab4() {
this.num1 = 56;
this.num2 = 98;
}

public int calculateGCD(int a, int b) {


if (b == 0) return a;
return calculateGCD(b, a % b);
}

public void display() {


System.out.println("GCD of " + num1 + " and " + num2 + " is: " + calculateGCD(num1,
num2));
}

public static void main(String[] args) {


Exp4PostLab4 obj = new Exp4PostLab4();
obj.display();
}
}
File: Exp4_1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP4 : Write a program to find Area of Circle using
(i) method overloading
(ii) constructor overloading
*/
class AreaOfMethodOverload {
double area(double radius) {
return Math.PI * radius * radius;
}

double area(double radius, double piValue) {


return piValue * radius * radius;
}
}

public class Exp4_1 {


public static void main(String[] args) {
AreaOfMethodOverload obj = new AreaOfMethodOverload();
double radius = 5;

System.out.println("Area with default PI: " + obj.area(radius));


System.out.println("Area with custom PI: " + obj.area(radius, 3.14));
}
}
File: Exp4_2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP4 : Write a program to find Area of Circle using
(i) method overloading
(ii) constructor overloading
*/
class AreaOf {
double radius;

AreaOf() {
radius = 1.0;
}

AreaOf(double r) {
radius = r;
}

double findArea() {
return Math.PI * radius * radius;
}
}

public class Exp4_2 {


public static void main(String[] args) {
AreaOf defaultCircle = new AreaOf();
AreaOf customCircle = new AreaOf(5);

System.out.println("Area of default circle: " + defaultCircle.findArea());


System.out.println("Area of custom circle: " + customCircle.findArea());
}
}
File: Exp5PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP5 : Matrix Addition and Count Frequency of a Letter in String
*/
public class Exp5PostLab1 {

public static void main(String[] args) {


int[][] matrix1 = { {1, 2}, {3, 4} };
int[][] matrix2 = { {5, 6}, {7, 8} };
int[][] result = new int[2][2];

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
result[i][j] = 0;
for (int k = 0; k < 2; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}
File: Exp5PostLab2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP5 : Matrix Addition and Count Frequency of a Letter in String
*/
public class Exp5PostLab2 {

public static void main(String[] args) {


int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int sum = 0;

for (int i = 0; i < 3; i++) {


sum += matrix[i][i]; // Adding primary diagonal elements
}

System.out.println("Sum of diagonal elements: " + sum);


}
}
File: Exp5PostLab3.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP5 : Matrix Addition and Count Frequency of a Letter in String
*/
public class Exp5PostLab3 {

public static void main(String[] args) {


String input = "Programming is fun!";
int vowelCount = 0;
String vowels = "AEIOUaeiou";

for (int i = 0; i < input.length(); i++) {


if (vowels.indexOf(input.charAt(i)) != -1) {
vowelCount++;
}
}

System.out.println("Number of vowels: " + vowelCount);


}
}
File: Exp5PostLab4.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP5 : Matrix Addition and Count Frequency of a Letter in String
*/
public class Exp5PostLab4 {

public static void main(String[] args) {


String str = "Hello World";

System.out.println("Length of the string: " + str.length());


System.out.println("Character at index 4: " + str.charAt(4));
System.out.println("Substring (0, 5): " + str.substring(0, 5));
System.out.println("Index of 'o': " + str.indexOf('o'));
System.out.println("String in uppercase: " + str.toUpperCase());
}
}
File: Exp5_1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP5 : Write a program in java to perform
(i) Matrix Addition
(ii) Count frequency
of a given letter in a String.
*/
import java.util.Scanner;

class Exp5_1 {
public static void main(String[] args) {
int i, j;
int[][] set1 = new int[3][3];
int[][] set2 = new int[3][3];
int[][] add = new int[3][3];

Scanner sc = new Scanner(System.in);

System.out.println("Enter the values of the first matrix:");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
set1[i][j] = sc.nextInt();
}
}

System.out.println("Enter the values of the second matrix:");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
set2[i][j] = sc.nextInt();
}
}

// Adding the matrices


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
add[i][j] = set1[i][j] + set2[i][j];
}
}

System.out.println("The resultant matrix after addition is:");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
System.out.print(add[i][j] + " ");
}
System.out.println();
}
}
}
File: Exp5_2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP5 : Write a program in java to perform
(i) Matrix Addition
(ii) Count frequency
of a given letter in a String.
*/
import java.util.Scanner;

class Exp5_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter a string:");
String inputString = sc.nextLine();

System.out.println("Enter the character to find its frequency:");


char letter = sc.next().charAt(0);

int count = 0;
for (int i = 0; i < inputString.length(); i++) {
if (inputString.charAt(i) == letter) {
count++;
}
}

System.out.println("The frequency of '" + letter + "' in the string is: " + count);
}
}
File: Exp6PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP6 : Vector Operations and String Buffer Functions
*/
import java.util.*;

public class Exp6PostLab1 {


public static void main(String[] args) {
Vector<String> students = new Vector<>();
Scanner sc = new Scanner(System.in);
int choice;

do {
System.out.println("1. Add Student Name\n2. Remove Student Name\n3. Display
Names\n4. Exit");
choice = sc.nextInt();
sc.nextLine(); // consume newline

switch (choice) {
case 1:
System.out.println("Enter student name: ");
String name = sc.nextLine();
students.add(name);
break;
case 2:
System.out.println("Enter student name to remove: ");
String removeName = sc.nextLine();
students.remove(removeName);
break;
case 3:
Enumeration<String> names = students.elements();
System.out.println("Student Names:");
while (names.hasMoreElements()) {
System.out.println(names.nextElement());
}
break;
} } while (choice != 4);
sc.close();
}
}
File: Exp6PostLab2.java

/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP6 : Vector Operations and String Buffer Functions
*/
import java.util.*;

public class Exp6PostLab2 {


public static void main(String[] args) {
Vector<String> shoppingList = new Vector<>();
Scanner sc = new Scanner(System.in);
int choice;

do {
System.out.println("1. Add Item\n2. Delete Item\n3. Display Items\n4. Exit");
choice = sc.nextInt();
sc.nextLine(); // consume newline

switch (choice) {
case 1:
System.out.println("Enter item name: ");
String item = sc.nextLine();
System.out.println("Enter position to insert item: ");
int position = sc.nextInt();
sc.nextLine();
shoppingList.add(position, item);
break;
case 2:
System.out.println("Enter item name to delete: ");
String removeItem = sc.nextLine();
shoppingList.remove(removeItem);
break;
case 3:
System.out.println("Shopping List: " + shoppingList);
break;
} } while (choice != 4);
sc.close();
}}
File: Exp6PostLab3.java

/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP6 : Vector Operations and String Buffer Functions
*/
public class Exp6PostLab3 {

public static void main(String[] args) {


StringBuffer sb = new StringBuffer("Hello World");
System.out.println("Original String: " + sb);

sb.delete(5, 11); // Deletes " World"


System.out.println("After deletion: " + sb);
}
}
File: Exp6PostLab4.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP6 : Vector Operations and String Buffer Functions
*/
public class Exp6PostLab4 {

public static void main(String[] args) {


StringBuffer sb = new StringBuffer("Hello World");
System.out.println("Original String: " + sb);

sb.replace(6, 11, "Java"); // Replaces "World" with "Java"


System.out.println("After replacement: " + sb);
}
}
File: Exp6_1.java
/*
NAME : Yash Pandey
UIN : 231P083
R O L L N O : 23
EXP6 : Write a program in java to perform
(i) Vector operations
(ii) String Buffer functions
*/
import java.util.Scanner;

class Exp6_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter a string to check if it's a palindrome:");


String inputString = sc.nextLine();

// Append the string to StringBuffer and reverse it


StringBuffer sb = new StringBuffer(inputString);
sb.reverse();

// Convert reversed StringBuffer to String


String reversedString = sb.toString();

// Compare the original string with the reversed string


if (inputString.equals(reversedString)) {
System.out.println(inputString + " is a palindrome.");
} else {
System.out.println(inputString + " is not a palindrome.");
}
}
}
File: Exp6_2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP6 : Write a program in java to perform
(i) Vector operations
(ii) String Buffer functions
*/
import java.util.Vector;
import java.util.Scanner;

class Exp6_2 {
public static void main(String[] args) {
Vector<String> shoppingList = new Vector<>();
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of items you want to add:");


int n = sc.nextInt();
sc.nextLine(); // Consume the newline

System.out.println("Enter the items:");


for (int i = 0; i < n; i++) {
String item = sc.nextLine();
shoppingList.add(item);
}

System.out.println("Items in the vector: " + shoppingList);

// Copy vector elements into a string


String allItems = String.join(", ", shoppingList);

System.out.println("All items as a single string: " + allItems);


}
}
File: Exp7.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23

EXP7 : Write a program to implement single and multilevel


inheritance using super keyword.
*/
// Base class
class Person {
String name;

Person(String name) {
this.name = name;
}

void displayPersonInfo() {
System.out.println("Name: " + name);
}
}

// Single Inheritance: Child class extending Person


class Online extends Person {
String course;

Online(String name, String course) {


super(name); // Calling the constructor of Person class
this.course = course;
}

void displayOnlineInfo() {
System.out.println("Course: " + course);
}
}

// Another single inheritance: Child class extending Person


class SavingAccount extends Person {
double balance;

SavingAccount(String name, double balance) {


super(name); // Calling the constructor of Person class
this.balance = balance;
}

void displaySavingAccountInfo() {
System.out.println("Balance: $" + balance);
}
}

// Multilevel Inheritance: Child class extending SavingAccount


class AccountDetails extends SavingAccount {
String accountNumber;

AccountDetails(String name, double balance, String accountNumber) {


super(name, balance); // Calling the constructor of SavingAccount class
this.accountNumber = accountNumber;
}

void displayAccountDetails() {
System.out.println("Account Number: " + accountNumber);
}
}

// Main class
public class Exp7 {
public static void main(String[] args) {
// Creating object of Online class (Single Inheritance)
Online onlineStudent = new Online("John", "Java Programming");
onlineStudent.displayPersonInfo();
onlineStudent.displayOnlineInfo();

System.out.println();

// Creating object of AccountDetails class (Multilevel Inheritance)


AccountDetails account = new AccountDetails("Alice", 1500.75, "ACC12345");
account.displayPersonInfo();
account.displaySavingAccountInfo();
account.displayAccountDetails();
}
}
File: Exp7PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP7 : Single and Multilevel Inheritance using super keyword
*/
import java.util.Scanner;

class Radius {
double radius;

public void acceptRadius() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = scanner.nextDouble();
}
}

class Circle extends Radius {


public double findArea() {
return Math.PI * radius * radius;
}
}

class Sphere extends Circle {


public double findVolume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}

public void displayVolume() {


System.out.println("Volume of Sphere: " + findVolume());
}
}

public class Exp7PostLab1 {

public static void main(String[] args) {


Sphere sphere = new Sphere();
sphere.acceptRadius();
sphere.displayVolume();
}
}
File: Exp7PostLab2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
*/
import java.util.Scanner;

class RadiusBase {
double radius;

public void acceptRadius() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = scanner.nextDouble();
}

public void display() {


System.out.println("This is the base class display method.");
}
}

class CircleDerived extends RadiusBase {


public double findArea() {
return Math.PI * radius * radius;
}

@Override
public void display() {
System.out.println("Area of Circle: " + findArea());
}
}

class SphereDerived extends CircleDerived {


public double findVolume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
@Override
public void display() {
System.out.println("Volume of Sphere: " + findVolume());
}
}

public class Exp7PostLab2 {

public static void main(String[] args) {


SphereDerived sphere = new SphereDerived();
sphere.acceptRadius();
sphere.display();
}
}
File: Exp8.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP8 : Write a program to implement interface demonstrating the concept of
multiple inheritance.
*/
class Student {
String name;
int rollNo;

Student(String name, int rollNo) {


this.name = name;
this.rollNo = rollNo;
}

void displayStudentInfo() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
}
}

// Single inheritance
class Test extends Student {
int marks1, marks2;

Test(String name, int rollNo, int marks1, int marks2) {


super(name, rollNo); // Calling constructor of Student class
this.marks1 = marks1;
this.marks2 = marks2;
}

void displayTestMarks() {
System.out.println("Marks1: " + marks1);
System.out.println("Marks2: " + marks2);
}
}

// Interface
interface Sports {
int sportsMarks = 10;
void displaySportsMarks();
}

// Multiple inheritance
class Results extends Test implements Sports {
int totalMarks;

Results(String name, int rollNo, int marks1, int marks2) {


super(name, rollNo, marks1, marks2);
this.totalMarks = marks1 + marks2 + sportsMarks;
}

public void displaySportsMarks() {


System.out.println("Sports Marks: " + sportsMarks);
}

void displayTotalMarks() {
System.out.println("Total Marks (including sports): " + totalMarks);
}
}

public class Exp8 {


public static void main(String[] args) {
Results result = new Results("Alice", 101, 85, 90);

result.displayStudentInfo();
result.displayTestMarks();
result.displaySportsMarks();
result.displayTotalMarks();
}
}
File: Exp8PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP8 : Interface demonstrating the concept of multiple inheritance
*/

import java.util.Scanner;

interface Matrix {
int M = 2, N = 2;
void readMatrix();
void displayMatrix();
void addMatrix();
}

class MatrixOperations implements Matrix {


int[][] matrix1 = new int[M][N];
int[][] matrix2 = new int[M][N];
int[][] result = new int[M][N];

@Override
public void readMatrix() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter elements for Matrix 1:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix1[i][j] = scanner.nextInt();
}
}

System.out.println("Enter elements for Matrix 2:");


for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix2[i][j] = scanner.nextInt();
}
}
}

@Override
public void displayMatrix() {
System.out.println("Resultant Matrix after Addition:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}

@Override
public void addMatrix() {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
}

public class Exp8PostLab1 {


public static void main(String[] args) {
MatrixOperations matOps = new MatrixOperations();
matOps.readMatrix();
matOps.addMatrix();
matOps.displayMatrix();
}
}
File: Exp8PostLab2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP8 : Interface demonstrating the concept of multiple inheritance
*/

import java.util.Scanner;

interface Matrix {
int M = 2, N = 2;
void readMatrix();
void displayMatrix();
void sum_Diagonal_Matrix();
}

class DiagonalMatrixOperations implements Matrix {


int[][] matrix = new int[M][N];

@Override
public void readMatrix() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter elements for the Matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = scanner.nextInt();
}
}
}

@Override
public void displayMatrix() {
System.out.println("Matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}

@Override
public void sum_Diagonal_Matrix() {
int sum = 0;
for (int i = 0; i < M; i++) {
sum += matrix[i][i]; // Summing diagonal elements
}
System.out.println("Sum of Diagonal Elements: " + sum);
}
}

public class Exp8PostLab2 {


public static void main(String[] args) {
DiagonalMatrixOperations diagOps = new DiagonalMatrixOperations();
diagOps.readMatrix();
diagOps.displayMatrix();
diagOps.sum_Diagonal_Matrix();
}
}
File: Exp9.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP9 : Write a program to calculate area of Rectangle and Circle using abstract class.
*/
abstract class Shape {
abstract void calculateArea();
}

class Rectangle extends Shape {


int length, breadth;

Rectangle(int length, int breadth) {


this.length = length;
this.breadth = breadth;
}

void calculateArea() {
int area = length * breadth;
System.out.println("Area of Rectangle: " + area);
}
}

class Circle extends Shape {


double radius;

Circle(double radius) {
this.radius = radius;
}

void calculateArea() {
double area = Math.PI * radius * radius;
System.out.println("Area of Circle: " + area);
}
}

public class Exp9 {


public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 3);
Circle circle = new Circle(7.0);
rectangle.calculateArea();
circle.calculateArea();
}
}
File: Exp9PostLab1.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP9 : Calculate area of Rectangle and Circle using abstract class
*/

abstract class Shape {


abstract double volume();
}

class Sphere extends Shape {


double radius;

Sphere(double radius) {
this.radius = radius;
}

@Override
double volume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}

class Hemisphere extends Shape {


double radius;

Hemisphere(double radius) {
this.radius = radius;
}

@Override
double volume() {
return (2.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}

public class Exp9PostLab1 {


public static void main(String[] args) {
Sphere sphere = new Sphere(5);
Hemisphere hemisphere = new Hemisphere(5);
System.out.println("Volume of Sphere: " + sphere.volume());
System.out.println("Volume of Hemisphere: " + hemisphere.volume());
}
}
File: Exp9PostLab2.java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP9 : Calculate area of Rectangle and Circle using abstract class
*/

abstract class Shape {


abstract double area();
}

class Rectangle extends Shape {


double length, width;

Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

@Override
double area() {
return length * width;
}
}

class Square extends Shape {


double side;

Square(double side) {
this.side = side;
}

@Override
double area() {
return side * side;
}
}

public class Exp9PostLab2 {


public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 3);
Square square = new Square(4);
System.out.println("Area of Rectangle: " + rectangle.area());
System.out.println("Area of Square: " + square.area());
}
}

You might also like