Answer to Question #300274 in Java | JSP | JSF for jhadz

Question #300274

Create a Java program named PE2Lastname. (ex. PE2Dela_Cruz)

Construct a simple purchasing program based on the UML Class Diagram below.

The (-) symbol represents private variables, while(+) represent public.

PE2Lastname

-itemName: String

-itemPrice:double

-itemQuantity:int

-amountDue:double

+setItemName(String newItemName): void

+setTotalCost(int quantity, double price): void

+getItemName(): String

getTotalCost(): double

+readInput(): void

+writeOutput():void

Note: The readInput() method will be used to accept user input through the Scanner

class. This is done by doing the following:

a.

Importing Scanner from the java.util package.

b.

Instantiating an object of the Scanner class, Scanner s = new

Scanner(System.in);

c.

Storing the input to the variable name based on data type:

a.

For String: s.nextLine()

b.

For int: s.nextInt()

c.

For double: s.nextDouble()

d.



1
Expert's answer
2022-02-20T14:40:47-0500


import java.util.*;


class PE2Lastname {
	private String itemName;
	private double itemPrice;
	private int itemQuantity;
	private double amountDue;


	public void setItemName(String newItemName) {
		this.itemName = newItemName;
	}


	public void setTotalCost(int quantity, double price) {
		this.itemQuantity = quantity;
		this.amountDue = price;
	}


	public String getItemName() {
		return itemName;
	}


	public double getTotalCost() {
		return amountDue;
	}


	public void readInput() {
		Scanner s = new Scanner(System.in);
		System.out.print("Enter item name: ");
		this.itemName = s.nextLine();
		System.out.print("Enter item price: ");
		this.itemPrice = s.nextDouble();
		System.out.print("Enter item quantity: ");
		this.itemQuantity = s.nextInt();
		this.amountDue = itemPrice * itemQuantity;
		s.close();
	}


	public void writeOutput() {
		System.out.println("The item name: " + this.itemName);
		System.out.println("The item price: " + this.itemPrice);
		System.out.println("The item quantity: " + this.itemQuantity);
		System.out.println("The item amount due: " + this.amountDue);
	}


}


class App {


	public static void main(String[] args) {
		PE2Lastname PE2Lastname = new PE2Lastname();
		PE2Lastname.readInput();
		PE2Lastname.writeOutput();
	}
}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS