0% found this document useful (0 votes)
4 views49 pages

Menu Driven Arithmetic Operations in 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 views49 pages

Menu Driven Arithmetic Operations in 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

File: Exp1.

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

import [Link];

public class Exp1 {


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

[Link]("Enter two numbers: ");


double num1 = [Link]();
double num2 = [Link]();

[Link]("Choose an operation:");
[Link]("1. Addition (+)");
[Link]("2. Subtraction (-)");
[Link]("3. Multiplication (*)");
[Link]("4. Division (/)");
[Link]("5. Modulus (%)");
int choice = [Link]();

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

public class Exp1PostLab1 {


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

[Link]("Enter first number: ");


num1 = [Link]();
[Link]("Enter second number: ");
num2 = [Link]();
[Link]("Choose operation (+, -, *, /): ");
choice = [Link]().charAt(0);

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

public class Exp1PostLab2 {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter month number (1-12): ");
int month = [Link]();
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;
}

[Link]("Month: " + monthName);


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

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;
}
[Link]("* ");
}
[Link]();
}
}
}
File: [Link]
/*
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++) {
[Link](i);
if (i == 5) break;
}
}
}
File: [Link]
/*
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;
[Link](i);
}
}
}
File: [Link]
/*
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++) {
[Link](col + " ");
}
[Link]();
}
}
}
File: [Link]
/*
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 [Link];

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 {
[Link]("Insufficient balance.");
}
}

void checkBalance() {
[Link]("Balance: " + balance);
}

void display() {
[Link]("Account Number: " + accountNumber);
[Link]("Account Holder Name: " + accountHolderName);
[Link]("Balance: " + balance);
}
}

public class Exp3 {


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

[Link]("Enter account number:");


int accNo = [Link]();
[Link]();
[Link]("Enter account holder name:");
String name = [Link]();
[Link]("Enter initial balance:");
double balance = [Link]();

[Link](accNo, name, balance);


[Link]();

[Link]("Enter amount to deposit:");


double depositAmount = [Link]();
[Link](depositAmount);
[Link]();

[Link]("Enter amount to withdraw:");


double withdrawAmount = [Link]();
[Link](withdrawAmount);
[Link]();
}
}
File: [Link]
/*
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;
[Link]("GCD of " + num1 + " and " + num2 + " is: " + gcd(num1, num2));
}
}
File: [Link]
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP3 : Create a Class Account to Perform Insert, Deposit, and Withdrawal Operations
*/
import [Link];

public class Exp3PostLab2 {


static class Circle {
private double radius;

public void acceptRadius() {


Scanner sc = new Scanner([Link]);
[Link]("Enter radius: ");
radius = [Link]();
}

public double calculateArea() {


return [Link] * radius * radius;
}

public void displayArea() {


[Link]("The area of the circle is: " + calculateArea());
}
}

public static void main(String[] args) {


Circle circle = new Circle();
[Link]();
[Link]();
}
}
File: [Link]
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP4 : Find Area of Circle Using Method Overloading and Constructor Overloading
*/
import [Link];

public class Exp4PostLab1 {


public double area(double radius) {
return [Link] * 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([Link]);

[Link]("Enter radius of circle: ");


double radius = [Link]();
[Link]("Area of Circle: " + [Link](radius));

[Link]("Enter length and width of rectangle: ");


double length = [Link]();
double width = [Link]();
[Link]("Area of Rectangle: " + [Link](length, width));

[Link]("Enter base and height of triangle: ");


double base = [Link]();
double height = [Link]();
[Link]("Area of Triangle: " + [Link](base, height, true));
}
}
File: [Link]
/*
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();
[Link]("Sum of integers: " + [Link](10, 20));
[Link]("Concatenation of strings: " + [Link]("Hello, ", "World!"));
}
}
File: [Link]
/*
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) {


[Link]("Area of Circle: " + ([Link] * radius * radius));
}

public Exp4PostLab3(double length, double breadth) {


[Link]("Area of Rectangle: " + (length * breadth));
}

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


[Link]("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: [Link]
/*
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() {


[Link]("GCD of " + num1 + " and " + num2 + " is: " + calculateGCD(num1,
num2));
}

public static void main(String[] args) {


Exp4PostLab4 obj = new Exp4PostLab4();
[Link]();
}
}
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 [Link] * 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;

[Link]("Area with default PI: " + [Link](radius));


[Link]("Area with custom PI: " + [Link](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 [Link] * radius * radius;
}
}

public class Exp4_2 {


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

[Link]("Area of default circle: " + [Link]());


[Link]("Area of custom circle: " + [Link]());
}
}
File: [Link]
/*
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++) {
[Link](result[i][j] + " ");
}
[Link]();
}
}
}
File: [Link]
/*
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
}

[Link]("Sum of diagonal elements: " + sum);


}
}
File: [Link]
/*
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 < [Link](); i++) {


if ([Link]([Link](i)) != -1) {
vowelCount++;
}
}

[Link]("Number of vowels: " + vowelCount);


}
}
File: [Link]
/*
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";

[Link]("Length of the string: " + [Link]());


[Link]("Character at index 4: " + [Link](4));
[Link]("Substring (0, 5): " + [Link](0, 5));
[Link]("Index of 'o': " + [Link]('o'));
[Link]("String in uppercase: " + [Link]());
}
}
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 [Link];

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([Link]);

[Link]("Enter the values of the first matrix:");


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

[Link]("Enter the values of the second matrix:");


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

// Adding the matrices


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

[Link]("The resultant matrix after addition is:");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
[Link](add[i][j] + " ");
}
[Link]();
}
}
}
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 [Link];

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

[Link]("Enter a string:");
String inputString = [Link]();

[Link]("Enter the character to find its frequency:");


char letter = [Link]().charAt(0);

int count = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == letter) {
count++;
}
}

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

public class Exp6PostLab1 {


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

do {
[Link]("1. Add Student Name\n2. Remove Student Name\n3. Display
Names\n4. Exit");
choice = [Link]();
[Link](); // consume newline

switch (choice) {
case 1:
[Link]("Enter student name: ");
String name = [Link]();
[Link](name);
break;
case 2:
[Link]("Enter student name to remove: ");
String removeName = [Link]();
[Link](removeName);
break;
case 3:
Enumeration<String> names = [Link]();
[Link]("Student Names:");
while ([Link]()) {
[Link]([Link]());
}
break;
} } while (choice != 4);
[Link]();
}
}
File: [Link]

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

public class Exp6PostLab2 {


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

do {
[Link]("1. Add Item\n2. Delete Item\n3. Display Items\n4. Exit");
choice = [Link]();
[Link](); // consume newline

switch (choice) {
case 1:
[Link]("Enter item name: ");
String item = [Link]();
[Link]("Enter position to insert item: ");
int position = [Link]();
[Link]();
[Link](position, item);
break;
case 2:
[Link]("Enter item name to delete: ");
String removeItem = [Link]();
[Link](removeItem);
break;
case 3:
[Link]("Shopping List: " + shoppingList);
break;
} } while (choice != 4);
[Link]();
}}
File: [Link]

/*
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");
[Link]("Original String: " + sb);

[Link](5, 11); // Deletes " World"


[Link]("After deletion: " + sb);
}
}
File: [Link]
/*
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");
[Link]("Original String: " + sb);

[Link](6, 11, "Java"); // Replaces "World" with "Java"


[Link]("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 [Link];

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

[Link]("Enter a string to check if it's a palindrome:");


String inputString = [Link]();

// Append the string to StringBuffer and reverse it


StringBuffer sb = new StringBuffer(inputString);
[Link]();

// Convert reversed StringBuffer to String


String reversedString = [Link]();

// Compare the original string with the reversed string


if ([Link](reversedString)) {
[Link](inputString + " is a palindrome.");
} else {
[Link](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 [Link];
import [Link];

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

[Link]("Enter the number of items you want to add:");


int n = [Link]();
[Link](); // Consume the newline

[Link]("Enter the items:");


for (int i = 0; i < n; i++) {
String item = [Link]();
[Link](item);
}

[Link]("Items in the vector: " + shoppingList);

// Copy vector elements into a string


String allItems = [Link](", ", shoppingList);

[Link]("All items as a single string: " + allItems);


}
}
File: [Link]
/*
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) {
[Link] = name;
}

void displayPersonInfo() {
[Link]("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
[Link] = course;
}

void displayOnlineInfo() {
[Link]("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
[Link] = balance;
}

void displaySavingAccountInfo() {
[Link]("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
[Link] = accountNumber;
}

void displayAccountDetails() {
[Link]("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");
[Link]();
[Link]();

[Link]();

// Creating object of AccountDetails class (Multilevel Inheritance)


AccountDetails account = new AccountDetails("Alice", 1500.75, "ACC12345");
[Link]();
[Link]();
[Link]();
}
}
File: [Link]
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP7 : Single and Multilevel Inheritance using super keyword
*/
import [Link];

class Radius {
double radius;

public void acceptRadius() {


Scanner scanner = new Scanner([Link]);
[Link]("Enter radius: ");
radius = [Link]();
}
}

class Circle extends Radius {


public double findArea() {
return [Link] * radius * radius;
}
}

class Sphere extends Circle {


public double findVolume() {
return (4.0 / 3.0) * [Link] * [Link](radius, 3);
}

public void displayVolume() {


[Link]("Volume of Sphere: " + findVolume());
}
}

public class Exp7PostLab1 {

public static void main(String[] args) {


Sphere sphere = new Sphere();
[Link]();
[Link]();
}
}
File: [Link]
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
*/
import [Link];

class RadiusBase {
double radius;

public void acceptRadius() {


Scanner scanner = new Scanner([Link]);
[Link]("Enter radius: ");
radius = [Link]();
}

public void display() {


[Link]("This is the base class display method.");
}
}

class CircleDerived extends RadiusBase {


public double findArea() {
return [Link] * radius * radius;
}

@Override
public void display() {
[Link]("Area of Circle: " + findArea());
}
}

class SphereDerived extends CircleDerived {


public double findVolume() {
return (4.0 / 3.0) * [Link] * [Link](radius, 3);
}
@Override
public void display() {
[Link]("Volume of Sphere: " + findVolume());
}
}

public class Exp7PostLab2 {

public static void main(String[] args) {


SphereDerived sphere = new SphereDerived();
[Link]();
[Link]();
}
}
File: [Link]
/*
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) {


[Link] = name;
[Link] = rollNo;
}

void displayStudentInfo() {
[Link]("Name: " + name);
[Link]("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() {
[Link]("Marks1: " + marks1);
[Link]("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);
[Link] = marks1 + marks2 + sportsMarks;
}

public void displaySportsMarks() {


[Link]("Sports Marks: " + sportsMarks);
}

void displayTotalMarks() {
[Link]("Total Marks (including sports): " + totalMarks);
}
}

public class Exp8 {


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

[Link]();
[Link]();
[Link]();
[Link]();
}
}
File: [Link]
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP8 : Interface demonstrating the concept of multiple inheritance
*/

import [Link];

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([Link]);
[Link]("Enter elements for Matrix 1:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix1[i][j] = [Link]();
}
}

[Link]("Enter elements for Matrix 2:");


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

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

@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();
[Link]();
[Link]();
[Link]();
}
}
File: [Link]
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP8 : Interface demonstrating the concept of multiple inheritance
*/

import [Link];

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([Link]);
[Link]("Enter elements for the Matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = [Link]();
}
}
}

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

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

public class Exp8PostLab2 {


public static void main(String[] args) {
DiagonalMatrixOperations diagOps = new DiagonalMatrixOperations();
[Link]();
[Link]();
diagOps.sum_Diagonal_Matrix();
}
}
File: [Link]
/*
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) {


[Link] = length;
[Link] = breadth;
}

void calculateArea() {
int area = length * breadth;
[Link]("Area of Rectangle: " + area);
}
}

class Circle extends Shape {


double radius;

Circle(double radius) {
[Link] = radius;
}

void calculateArea() {
double area = [Link] * radius * radius;
[Link]("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);
[Link]();
[Link]();
}
}
File: [Link]
/*
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) {
[Link] = radius;
}

@Override
double volume() {
return (4.0 / 3.0) * [Link] * [Link](radius, 3);
}
}

class Hemisphere extends Shape {


double radius;

Hemisphere(double radius) {
[Link] = radius;
}

@Override
double volume() {
return (2.0 / 3.0) * [Link] * [Link](radius, 3);
}
}

public class Exp9PostLab1 {


public static void main(String[] args) {
Sphere sphere = new Sphere(5);
Hemisphere hemisphere = new Hemisphere(5);
[Link]("Volume of Sphere: " + [Link]());
[Link]("Volume of Hemisphere: " + [Link]());
}
}
File: [Link]
/*
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) {


[Link] = length;
[Link] = width;
}

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

class Square extends Shape {


double side;

Square(double side) {
[Link] = 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);
[Link]("Area of Rectangle: " + [Link]());
[Link]("Area of Square: " + [Link]());
}
}

Common questions

Powered by AI

Method overloading allows a single class to have multiple methods with the same name but different parameters, enabling the calculation of area for various shapes. In the provided examples, methods are overloaded to compute the area of a circle, rectangle, and triangle by passing different parameters such as radius, length and width, or base and height. This technique simplifies code management by using a single method name, increases readability, and leverages polymorphism to choose the appropriate method at runtime .

Method and constructor overloading improve code readability by using meaningful names for various behaviors acting on similar entities and contexts—facilitating a clean, intuitive API design. Overloaded methods provide flexible interfaces for different input configurations without cluttering code with multiple method names, making it easier to understand and maintain. Similarly, constructor overloading provides multiple initialization pathways, adapting software to varied scenarios and reducing constructor chaining complexity. By unifying and simplifying method calls, overloading lessens the cognitive load for developers maintaining or extending large applications and enhances consistency .

Constructor overloading allows the creation of objects with varying initial parameters, facilitating area calculation by initializing different geometrical entities such as circles, rectangles, and triangles with relevant dimensions. Unlike method overloading, constructor overloading binds area calculation operations explicitly at object creation, ensuring that each object is inherently linked with its type and the calculation logic. However, while effective for initializing state, constructor overloading lacks the reusability and flexibility of method overloading, which can process inputs variably without requiring new object instantiations .

The document demonstrates matrix operations through straightforward, nested loop algorithms that carefully ensure dimension compatibility and correctness. The implementations stress the importance of computational efficiency by optimizing data access patterns and loop iteration for element-wise operations, essential for swift execution in constrained environments. Accurate calculations are maintained by structured iteration and comprehensive dimension checking. Such practices reflect core algorithm design principles focused on performance optimization and error minimization while ensuring algorithm correctness .

Multilevel inheritance allows a hierarchical organization where derived classes further specify and enhance functionalities from their immediate superclasses. In the context of account management, 'AccountDetails' extends 'SavingAccount', which in turn extends 'Person'. This structure enables systematic accumulation of properties and behaviors—each level adding specificity, such as basic personal info, banking details, and specific account features. It organizes related functionalities logically across layers, promoting reusability, reducing redundancy, and facilitating future scalability .

Abstract classes in Java allow a base class to hold both implemented and non-implemented (abstract) methods, facilitating a uniform structure with shared code across subclasses, such as predefined methods for common behaviors like shape area computation. Interfaces, however, are contracts for behavior outlining methods that must be implemented in deriving classes, promoting multiple inheritance of contracts but not of state or implemented behavior. In geometric computations, abstract classes provide shared implementation and state, while interfaces define capabilities across potentially unrelated classes where implemented details vary significantly .

Vector operations provide a dynamic array data structure, allowing elements to be added, removed, and accessed efficiently. This dynamic nature is useful for managing datasets that change over time, such as student names or shopping lists in the examples. Vectors automatically resize, offering convenient insertion and deletion processes compared to static arrays. They support using enumerations for traversing elements, thus facilitating flexible, scalable data handling in programs needing dynamic data structure management .

Matrix addition is a straightforward element-wise operation, where corresponding elements of two matrices are summed independently. It requires matrices of identical dimensions and is straightforward to implement with nested loops. Conversely, matrix multiplication is more complex, involving an element of the resulting matrix being the dot product of a row from the first matrix and a column of the second, necessitating compatible matrices for multiplication (matching columns of the first to rows of the second). This complexity requires deeper nested iterations and impacts computational efficiency and application scope, such as transformations and computations in graphics and scientific computing .

Inheritance in Java allows a new class to take on properties and methods of existing classes, promoting code reuse and hierarchical class structures. In the document, a 'Person' class provides basic attributes, which are extended through 'Online' and 'SavingAccount' subclasses, adding specific fields and behaviors like 'course' and 'balance'. Furthermore, 'AccountDetails' use multilevel inheritance to further extend 'SavingAccount'. This allows for flexible design, easier maintenance, and enhanced extensibility. Inheritance abstracts general behaviors in the superclass, thus allowing enriched data and behavior encapsulation in subclasses without redundancy .

The StringBuffer class serves as a mutable sequence of characters, enhancing performance over regular strings with built-in methods like delete, append, and replace. The document exemplifies usage by altering strings efficiently, with changes visible after operations such as deletion or replacement, avoiding the immutability and inefficiency of traditional String modifications. Such dynamism facilitates tasks requiring frequent string alterations, such as building complex outputs iteratively or ensuring data integrity during transformation processes .

You might also like