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

ASS1

The document discusses assignments on Java programming concepts like classes, objects, methods, arrays, and stacks. It includes code examples to create classes for students, cars, salespeople, a stack, and an array. Methods are used to get input, display output, and perform operations on the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

ASS1

The document discusses assignments on Java programming concepts like classes, objects, methods, arrays, and stacks. It includes code examples to create classes for students, cars, salespeople, a stack, and an array. Methods are used to get input, display output, and perform operations on the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

12202040501019 Dharmik Rabadiya

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 {

private String rollNo;

private String name;

private float[] marks;

public Student() {

rollNo = "";

name = "";

marks = new float[5];

public void getInfo() {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Roll No: ");

rollNo = scanner.nextLine();

System.out.print("Enter Name: ");

name = scanner.nextLine();

for (int i = 0; i < 5; i++) {

System.out.print("Enter marks for subject " + (i + 1) + ": ");

marks[i] = scanner.nextFloat();

public void putInfo() {

System.out.println("Roll No: " + rollNo);


12202040501019 Dharmik Rabadiya

System.out.println("Name: " + name);

System.out.print("Marks: ");

for (int i = 0; i < 5; i++) {

System.out.print(marks[i] + " ");

System.out.println();

public float calCPI() {

float totalMarks = 0;

for (float mark : marks) {

totalMarks += mark;

float cpi = totalMarks / 500 * 10;

return cpi;

public void displayResult() {

boolean failed = false;

for (float mark : marks) {

if (mark < 35) {

failed = true;

break;

if (failed) {

System.out.println("Sorry! You have failed in one or more subjects.");

} else {

float cpi = calCPI();

System.out.println("CPI: " + cpi);

public class Main {


12202040501019 Dharmik Rabadiya

public static void main(String[] args) {

Student student1 = new Student();

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 {

private String modelNo;

private String name;

private String color;

private double cost;

public void getCarDetails() {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Model No: ");

modelNo = scanner.nextLine();

System.out.print("Enter Name: ");

name = scanner.nextLine();

System.out.print("Enter Color: ");

color = scanner.nextLine();

System.out.print("Enter Cost: ");


12202040501019 Dharmik Rabadiya

cost = scanner.nextDouble();

public void showCarDetails() {

System.out.println("Model No: " + modelNo);

System.out.println("Name: " + name);

System.out.println("Color: " + color);

System.out.println("Cost: " + cost);

public class Main {

public static void main(String[] args) {

Car[] cars = new Car[3];

for (int i = 0; i < 3; i++) {

System.out.println("Enter details for Car " + (i + 1));

cars[i] = new Car();

cars[i].getCarDetails();

System.out.println("\nCar Details:");

for (int i = 0; i < 3; i++) {

System.out.println("\nDetails of Car " + (i + 1));

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 {

private String name;

private double sales;

public void getSalesDetails() {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Name: ");

name = scanner.nextLine();

System.out.print("Enter Sales: ");

sales = scanner.nextDouble();

public void showSalesDetails() {

double commission;

if (sales >= 25000) {

commission = sales * 0.01; // Rs. 10 per 1000

} else {

commission = sales * 0.005; // Rs. 5 per 1000

System.out.println("Name: " + name);

System.out.println("Sales: Rs." + sales);

System.out.println("Commission: Rs." + commission);

public class Main {

public static void main(String[] args) {

Sales salesman1 = new Sales();


12202040501019 Dharmik Rabadiya

Sales salesman2 = new Sales();

Sales salesman3 = new Sales();

System.out.println("Enter details for Salesman 1:");

salesman1.getSalesDetails();

System.out.println("\nEnter details for Salesman 2:");

salesman2.getSalesDetails();

System.out.println("\nEnter details for Salesman 3:");

salesman3.getSalesDetails();

System.out.println("\nSales Details:");

System.out.println("\nDetails of Salesman 1:");

salesman1.showSalesDetails();

System.out.println("\nDetails of Salesman 2:");

salesman2.showSalesDetails();

System.out.println("\nDetails of Salesman 3:");

salesman3.showSalesDetails();

OUTPUT :

4] class Stack {
private char[] array;

private int size;

private int top;


12202040501019 Dharmik Rabadiya

public Stack(int size) {

this.size = size;

array = new char[size];

top = -1;

public void push(char ch) {

if (!isFull()) {

array[++top] = ch;

} else {

System.out.println("Stack is full. Cannot push element.");

public char pop() {

if (!isEmpty()) {

return array[top--];

} else {

System.out.println("Stack is empty. Cannot pop element.");

return '\0';

public boolean isEmpty() {

return top == -1;

public boolean isFull() {

return top == size - 1;

}
12202040501019 Dharmik Rabadiya

public class Main {

public static void main(String[] args) {

Stack stack = new Stack(5);

System.out.println("Pushing elements into the stack:");

stack.push('A');

stack.push('B');

stack.push('C');

stack.push('D');

stack.push('E');

stack.push('F'); // Trying to push when the stack is full

System.out.println("\nPopping elements from the stack:");

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());

System.out.println(stack.pop()); // Trying to pop when the stack is empty

OUTPUT :

5] import java.util.Arrays;

class Array {

private int[] data;


12202040501019 Dharmik Rabadiya

private int size;

public Array() {

size = 10;

data = new int[size];

public Array(int size) {

this.size = size;

data = new int[size];

public void reverseArray() {

for (int i = 0; i < size / 2; i++) {

int temp = data[i];

data[i] = data[size - i - 1];

data[size - i - 1] = temp;

public void sorting() {

Arrays.sort(data);

Method to display elements of the array

public void display() {

System.out.println("Array elements:");

for (int i = 0; i < size; i++) {

System.out.print(data[i] + " ");

System.out.println();

}
12202040501019 Dharmik Rabadiya

public int search(int element) {

for (int i = 0; i < size; i++) {

if (data[i] == element) {

return i;

return -1; // Element not found

public int size() {

return size;

public class Main {

public static void main(String[] args) {

Array array = new Array(5);

array.display();

array.data = new int[]{5, 3, 8, 2, 7};

array.display();

array.reverseArray();

System.out.println("Array after reversing:");

array.display();

array.sorting();

System.out.println("Array after sorting:");

array.display();

int element = 8;

int index = array.search(element);


12202040501019 Dharmik Rabadiya

if (index != -1) {

System.out.println("Element " + element + " found at index " + index);

} else {

System.out.println("Element " + element + " not found");

System.out.println("Size of array: " + array.size());

OUTPUT

6]
import java.util.Scanner;

class Book {

String title, author, publisher;

double price;

int stockPosition;

Book(String title, String author, double price, String publisher, int stockPosition) {

this.title = title; this.author = author;

this.price = price; this.publisher = publisher;

this.stockPosition = stockPosition;

} void displayDetails() {

System.out.println("Title: " + title + "\nAuthor: " + author + "\nPrice: " + price +

"\nPublisher: " + publisher + "\nStock Position: " + stockPosition);

class Inventory {

Book[] books;

int success, fail;

Inventory(Book[] books) {
12202040501019 Dharmik Rabadiya

this.books = books; success = 0; fail = 0;

void searchBook(String searchString) {

for (Book b : books) {

if ((b.title.equalsIgnoreCase(searchString) || b.author.equalsIgnoreCase(searchString)) &&

b.stockPosition > 0) {

System.out.println("Required copies are available.\nThe total cost of the requested copies is: " +

(b.price * b.stockPosition)); b.stockPosition--; success++; return;

System.out.println("Required copies not in stock."); fail++;

public class Bookshop {

public static void main(String[] args) {

Book[] books = {

new Book("Book1", "Author1", 10.0, "Publisher1", 5),

new Book("Book2", "Author2", 15.0, "Publisher2", 3),

new Book("Book3", "Author3", 20.0, "Publisher3", 0)

} Inventory inventory = new Inventory(books);

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("\nEnter the title or author of the book you are searching for (type 'exit' to quit): ");

String searchString = scanner.nextLine().toLowerCase();

if (searchString.equals("exit")) break;

inventory.searchBook(searchString);

System.out.println("\nSuccessful Transactions: " + inventory.success + "\nUnsuccessful Transactions: " +


inventory.fail);

scanner.close();

Output:Enter the title or author of the book you are searching for (type 'exit' to quit):
12202040501019 Dharmik Rabadiya

Book1

Required copies are available.

The total cost of the requested copies is: 10.0

Successful Transactions: 1

Unsuccessful Transactions: 0

Enter the title or author of the book you are searching for (type 'exit' to quit):

Book3

Required copies not in stock.

Successful Transactions: 1

Unsuccessful Transactions: 1

Enter the title or author of the book you are searching for (type 'exit' to quit):

Book5

Required copies not in stock.

Successful Transactions: 1

Unsuccessful Transactions: 2

7]
import java.util.ArrayList;

import java.util.Scanner;

class Student {

int rollNumber;

String name;

int[] marks;

Student(int rollNumber, String name, int[] marks) {

this.rollNumber = rollNumber;

this.name = name;
12202040501019 Dharmik Rabadiya

this.marks = marks;

double calculateTotalMarks() {

double total = 0;

for (int mark : marks) {

total += mark;

return total;

void displayDetails() {

System.out.println("\nRoll Number: " + rollNumber);

System.out.println("Name: " + name);

System.out.println("Marks:");

System.out.println("Mathematics: " + marks[0]);

System.out.println("English: " + marks[1]);

System.out.println("Science: " + marks[2]);

System.out.println("History: " + marks[3]);

System.out.println("Computer Science: " + marks[4]);

System.out.println("Total Marks: " + calculateTotalMarks());

public class StudentManagementSystem {

ArrayList<Student> students;

StudentManagementSystem() {

students = new ArrayList<>();

void addStudent(int rollNumber, String name, int[] marks) {

students.add(new Student(rollNumber, name, marks));


12202040501019 Dharmik Rabadiya

void displayAllStudents() {

System.out.println("\nDetails of all students:");

for (Student student : students) {

student.displayDetails();

void calculateAverageMarks() {

System.out.println("\nAverage marks of all students:");

int[] totalMarks = new int[5];

for (Student student : students) {

for (int i = 0; i < 5; i++) {

totalMarks[i] += student.marks[i];

int totalStudents = students.size();

for (int i = 0; i < 5; i++) {

double average = totalMarks[i] / (double) totalStudents;

System.out.println("Average marks in subject " + (i + 1) + ": " + average);

void displayStudentWithHighestTotalMarks() {

System.out.println("\nDetails of student with the highest total marks:");

Student topStudent = students.get(0);

for (Student student : students) {

if (student.calculateTotalMarks() > topStudent.calculateTotalMarks()) {

topStudent = student;

topStudent.displayDetails();
12202040501019 Dharmik Rabadiya

void displayStudentWithLowestTotalMarks() {

System.out.println("\nDetails of student with the lowest total marks:");

Student bottomStudent = students.get(0);

for (Student student : students) {

if (student.calculateTotalMarks() < bottomStudent.calculateTotalMarks()) {

bottomStudent = student;

bottomStudent.displayDetails();

void searchStudentByRollNumber(int rollNumber) {

boolean found = false;

for (Student student : students) {

if (student.rollNumber == rollNumber) {

student.displayDetails();

found = true;

break;

if (!found) {

System.out.println("Student not found with roll number: " + rollNumber);

void updateMarksByRollNumber(int rollNumber, int[] newMarks) {

for (Student student : students) {

if (student.rollNumber == rollNumber) {

student.marks = newMarks;

System.out.println("Marks updated successfully for roll number: " + rollNumber);

return;
12202040501019 Dharmik Rabadiya

System.out.println("Student not found with roll number: " + rollNumber);

void deleteStudentByRollNumber(int rollNumber) {

for (Student student : students) {

if (student.rollNumber == rollNumber) {

students.remove(student);

System.out.println("Student deleted successfully with roll number: " + rollNumber);

return;

System.out.println("Student not found with roll number: " + rollNumber);

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

StudentManagementSystem system = new StudentManagementSystem();

while (true) {

System.out.println("\nStudent Management System");

System.out.println("1. Add a new student");

System.out.println("2. Display details of all students");

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("7. Update the marks of a student by roll number");

System.out.println("8. Delete a student by roll number");

System.out.println("9. Exit");

System.out.print("Enter your choice: ");


12202040501019 Dharmik Rabadiya

int choice = scanner.nextInt();

scanner.nextLine(); // Consume newline

switch (choice) {

case 1:

System.out.print("Enter roll number: ");

int rollNumber = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Enter name: ");

String name = scanner.nextLine();

int[] marks = new int[5];

for (int i = 0; i < 5; i++) {

System.out.print("Enter marks in subject " + (i + 1) + ": ");

marks[i] = scanner.nextInt();

system.addStudent(rollNumber, name, marks);

System.out.println("Student added successfully.");

break;

case 2:

system.displayAllStudents();

break;

case 3:

system.calculateAverageMarks();

break;

case 4:

system.displayStudentWithHighestTotalMarks();

break;

case 5:

system.displayStudentWithLowestTotalMarks();

break;

case 6:

System.out.print("Enter roll number to search: ");

int searchRollNumber = scanner.nextInt();


12202040501019 Dharmik Rabadiya

system.searchStudentByRollNumber(searchRollNumber);

break;

case 7:

System.out.print("Enter roll number to update marks: ");

int updateRollNumber = scanner.nextInt();

int[] newMarks = new int[5];

for (int i = 0; i < 5; i++) {

System.out.print("Enter new marks in subject " + (i + 1) + ": ");

newMarks[i] = scanner.nextInt();

system.updateMarksByRollNumber(updateRollNumber, newMarks);

break;

case 8:

System.out.print("Enter roll number to delete: ");

int deleteRollNumber = scanner.nextInt();

system.deleteStudentByRollNumber(deleteRollNumber);

break;

case 9:

System.out.println("Exiting...");

scanner.close();

System.exit(0);

default:

System.out.println("Invalid choice. Please enter a number between 1 and 9.");

Output:

mathematica

Copy code

Student Management System

1. Add a new student


12202040501019 Dharmik Rabadiya

2. Display details of all students

3. Calculate and display average marks of all students in each subject

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

6. Search for a student by roll number and display their details

7. Update the marks of a student by roll number

8. Delete a student by roll number

9. Exit

Enter your choice: 1

Enter roll number: 101

Enter name: John

Enter marks in subject 1: 90

Enter marks in subject 2: 85

Enter marks in subject 3: 80

Enter marks in subject 4: 75

Enter marks in subject 5: 95

Student added successfully.

8] write a program to calculate and display the sum of all elements in the array.

import java.util.Scanner;

public class ArraySum {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Ask the user to enter the size of the array

System.out.print("Enter the size of the array: ");

int size = scanner.nextInt();

// Declare an array of the given size

int[] array = new int[size];

// Ask the user to enter the elements of the array


12202040501019 Dharmik Rabadiya

System.out.println("Enter the elements of the array:");

for (int i = 0; i < size; i++) {

System.out.print("Enter element " + (i + 1) + ": ");

array[i] = scanner.nextInt();

// Calculate the sum of all elements

int sum = 0;

for (int i = 0; i < size; i++) {

sum += array[i];

// Display the sum of all elements

System.out.println("Sum of all elements in the array: " + sum);

scanner.close();

You might also like