Professional Documents
Culture Documents
Instruction:
Use word document to submit your answer. Document must have full
screenshot of syntax and its output. Answer the activity below.
-------------------------Screenshot of codes ---------------------------
*All the description for the code is included as a comment on the IDE
*All the description for the code is included as a comment on the IDE
*This are the actual codes from the IDE with the explanation and comments.
package ACT1_OOP;
import java.util.*; //Importing the Java util package
/*Making a method readInput() to scan/read all the input from the user*/
public void readInput(){
Scanner s = new Scanner(System.in); //Instantiating scanner class object
/*Serves as a prompt to the user to enter the name of item of their choice
* and storing the input on variable itemName*/
System.out.println("▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄");
System.out.println(" SIMPLE PURCHASING CONSOLE PROGRAM");
System.out.println("▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄");
System.out.println("Enter the name of the item you purchasing.");
System.out.print("> ");
itemName = s.nextLine();
/*Serves as a prompt to the user to enter the quantity of item of their choice and
storing the input
* on variable itemQuantity. The price is also asked and stored on the itemPrice
variable*/
System.out.println("Enter the quantity and price separated by a space.");
System.out.print("> ");
itemQuantity = s.nextInt();
itemPrice = s.nextDouble();
/*Closing the scanner class object to stop the process and avoid the leak of
information*/
s.close();
}
/*For us to view the result, the displayResult() method is made with the functions
*println and calling the variables inside*/
public void writeOutput(){
System.out.println("You are purchasing "+itemQuantity+" "+itemName+"(s)"+ " at
"+itemPrice+ " each");
System.out.println("Amount due is "+getTotalCost());
}
/*Finally, we should create an object name for LabExer1. The purpose of this is to
*display all the result from the LabExer1 and call it then display it on the console*/
public static void main(String[] args) {
LabExer1 fin = new LabExer1();
fin.readInput(); //Calling readInput() method
fin.writeOutput (); //Calling writeOutput() method
}
}
------------------------Screenshot of Output ---------------------------
Explanation
We must first know that the setItemName(String newItemName) method
is used to set the name of an item, the setTotalCost(int quantity, double
price) is a function for setting the item's overall cost, the
getItemName() is used to capture the value of the item name, the
quantity, and the price of the item. On the other hand, the purpose of
the private variables is a storage for all the inputs from the user as
well as the results of the computation based on the quantity and price
the user will input. Lastly, the function writeOutput() is just for
displaying all the result of the code. The readInput() and
getTotalCost() are used to set up the input values and capture the value
of the total cost, respectively.