moodle.umass.edu podle at UMass Amherst Consider the Vehicle and Truck classes: public class Vehicle String makerName; String model String color: public Vehicle (S
Discipline: Computer science
Type of Paper: Question-Answer
Academic Level: Undergrad. (yrs 3-4)
Paper Format: APA
Pages: 1
Words: 275
Question
:
moodle.umass.edu
podle at UMass Amherst Consider the Vehicle and Truck classes: public
class Vehicle String makerName; String model String color: public
Vehicle (String make, String model, String color) makerName = make; this
model = model: this.color color, public String getMake(){ return
makerName:) public String get Model (return model:) public String
getColor(){return color:) public String toString{ return maker Name",
"model"; } // end class import java.util.ArrayList; public class Truck
extends Vehicle int payloadCapacity: color:) public Truck..............)
public int getPayloadCap({return payloadCapacity:) public String
toString..........) Write the static getHeaviestDuty TruckByColor method
for the Truck class that takes a String parameter, targetColor, and an
ArrayList of Truck objects and returns the Truck object that has the
highest payload capacity that matches the target color. The method
returns null if there is no Truck that matches the targetColor in the
list. Assume the list has length >=0; Example: Executing this code:
ArrayList trucks new ArrayList();
trucks.add(new Truck("Ford", "F150", "red", 1520)); trucks.add(new
Truck("Chevy, "Silverado", "silver", 1800)); trucks.add(new
Truck("Toyota", "Tacoma", "white", 1450)); trucks.add(new Truck("Jeep",
"Gladiator", "red", 1420)); Truck bestTruck
Truck.getHeaviestDutyTruckByColor("red", trucks);
System.out.println(best Truck); -would result in this printed to the
console: Ford, F150, red, 1520 Enter your complete getHeaviestDuty
TruckByColor method definition below. a > <
Expert Answer
public class Vehicle { String makerName; String model; String color; public Vehicle(String make, String model, String color) { this.makerName = make; this.model = model; this.color = color; } public String getMake() { return makerName; } public String getModel() { return model; } public String getColor() { return color; } @Override public String toString() { return makerName +", "+model+", "+color; } }
import java.util.ArrayList; public class Truck extends Vehicle{ int payloadCapacity; public Truck(String make, String model, String color, int payloadCapacity) { super(make, model, color); this.payloadCapacity = payloadCapacity; } public int getPayloadCapacity() { return payloadCapacity; } @Override public String toString() { return super.toString()+", "+payloadCapacity; } public static Truck getHaviestDutyTruckByColor(String targetColor, ArrayList<Truck> trucks){ Truck truck = null; // initialize the variable with min value int maxCapacity = Integer.MIN_VALUE; for(Truck t: trucks){ // looping through trucks if(t.getColor().equals(targetColor)){ // comparing the target color if(t.payloadCapacity > maxCapacity){ // comparing capacity with maxcapacity truck = t; maxCapacity = t.payloadCapacity; } } } return truck; } }
// ---------------------TEST CODE -----------------------------
import java.util.ArrayList; public class VehicleMain { public static void main(String[] args) { ArrayList<Truck> trucks = new ArrayList<Truck>(); trucks.add(new Truck("Ford", "F150", "red", 1520)); trucks.add(new Truck("Chevy", "Silverado", "silver", 1800)); trucks.add(new Truck("Toyota", "Tacoma", "white", 1450)); trucks.add(new Truck("Jeep", "Gladiator", "red", 1420)); Truck bestTruck = Truck.getHaviestDutyTruckByColor("red", trucks); System.out.println(bestTruck); } }
// OUT