Question: Problem 2 ( Sorting Laptops ) : In a file Laptop.java, create a Laptop class and make the class implement the Comparable interface. The member
Problem Sorting Laptops:
In a file Laptop.java, create a Laptop class and make the class implement the Comparable interface. The member variables should include the following laptop configuration details cpu, ram, hdd graphics card boolean yesno screen size, weight, battery life hours and price. The constructor for the class should allow the initialization of all the member variables. The constructor for a Laptop instance should also calculate a score variable out of which will be calculated as follows:
laptopScore cpucpuMax ramramMax hddhddMaxgraphics screenscreenMax weightweightMax batterybatteryMax pricepriceMax
Here, use the following max values:
cpuMax ramMax hddMax screenMax weightMax batteryMax priceMax
You will also create a toString method, which will print out the configuration of a laptop similar to the octagon problem above including the laptop score. The overridden compareTo method in the Laptop class should compare the configurations of the laptops based on the laptop score.
In a separate file LaptopFactory.java, create a method randomLaptopCreator to create a list of laptops with randomly generated configurations for each of the specification variables. Specify the range for the random values for each specification item based on a reasonably assumed min value and the max value. Next, you will use the Arrays.sort method to sort the list of randomly generated laptops, and print them out using the toString method in ascending order.
NOTE: All attributes must be private. If the attributes are accessed from outside the class, you have implement settersmodifiers and gettersaccessors
LaptopFactory.java
import java.util.Random;
import any other classes here
public class LaptopFactory
private static final Random rand new Random;
public static Laptop randomLaptop
complete this method following the specifications
return new Laptopcpu ram, hdd graphicsCard, screenSize, weight, batteryLife, price;
public static void mainString args
Laptop laptops new Laptop;
for int i ; i laptops.length; i
create random laptop and assign to laptops array in a sequence
Sort here with Arrays.sort
for
Laptop laptop : laptops
print all laptos
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
