Question
CIS 180 Lab 08 For this lab, we will work with objects and take a closer look at constructors. Car - modelName : String -
CIS 180 Lab 08 For this lab, we will work with objects and take a closer look at constructors.
Car
- modelName : String - brand : String - year: int - mpg : double + Car(brand: String, modelName : String, year : int, mpg : double) + Car(brand : String, year : int, mpg : double) + getModelName() : String + getBrand() : String + getYear () : int + getMPG() : double + isPractical() : boolean 1. Start by making the Car class listed in the above UML diagram. Do not worry about filling in the body of the isPractical() method yet. 2. Now we will make our constructor for this class. We will make a public constructor for the Car class. For its method signature, we will use two Strings, an int, and a double. The names of these variables will be the same as the attributes in our class. Assign the attributes of our class to the variables we take in (do not forget about the "this" keyword). Also, constructors should be the first methods in your class body. 3. What if a car does not have a model name? In order to remedy this situation, we will "Overload" the constructor and make another one that will automatically set modelName to "NA". Overloading means that we are going to make another method with a different signature (parameters taken in) but the same name. Make another car constructor except that this time do not take in a modelName String but instead set it to "NA" in the method body. 4. The final method in our class body is the isPractical() method. The purpose of this method is to determine if the created car is practical based on the specs. This isPractical() method will perform up to three checks. If any condition fails, it will return false, which means an impractical car. The conditions are the car is less than 15 years old, MPG > 25, and the brand not being "NA". When we want to check whether a Strings contents are the same as another one, we cannot use "==" like we do with numbers. Instead, we need to use the equals() method. You may also need to use the not operator (!)for this part.
5. Now that our Car class is finished. We will implement the CarDriver class, you can find code for this attached to the lab. If you did the previous steps correctly, you should be all set to get checked off. Once checked off all you need to submit is the Car.java file.
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