Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Part 2 - Developing Class Car Section A 1. Add Javadoc comments at the top of the class. See class Seat for an example. 2.

Part 2 - Developing Class Car Section A 1. Add Javadoc comments at the top of the class. See class Seat for an example. 2. Class Car has one constructor, which has two parameters. The first parameter is the integer id of the car (for example, 1385). The second parameter is a boolean value: if true, the car is a business-class car; if false, the car is an economy-class car. Write this constructor so that it initializes the id and businessClass fields with the values of the corresponding parameters, and initializes the seats field with a new list (array) of seats. Notes: o There are 30 seats in a business-class car, and 40 seats in an economy-class car. (Be sure to use the constants provided.) o Each element in the list must be initialized with a new Seat object. o The seats in each car are numbered consecutively, starting from 1. o The cost of a ticket for a seat in a business-class car is $125, while the cost of a ticket for a seat in an economy-class car is $50. (Be sure to use the constants provided.) o If your constructor works, testCreateBusinessCar() and testCreateEconomyCar() should pass. o Complete the Javadoc comments for the constructor. See the methods in class Seat for some examples. 3. Generate the Javadoc and check that it looks good for the portions of class Car completed. Part 3 - Developing Class Car Section B 1. Method isBusinessClass() has no parameters and returns a boolean value. It returns true if the car is a business-class car and false if it is an economy-class car. Write this method. If your method works, testIsBusinessClass() should pass. 2. Method id() has no parameters and returns an int. This method returns the id of the car. Write this method.

Part 4 - Developing Class Car Section C 1. Method bookNextSeat() has no parameters and returns a boolean. This method searches the list of seats in the car and reserves the first available seat that it can find. After booking a seat, this method should print a ticket by invoking printTicket() and then return true. If no seats in the car are available, this method returns false. Write this method. Before testing this method, from the BlueJ menu select View > Show Terminal. If your method works, testBookNextSeat() should pass, and the tickets will be "printed" in the Terminal Window. 2. Method cancelSeat() has a single parameter (an integer seat number) and returns a boolean. If the seat number is valid and if that seat has been reserved, this method cancels the booking for the seat and returns true. If the seat number is not valid, the method returns false. If the seat number is valid, but the seat has not been reserved, the method returns false.

public class Car { /** This car's identifier. */ private int id; /** * true == this car is a business-class car, * false == this car is an economy-class car. */ private boolean businessClass; /** The cost of a business class seat, in dollars. */ public static final double BUSINESS_SEAT_COST = 125.0; /** The cost of an economy class seat, in dollars. */ public static final double ECONOMY_SEAT_COST = 50.0; /** The number of seats in a business class car. */ public static final int BUSINESS_SEATS = 30; /** The number of seats in an economy class car. */ public static final int ECONOMY_SEATS = 40; /** The list of this car's seats. */ private Seat[] seats; /** * Constructs a new Car object with the specified id. * If parameter isBusinessClass is true, the car is a business-class * car. If parameter isBusinessClass is false, the car is an * economy-class car. */ public Car(int carId, boolean isBusinessClass) { }

/** * Returns this car's list of seats. This method is intended for * testing purposes only, and should not be called by other objects, * as it may be removed from the final version of this class. * * @return The seats in this car, an array of Seat objects. */ public Seat[] seats() { return seats; } /** * Returns true if this is a business-class car, * false if this is an economy-class car. */ public boolean isBusinessClass() { return false; } /** * Returns the id of this car. */ public int id() { return -1; } /** * This method is called when the specified seat has been booked, * to print a ticket for that seat. * * @param seatNo The integer identifier of the seat. */ private void printTicket(int seatNo) { System.out.println("Car ID: " + id); System.out.println("Seat number: " + seatNo); System.out.println("Price: "); if (businessClass) { System.out.println(BUSINESS_SEAT_COST); } else { System.out.println(ECONOMY_SEAT_COST); } } /** * Attempts to book a seat. If successful, this method prints a * ticket and returns true. * If no seats are available, this method returns false. */ public boolean bookNextSeat() { // After booking an available seat, print the ticket by calling // private method printTicket(); e.g., // printTicket(seats[i].number()); return false; }

/** * Cancels the booking for the specified seat, which must be between * 1 and the maximum number of seats in the car. * If the seat number is valid and if the seat has been reserved, this * method cancels the booking for that seat and returns true. * If the seat number is not valid, this method returns false. * If the seat number is valid, but the seat has not been reserved, * this method returns false. */ public boolean cancelSeat(int seatNo) { return false; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions