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: 3814413 • 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"

it starts like this..

public class TestTruck {
public static void main(String[] args) {
Truck garbage = new Truck(5000);garbage.load(200);
garbage.load(1000);
garbage.unload(3);
Truck dodgeRam = new Truck(1500);
Truck construction = new Truck(4500);
construction.load(4500);
if (garbage.isEmpty())
System.out.println("Empty");
else
System.out.println("NOT empty");
System.out.println(dodgeRam.isEmpty());
System.out.println(garbage.isFull());
System.out.println(construction.isFull());
  
System.out.printf("Left: %d ", garbage.capacityLeft());
System.out.printf("Current: %d pounds ", garbage.getAmountLoaded());
  
System.out.println(garbage.toString());
System.out.println(dodgeRam.toString());
System.out.println(construction.toString());
}
}
public class TestTruck2 {
public static void main(String[] args) {
Truck t1 = new Truck(3);
System.out.printf("Capacity left should be 50: %d ", t1.capacityLeft());
}
}

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) {
  

Truck garbage = new Truck(5000);garbage.load(200);
garbage.load(1000);
garbage.unload(3);
Truck dodgeRam = new Truck(1500);
Truck construction = new Truck(4500);
construction.load(4500);
if (garbage.isEmpty())
System.out.println("Empty");
else
System.out.println("NOT empty");
System.out.println(dodgeRam.isEmpty());
System.out.println(garbage.isFull());
System.out.println(construction.isFull());
  
System.out.printf("Left: %d ", garbage.capacityLeft());
System.out.printf("Current: %d pounds ", garbage.getAmountLoaded());
  
System.out.println(garbage.toString());
System.out.println(dodgeRam.toString());
System.out.println(construction.toString());
   }

}

___________________

Output#1:

NOT empty
true
false
true
Left: 3803
Current: 1197 pounds
Truck with a capacity of 5000 pounds, currently loaded with 1197 pounds
Truck with a capacity of 1500 pounds, currently loaded with 0 pounds
Truck with a capacity of 4500 pounds, currently loaded with 4500 pounds

______________

TestTruck2.java

public class TestTruck2 {

   public static void main(String[] args) {
       Truck t1 = new Truck(3);
   System.out.printf("Capacity left should be 50: %d ", t1.capacityLeft());

   }

}

_________________

Output#2:

Capacity left should be 50: 50

_______________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