You are on page 1of 6

Laboratory Activity 1 for OOP

Lee, Jennefer Brazil


IT21S3
September 03, 2022
Integrative Programming and Technologies

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

public class LabExer1{


/*Declaring all the variables based on the given information on the activity */
private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;
/*Implementing the setter for the newItemName */
public void setItemName(String newItemName){
this.itemName = newItemName;
}

/*Implementing the getters for the newItemName */


public String getItemName(){
return itemName;
}
/*Assigning the variables/getting the output from user*/
public void setTotalCost(int quantity, double price){
this.itemQuantity = quantity;
this.itemPrice = price;
}
/*Setting-up the function for getting the total cost of the item multiplied by the
quantity */
public double getTotalCost(){
amountDue = itemPrice*itemQuantity;
return amountDue;
}

/*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.

As seen on the instructions, the given public variables are the


itemName, itemPrice, itemquantity, and amountDue. I stored the prompts
of the user on the given variables by their data type by using the
Scanner (s). This is done by setting the newItemName as itemName, int
quantity and double price as itemQuantity and itemPrice respectively.
After that we get the stored data on it using the getters getItemName
and getTotalCost and assigning them as amountDue or the calculated
result.

Honor Pledge for Graded Assignments


“I affirm that I have not given or received any unauthorized help on
this assignment and that this work is my own.”
-Lee, Jennefer Brazil

You might also like