Question
Do the following coding exercises and submit your source code along with a copy of your output screens in the Word document. C5.1. (Chapter 9)
Do the following coding exercises and submit your source code along with a copy of your output screens in the Word document. C5.1. (Chapter 9) Design a public class named Car to represent a car. The class contains:
a. Five public integer constants named SUV, TRUCK, SEDAN, MINIVAN, SPORTS_CAR
b. A private int data field named type that specifies the type of car (default value is MINIVAN)
c. Eight public integer constants named GAS, DIESEL, ELECTRICITY, HYDROGEN, NATURAL_GAS, WIND, SOLAR, WATER
d. A private int data field named fuel that specifies the type of fuel for the car (default value is GAS)
e. A private int data field named year that specifies the year of car (default value is 1980)
f. A private String data field named brand that specifies the brand of car (default value is Ford)
g. A private String data field named colour that specifies the colour of car (default value is Red)
h. The accessor and mutator methods for all five private data fields (type, fuel, year, brand, colour).
i. A no-arg constructor that creates a default car
j. A constructor that creates a car with the specified type, fuel, year, brand, and colour. Ensure that the parameter names of the constructor are the same name as the corresponding data fields. Utilize the this keyword to ensure the data fields are not hidden.
k. A method toString() that returns a string description for the car. For example, the default car must return the following string (year + colour + brand + fuel + type): i. 1980 Red Ford Gas-powered Minivan
Draw the UML diagram for the class Car and then implement the class. The Car class must be part of the ca.car.mypackage package (you need to figure out how to do this in Java).
Write a test program (TestCar) that must also be part of the ca.car.mypackage package.
In the test program, create five Car objects using an array of Car objects:
One of them must be the default car (to test the default constructor)
Each five cars must be unique, with a different year, colour, brand, fuel and type
For each car, print out the description using the toString() method and encapsulate in a method that is called by the main method:
o static void printCars (Car [] c)
Next, test the mutator methods:
Ensure all of the cars are the same colour Black
Randomly select two cars and set their fuel to GAS powered (use the Math.Random()) function to select the two, be careful to generate two different cars to modify)
Randomly select two cars and set their brand to Tesla (use the Math.Random()) function to select the two, be careful to generate two different cars to modify)
Randomly select three cars and set their type to SEDAN (use the Math.Random()) function to select the three, be careful to generate three different cars to modify)
Encapsulate this code in its own method and call from the main method:
o static void testMutatorMethods (Car [] car)
Print out all the mutated cars from the main method after returning from the testMutatorMethods method, using printCars
Test the accessor methods and print out (using the Car class toString() method):
Only the Black GAS-powered cars
Only the cars with the brand Tesla and type of SEDAN
Only the alternate fuel cars (fuel is not equal to GAS and not equal to DEISEL) Encapsulate this code in its own method and call from the main method:
o testAccessorMethods (Car [] car)
o Inside this method, print out the cars that match the three selections mentioned above
NOTE: Please define the public Car class in its own Java file separate from the test program as shown in Lesson #8.
Here is a sample output of the program:
This program tests the public Car class. After we create five cars, we print out each cars description. We then test the mutator and accesor methods as specified in the assignment requirements. Here are the cars: Car #0: 1988 Red Ford Gas-powered Minivan Car #1: 1998 Silver Volkswagon Diesel-powered SUV Car #2: 2025 Black Dodge Solar-powered Truck Car #3: 2038 Orange Tesla Wind-powered Sedan Car #4: 2050 Blue Toyota Water-powered Sports car Testing the car mutator methods: Car #0: 1988 Black Ford Gas-powered Sedan Car #1: 1998 Black Volkswagon Diesel-powered Sedan Car #2: 2025 Black Dodge Gas-powered Truck Car #3: 2038 Black Tesla Wind-powered Sedan Car #4: 2058 Black Tesla Gas-powered Sports car Testing the car accessor methods: Here are the Black GAS-powered cars: Car #0: 1980 Black Ford Gas-powered Sedan Car #2: 2025 Black Dodge Gas-powered Truck Car #4: 2050 Black Tesla Gas-powered Sports car Here are the Tesla Sedan cars: Car #3: 2030 Black Tesla Wind-powered Sedan Here are the alternate fuel cars: Car #3: 2030 Black Tesla Wind-powered Sedan Written by Warren Edwards, INFO 2312 A12Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started