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 2(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 1=yes/0=no), 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 10, which will be calculated as follows:
laptopScore =(2* cpu/cpuMax)+(2* ram/ramMax)+(1* hdd/hddMax)+(graphics)+(1* screen/screenMax)+(1* weight/weightMax)+(1* battery/batteryMax)+(1* price/priceMax)
Here, use the following max values:
cpuMax =3.0, ramMax =32, hddMax =2048, screenMax =17.0, weightMax =6, batteryMax =9, priceMax =2000
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 5 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 setters(modifiers) and getters(accessors).
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 Laptop(cpu, ram, hdd, graphicsCard, screenSize, weight, batteryLife, price);
}
public static void main(String[] args){
Laptop[] laptops = new Laptop[5];
for (int i =0; 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!