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

OOPs Hard Questions

The document discusses an assignment to create a Student class with attributes like roll number, name, department and marks. It involves storing details of 5 students in an array of Student objects. The assignment requires finding the student with the highest and lowest marks, students with marks above average, and sorting the student array using bubble sort and quicksort algorithms. It also demonstrates auto-boxing and unboxing of primitive data types using wrapper classes.

Uploaded by

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

OOPs Hard Questions

The document discusses an assignment to create a Student class with attributes like roll number, name, department and marks. It involves storing details of 5 students in an array of Student objects. The assignment requires finding the student with the highest and lowest marks, students with marks above average, and sorting the student array using bubble sort and quicksort algorithms. It also demonstrates auto-boxing and unboxing of primitive data types using wrapper classes.

Uploaded by

aditya.cse121118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment - 8

Name – Sourasish Mondal


University Roll No. – 11500221053
Stream – CSE Section – B Group – 2
Subject – Object Oriented Programming
Subject Code – PCC-CS593
Objec&ve:

To understand the concept of construc0ng array of objects and actualize. In this


assignment we also analyse the appropriate use of wrapper class and actualize.

Problem Statement:

1. Create a Student class having roll no., name, dept., marks. Use array of objects
to store details of 5 students. List the name of the student
a) having highest marks
b) lowest marks
c) Marks more than average.

2. Implement 2 sor0ng algorithms for sor0ng student array.

Code:
import java.util.Scanner;

class Student {
private int rollNo;
private String name;
private String department;
private int marks;

public Student(int rollNo, String name, String department, int marks) {


this.rollNo = rollNo;
this.name = name;
this.department = department;
this.marks = marks;
}

public int getMarks() {


return marks;
}

public String getName() {


return name;
}
}
public class assignment8 {
public static void main(String[] args) {
Student[] students = new Student[5];
Scanner scanner = new Scanner(System.in);

// Input details for 5 students


for (int i = 0; i < 5; i++) {
System.out.println("Enter details for student " + (i + 1));
System.out.print("Roll No: ");
int rollNo = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Department: ");
String department = scanner.nextLine();
System.out.print("Marks: ");
int marks = scanner.nextInt();
students[i] = new Student(rollNo, name, department, marks);
}

// Find and display the student with the highest marks


Student highestMarksStudent = findStudentWithHighestMarks(students);
System.out.println("Student with highest marks: " + highestMarksStudent.getName());

// Find and display the student with the lowest marks


Student lowestMarksStudent = findStudentWithLowestMarks(students);
System.out.println("Student with lowest marks: " + lowestMarksStudent.getName());

// Find and display students with marks greater than the average
double averageMarks = calculateAverageMarks(students);
System.out.println("Students with marks greater than the average:");
for (Student student : students) {
if (student.getMarks() > averageMarks) {
System.out.println(student.getName());
}
}
bubbleSort(students);
System.out.println("Students sorted by marks : ascending order - Bubble Sort");
for(Student student : students){
System.out.println(student.getName() + ": " + student.getMarks());
}
quicksort(students, 0, students.length - 1);
System.out.println("Students sorted by marks (Descending - Quick Sort):");
for (Student student : students) {
System.out.println(student.getName() + ": " + student.getMarks());
}

public static Student findStudentWithHighestMarks(Student[] students) {


Student highestMarksStudent = students[0];
for (int i = 1; i < students.length; i++) {
if (students[i].getMarks() > highestMarksStudent.getMarks()) {
highestMarksStudent = students[i];
}
}
return highestMarksStudent;
}
public static Student findStudentWithLowestMarks(Student[] students) {
Student lowestMarksStudent = students[0];
for (int i = 1; i < students.length; i++) {
if (students[i].getMarks() < lowestMarksStudent.getMarks()) {
lowestMarksStudent = students[i];
}
}
return lowestMarksStudent;
}

public static double calculateAverageMarks(Student[] students) {


double sum = 0;
for (Student student : students) {
sum += student.getMarks();
}
return sum / students.length;
}

public static void bubbleSort(Student[] students) {


int n = students.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (students[j].getMarks() > students[j + 1].getMarks()) {
// Swap students[j] and students[j+1]
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
swapped = true;
}
}
// If no two elements were swapped in inner loop, the array is already sorted
if (!swapped) {
break;
}
}
}
public static void quicksort(Student[] students, int low, int high){
if(low < high){
int pivotIndex = partition(students, low, high);
quicksort(students, low, pivotIndex - 1);
quicksort(students, pivotIndex + 1, high);
}
}

public static int partition(Student[] students, int low, int high){


double pivot = students[high].getMarks();
int i = low - 1;
for(int j = low; j < high; j++){
if(students[j].getMarks() >= pivot){
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
Student temp = students[i + 1];
students[i + 1] = students[high];
students[high] = temp;
return i + 1;
}
}

Output:

Taking input of students: Highest, Lowest and average mark:

Bubble Sort and Quick Sort:


3. Use wrapper class for explaining auto-boxing and unboxing.

Code:
public class assignment8iii {
public static void main(String[] args) {
// Auto-boxing: Converting primitive types to wrapper classes
Integer intWrapper = 42; // int to Integer
Double doubleWrapper = 3.14; // double to Double
Character charWrapper = 'A'; // char to Character
Boolean boolWrapper = true; // boolean to Boolean

// Unboxing: Converting wrapper classes to primitive types


int intValue = intWrapper; // Integer to int
double doubleValue = doubleWrapper; // Double to double
char charValue = charWrapper; // Character to char
boolean boolValue = boolWrapper; // Boolean to boolean

// Printing the values


System.out.println("Auto-boxing:");
System.out.println("Integer Wrapper: " + intWrapper);
System.out.println("Double Wrapper: " + doubleWrapper);
System.out.println("Character Wrapper: " + charWrapper);
System.out.println("Boolean Wrapper: " + boolWrapper);

System.out.println("\nUnboxing:");
System.out.println("int Value: " + intValue);
System.out.println("double Value: " + doubleValue);
System.out.println("char Value: " + charValue);
System.out.println("boolean Value: " + boolValue);
}
}

Output:

You might also like