Week 3 Java Practice questions – Classes and Object Creation
1. Create a class named Car with the following attributes:
➢ model (String)
➢ year (int)
➢ color (String)
Create a method display CarInfo() to print the car's information. Create two Car
objects and display their information.
2. Create a class named Rectangle with attributes width and height (both double).
Create methods to calculate the area and perimeter of the rectangle. Create a
Rectangle object, set its dimensions, and calculate and print its area and perimeter.
3. Create a class named Student with attributes name, rollNumber, and marks. Create
a method calculateGrade() to calculate the grade based on marks. Create a Student
object, set its details, calculate the grade, and print the student's information along
with the grade.
4. Create a class named BankAccount with attributes accountNumber,
accountHolderName, and balance. Create methods to deposit and withdraw money.
Create a BankAccount object, perform deposit and withdrawal operations, and print
the final balance.
5. Create a class named Product with attributes name, price, and quantity. Create a
class named ShoppingCart with an attribute items (ArrayList of Product objects).
Implement the following methods in the ShoppingCart class:
➢ addProduct(Product product): Adds a product to the cart.
➢ removeProduct(Product product): Removes a product from the cart.
➢ calculateTotal(): Calculates the total price of all items in the cart.
Constructor Creation(Default and Parameterized)
1. Create a class named Car with the following attributes:
➢ model (String)
➢ year (int)
➢ color (String)
Create a constructor to initialize these attributes. Create a method
displayCarInfo() to print the car's information. Create two Car objects and display
their information.
2. Create a class named Person with attributes name, age, and address. Create a
default constructor and a parameterized constructor. Create a method
displayPersonInfo() to print the person's information. Create two Person objects,
one using the default constructor and the other using the parameterized
constructor. Display information for both objects.