yash_java
yash_java
java
/*
NAME : Yash Pandey
UIN : 231P083
ROLL NO : 23
EXP1 : Write a program to perform Menu Driven Arithmetic Operation.
*/
import java.util.Scanner;
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;
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;
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;
}
class Account {
int accountNumber;
String accountHolderName;
double 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 Exp4PostLab4() {
this.num1 = 56;
this.num2 = 98;
}
AreaOf() {
radius = 1.0;
}
AreaOf(double r) {
radius = r;
}
double findArea() {
return Math.PI * radius * radius;
}
}
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];
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();
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.*;
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.*;
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 {
class Exp6_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
class Exp6_2 {
public static void main(String[] args) {
Vector<String> shoppingList = new Vector<>();
Scanner sc = new Scanner(System.in);
Person(String name) {
this.name = name;
}
void displayPersonInfo() {
System.out.println("Name: " + name);
}
}
void displayOnlineInfo() {
System.out.println("Course: " + course);
}
}
void displaySavingAccountInfo() {
System.out.println("Balance: $" + balance);
}
}
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();
class Radius {
double radius;
class RadiusBase {
double radius;
@Override
public void display() {
System.out.println("Area of Circle: " + findArea());
}
}
void displayStudentInfo() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
}
}
// Single inheritance
class Test extends Student {
int marks1, 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;
void displayTotalMarks() {
System.out.println("Total Marks (including sports): " + totalMarks);
}
}
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();
}
@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();
}
}
@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];
}
}
}
}
import java.util.Scanner;
interface Matrix {
int M = 2, N = 2;
void readMatrix();
void displayMatrix();
void sum_Diagonal_Matrix();
}
@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);
}
}
void calculateArea() {
int area = length * breadth;
System.out.println("Area of Rectangle: " + area);
}
}
Circle(double radius) {
this.radius = radius;
}
void calculateArea() {
double area = Math.PI * radius * radius;
System.out.println("Area of Circle: " + area);
}
}
Sphere(double radius) {
this.radius = radius;
}
@Override
double volume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}
Hemisphere(double radius) {
this.radius = radius;
}
@Override
double volume() {
return (2.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}
@Override
double area() {
return length * width;
}
}
Square(double side) {
this.side = side;
}
@Override
double area() {
return side * side;
}
}