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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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

 

and here is my code for the test class: 

import java.util.*;
import java.util.Scanner;
public class CarTester
{
String s1 = input.nextLine();
String s2 = input.nextLine();
String s3 = input.nextLine();
Car c2 = new Car(s1,s3);
Car c1 = new ElectricCar(s1);
ArrayList<Car> cars = new ArrayList<Car>();
for (int i = 0;i<Integer.MAX_VALUE;i++)
{
System.out.println("Please enter a car model name(exit to quit): ");

if(s1.equals("exit")){
break;
}
else{
System.out.println("Is this car electric? (y or n) ");

if(s2.equals("n")){
System.out.println("How many miles per gallon: ");


cars.add(c2);
}
else{

cars.add(c1);
}
}
}
for (int j=0;j<cars.size();j++){
System.out.println("Car: "+cars.get(j).getModel());
System.out.println("MPG: "+cars.get(j).getMPG());
System.out.println();






}

}

 

there always appears some errors

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY