Answer to Question #304013 in Java | JSP | JSF for 1789

Question #304013
  • Create a folder named LastName_FirstName (ex. Reyes_Mark) in your local drive.
  • Create a project named LabExer2. Set the project location to your own folder.
  • Construct a simple purchasing program based on the UML Class Diagram below.

LabExer2

  • 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:

  • Writing import java.util.*; on top of the code, before the line for the class name
  • Instantiating an object of the Scanner class, Scanner s = new Scanner (System.in);
  • Storing the input to the variable name based on data type For String: s.nextLine()

For int: s.nextInt()

For double: s.nextDouble()


You are purchasing 3 bag(s) at 1,745.5 each.


1
Expert's answer
2022-02-28T13:53:27-0500
import java.util.Scanner;

public class LabExer2 {
    private String itemName;
    private double itemPrice;
    private int itemQuality;
    private double amountDue;

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

    public void setTotalCost(int quantity, double price) {
        amountDue = quantity * price;
    }

    public String getItemName() {
        return itemName;
    }

    public double getTotalCost() {
        return amountDue;
    }

    public void readInput() {
        Scanner s = new Scanner(System.in);
        System.out.println("Name:");
        itemName = s.nextLine();
        System.out.println("Item price:");
        itemPrice = s.nextDouble();
        System.out.println("Item quantity:");
        itemQuality = s.nextInt();
    }

    public void writeOutput() {
        System.out.println("You are purchasing " + itemQuality + " " + itemName + "(s) at " + itemPrice + " each.");
    }
}

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