Part I Write a class PassengerListException that extends Exception with the foll
ID: 3685911 • 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:
Creates a car with the model Ford Mustang
Assigns some Passengers to the Ford Mustang
Writes the information about the Ford Mustang to the console window
Creates a Coupe with the name Deuce Coupe
Assigns passengers to the Deuce Coupe
Writes the information about the Deuce Coupe to the console
Explanation / Answer
PassengerListException.Java
package com.chegg.test;
/**
* Custom Exception Class
*
* @author yourName
*
*/
public class PassengerListException extends Exception {
/**
* Generated serialVersionUID
*/
private static final long serialVersionUID = 5699637338573851662L;
PassengerListException() {
System.out.println("Exception ocuured while running the program");
}
PassengerListException(String exception) {
System.out.println(exception);
}
}
PassengerCar.java
package com.chegg.test;
import java.util.Arrays;
import java.util.Scanner;
public class PassengerCar {
private String model;
private String[] passengers = new String[4];
PassengerCar() {
for (int i = 0; i < passengers.length; i++) {
setPassengers(i, "NoName");
}
}
PassengerCar(String model) {
super();
this.model = model;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model
* the model to set
*/
public void setModel(String model) {
this.model = model;
}
/**
* @return the passengers
*/
public String[] getPassengers() {
return passengers;
}
/**
* @param passengers
* the passengers to set
*/
public void setPassengers(int pos, String value) {
this.passengers[pos] = value;
}
public void assignPassengers() throws PassengerListException {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String name = null;
int i = 0;
do {
System.out.println("Please enter the passenger Name:");
name = sc.nextLine();
try {
if (!name.isEmpty()) {
setPassengers(i, name);
i++;
} else {
break;
}
} catch (ArrayIndexOutOfBoundsException aioe) {
throw new PassengerListException(
"User enters more than 4 passengers");
}
} while (!name.isEmpty());
}
// Writes the information
public void writeCar() {
System.out.println("Model of the car:" + this.getModel());
System.out.println("Passengers of the car:"
+ Arrays.toString(this.getPassengers()));
}
}
Coupe.java
package com.chegg.test;
import java.util.Scanner;
/**
* Class for Coupe characteristics
*
* @author yourName
*
*/
public class Coupe extends PassengerCar {
Coupe() {
super();
}
Coupe(String model) {
setModel(model);
}
@Override
public void assignPassengers() throws PassengerListException {
int count = 0;
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String name = null;
do {
System.out.println("Please enter the passenger Name:");
name = sc.nextLine();
if (count <= 2) {
if (!name.isEmpty()) {
setPassengers(count, name);
count++;
} else {
break;
}
} else {
throw new PassengerListException(
"User tries to assign more than 2 passengers to a Coupe");
}
} while (!name.isEmpty());
}
}
TestCar.java
package com.chegg.test;
/**
* Tester class to get the details of car and model and passenger information
*
* @author yourName
*
*/
public class TestCar {
public static void main(String[] args) {
try {
PassengerCar car = new PassengerCar("Ford Mustang");
car.assignPassengers();
car.writeCar();
Coupe coupe = new Coupe("Deuce Coupe");
coupe.assignPassengers();
coupe.writeCar();
} catch (PassengerListException ple) {
}
}
}
Output:
Please enter the passenger Name:
pavan
Please enter the passenger Name:
kumar
Please enter the passenger Name:
Reddy
Please enter the passenger Name:
Kamatham
Please enter the passenger Name:
Model of the car:Ford Mustang
Passengers of the car:[pavan, kumar, Reddy, Kamatham]
Please enter the passenger Name:
Penchal
Please enter the passenger Name:
kamatham
Please enter the passenger Name:
Model of the car:Deuce Coupe
Passengers of the car:[Penchal, kamatham, NoName, NoName]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.