Sure! Here's a task for Java beginners on class, methods, objects, getters, and setters:
Task: Create a simple Java program that models a car dealership.
- Create a class called "Car" that has the following instance variables: make, model, year, and price.
- Write a constructor method that sets the values of the instance variables based on parameters passed to it.
- Write getter methods for each of the instance variables.
- Write setter methods for each of the instance variables.
- Create a method in the "Car" class called "printDetails" that prints out the make, model, year, and price of a car object.
- Create a main method that creates several car objects and calls the "printDetails" method for each one.
- Add a method to the "Car" class called "discount" that takes a double parameter representing a percentage discount, and reduces the price of the car by that percentage.
- In the main method, call the "discount" method for one of the car objects, and then call the "printDetails" method again to verify that the price was reduced.
Example output:
Make: Toyota Model: Camry Year: 2020 Price: $25,000.00
Make: Ford Model: Mustang Year: 2019 Price: $30,000.00
Make: Chevrolet Model: Corvette Year: 2021 Price: $70,000.00
Discounting the price of the Chevrolet Corvette by 10%... Make: Chevrolet Model: Corvette Year: 2021 Price: $63,000.00
Note: You can use the DecimalFormat class to format the price as a dollar amount with two decimal places.
Good luck!