Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Anyone helps me this java problem 1. In Eclipse, create a new project named lab3 - Cars. 2. In the project, create 2 new classes:

Anyone helps me this java problem
image text in transcribed
image text in transcribed
image text in transcribed
1. In Eclipse, create a new project named lab3 - Cars. 2. In the project, create 2 new classes: Car (no main method in this one) and CarTester (include a main method in this one) 3. In the Car class: Create instance variables for the make (a String), the mileage (double), and mileage at the time of the last oil change (double) Create two constructors: one where the make, mileage, and last oil change are specified, and one where only the make is specified (setting the other default values to zero). One of your constructors should call on the other constructor to do most of the work. Attempting to create a car with negative numbers should throw an IllegalArgumentException Also add the following public static constant (even though instance variables should be private, make this constant public). You will use it later o o o o public static final double MILES BETWEEN_OIL_CHANGES 5000.0; 4. In the CarTester class, write code to create 2 cars: Use the three-parameter constructor to construct a Ford with 40,000 miles on it, and with the last oil change at 39,950 miles. o o Use the one-parameter constructor to construct a brand new Mazda. The toString method: Most classes we write will have atostring method. This is a method statements in this method). The string it returns typically combines all the useful information that returns a String (it doesn't print...don't make the common mistake of putting print about the object, formatted in some useful way. 5. Write the toString method so that it returns strings formatted as shown in this example (don't worry about rounding): Ford, mileage-40000.e, last oil change 3995.0 Now, switch to the tester and print the toString0 for each of your cars. Accessor (getter) methods are methods that return information about an object. Often (but not always), we write getters for each instance variable. That's what we will do for the Car class. In the Car class, write the standard getters for all three instance variables. Switch to the CarTester class. Test all three getters using printin statements. 6. methods. These are the opposite of accessor methods. Accessor methods Mutator (setter) tell you about the object, but they don't change the object. Mutator methods change the object, and often (but not always) have a return type of void. There are two ways we can change our car: . Driving the car will change the mileage. Getting the oil changed will cause the mileageOfLastoilChange to be set to the current mileage. The drive0 mutator method: In the Car class, write the drive method. This method has a double parameter, allowing us to specify the number of miles driven. Driving should increase the car's mileage by the number of miles driven when the car is driven forwards and backwards. Here is the header: 9. public void drive(double miles) ( 10. In the carTester class, verify that driving a car forward and backward will make the mileage increase. To test it o Print your car's mileage o then drive it forward and print the mileage again o then drive it backward and print the mileage again Now is a good time to fix the formatting of your code. In Eclipse's "Source" menu, choose "Format" (or use the Ctri-Shift-F key combination). The changeOil() mutator method: You have an instance variable that keeps track of the mileage of the car when the oil was last changed. Whenever a car gets an oil change, that instance variable should be changed to match the current mileage of the car. In the Car class, write a changeoil method. This method is void, and has no parameters. All it does is updates the appropriate instance variable. 13. In the CarTester class, verify that this method is working: o Print the mileage of the last oil change o Drive the car o Change the oil o Print the mileage of the last oil change The needsOilChange method: 14. In the Car class, write the needsoilchange() method. Don't put the number 5000 in your code. Use the previously defined constant, MILES BETWEEN OIL_CHANGES, instead. public boolean needsOi1Change) // Write code here that returns true if a car I/ needs an oil change, and false otherwise. It // should NOT have the number 58ee in it anywhere. It // should use the constant you defined above (along with // two of your instance variables). 14. In the CarTester class, test this method: o Change the cars oil. o See if it needs an oil change (the method should return false) o o o o Drive the car 4999 miles See if it needs an oil change (still false) Drive the car 1 mile. See if it needs an oil change (should now be true) 15. Modify the toString method so that it includes whether or not the car needs an oil change. For example, it should look like one of these: Ford, mileage 40000.e, last oil change-39958.0, needs oil change: false Ford, mileage seeee . e, last oil change-39950.0, needs oil change: true Using your Car class 16. Frank's Used Fords sells used Ford cars. Write a method in your tester class with the following header: public static ArrayList randomFords (int n) This method should return an arraylist of n random cars. It should not do any printing. Each car should be a Ford. Each car should have a random mileage between 0 and 20,000. Each should have a random "miles of last oil change" between 0 and and the current mileage of the car. (In other words, if the random mileage of the car is 7449.2, then the miles of last oil change should be a random number between 0 and 7449.2.) . 17. In your main method, comment out your other code, and call this method to get an arraylist of 30 cards. Print the cars and let your instructor or TA know when you have the results on the screen

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

More Books

Students also viewed these Databases questions

Question

Type of Study Is the study retrospective or prospective?

Answered: 1 week ago

Question

=+23-1 Describe sleep as a state of consciousness.

Answered: 1 week ago