Been look around and can not find/understand how comparing objects within an array works. In the code I provided below, how would I be able to compare the objects, within an array based specifically on the cost of the two? Then sorting the Array so the cost is in descending order?
public class PC
{
private int VRAM;
public String Processor;
public int RAM;
public int Harddrive;
public double Cost;
public Desktop(String Proc, int ram, int HD, int Vram)
{
Processor = Proc;
RAM = ram;
Harddrive = HD;
VRAM = Vram ;
}
public double getCost()
{
Cost = 250 + (5.50*RAM) + (0.10*Harddrive) + (0.30*VRAM);
return Cost;
}
public String toString()
{
return "Desktop\n" + "--------\n" + "CPU: " + Processor + "\nRAM: " + RAM + "GB\n" + "HDD: " + Harddrive + "GB\n" + "VRAM: " + VRAM + "MB" + "\nCost: $" + Cost + "\n";
}
}
public class Main
{
public static void main(String[] args)//Main method has been tested throughly, but the output seems to get a bit nasty
{
Computer[] ComputerArray = new Computer[5];//when to many are called all out once.
ComputerArray[0] = (new PC ("Intel Core i7 2600k", 4, 700, 1400));
ComputerArray[1] = (new PC ("AMD FX-8150", 16, 1950, 1100));
}
}
ComputerArrayshould becomputerArray