0
Guys can you help me? Java encapsulation
8 Antworten
+ 2
You have to call your readInput() method in main
public static void main(String[]args){
LabExer2 lab = new LabExer2();
lab.readInput();
System.out.println(lab.itemName);
System.out.println(lab.itemQuantity + "" + lab.itemPrice);
}
+ 2
Carlo Jade
Where did you call "readInput" and "writeOutput" method?
+ 1
Carlo Jade
Code is incomplete.
Write code in Code Playground and share code here.
0
import java.util.*;
public class LabExer2{
private String itemName;
private int itemQuantity;
private double itemPrice;
private double amountDue;
public void setName(String newItemName){
newItemName = itemName;
}
public void setTotalCost(int quantity, double price){
quantity = itemQuantity;
price = itemPrice;
}
public void readInput(){
Scanner s = new Scanner(System.in);
System.out.println("Enter name of item you are purchasing.");
itemName = s.nextLine();
System.out.println("Enter the quantity and price separated by a space.");
itemQuantity = s.nextInt();
itemPrice = s.nextDouble();
}
public void writeOutput(){
System.out.println(itemName);
System.out.println("You are purchasing " + itemQuantity + " at " + itemPrice + " each.");
}
public String getItemName(){
return itemName;
}
public double getTotalCost(){
ret
0
Here's my code and im keep getting null
0
Heres my code
0
also print result of getTotalCost() at the end
setName() and setTotalCost() are wrong because
you assign class field to method input parameter
newItemName = itemName;