Answer to Question #316481 in Java | JSP | JSF for Dreaper

Question #316481
  1. Create a project named LabExer2
  2. Construct a simple purchasing program based on the UML Class Diagram below.
  • itemName: String
  • itemPrice: double
  • itemQuantity: int
  • amountDue: double


  • setItemName(StringnewitemName): void
  • setTotalCost(int quantity, double price): void
  • getItemName(): String
  • getTotalCost(): double
  • readInput(): void
  • writeOutput(): void

storing the input to the next variable name based on data type

For String s.nextLine()

For int s.nextInt()

For double: s.nextDouble()


1
Expert's answer
2022-03-23T13:44:57-0400
import java.util.Scanner;

class SecondClass{
    String itemName;
    double itemPrice;
    int itemQuantity;
    double amountDue;


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

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

    public String getItemName(){
        return itemName;
    }

    public double getTotalCost(){
        return itemPrice*itemQuantity;
    }

    public void readInput(){

    }

    public void writeOutput(){
        System.out.print(   "Name: " + getItemName() +
                            "\nTotalCost: "+ getTotalCost());
    }
}

public class LabExer2 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        SecondClass secClass = new SecondClass();

        System.out.print("Enter name: ");
        secClass.setItemName(s.nextLine());
        System.out.print("Enter quantity: ");
        int a = s.nextInt();
        System.out.print("Enter price: ");
        secClass.setTotalCost(a, s.nextDouble());

        secClass.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