Final Assignment
Q1:
Ans: package finalAssignment;
import [Link];
class Car {
private String make;
private String model;
private int year;
private String color;
public Car(String make, String model, int year, String color) {
[Link] = make;
[Link] = model;
[Link] = year;
[Link] = color;
}
public void displayAttributes() {
[Link]("Car Make: " + make);
[Link]("Car Model: " + model);
[Link]("Car Year: " + year);
[Link]("Car Color: " + color);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter car make:");
String make = [Link]();
[Link]("Enter car model:");
String model = [Link]();
[Link]("Enter car year:");
int year = [Link]();
[Link]();
[Link]("Enter car color:");
String color = [Link]();
Car userCar = new Car(make, model, year, color);
[Link]("\nUser Car Attributes:");
[Link]();
[Link]();
}
}
Q2:
Ans: package finalAssignment;
class Book {
private String title;
private String author;
private String isbn;
public Book(String title, String author, String isbn) {
[Link] = title;
[Link] = author;
[Link] = isbn;
}
// Getters for the attributes
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
public void displayDetails() {
[Link]("Title: " + title);
[Link]("Author: " + author);
[Link]("ISBN: " + isbn);
[Link]();
}
public static void displayLibrary(Book[] library) {
[Link]("Library Collection:");
for (Book book : library) {
if (book != null) {
[Link]();
}
}
}
public static void searchBook(Book[] library, String title) {
boolean found = false;
for (Book book : library) {
if (book != null && [Link]().equalsIgnoreCase(title)) {
[Link]("Book found:");
[Link]();
found = true;
break;
}
}
if (!found) {
[Link]("The book titled \"" + title + "\" is not in
the library.");
}
}
public static void main(String[] args) {
Book[] library = new Book[5];
// Initialize the library array with different Book objects
library[0] = new Book("1984", "George Orwell", "978-0451524935");
library[1] = new Book("To Kill a Mockingbird", "Harper Lee", "978-
0061120084");
library[2] = new Book("The Great Gatsby", "F. Scott Fitzgerald",
"978-0743273565");
library[3] = new Book("Moby Dick", "Herman Melville", "978-
1503280786");
library[4] = new Book("The Catcher in the Rye", "J.D. Salinger",
"978-0316769488");
displayLibrary(library);
String searchTitle = "1984";
searchBook(library, searchTitle);
searchTitle = "The Hobbit";
searchBook(library, searchTitle);
}
}
Q3:
Ans: package finalAssignment;
class Student {
private String name;
private int id;
private double grade;
public Student(String name, int id, double grade) {
[Link] = name;
[Link] = id;
[Link] = grade;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double getGrade() {
return grade;
}
public void displayDetails() {
[Link]("Name: " + name + ", ID: " + id + ", Grade: " +
grade);
}
public static void displayStudents(Student[] studentArray) {
[Link]("Student List:");
for (Student student : studentArray) {
if (student != null) {
[Link]();
}
}
}
public static void sortStudents(Student[] studentArray) {
int n = [Link];
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (studentArray[j] != null && studentArray[j + 1] != null)
{
if (studentArray[j].getGrade() > studentArray[j +
1].getGrade()) {
Student temp = studentArray[j];
studentArray[j] = studentArray[j + 1];
studentArray[j + 1] = temp;
}
}
}
}
}
public static void main(String[] args) {
Student[] studentArray = new Student[10];
studentArray[0] = new Student("Vishnu", 101, 88.5);
studentArray[1] = new Student("Ram", 102, 92.0);
studentArray[2] = new Student("Arjun", 103, 85.0);
studentArray[3] = new Student("Krishna", 104, 78.5);
studentArray[4] = new Student("Maruti", 105, 90.0);
studentArray[5] = new Student("Sita", 106, 82.0);
studentArray[6] = new Student("Shiva", 107, 95.0);
studentArray[7] = new Student("Gauri", 108, 80.0);
studentArray[8] = new Student("Karna", 109, 87.0);
studentArray[9] = new Student("Lakshmi", 110, 91.5);
[Link]("Before Sorting:");
displayStudents(studentArray);
sortStudents(studentArray);
[Link]("\nAfter Sorting by Grade:");
displayStudents(studentArray);
}
}
Q4:
Ans: