0

Guys can you help me? Java encapsulation

27th Mar 2022, 10:29 AM
Carlo Jade
Carlo Jade - avatar
8 ответов
+ 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); }
27th Mar 2022, 12:24 PM
Roland
Roland - avatar
+ 2
Carlo Jade Where did you call "readInput" and "writeOutput" method?
27th Mar 2022, 1:05 PM
A͢J
A͢J - avatar
+ 1
Carlo Jade Code is incomplete. Write code in Code Playground and share code here.
27th Mar 2022, 10:37 AM
A͢J
A͢J - avatar
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
27th Mar 2022, 10:29 AM
Carlo Jade
Carlo Jade - avatar
0
Here's my code and im keep getting null
27th Mar 2022, 10:29 AM
Carlo Jade
Carlo Jade - avatar
27th Mar 2022, 12:08 PM
Carlo Jade
Carlo Jade - avatar
0
Heres my code
27th Mar 2022, 12:08 PM
Carlo Jade
Carlo Jade - avatar
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;
27th Mar 2022, 10:39 PM
zemiak