The document defines a LabExer2 class that calculates item costs. It contains private fields to store the item name, price, quantity, and amount due. Methods set the item name and total cost based on quantity and price. The readInput method prompts the user for input and the writeOutput method displays the purchase details and amount due.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
247 views3 pages
Codes
The document defines a LabExer2 class that calculates item costs. It contains private fields to store the item name, price, quantity, and amount due. Methods set the item name and total cost based on quantity and price. The readInput method prompts the user for input and the writeOutput method displays the purchase details and amount due.
System.out.println("You are purchasing " + "(s) at" + itemPrice + "each."); System.out.println("Amount due is" + amountDue); } }
//package labexer2; import java.util.*;
import java.text.*; public class LabExer2 { //Variable initiation (private access modifier: since there will be no other classes which will access this) private static int itemQuantity; private static double itemPrice; private static double amountDue; private static String itemName; //main method declared public static void main(String[] arg) { //try catch to handle exceptions when an invalid value is entered try { readInput(); } //Throw system defined exceptions: catch (Exception e) { System.out.println(e); System.exit(0); } } //(private access modifier for methods: since there will be no other classes which will access this) private void setItemName(String newItemName) { //print all results itemName = newItemName; } private void setTotalCost(int qty, double price) { itemPrice = price; itemQuantity = qty; amountDue = price * qty; } private String getItemName() { return itemName; } private double getTotalCost() { return amountDue; } private static void readInput() { Scanner s = new Scanner(System.in); System.out.println("Enter the name of the item you are purchasing: "); itemName = s.nextLine(); System.out.println("Enter the quantity and the price of the item separated by a space: "); itemQuantity = s.nextInt(); itemPrice = s.nextDouble(); LabExer2 obj = new LabExer2(); obj.setItemName(itemName); obj.setTotalCost(itemQuantity, itemPrice); obj.writeOutput(); } private void writeOutput() { System.out.println("You are purchasing " + itemQuantity + " " + itemName + "(s) at " + String.format("%.2f", itemPrice) + " each."); System.out.println("Amount Due is " + String.format("%.2f", getTotalCost()) + "."); } }
Scanner ash = new Scanner(System.in); System.out.println("Enter the name of the item you are purchasing."); String itemName = ash.nextLine(); //ash is my nickname
System.out.println("Enter the quality and price seperated by a space.");
int itemQuantity = ash.nextInt(); double itemPrice = ash.nextDouble();