Question: The requirement: The ADT is generic and parameterized so that it may hold any type!I still want to use all methods in Truck package unl.cse.trucks;
The requirement: The ADT is generic and parameterized so that it may hold any type!I still want to use all methods in Truck package unl.cse.trucks; import java.util.Iterator; public class TruckListimplements Iterable { static TruckListNode head; private int currentIndex = 0; public int getCurrentIndex() { return currentIndex; } private TruckListNode start; private TruckListNode End; private int size; public void clear() { } public void addToStart(Truck t) { TruckListNode truckListNode = new TruckListNode(t); size++; if (start == null) { start = truckListNode; End = start; } else { truckListNode.setNext(start); start = truckListNode; } } public int getSize() { return size; } public void sortedList(){ TruckListNode truckListNode = start; for (int i = 0; i < size - 1; i++) { if (truckListNode != null) { TruckListNode TheNextNode = truckListNode.getNext(); double Node = Double.parseDouble(truckListNode.getTruck().getLicensePlate()); double NextNode = Double.parseDouble(TheNextNode.getTruck().getLicensePlate()); Truck CurrentTruck = truckListNode.getTruck(); Truck TheNectTruck = TheNextNode.getTruck(); if (Node < NextNode) { truckListNode.setItem(TheNectTruck); TheNextNode.setItem(CurrentTruck); } truckListNode = truckListNode.getNext(); } } } public void addToEnd(Truck t) { TruckListNode truckListNode = new TruckListNode(t); size++; if (start == null) { start = truckListNode; End = start; } else { End.setNext(truckListNode); End = truckListNode; } } public boolean remove(int position) { TruckListNode truckListNode =start; if (position < 1 || position > size) { return false; } else if(position == 1) { start= start.getNext(); size--; return true; }else if(position == size) { for (int i =1;i if (truckListNode.getNext()== null){ return false; } truckListNode = truckListNode.getNext(); truckListNode.setNext(null); End = truckListNode; size--; return true; } return true; }else { for(int i = 1;i if(truckListNode == null) { return false; } truckListNode = truckListNode.getNext(); } TruckListNode NodeTobeRemoved = truckListNode.getNext(); TruckListNode nextNode = NodeTobeRemoved.getNext(); truckListNode.setNext(nextNode); End =nextNode; size--; } return false; } public void setCurrentIndex(int currentIndex) { this.currentIndex = currentIndex; } public void addAll(TruckList trucklist){ TruckListNode truckListNode = start; for (int i = 0; i < size; i++) { if (truckListNode != null) { trucklist.addToEnd(truckListNode.getTruck()); } truckListNode = truckListNode.getNext(); } } private TruckListNode getTruckListNode(int position) { TruckListNode truckListNode = start; for (int i = 0; i < position-1; i++) { if (truckListNode != null) { truckListNode = truckListNode.getNext(); } } return truckListNode; } public Truck getTruck(int position) { TruckListNode truckListNode = start; for (int i = 0; i < position-1; i++) { if (truckListNode != null) { truckListNode = truckListNode.getNext(); } } return truckListNode.getTruck(); } @Override public Iterator iterator() { return new Iterator() { @Override public boolean hasNext() { if (currentIndex<size){ currentIndex++; return true; }else { return false; } } @Override public Object next() { if (currentIndex<size){ currentIndex++; return getTruck(currentIndex); }else { return false; } } }; } }
package unl.cse.trucks; import java.lang.*; public class Driver { public static void main (String[] args){ TruckList TruckList = new TruckList(); Truck t4 = new Truck.Builder().licensePlate("3").build(); Truck t3 = new Truck.Builder().licensePlate("76").build(); Truck t5 = new Truck.Builder().licensePlate("13").build(); Truck t = new Truck.Builder().licensePlate("123").build(); Truck t2 = new Truck.Builder().licensePlate("165").build(); TruckList.addToEnd(t4); TruckList.addToEnd(t); TruckList.addToEnd(t3); TruckList.addToEnd(t2); TruckList.addToEnd(t5); TruckList.sortedList(); TruckList truckList = new TruckList(); for(int U = 0;Uwhile (TruckList.iterator().hasNext()) { System.out.println(TruckList.getTruck(TruckList.getCurrentIndex()).toString()); } System.out.println(); System.out.println(); truckList.setCurrentIndex(0); while (truckList.iterator().hasNext()) { System.out.println(TruckList.getTruck(truckList.getCurrentIndex()).toString()); } } } package unl.cse.trucks; import unl.cse.utils.RandomStringUtils; public class Truck { public static final int HORSEPOWER_MIN = 320; public static final int HORSEPOWER_MAX = 600; public static final int CARRYINGCAPACITY_MIN = 44000; public static final int CARRYINGCAPACITY_MAX = 48000; public static final String[] TRANSMISSION_VALUES = {"Manual", "Automatic"}; public static final int WHEELBASE_MIN = 230; public static final int WHEELBASE_MAX = 280; public static final int WHEELCUT_MIN = 40; public static final int WHEELCUT_MAX = 50; public static class Builder { private String licensePlate; private int carryingCapacity; private int horsePower; private String transmission; private int wheelBase; private int wheelCut; public Builder licensePlate(final String licensePlate) { this.licensePlate = licensePlate; return this; } public Builder carryingCapacity(final int carryingCapacity) { this.carryingCapacity = carryingCapacity; return this; } public Builder horsePower(final int horsePower) { this.horsePower = horsePower; return this; } public Builder transmission(final String transmission) { this.transmission = transmission; return this; } public Builder wheelBase(final int wheelBase) { this.wheelBase = wheelBase; return this; } public Builder wheelCut(final int wheelCut) { this.wheelCut = wheelCut; return this; } public Truck build() { return new Truck(this.licensePlate, this.carryingCapacity, this.horsePower, this.transmission, this.wheelBase, this.wheelCut); } } private final String licensePlate; private final int carryingCapacity; private final int horsePower; private final String transmission; private final int wheelBase; private final int wheelCut; private Truck(String licensePlate, int carryingCapacity, int horsePower, String transmission, int wheelBase, int wheelCut) { this.licensePlate = licensePlate; this.carryingCapacity = carryingCapacity; this.horsePower = horsePower; this.transmission = transmission; this.wheelBase = wheelBase; this.wheelCut = wheelCut; } public static String[] getTransmissionValues() { return TRANSMISSION_VALUES; } /** * Prints the information about the truck */ public void print() { System.out.println("----------------------------------------"); System.out.println("Licence Plate: " + this.licensePlate); System.out.println("Carrying Capacity: " + this.carryingCapacity); System.out.println("Horse Power: " + this.horsePower); System.out.println("Transmission: " + this.transmission); System.out.println("Wheel Base: " + this.wheelBase); System.out.println("Wheel Cut: " + this.wheelCut); System.out.println("----------------------------------------"); return; } /** * * @return a randomly generated Truck */ public static Truck createRandomTruck() { Truck.Builder randomTruck = new Truck.Builder(); String licensePlate = RandomStringUtils.randomAlphabetic(3) + " " + RandomStringUtils.randomNumeric(3); java.util.Random randomNumberGenerator = new java.util.Random(12345678); int carryingCapacity = Truck.CARRYINGCAPACITY_MIN+randomNumberGenerator.nextInt(Truck.CARRYINGCAPACITY_MAX-Truck.CARRYINGCAPACITY_MIN); int horsePower = Truck.HORSEPOWER_MIN+randomNumberGenerator.nextInt(Truck.HORSEPOWER_MAX-Truck.HORSEPOWER_MIN); String transmission = Truck.TRANSMISSION_VALUES[randomNumberGenerator.nextInt(2)]; int wheelBase = Truck.WHEELBASE_MIN+randomNumberGenerator.nextInt(Truck.WHEELBASE_MAX-Truck.WHEELBASE_MIN); int wheelCut = Truck.WHEELCUT_MIN+randomNumberGenerator.nextInt(Truck.WHEELCUT_MAX-Truck.WHEELCUT_MIN); randomTruck.carryingCapacity(carryingCapacity); randomTruck.horsePower(horsePower); randomTruck.licensePlate(licensePlate); randomTruck.wheelBase(wheelBase); randomTruck.wheelCut(wheelCut); randomTruck.transmission(transmission); return randomTruck.build(); } /** * * @return carrying capacity */ public int getCarryingCapacity() { return carryingCapacity; } /** * * @return horsepower */ public int getHorsePower() { return horsePower; } /** * @return License plate number */ public String getLicensePlate() { return licensePlate; } /** * * @return transmission */ public String getTransmission() { return transmission; } /** * * @return wheel base of the truck */ public int getWheelBase() { return wheelBase; } /** * * @return wheel cut of the truck */ public int getWheelCut() { return wheelCut; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Truck "); sb.append(licensePlate); sb.append(", capacity = "+carryingCapacity); sb.append(", horsePower = "+horsePower); sb.append(", transmission = "+transmission); sb.append(", wheelBase = "+wheelBase); sb.append(", wheelCut = "+wheelCut); return sb.toString(); } } package unl.cse.trucks; public class TruckListNode { private TruckListNode next; private Truck item; public void setItem(Truck item) { this.item = item; } public TruckListNode(Truck item) { this.item = item; this.next = null; } public Truck getTruck() { return item; } public TruckListNode getNext() { return next; } public void setNext(TruckListNode next) { this.next = next; } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
