Upload files to "OOP/Java/Lab/Week5"
This commit is contained in:
parent
cb924d0b60
commit
8c2e55a281
61
OOP/Java/Lab/Week5/Book.java
Normal file
61
OOP/Java/Lab/Week5/Book.java
Normal file
@ -0,0 +1,61 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class Book {
|
||||
|
||||
String title;
|
||||
String author;
|
||||
int edition;
|
||||
|
||||
public Book(String title, String author, int edition) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.edition = edition;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"Title: %s, Author: %s, Edition: %d",
|
||||
title,
|
||||
author,
|
||||
edition
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Enter the number of books: ");
|
||||
int numBooks = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
Book[] books = new Book[numBooks];
|
||||
|
||||
for (int i = 0; i < numBooks; i++) {
|
||||
System.out.println("Enter details for book " + (i + 1) + ":");
|
||||
System.out.print("Title: ");
|
||||
String title = sc.nextLine();
|
||||
System.out.print("Author: ");
|
||||
String author = sc.nextLine();
|
||||
System.out.print("Edition: ");
|
||||
int edition = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
books[i] = new Book(title, author, edition);
|
||||
}
|
||||
|
||||
System.out.print("Enter author name to search: ");
|
||||
String searchAuthor = sc.nextLine();
|
||||
|
||||
for (Book book : books) {
|
||||
if (book.getAuthor().equals(searchAuthor)) {
|
||||
System.out.println(book);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
101
OOP/Java/Lab/Week5/IntArray.java
Normal file
101
OOP/Java/Lab/Week5/IntArray.java
Normal file
@ -0,0 +1,101 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class IntArray {
|
||||
|
||||
private int[] arr;
|
||||
|
||||
public IntArray() {
|
||||
arr = new int[10];
|
||||
}
|
||||
|
||||
public void inputValues() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Enter 10 integer values:");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
arr[i] = scanner.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
public void displayValues() {
|
||||
System.out.println("Array values:");
|
||||
for (int value : arr) {
|
||||
System.out.print(value + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public void displayLargest() {
|
||||
int largest = arr[0];
|
||||
for (int i = 1; i < arr.length; i++) {
|
||||
if (arr[i] > largest) {
|
||||
largest = arr[i];
|
||||
}
|
||||
}
|
||||
System.out.println("Largest value: " + largest);
|
||||
}
|
||||
|
||||
public void displayAverage() {
|
||||
int sum = 0;
|
||||
for (int value : arr) {
|
||||
sum += value;
|
||||
}
|
||||
double average = (double) sum / arr.length;
|
||||
System.out.println("Average value: " + average);
|
||||
}
|
||||
|
||||
public void sortArray() {
|
||||
for (int i = 0; i < arr.length - 1; i++) {
|
||||
for (int j = 0; j < arr.length - i - 1; j++) {
|
||||
if (arr[j] > arr[j + 1]) {
|
||||
int temp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Array sorted in ascending order.");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
IntArray intArray = new IntArray();
|
||||
java.util.Scanner scanner = new java.util.Scanner(System.in);
|
||||
int choice;
|
||||
|
||||
do {
|
||||
System.out.println("\nMenu:");
|
||||
System.out.println("1. Input Values");
|
||||
System.out.println("2. Display Values");
|
||||
System.out.println("3. Display Largest Value");
|
||||
System.out.println("4. Display Average");
|
||||
System.out.println("5. Sort Array");
|
||||
System.out.println("0. Exit");
|
||||
System.out.print("Enter your choice: ");
|
||||
choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
intArray.inputValues();
|
||||
break;
|
||||
case 2:
|
||||
intArray.displayValues();
|
||||
break;
|
||||
case 3:
|
||||
intArray.displayLargest();
|
||||
break;
|
||||
case 4:
|
||||
intArray.displayAverage();
|
||||
break;
|
||||
case 5:
|
||||
intArray.sortArray();
|
||||
break;
|
||||
case 0:
|
||||
System.out.println("Exiting program. Goodbye!");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Invalid choice. Please try again.");
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
}
|
111
OOP/Java/Lab/Week5/Stack.java
Normal file
111
OOP/Java/Lab/Week5/Stack.java
Normal file
@ -0,0 +1,111 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class Stack {
|
||||
|
||||
private int maxSize;
|
||||
private int[] stackArray;
|
||||
private int top;
|
||||
|
||||
public Stack(int size) {
|
||||
maxSize = size;
|
||||
stackArray = new int[maxSize];
|
||||
top = -1;
|
||||
}
|
||||
|
||||
public void push(int value) {
|
||||
if (isFull()) {
|
||||
System.out.println("Stack is full");
|
||||
} else {
|
||||
top++;
|
||||
stackArray[top] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int pop() {
|
||||
if (isEmpty()) {
|
||||
System.out.println("Stack is empty");
|
||||
return -1;
|
||||
} else {
|
||||
return stackArray[top--];
|
||||
}
|
||||
}
|
||||
|
||||
public int peek() {
|
||||
if (isEmpty()) {
|
||||
System.out.println("Stack is empty");
|
||||
return -1;
|
||||
} else {
|
||||
return stackArray[top];
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return (top == -1);
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
return (top == maxSize - 1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Stack stack = new Stack(5);
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int choice;
|
||||
|
||||
do {
|
||||
System.out.println("\nStack Operations:");
|
||||
System.out.println("1. Push");
|
||||
System.out.println("2. Pop");
|
||||
System.out.println("3. Peek");
|
||||
System.out.println("4. Check if empty");
|
||||
System.out.println("5. Check if full");
|
||||
System.out.println("0. Exit");
|
||||
System.out.print("Enter your choice: ");
|
||||
|
||||
choice = sc.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
System.out.print("Enter value to push: ");
|
||||
int value = sc.nextInt();
|
||||
if (stack.isFull()) {
|
||||
System.out.println(
|
||||
"Cannot push anymore. Stack is full."
|
||||
);
|
||||
} else {
|
||||
stack.push(value);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (stack.isEmpty()) {
|
||||
System.out.println("Cannot pop. Stack is empty.");
|
||||
} else {
|
||||
int poppedValue = stack.pop();
|
||||
if (poppedValue != -1) {
|
||||
System.out.println("Popped value: " + poppedValue);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
int peekedValue = stack.peek();
|
||||
if (peekedValue != -1) {
|
||||
System.out.println("Top value: " + peekedValue);
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
System.out.println("Is stack empty? " + stack.isEmpty());
|
||||
break;
|
||||
case 5:
|
||||
System.out.println("Is stack full? " + stack.isFull());
|
||||
break;
|
||||
case 0:
|
||||
System.out.println("Exiting...");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Invalid choice. Please try again.");
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
sc.close();
|
||||
}
|
||||
}
|
93
OOP/Java/Lab/Week5/StuRes.java
Normal file
93
OOP/Java/Lab/Week5/StuRes.java
Normal file
@ -0,0 +1,93 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class StuRes {
|
||||
|
||||
int[][] marks = new int[3][3];
|
||||
int[] totalMarks = new int[3];
|
||||
int[] rollNumbers = new int[3];
|
||||
|
||||
void storeMarks(int[][] studentMarks) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
System.arraycopy(studentMarks[i], 0, marks[i], 0, 3);
|
||||
}
|
||||
}
|
||||
|
||||
void calculateTotalMarks() {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
totalMarks[i] = 0;
|
||||
for (int j = 0; j < 3; j++) {
|
||||
totalMarks[i] += marks[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void findHighestMarksInSubjects() {
|
||||
for (int subject = 0; subject < 3; subject++) {
|
||||
int highestMark = marks[0][subject];
|
||||
int highestRollNo = rollNumbers[0];
|
||||
for (int student = 1; student < 3; student++) {
|
||||
if (marks[student][subject] > highestMark) {
|
||||
highestMark = marks[student][subject];
|
||||
highestRollNo = rollNumbers[student];
|
||||
}
|
||||
}
|
||||
System.out.println(
|
||||
"Highest mark in Subject " +
|
||||
(subject + 1) +
|
||||
": " +
|
||||
highestMark +
|
||||
" (Roll No: " +
|
||||
highestRollNo +
|
||||
")"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void findHighestTotalMarks() {
|
||||
int highestTotal = totalMarks[0];
|
||||
int highestRollNo = rollNumbers[0];
|
||||
for (int i = 1; i < 3; i++) {
|
||||
if (totalMarks[i] > highestTotal) {
|
||||
highestTotal = totalMarks[i];
|
||||
highestRollNo = rollNumbers[i];
|
||||
}
|
||||
}
|
||||
System.out.println(
|
||||
"Highest total marks: " +
|
||||
highestTotal +
|
||||
" (Roll No: " +
|
||||
highestRollNo +
|
||||
")"
|
||||
);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
StuRes studentResults = new StuRes();
|
||||
int[][] studentMarks = new int[3][3];
|
||||
|
||||
System.out.println("Enter details for 3 students:");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
System.out.print("Enter Roll Number for Student " + (i + 1) + ": ");
|
||||
studentResults.rollNumbers[i] = scanner.nextInt();
|
||||
System.out.println(
|
||||
"Enter marks for Student with Roll No " +
|
||||
studentResults.rollNumbers[i] +
|
||||
":"
|
||||
);
|
||||
for (int j = 0; j < 3; j++) {
|
||||
System.out.print("Subject " + (j + 1) + ": ");
|
||||
studentMarks[i][j] = scanner.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
studentResults.storeMarks(studentMarks);
|
||||
studentResults.calculateTotalMarks();
|
||||
|
||||
System.out.println("\nResults:");
|
||||
studentResults.findHighestMarksInSubjects();
|
||||
studentResults.findHighestTotalMarks();
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
}
|
47
OOP/Java/Lab/Week5/Swap.java
Normal file
47
OOP/Java/Lab/Week5/Swap.java
Normal file
@ -0,0 +1,47 @@
|
||||
class Swap {
|
||||
|
||||
static void swap(int a, int b) {
|
||||
int temp = a;
|
||||
a = b;
|
||||
b = temp;
|
||||
System.out.println("Inside swap method: a = " + a + ", b = " + b);
|
||||
}
|
||||
|
||||
static void swap(int[] arr) {
|
||||
int temp = arr[0];
|
||||
arr[0] = arr[1];
|
||||
arr[1] = temp;
|
||||
System.out.println(
|
||||
"Inside swap method: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
||||
);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
java.util.Scanner scanner = new java.util.Scanner(System.in);
|
||||
|
||||
System.out.print("Enter value for x: ");
|
||||
int x = scanner.nextInt();
|
||||
System.out.print("Enter value for y: ");
|
||||
int y = scanner.nextInt();
|
||||
|
||||
System.out.println("Before swap: x = " + x + ", y = " + y);
|
||||
swap(x, y);
|
||||
System.out.println("After swap: x = " + x + ", y = " + y);
|
||||
|
||||
int[] arr = new int[2];
|
||||
System.out.print("\nEnter value for arr[0]: ");
|
||||
arr[0] = scanner.nextInt();
|
||||
System.out.print("Enter value for arr[1]: ");
|
||||
arr[1] = scanner.nextInt();
|
||||
|
||||
System.out.println(
|
||||
"Before swap: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
||||
);
|
||||
swap(arr);
|
||||
System.out.println(
|
||||
"After swap: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
||||
);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user