Given the definition of Laptop class as follows:
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
//normal constructor: public Laptop (String, double, int, int)
//processor method: upradeRAM(int)
//accessors: getBrand (), getPrice(), getRAM(), getUSB()
}
2. Write the upgradeRAM(int) processor method as folows:
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 that provides 4 USB ports
import java.util.Scanner;
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
/**
* normal constructor: public Laptop (String, double, int, int)
*
* @param brand
* @param price
* @param RAM
* @param USBport
*/
public Laptop(String brand, double price, int RAM, int USBport) {
this.brand = brand;
this.price = price;
this.RAM = RAM;
this.USBport = USBport;
}
/**
* the upgradeRAM(int) processor method as folows:
*
* 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
*
* @param n
*/
public double upgradeRAM(int n) {
if (n == 8) {
this.price = 98.00;
}
if (n == 16) {
this.price = 299.00;
}
return this.price;
}
// accessors: getBrand (), getPrice(), getRAM(), getUSB()
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
public int getRAM() {
return RAM;
}
public int getUSDport() {
return USBport;
}
}
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
// a. Declare an array of objects named Laptops that store 10 laptop objects.
Laptop laptops[] = new Laptop[10];
double totalPrice = 0;
// b. Ask user to enter all information required and store the data in the array
// above.
for (int i = 0; i < 10; i++) {
System.out.print("Ente the laptop brand " + (i + 1) + ": ");
String brand = keyBoard.nextLine();
System.out.print("Ente the laptop price " + (i + 1) + ": ");
double price = keyBoard.nextDouble();
System.out.print("Ente the laptop RAM " + (i + 1) + ": ");
int RAM = keyBoard.nextInt();
System.out.print("Ente the laptop USBport " + (i + 1) + ": ");
int USBport = keyBoard.nextInt();
laptops[i] = new Laptop(brand, price, RAM, USBport);
System.out.printf("The price of RAM is: %.2f\n\n", laptops[i].upgradeRAM(RAM));
keyBoard.nextLine();
System.out.println("");
}
// d. Display the brand of laptop that provides 4 USB ports
System.out.println("Laptops with 4 USBport are: ");
for (int i = 0; i < 10; i++) {
if (laptops[i].getBrand().compareToIgnoreCase("Acer") == 0) {
totalPrice += laptops[i].getPrice();
}
if (laptops[i].getUSDport() == 4) {
System.out.println(laptops[i].getBrand());
}
}
// c. Calculate and display the total price of all Acer laptops
System.out.println("Total price of Acer laptops is " + totalPrice);
keyBoard.close();
}
}
Comments
Leave a comment