Question
import java.awt.Color ; /** Car class. So far it has a Color instance variable. Your job is to add an instance variable called kilometers that
import java.awt.Color ; /** Car class. So far it has a Color instance variable. Your job is to add an instance variable called kilometers that stores how many kilometers the car has driven (i.e. the odometer) Adjust the constructor method to initialize kilometers Adjust the toString() method as inidicated. */ public class Car { //instance variables private Color color ; //-----------Start below here. To do: approximate lines of code = 1 // put here another instance variable, one for kilometers //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
/** Constructor method: initialize instance variables @param color1 the given color @param odometer the given odometer reading */ public Car(Color color1, double odometer) { color = color1 ; //-----------Start below here. To do: approximate lines of code = 1 // Complete this constructor method //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } /** Returns a string representation of the car. @return something like Car[color = ..., kilometers = ...] */ public String toString() { //-----------Start below here. To do: approximate lines of code = 1 // return "Car[color = " + color + "]" ; but include kilometers //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } }
//This is a tester class no need to change anything.
import java.awt.Color ; /** This is a tester code that constructs a Car object. */ public class CarTester { public static void main(String[] args) { Car car = new Car(Color.BLUE, 555.5) ; System.out.println(car.toString()) ; System.out.println("Expect:") ; System.out.println("Car[color = java.awt.Color[r=0,g=0,b=255], kilometers = 555.5]") ; } }
Step 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