In this exercise, we are going to revist our Car and ElectricCar class from lesson 3. In lesson 3, you were given the Car class and needed to complete the ElectricCar class. You can use your code from that exercise to get started here. The focus of this exercise is the CarTester class. In this class, you will prompt the user for a car model and if the car is not electric, you will also prompt the user for the miles per gallon. For each car, you will add it to a single ArrayList. You will continue asking the user until they exit, then you will loop through your ArrayList and print the results. Remember, for a method call to compile, it needs to be defined in the reference type (the Car class in this exercise), but the actual method that executes will be based on the instantiated class. In this exercise, we want electric cars to run the Override methods.
Max Function
Statistical function is of many categories. One of them is a MAX function. The MAX function returns the largest value from the list of arguments passed to it. MAX function always ignores the empty cells when performing the calculation.
Power Function
A power function is a type of single-term function. Its definition states that it is a variable containing a base value raised to a constant value acting as an exponent. This variable may also have a coefficient. For instance, the area of a circle can be given as:
In this exercise, we are going to revist our Car and ElectricCar class from lesson 3. In lesson 3, you were given the Car class and needed to complete the ElectricCar class. You can use your code from that exercise to get started here.
The focus of this exercise is the CarTester class. In this class, you will prompt the user for a car model and if the car is not electric, you will also prompt the user for the miles per gallon. For each car, you will add it to a single ArrayList.
You will continue asking the user until they exit, then you will loop through your ArrayList and print the results.
Remember, for a method call to compile, it needs to be defined in the reference type (the Car class in this exercise), but the actual method that executes will be based on the instantiated class. In this exercise, we want electric cars to run the Override methods.
Sample Output
Please enter a car model name(exit to quit): Tesla Is this car electric? (y or n) y Please enter a car model name(exit to quit): F-150 Is this car electric? (y or n) n How many miles per gallon: 18 Please enter a car model name(exit to quit): exit Car: Tesla MPG: Electric cars do not calculate MPG Car: F-150 MPG: 18
=========================================
import java.util.*;
public class CarTester
{
public static void main(String[] args)
{
// Start here
}
}
=========================================
public class ElectricCar extends Car {
/**
* If you completed the Electric Car exercise in lesson 3,
* copy your code for the ElectricCar class here.
*/
// Complete the constructor
public ElectricCar(String model){
}
// Override the getMPG here.
// It shouls return: "Electric cars do not calcualte MPG
// Override the toString() here.
// (model) is an electric car.
// Remember, to get the name, use super.getModel()
}
=========================================
public class Car {
//This code is complete
private String model;
private String mpg;
public Car(String model, String mpg){
this.model = model;
this.mpg = mpg;
}
public String getModel(){
return model;
}
public String getMPG(){
return mpg;
}
public String toString(){
return model + " gets " + mpg + " mpg.";
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images