Java Assignment 22ad014
Java Assignment 22ad014
22AD014
CLASS
1. Define a Java class named BankAccount that represents a simple bank account.
The class should have
instance variables for account number, account holder's name, and account
balance. Include methods to
deposit and withdraw funds, and a method to display the account details.
// Constructor
public BankAccount(int accountNumber, String accountHolderName, double
initialBalance) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.accountBalance = initialBalance;
}
// Constructor
public Student(String name, int age, double grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
Output:
Name: Alice
Age: 18
Grade: 87.5
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
Calculated Grade: B
3. Define a Java class named Rectangle that models a rectangle shape. Include
attributes for width and
height, and provide methods to calculate the area and perimeter of the rectangle.
Create multiple
instances of the class and demonstrate the usage of these methods.
// Constructor
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
Output:
Rectangle 1:
Area: 50.0
Perimeter: 30.0
Rectangle 2:
Area: 21.0
Perimeter: 20.0
4. Implement a Java class called Book to represent a book's details, including title,
author, year of
publication, and ISBN number. Create methods to display these details and
compare two books based
on their publication years.
// Constructor
public Book(String title, String author, int yearOfPublication, String isbn) {
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
this.title = title;
this.author = author;
this.yearOfPublication = yearOfPublication;
this.isbn = isbn;
}
System.out.println("Book 2:");
book2.displayDetails();
System.out.println();
Output:
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
Book 1:
Title: To Kill a Mockingbird
Author: Harper Lee
Year of Publication: 1960
ISBN: 978-0-06-112008-4
Book 2:
Title: 1984
Author: George Orwell
Year of Publication: 1949
ISBN: 978-0-452-28423-4
5. Create a base class named Vehicle with attributes like make, model, and year.
Derive a subclass Car
from Vehicle with additional attributes specific to cars, such as the number of
doors and fuel efficiency.
Include methods to display the car's details and calculate fuel consumption.
// Base class: Vehicle
class Vehicle {
// Instance variables
protected String make;
protected String model;
protected int year;
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
// Constructor
public Vehicle(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// Constructor
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
public Car(String make, String model, int year, int numDoors, double
fuelEfficiency) {
super(make, model, year); // Calling base class constructor
this.numDoors = numDoors;
this.fuelEfficiency = fuelEfficiency;
}
Output:
Car Details:
Make: Toyota
Model: Camry
Year: 2022
Number of Doors: 4
Fuel Efficiency: 30.5 miles per gallon
Object creation
1. Write a Java program to define a Person class with attributes for name, age, and
occupation. Create
objects for different people and display their information.
// Constructor
public Person(String name, int age, String occupation) {
this.name = name;
this.age = age;
this.occupation = occupation;
}
System.out.println("Person 2:");
person2.displayInfo();
System.out.println("Person 3:");
person3.displayInfo();
}
}
Output:
Person 1:
Name: John Doe
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
Age: 30
Occupation: Engineer
Person 2:
Name: Alice Smith
Age: 25
Occupation: Teacher
Person 3:
Name: Michael Johnson
Age: 45
Occupation: Doctor
3. Create a Java program for a library catalog. Define a Book class with attributes
for title, author, and
genre. Create objects for various books and display their details.
// Constructor
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
public Book(String title, String author, String genre) {
this.title = title;
this.author = author;
this.genre = genre;
}
System.out.println("Book 3:");
book3.displayDetails();
}
}
Output:
Book 1:
Title: To Kill a Mockingbird
Author: Harper Lee
Genre: Fiction
Book 2:
Title: 1984
Author: George Orwell
Genre: Dystopian
Book 3:
Title: Pride and Prejudice
Author: Jane Austen
Genre: Romance
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
4. Implement a Java program to represent basic shapes. Define a Shape class with
attributes for type (e.g.,
circle, triangle) and properties related to that shape (e.g., radius, side lengths).
Create objects for
different shapes and calculate/display relevant properties.
// Constructor
public Shape(String type, double... properties) {
this.type = type;
this.properties = properties;
}
System.out.println("Triangle:");
triangle.displayDetails();
}
}
Output:
Circle:
Shape type: circle
Property 1: 5.0
Area: 78.53981633974483
Triangle:
Shape type: triangle
Property 1: 3.0
Property 2: 4.0
Property 3: 5.0
Area: 6.0
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
5. Write a Java program to manage employee records. Define an Employee class
with attributes for name,
role, and salary. Create objects for employees and implement methods to update
salary and display their
information.
// Constructor
public Employee(String name, String role, double salary) {
this.name = name;
this.role = role;
this.salary = salary;
}
System.out.println("Employee 2:");
employee2.displayInfo();
System.out.println();
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
Output:
Employee 1:
Name: John Doe
Role: Manager
Salary: 50000.0
Employee 2:
Name: Alice Smith
Role: Developer
Salary: 60000.0
Updated Employee 1:
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
Name: John Doe
Role: Manager
Salary: 55000.0
Default Constructor:
1. Create a Java program to define a class BankAccount with instance variables for
account number and
balance. Implement a default constructor that initializes the account number to a
default value and
balance to 0. Display the account details after initialization.
// Default constructor
public BankAccount() {
accountNumber = 123456; // Default account number
balance = 0.0; // Default balance
}
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
// Method to display account details
public void displayAccountDetails() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: " + balance);
}
2. Write a Java program that defines a class Student with instance variables for
name, age, and student ID.
Use a default constructor to initialize the student's information with default
values. Print the student's
details after initialization.
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
public class Student {
// Instance variables
private String name;
private int age;
private int studentID;
// Default constructor
public Student() {
name = "John Doe"; // Default name
age = 18; // Default age
studentID = 123456; // Default student ID
}
3. Design a Java class Calculator with variables for two numbers and a result.
Create a default constructor
that sets the numbers to default values and initializes the result. Implement
methods for addition,
subtraction, multiplication, and division. Display the calculated results.
// Default constructor
public Calculator() {
num1 = 0.0; // Default value
num2 = 0.0; // Default value
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
result = 0.0; // Initialized result
}
calculator.subtract();
System.out.print("Subtraction: ");
calculator.displayResult();
calculator.num1 = 10;
calculator.num2 = 5;
calculator.multiply();
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
System.out.print("Multiplication: ");
calculator.displayResult();
calculator.divide();
System.out.print("Division: ");
calculator.displayResult();
}
}
Output:
Addition: Result: 0.0
Subtraction: Result: 0.0
Multiplication: Result: 50.0
Division: Result: 2.0
4. Develop a Java class Person with attributes for name, age, and gender. Include a
default constructor that
sets default valuesfor these attributes. Create instances of the class and print out
the details of the person
objects.
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
public class Person {
// Instance variables
private String name;
private int age;
private String gender;
// Default constructor
public Person() {
name = "Unknown"; // Default name
age = 0; // Default age
gender = "Unknown"; // Default gender
}
System.out.println("Person 2:");
person2.displayDetails();
}
}
Output:
Person 1:
Name: Unknown
Age: 0
Gender: Unknown
Person 2:
Name: Unknown
Age: 0
Gender: Unknown
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
5. Build a Java class Vehicle with instance variables for make, model, and year.
Define a default
constructor that initializes the vehicle details to default values. Create objects of
the class and display
the vehicle information.
// Default constructor
public Vehicle() {
make = "Unknown"; // Default make
model = "Unknown"; // Default model
year = 0; // Default year
}
System.out.println("Vehicle 2:");
vehicle2.displayInfo();
}
}
Output:
Vehicle 1:
Make: Unknown
Model: Unknown
Year: 0
Vehicle 2:
Make: Unknown
ASSIGNMENT-JAVA BALA ADTYA J
22AD014
Model: Unknown
Year: 0