public class LabExer2 {
private String itemName;
private double itemPrice, amountDue;
private int itemQuantity;
public void setItemName(String newItemName) {
itemName = newItemName;
}
public void setTotalCost(int quantity, double price) {
itemPrice = price;
itemquantity = quantity;
}
public String getItemName() {
return itemName;
}
public double getTotalCost () {
amountDue = itemPrice * itemquantity;
return amountDue;
}
public void readInput () {
Scanner a = new Scanner([Link]);
[Link]("Enter the name of the item you are purchasing.");
itemName = [Link]();
[Link]("Enter the quality and price seperated by a space.");
itemquantity = [Link]();
itemPrice = [Link]();
}
public void writeOutput() {
[Link]("You are purchasing " + "(s) at" + itemPrice + "each.");
[Link]("Amount due is" + amountDue);
}
}
//package labexer2; import [Link].*;
import [Link].*; 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) { [Link](e); [Link](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([Link]); [Link]("Enter the name of the
item you are purchasing: "); itemName = [Link](); [Link]("Enter the quantity
and the price of the item separated by a space: "); itemQuantity = [Link](); itemPrice =
[Link](); LabExer2 obj = new LabExer2(); [Link](itemName);
[Link](itemQuantity, itemPrice); [Link](); } private void writeOutput()
{ [Link]("You are purchasing " + itemQuantity + " " + itemName + "(s) at " +
[Link]("%.2f", itemPrice) + " each."); [Link]("Amount Due is " +
[Link]("%.2f", getTotalCost()) + "."); } }
import [Link];
public class LabExer2 {
private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;
public void setItemName(String newItemName) {
itemName = newItemName;
}
public void setTotalCost(int quantity, double price) {
itemQuantity = quantity;
itemPrice = price;
amountDue = quantity*price;
}
public String getItemName() {
return itemName;
}
public double getTotalCost () {
return amountDue;
}
public static void readInput () {
Scanner ash = new Scanner([Link]);
[Link]("Enter the name of the item you are purchasing.");
String itemName = [Link](); //ash is my nickname
[Link]("Enter the quality and price seperated by a space.");
int itemQuantity = [Link]();
double itemPrice = [Link]();
LabExer2 obj = new LabExer2();
[Link](itemName);
[Link](itemQuantity, itemPrice);
[Link]();
public void writeOutput() {
[Link]("You are purchasing " + itemQuantity + "(s)" + "at" + itemPrice +
"each.");
[Link]("Amount due is" + getTotalCost() );
}
public static void main(String[] args) {
readInput();
}
}