Answer to Question #279705 in Java | JSP | JSF for syahidah

Question #279705

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()

}


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


1
Expert's answer
2021-12-15T07:00:14-0500


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;
	}


	// processor method: upradeRAM(int)
	public void upgradeRAM(int n) {
		this.RAM += n;
	}


	// 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);
			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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment