Part I Write a class PassengerListException that extends Exception with the foll
ID: 3685896 • Letter: P
Question
Part I
Write a class PassengerListException that extends Exception with the following constructors:
- A default constructor that gives the error message "Too many passengers."
- A constructor that takes a string and uses this for the error message
Part II
Write a class PassengerCar with the following attributes:
-Two instance variables (with corresponding getters and setters):
-model - a String for the model of the passenger car.
-passengers - an array of Strings with length 4, holding the names of the passengers the Car holds.
-A default constructor that initializes name to be "NoName".
-A constructor that takes a String to set the model of the car
Two methods:
-assignPassengers (no input parameters or return values) - This method should ask a user to input passenger from the console window until the user enters an empty line, which indicates the end of the array. If the user enters more than 4 passengers, throw a PassengerListException
-writeCar (no input parameters or return values) - writes the model of the car and the names of the cars's passengers to the console window
Part III
Write a class Coupe, a subclass of PassengerCar, for cars which can hold at most 2 passengers. The class should include the following:
-A default constructor that invokes the superclass constructor
-A constructor that takes a String to set the model of the Coupe
-An overridden assignPassengers that throws a PassengerListException when a user tries to assign more than 2 passengers to a Coupe.
Part IV
Write a class TestCar that does the following:
1. Creates a car with the model Ford Mustang
2. Assigns some Passengers to the Ford Mustang
3. Writes the information about the Ford Mustang to the console window
4. Creates a Coupe with the name Deuce Coupe
5. Assigns passengers to the Deuce Coupe
6. Writes the information about the Deuce Coupe to the console
Explanation / Answer
Solution:
package com.nancy.chegg.exception.example;
public class PassengerListException extends Exception {
private static final long serialVersionUID = 1L;
String msg = null;
public PassengerListException() {
super();
msg = "Too many passengers.";
System.out.println("********************");
System.out.println("ERROR : " + msg);
System.out.println("********************");
}
public PassengerListException(String msg) {
super(msg);
this.msg = msg;
System.out.println("********************");
System.out.println("ERROR : " + msg);
System.out.println("********************");
}
}
______________________________________________
package com.nancy.chegg.exception.example;
import java.util.Scanner;
public class PassengerCar {
String model;
String[] passengers = new String[4];
PassengerCar() {
this.model = "NoName";
}
PassengerCar(String name) {
this.model = name;
}
public void assignPassengers() throws PassengerListException {
System.out.println("Please assign some passengers to " + this.model);
Scanner scan = new Scanner(System.in);
int counter = 0;
while (counter <= 4) {
System.out.println("Enter passengers name : ");
passengers[counter] = scan.nextLine();
if (passengers[counter].equals("")) {
break;
}
counter++;
if (counter == 4) {
throw new PassengerListException();
}
}
}
public void writeCar() {
System.out.println("Model : " + model);
System.out.println("passengers details : ");
for (int i = 0; i < 4 && passengers[i] != null
&& !passengers[i].equals(""); i++) {
System.out.println((i + 1) + ". " + passengers[i]);
}
}
}
___________________________________________________________
package com.nancy.chegg.exception.example;
import java.util.Scanner;
public class Coupe extends PassengerCar{
Coupe(){
super();
}
Coupe(String name){
this.model = name;
}
@Override
public void assignPassengers() throws PassengerListException {
System.out.println("Please assign passengers to " + this.model);
Scanner scan = new Scanner(System.in);
int counter = 0;
while (counter <= 2) {
System.out.println("Enter passengers name : ");
passengers[counter] = scan.nextLine();
counter++;
if(counter == 2) {
throw new PassengerListException();
}
}
}
}
____________________________________________________
package com.nancy.chegg.exception.example;
public class TestCar {
public static void main(String[] args) {
PassengerCar stu1 = new PassengerCar("Ford Mustang");
try {
stu1.assignPassengers();
} catch (PassengerListException e) {
}
stu1.writeCar();
Coupe stu2 = new Coupe("Deuce Coupe");
try {
stu2.assignPassengers();
} catch (PassengerListException e) {
}
stu2.writeCar();
}
}
Sample run :
Please assign some passengers to Ford Mustang
Enter passengers name :
nancy
Enter passengers name :
menu
Enter passengers name :
meku
Enter passengers name :
Model : Ford Mustang
passengers details :
1. nancy
2. menu
3. meku
Please assign passengers to Deuce Coupe
Enter passengers name :
manir
Enter passengers name :
many
********************
ERROR : Too many passengers.
********************
Model : Deuce Coupe
passengers details :
1. manir
2. many
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.