Question

In Assignment 5, you will write a Toll Collection system. A toll collection system will provides...

In Assignment 5, you will write a Toll Collection system. A toll collection system will provides the total cost of a truck and car when they exit the toll system.

Total cost for Trucks depends on:

1) the number of axles,

2) weight of the vehicle, and

3) total kilometres on the toll highway

Total cost for Cars depends on:

1) the number of axles,

2) total kilometres on the toll highway.

Below is the breakdown of the cost:

Trucks:

One-time cost per axle: $2

Charge per Km: $0.2 per km

Weight:

Less than 10,000 lbs: $0.003 per lbs

10,000 lbs - 20,000 lbs (inclusive): $0.0031 per lbs

Greater than 20,000 lbs: $0.0032 per lbs

Cars:

One-time cost per two axles: $3

Assume that all cars will have exactly 2 axles

Charge per Km: $0.25 per km

Classes:

You should have a Class for Truck and another class for Car with appropriate attributes that would help you calculate the toll price based on criteria above. Weight will be entered in Pounds (lbs) and distance will be entered in Kilometres (kms)

A Car object can be instantiated using Car nissan = new Car(36.2) - The distance is provided in the parameter.

Total cost for the Car instantiated above should be:
One-time Cost: $3.0

Distance cost: 36.2*0.25 = $9.05

Total = $12.05

A Truck object can be instantiated using Truck mack = new Truck(5, 21000, 65.0) - The number of axles in the first parameter, the weight of the truck in the second parameter, and finally, the distance in the third parameter.

Total cost for a Truck instantiated above should be:
Axle cost: 5*2 = $10

Weight cost: 21000*0.0032 = $67.20

Distance cost: 65.0*0.2 = $13

Total = $90.2

In addition to the Truck and Car classes, you will have a Class called Assignment 5 which will have the main() method. I am providing three sample Assignment5.java for you to test. Your Truck and Car classes MUST work with the below example. You are welcome to create helper methods in Truck and Car classes. You MUST have the right checks in both Truck and Car classes to ensure the program won't throw exceptions or return incorrect values. Additionally, if values provided is < 0 (for axles) and < 0.0 (for kms and distance), you should default the value to 0 (for axles) and 0.0 (for kms and distance).

// Sample Assignment5.java

public class Assignment5 {

    public static void main(String[] args) {

        Truck mack1 = new Truck(5, 21000.0, 65.0);
        Truck mack2 = new Truck(6, 22000.4, 15.3);
        Truck mack3 = new Truck(3, 2949.4, 19.0);

        double mack1Cost = mack1.getCost();
        double mack2Cost = mack2.getCost();
        double mack3Cost = mack3.getCost();

        System.out.printf("$%.2f", mack1Cost + mack2Cost + mack3Cost);
        // should output $194.31
    }
}

// Sample Assignment5.java
public class Assignment5 {

    public static void main(String[] args) {

        Truck mack1 = new Truck(5, 21000.0, 65.0);
        Car nissan1 = new Car(36.2);

        double mack1Cost = mack1.getCost();
        double nissan1Cost = nissan1.getCost();

        System.out.printf("$%.2f", mack1Cost + nissan1Cost);
        // should output $102.25
    }
}

// Sample Assignment5.java
public class Assignment5 {

    public static void main(String[] args) {

        Truck mack1 = new Truck(5, 21000.0, 65.0);
        Car nissan1 = new Car(36.2);
        Car nissan2 = new Car(16.2);

        double mack1Cost = mack1.getCost();
        double nissan1Cost = nissan1.getCost();
        double nissan2Cost = nissan2.getCost();

        System.out.printf("$%.2f", mack1Cost + nissan1Cost + nissan2Cost);
        // should output $109.30
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Car.java

public class Car {
   private double distance;

   public Car(double distance) {
       if (distance < 0)
           this.distance = 0.0;
       else
           this.distance = distance;
   }

   public double getDistance() {
       return distance;
   }

   public void setDistance(double distance) {
       this.distance = distance;
   }

   public double getCost() {
       double totCost = 0;
       totCost += 3.0;
       totCost += distance * 0.25;
       return totCost;
   }
}
__________________________

// Truck.java

public class Truck {
   private int noOfAxles;
   private double distance;
   private double weight;

   public Truck(int noOfAxles, double weight, double distance) {
       if (noOfAxles < 0)
           this.noOfAxles = 0;
       else
           this.noOfAxles = noOfAxles;

       if (distance < 0)
           this.distance = 0.0;
       else
           this.distance = distance;

       this.weight = weight;
   }

   public int getNoOfAxles() {
       return noOfAxles;
   }

   public void setNoOfAxles(int noOfAxles) {
       this.noOfAxles = noOfAxles;
   }

   public double getDistance() {
       return distance;
   }

   public void setDistance(double distance) {
       this.distance = distance;
   }

   public double getWeight() {
       return weight;
   }

   public void setWeight(double weight) {
       this.weight = weight;
   }

   public double getCost() {
       double totCost = 0;
       totCost += noOfAxles * 2;

       if (weight < 10000) {
           totCost += weight * 0.003;
       } else if (weight >= 10000 && weight < 20000) {
           totCost += weight * 0.0031;
       } else if (weight > 20000) {
           totCost += weight * 0.0032;
       }

       totCost += distance * 0.2;
       return totCost;
   }
}
_____________________________

// Assignment5.java

public class Assignment5 {

public static void main(String[] args) {

Truck mack1 = new Truck(5, 21000.0, 65.0);
Truck mack2 = new Truck(6, 22000.4, 15.3);
Truck mack3 = new Truck(3, 2949.4, 19.0);

double mack1Cost = mack1.getCost();
double mack2Cost = mack2.getCost();
double mack3Cost = mack3.getCost();

System.out.printf("$%.2f", mack1Cost + mack2Cost + mack3Cost);
// should output $194.31
}
}

___________________________

Output:

$194.31

___________________________

// Assignment5.java

public class Assignment5 {

public static void main(String[] args) {

Truck mack1 = new Truck(5, 21000.0, 65.0);
Car nissan1 = new Car(36.2);

double mack1Cost = mack1.getCost();
double nissan1Cost = nissan1.getCost();

System.out.printf("$%.2f", mack1Cost + nissan1Cost);
// should output $102.25
}
}

____________________________

Output:

$102.25

____________________________

// Assignment5.java

// Sample Assignment5.java
public class Assignment5 {

    public static void main(String[] args) {

        Truck mack1 = new Truck(5, 21000.0, 65.0);
        Car nissan1 = new Car(36.2);
        Car nissan2 = new Car(16.2);

        double mack1Cost = mack1.getCost();
        double nissan1Cost = nissan1.getCost();
        double nissan2Cost = nissan2.getCost();

        System.out.printf("$%.2f", mack1Cost + nissan1Cost + nissan2Cost);
        // should output $109.30
    }
}

____________________________

Output:

$109.30


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
In Assignment 5, you will write a Toll Collection system. A toll collection system will provides...
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
  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are two parts for one code (one question) the two parts branch off, one part has 2 sheets of code, the other has 3 sheets of code, all are small code segments for one question. Pt.1 - 2 sheets of code: public class Computer { private String make; private String type; private String modelNumber; private double cpuSpeed_GHz; private int ramSize_GB; private int hardDriveSize_GB; private...

  • The class Engineer Test below is used to test two other class named Project & Engineer....

    The class Engineer Test below is used to test two other class named Project & Engineer. A project has three attributes: projNo (int), projName (string), revenue (double). Add a parameterized constructor, and setters & getters for all attributes. An engineer has four attributes: jobld (int), name (string), rate (double), and a list of projects. For each project, the engineer has a specific amount as profit (project revenue rate). Add a constructor that initializes the attributes: jobild, name and rate. Implement...

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

  • Complete the CashRegister class by implementing the methods and adding the correct attributes (characteristics) as discussed...

    Complete the CashRegister class by implementing the methods and adding the correct attributes (characteristics) as discussed in class. Once complete, test the class using the CashRegisterTester class. Make sure you have 3 total items in the cash register. I have two classes, the first is CashRegisterTester below: public class CashRegisterTester { public static void main(String[] args) { //Initialize all variables //Construct a CashRegister object CashRegister register1 = new CashRegister(); //Invole a non-static method of the object //since it is non-static,...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

  • Use java and continue stage 2 and 3 stage 1 code public abstract class BabyItem { protected String name;...

    Use java and continue stage 2 and 3 stage 1 code public abstract class BabyItem { protected String name; public BabyItem() { name=""; } public BabyItem(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { } public abstract double getCost(); } ======================================================================================== public class BabyFood extends BabyItem { private int numberOfJars; private double pricePerDozen; public BabyFood() { super(); numberOfJars = 0; pricePerDozen = 0; } public BabyFood(int numberOfJars, double pricePerDozen) {...

  • Please help me with my JAVA programming Exercise. a. The Talk-A-Lot Cell Phone Company provides phone...

    Please help me with my JAVA programming Exercise. a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods—one that returns the phone number,...

  • code must be in java. Assignment 5 Purpose In this assignment, you will implement a class...

    code must be in java. Assignment 5 Purpose In this assignment, you will implement a class hierarchy in Java. Instructions The class hierarchy you will implement is shown below account Package Account manager Package SavingAccount CheckingAccount AccountManager The descriptions of each class and the corresponding members are provided below. Please note that the Visibility Modifiers (public, private and protected) are not specified and you must use the most appropriate modifiers Class AccountManager is a test main program. Create it so...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from an input file. This program consists of two classes. The first class is the Weight class, which is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods and two private methods: 1. A public constructor that allows...

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