For this discussion assignment, we will be working with flow control and excepti
ID: 3829081 • Letter: F
Question
For this discussion assignment, we will be working with flow control and exceptions. You will be creating a class that will allow the user to input a total of 10 objects that subclass Car into an ArrayList. If the user picks an invalid choice, a custom exception will be thrown and the program will terminate. Note that the Car class, as well as its subclasses (Aston Martin, BMW, Maserati, and Porsche), are provided at the end of this file.
You should copy these classes into your LastName_FirstName_TestException.java file, which you will submit as a response to this discussion. First, you should create a custom exception called CarException that extends Exception. You do not have to add anything to this class; just leave the class body blank.
You may create this exception in the same LastName_FirstName_TestException.java file. The LastName_FirstName_TestException class itself will have two methods: a main() method and an addCars() method. The addCars() method's purpose is to allow the user to add objects that subclass Car to the ArrayList. The addCars() method should declare that it can throw a CarException; main() should handle this exception, if it occurs. When choosing which cars to enter into the ArrayList, the user will enter the letter ‘a’ for Aston Martin, the letter ‘b’ for BMW, the letter ‘c’ for Maserati, and the letter ‘d’ for Porsche. The addCars() method should add 10 objects to the ArrayList. You should display the contents of the list if no exception takes place. If the user enters a letter other than ‘a’ through ‘c’, the method should throw a CarException. If a CarException occurs, display appropriate information to the user and allow the program to terminate. Do not display the ArrayList if an exception takes place.
Below are the classes and subclasses required for this assignment:
class Car { } class AstonMartin extends Car { public String toString() { return "Aston Martin"; } }
class BMW extends Car { public String toString() { return "BMW"; } }
class Maserati extends Car { public String toString() { return "Maserati"; } }
class Porsche extends Car { public String toString() { return "Porsche"; } }
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.ArrayList;
import java.util.Scanner;
public class Kumar_Pravesh_TestException {
public static void addCars(ArrayList<Car> carList) throws CarException{
System.out.println("Enter 10 car information");
Scanner sc = new Scanner(System.in);
int coutn = 0;
while(coutn < 10){
System.out.println("Select option: ");
System.out.println("a - Aston");
System.out.println("b - BMW");
System.out.println("c - Maserati");
System.out.println("d - Porsche");
char c = sc.next().charAt(0);
switch(c){
case 'a':
carList.add(new AstonMartin());
break;
case 'b':
carList.add(new BMW());
break;
case 'c':
carList.add(new Maserati());
break;
case 'd':
carList.add(new Porsche());
break;
default:
sc.close();
throw new CarException();
}
coutn++;
}
sc.close();
}
public static void main(String[] args) {
ArrayList<Car> carList = new ArrayList<>();
try{
addCars(carList);
for(Car c : carList)
System.out.println(c);
}catch (CarException e) {
System.out.println("Exception in adding car");
}
}
}
class CarException extends Exception{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.