Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this programming assignment you will create a class that models a Truck. The

ID: 3814342 • Letter: I

Question

In this programming assignment you will create a class that models a Truck. The feature of the truck that we will be modeling is the amount of material that it is carrying, measured in pounds. Each truck object will have a specified capacity, which must be in the range of 50 pounds to 10,000 pounds. The methods of the Truck class will allow weight to be loaded onto the truck, and unloaded from the truck. You can also ask if the truck is empty or full, how much is currently loaded on the truck, and how much room is left. Here are the methods that the Truck class must implement: Truck(int capacity). The constructor takes a single integer parameter that indicates the maximum capacity of this truck object, measured in pounds. The capacity must be between 50 and 10,000 pounds. If a value is given to the constructor for a capacity less than 50 pounds, then the constructor needs to set the capacity to 50. If value is requested that is greater than 10,000, then set the capacity to 10,000 pounds. load(int pounds). Load the indicated weight into the truck. The addition of this weight should not cause the truck's loaded weight to go out of range (i.e., not below zero and not above its capacity). unload(int pounds). Unload the indicated weight from the truck. The removal of this weight should not cause the truck's loaded weight to go out of range (i.e., not below zero and not above its capacity). isEmpty(). This method has a boolean return value. It should return true when then load is at zero, and false otherwise. isFull(). This method has a boolean return value. It should return true when then load is at capacity, and false otherwise. getAmountLoaded(). This method returns an integer that is the amount of content currently loaded onto the truck. capacityLeft(). This method returns an integer indicating how much more weight can be loaded into the truck before it hits capacity. toString(). Returns a String describing this Truck object. The String should be of this format: "Truck with a capacity of 5000 pounds, currently loaded with 1197 pounds"

Explanation / Answer

Truck.java

public class Truck {
   // Declaring variables
   private int capacity;
   private int material_weight;

   // parameterized constructor
   public Truck(int capacity) {
       if (capacity < 50)
           this.capacity = 50;
       else if (capacity > 10000)
           this.capacity = 10000;
       else
           this.capacity = capacity;

       this.material_weight = 0;
   }

   // This method will load the amount to truck
   public void load(int pounds) {
       if (this.material_weight + pounds <= 10000)
           this.material_weight += pounds;
   }

   // This method will unload the amount from truck
   void unload(int pounds) {
       if (this.material_weight - pounds >= 0)
           this.material_weight -= pounds;
   }

   // This method will check whether the truck is empty or not
   public boolean isEmpty() {
       if (material_weight == 0)
           return true;
       else
           return false;
   }

   // This method will check whether the truck is full or not
   public boolean isFull() {
       if (material_weight == capacity)
           return true;
       else
           return false;
   }

   // This method will return the current load weight
   public int getAmountLoaded() {
       return material_weight;
   }

   // This method will return the capacity left in the truck
   public int capacityLeft() {
       return capacity - material_weight;
   }

   @Override
   public String toString() {
       return "Truck with a capacity of " + capacity
               + " pounds, currently loaded with " + material_weight
               + " pounds";
   }

}

__________________

Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       // Declaring variables
       int capacity;
       int loadWt;

       // Scanner object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       // getting the capacity value entered by the user
       System.out.print("What is the Maximum capacity of the Truck :");
       capacity = sc.nextInt();

       // Setting the capacity based on the user entered value
       Truck truck1 = new Truck(capacity);

       // Displaying the info
       System.out.println(truck1.toString());

       // getting the weight of the material to be added to the truck
       System.out.print("How much amount you want to load :");
       loadWt = sc.nextInt();

       // Loading the material into the truck
       truck1.load(loadWt);
       System.out.println(truck1.toString());

       // getting the weight of the material to be unload from the truck
       System.out.print("How much amount you want to unload :");
       loadWt = sc.nextInt();

       // unloading from the truck
       truck1.unload(loadWt);
       System.out.println(truck1.toString());

       // checking whether the truck is empty or not
       boolean bool1 = truck1.isEmpty();
       if (bool1)
           System.out.println("The Truck is Empty");
       else
           System.out.println("The Truck is not Empty");

       // checking whether the truck is full or not
       boolean bool2 = truck1.isEmpty();
       if (bool1)
           System.out.println("The Truck is Full");
       else
           System.out.println("The Truck is not Full");

       // Displaying the weight of the material loaded into the truck
       System.out.println("The Loaded amount in the Truck is :"+ truck1.getAmountLoaded());

       // Displaying how much space left into the truck.
       System.out.println("The Capacity Left in the Truck is :"+ truck1.capacityLeft());

   }

}

__________________

Output:

What is the Maximum capacity of the Truck :9000
Truck with a capacity of 9000 pounds, currently loaded with 0 pounds
How much amount you want to load :4000
Truck with a capacity of 9000 pounds, currently loaded with 4000 pounds
How much amount you want to unload :2500
Truck with a capacity of 9000 pounds, currently loaded with 1500 pounds
The Truck is not Empty
The Truck is not Full
The Loaded amount in the Truck is :1500
The Capacity Left in the Truck is :7500

______________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote