Question

This question is designed to test your understanding and application of inheritance,composition, polymorphism, exceptions and file...

This question is designed to test your understanding and application of inheritance,composition, polymorphism, exceptions and file handling. You are required to write classes
representing a taxi fleet made up of cars and vans.
Section A
You are to write three classes named Fleet, Car and Van, to represent the fleet of a taxi company. An Fleet object is composed of upto 5 Car objects and 2 Van objects. In addition to the methods specified below you are free to place any necessary methods to promote code reuse. The Car class has three attributes ID, age and cost. It has two methods: …. It should also have a constructor ….. Assume age is always an integer. The Van is the subclass of Car, in addition to the attributes and methods of the Car class, has a surchargeRate attribute and the calculateSurcharge () method. …..This class should also have a constructor …
The Fleet class has four methods:
(1) to add a car
(2) to add a van
(3) calculate the total cost, and
(4) calculate the total surcharge.
Section B
In this section you are required to read the data from a file fleet.txt and then perform the following tasks.
…….
You may omit extra getter and setter methods for simplicity during exam conditions unless otherwise stated.
You do not need to consider imports or packaging. You may make use of the Scanner class to read from a file which as the format:
Scanner scan = new Scanner(new File(“fleet.txt”);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Car.java

public class Car {
private String id;
private int age;
private double cost;
  
public Car()
{
this.id = "";
this.age = 0;
this.cost = 0;
}

public Car(String id, int age, double cost)
{
this.id = id;
this.age = age;
this.cost = cost;
}

public String getId() {
return id;
}

public int getAge() {
return age;
}

public double getCost() {
return cost;
}
  
@Override
public String toString()
{
return ("Id: " + getId() + ", Age: " + getAge() + ", Cost: $" + String.format("%.2f", getCost()));
}
}

Van.java

public class Van extends Car{
private double surchargeRate;
  
public Van()
{
super();
this.surchargeRate = 0;
}
  
public Van(String id, int age, double cost)
{
super(id, age, cost);
}
  
public Van(String id, int age, double cost, double surchargeRate)
{
super(id, age, cost);
this.surchargeRate = surchargeRate;
}

public double getSurchargeRate() {
return surchargeRate;
}
  
public double calculateSurcharge()
{
return (getCost() * (getSurchargeRate() / 100));
}
  
@Override
public String toString()
{
return(super.toString() + ", Surcharge rate: " + String.format("%.2f", getSurchargeRate())
+ ", Surcharge amount: $" + String.format("%.2f", calculateSurcharge()));
}
}

Fleet.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Fleet {
  
private ArrayList<Van> fleet = new ArrayList<>();
  
public void readFile(String fileName)
{
File file = new File(fileName);
Scanner scan;
try
{
scan = new Scanner(file);
while(scan.hasNextLine())
{
String line = scan.nextLine().trim();
if(line.contains("C")) // it's a Car
{
String[] data1 = line.split(" ");
String id = data1[0];
int age = Integer.parseInt(data1[1]);
double cost = Double.parseDouble(data1[2]);
addCar(new Van(id, age, cost));
}
else if(line.contains("V")) // it's a Van
{
String[] data2 = line.split(" ");
String id = data2[0];
int age = Integer.parseInt(data2[1]);
double cost = Double.parseDouble(data2[2]);
double surchargeRate = Double.parseDouble(data2[3]);
addCar(new Van(id, age, cost, surchargeRate));
}
}
}catch(FileNotFoundException fnfe){
System.out.println("Could not find file: " + fileName);
}
}
  
public void addCar(Van van)
{
this.fleet.add(van);
}
  
public ArrayList<Van> getFleet(){ return this.fleet; }
  
public double calculateTotalCost()
{
double totalCost = 0;
for(int i = 0; i < getFleet().size(); i++)
{
totalCost += getFleet().get(i).getCost();
}
return totalCost;
}
  
public double calculateTotalSurcharge()
{
double totalSurcharge = 0;
for(int i = 0; i < getFleet().size(); i++)
{
totalSurcharge += getFleet().get(i).calculateSurcharge();
}
return totalSurcharge;
}
  
public void printAllCars()
{
System.out.println("===== ALL CARS =====\n--------------------");
for(int i = 0; i < getFleet().size(); i++)
{
if(getFleet().get(i).getId().contains("C"))
System.out.println(getFleet().get(i).toString());
}
}
  
public void printAllVans()
{
System.out.println("===== ALL VANS =====\n--------------------");
for(int i = 0; i < getFleet().size(); i++)
{
if(getFleet().get(i).getId().contains("V"))
System.out.println(getFleet().get(i).toString());
}
}
}

FleetDemo.java (Main class)

public class FleetDemo {

private static final String FILENAME = "fleet.txt";
  
public static void main(String[]args)
{
Fleet fleet = new Fleet();
fleet.readFile(FILENAME);
  
fleet.printAllCars();
System.out.println();
fleet.printAllVans();
System.out.println();
  
System.out.println("Total Cost: $" + String.format("%.2f", fleet.calculateTotalCost()));
System.out.println("Total Surcharge: $" + String.format("%.2f", fleet.calculateTotalSurcharge()) + "\n");
}
}

********************************************************************* SCREENSHOT ******************************************************

fleet.txt (below):

Sample Output:

Add a comment
Know the answer?
Add Answer to:
This question is designed to test your understanding and application of inheritance,composition, polymorphism, exceptions and file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA Design and code a class hierarchy that demonstrates your understanding of inheritance and polymorphism....

    IN JAVA Design and code a class hierarchy that demonstrates your understanding of inheritance and polymorphism. Your hierarchy should contain at least 5 classes and one driver program that instantiates each type of object and runs multiple methods on those objects. Inheritance and Polymorphism must be apparent in the project. Please keep in mind that polymorphism was in chapter 37, so you will need to have read both chapters to understand what goes into this project. Bonus if you add...

  • **Using NetBeans** Lesson Using inheritance to reuse classes. Deliverables person.java student.java studentAthlete.java app.java Using inheritance and...

    **Using NetBeans** Lesson Using inheritance to reuse classes. Deliverables person.java student.java studentAthlete.java app.java Using inheritance and the classes (below) The person class has first name last name age hometown a method called getInfo() that returns all attributes from person as a String The student class is a person with an id and a major and a GPA student has to call super passing the parameters for the super class constructor a method called getInfo() that returns all attributes from student...

  • Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes...

    Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes named Cat.java and Dog.java. Cat.java should add attributes for color and breed. Dog.java should add attributes for breed and size (use String for small, medium, large). Add appropriate constructors, getter/setter methods and toString() methods.   In a separate file named PetDriver.java, create a main. In the main, create an ArrayList of Pet. Add an instance of Cat and an instance of Dog to the ArrayList....

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student...

    Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student will demonstrate the ability to use inheritance in Java programs. Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following: Person Automobile Animal Based on your choice: If you choose Person, you will create a subclass of Person called Student. If you choose Automobile, you will create...

  • java. do it on eclipse. the same way its instructed Second Inheritance OOP assignment Outcome: ...

    java. do it on eclipse. the same way its instructed Second Inheritance OOP assignment Outcome:  Student will demonstrate the ability to understand inheritance Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following:  Person  Automobile  Animal  If you choose Person, you will create a subclass of Person called Student.  If you choose Automobile, you will create a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT