A processor method named upragedRAM(int) that receive the size of RAM to be upgraded as its parameter. This method will determine and return the price of RAM based on the following table:
RAM Size Price(RM)
8GB 98.00
16GB 299.00
3. Write main program :
a. Declare an array of objects named Laptops that store 10 laptop objects.
b. Ask user to enter all information required and store the data in the array above.
c. Calculate and display the total price of all Acer laptops
d. Display the brand of laptop thatt provides 4 USB ports
public class Laptop {
private String brand; //HP, Acer, ASUS
private double price; //price per unit
private int RAM; // memory space in GigaByte(GB),e.g:2,4
private int USBport; //number of USB port e.g:2, 3, 4
public Laptop (String brand, double price, int RAM, int USDport) {
this.brand=brand;
this.price=price;
this.RAM=RAM;
this.USDport=USDport;
}
void upgradeRAM(int n) {
this.RAM += n;
}
public String getBrand() { return brand; }
public double getPrice() { return price; }
public int getRAM() { return RAM; }
public int getUSDport() { return USDport; }
}
public class TestLaptop {
public static void main(String[] args) {
List<Laptop> laptops = new ArrayList<>(10);
int totalPrice = 0;
String name;
for (int i = 0; i < 10; i++) {
String brand = scanner.next();
double price = scanner.next();
int RAM = scanner.nextInt();
int USBport = scanner.nextInt();
laptops.add(new Laptop(brand, price, RAM, USBport));
if (brand == "Acer") { totalPrice += price; }
if (USBport == 4) { name = brand; }
}
System.out.println("Total price of Acer laptops is " + totalPrice);
System.out.println("Laptop with 4 USBport is " + name);
}
}
Comments
Leave a comment