ASS1
ASS1
Assingnment 1
1] Create a class that has a roll no, name, marks [5] array to store marks of five
subjects as data members and void getinfo (), void putinfo () and float calCPI ().
If students have marks less than 35 in any subjects then he will fail otherwise
calculate CPI. And display the student's details.
import java.util.Scanner;
class Student {
public Student() {
rollNo = "";
name = "";
rollNo = scanner.nextLine();
name = scanner.nextLine();
marks[i] = scanner.nextFloat();
System.out.print("Marks: ");
System.out.println();
float totalMarks = 0;
totalMarks += mark;
return cpi;
failed = true;
break;
if (failed) {
} else {
student1.getInfo();
student1.putInfo();
student1.displayResult();
OUTPUT :
2] Create a class Car that has Model no, Name, color & cost as Data members
and create void getCarDetails () and void showCardDetails () as member
function. Read details for 3 cars and display it.
import java.util.Scanner;
class Car {
modelNo = scanner.nextLine();
name = scanner.nextLine();
color = scanner.nextLine();
cost = scanner.nextDouble();
cars[i].getCarDetails();
System.out.println("\nCar Details:");
cars[i].showCarDetails();
OUTPUT :
12202040501019 Dharmik Rabadiya
3] Define a class Sales with the name and sales of salesmen as data members.
Calculate and print the Name, sales and commission, where commission is
Rs .10 per 1000, if sales are at least Rs. 25000 or more and Rs. 5 otherwise. Use
appropriate member functions.
import java.util.Scanner;
class Sales {
name = scanner.nextLine();
sales = scanner.nextDouble();
double commission;
} else {
salesman1.getSalesDetails();
salesman2.getSalesDetails();
salesman3.getSalesDetails();
System.out.println("\nSales Details:");
salesman1.showSalesDetails();
salesman2.showSalesDetails();
salesman3.showSalesDetails();
OUTPUT :
4] class Stack {
private char[] array;
this.size = size;
top = -1;
if (!isFull()) {
array[++top] = ch;
} else {
if (!isEmpty()) {
return array[top--];
} else {
return '\0';
}
12202040501019 Dharmik Rabadiya
stack.push('A');
stack.push('B');
stack.push('C');
stack.push('D');
stack.push('E');
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
OUTPUT :
5] import java.util.Arrays;
class Array {
public Array() {
size = 10;
this.size = size;
data[size - i - 1] = temp;
Arrays.sort(data);
System.out.println("Array elements:");
System.out.println();
}
12202040501019 Dharmik Rabadiya
if (data[i] == element) {
return i;
return size;
array.display();
array.display();
array.reverseArray();
array.display();
array.sorting();
array.display();
int element = 8;
if (index != -1) {
} else {
OUTPUT
6]
import java.util.Scanner;
class Book {
double price;
int stockPosition;
Book(String title, String author, double price, String publisher, int stockPosition) {
this.stockPosition = stockPosition;
} void displayDetails() {
class Inventory {
Book[] books;
Inventory(Book[] books) {
12202040501019 Dharmik Rabadiya
b.stockPosition > 0) {
System.out.println("Required copies are available.\nThe total cost of the requested copies is: " +
Book[] books = {
while (true) {
System.out.println("\nEnter the title or author of the book you are searching for (type 'exit' to quit): ");
if (searchString.equals("exit")) break;
inventory.searchBook(searchString);
scanner.close();
Output:Enter the title or author of the book you are searching for (type 'exit' to quit):
12202040501019 Dharmik Rabadiya
Book1
Successful Transactions: 1
Unsuccessful Transactions: 0
Enter the title or author of the book you are searching for (type 'exit' to quit):
Book3
Successful Transactions: 1
Unsuccessful Transactions: 1
Enter the title or author of the book you are searching for (type 'exit' to quit):
Book5
Successful Transactions: 1
Unsuccessful Transactions: 2
7]
import java.util.ArrayList;
import java.util.Scanner;
class Student {
int rollNumber;
String name;
int[] marks;
this.rollNumber = rollNumber;
this.name = name;
12202040501019 Dharmik Rabadiya
this.marks = marks;
double calculateTotalMarks() {
double total = 0;
total += mark;
return total;
void displayDetails() {
System.out.println("Marks:");
ArrayList<Student> students;
StudentManagementSystem() {
void displayAllStudents() {
student.displayDetails();
void calculateAverageMarks() {
totalMarks[i] += student.marks[i];
void displayStudentWithHighestTotalMarks() {
topStudent = student;
topStudent.displayDetails();
12202040501019 Dharmik Rabadiya
void displayStudentWithLowestTotalMarks() {
bottomStudent = student;
bottomStudent.displayDetails();
if (student.rollNumber == rollNumber) {
student.displayDetails();
found = true;
break;
if (!found) {
if (student.rollNumber == rollNumber) {
student.marks = newMarks;
return;
12202040501019 Dharmik Rabadiya
if (student.rollNumber == rollNumber) {
students.remove(student);
return;
while (true) {
System.out.println("3. Calculate and display average marks of all students in each subject");
System.out.println("4. Find and display details of the student with the highest total marks");
System.out.println("5. Find and display details of the student with the lowest total marks");
System.out.println("6. Search for a student by roll number and display their details");
System.out.println("9. Exit");
switch (choice) {
case 1:
marks[i] = scanner.nextInt();
break;
case 2:
system.displayAllStudents();
break;
case 3:
system.calculateAverageMarks();
break;
case 4:
system.displayStudentWithHighestTotalMarks();
break;
case 5:
system.displayStudentWithLowestTotalMarks();
break;
case 6:
system.searchStudentByRollNumber(searchRollNumber);
break;
case 7:
newMarks[i] = scanner.nextInt();
system.updateMarksByRollNumber(updateRollNumber, newMarks);
break;
case 8:
system.deleteStudentByRollNumber(deleteRollNumber);
break;
case 9:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
Output:
mathematica
Copy code
4. Find and display details of the student with the highest total marks
5. Find and display details of the student with the lowest total marks
9. Exit
8] write a program to calculate and display the sum of all elements in the array.
import java.util.Scanner;
array[i] = scanner.nextInt();
int sum = 0;
sum += array[i];
scanner.close();