+ 2

Pls i need help in my java code..i have issues in the println method

public class Program{ void name() { System.out.println("Peach slice"); } void weight(){ System.out.println("500grams"); } void calories() { System.out.println(70); } void servings() { System.out.println(4); } void cost(){ System.out.println(1.83); } } public class foodProduct { public static void main(String args[]); Program product=new Program(); System.out.println("The product "+ product.name + " has "+ product.calories + " at "+ product.servings + ". It cost

quot; + product.cost+ " and is weighed at "+ product.weight); }

1st Jul 2020, 1:54 AM
Osazuwa Lawson
Osazuwa Lawson - avatar
15 Antworten
+ 3
Inside the main method try this, Program product = new Program(); product.name(), product.weight(); product.calories(); product.serving(); product.cost(); Then, end the main method braces and class braces. I hope u got me......!☺
1st Jul 2020, 1:08 PM
Arya Deep Chowdhury
Arya Deep Chowdhury - avatar
+ 2
Btw. accessing the members would work that way, but it's not good style to do so.
1st Jul 2020, 2:25 AM
Sandra Meyer
Sandra Meyer - avatar
+ 2
Ohhh... thanks...I got that idea from a book by Barry Burd "Java for dummies" Well we keep learning every day thanks once again
1st Jul 2020, 2:26 AM
Osazuwa Lawson
Osazuwa Lawson - avatar
+ 2
😂😂😂don't be so harsh
1st Jul 2020, 2:30 AM
Osazuwa Lawson
Osazuwa Lawson - avatar
+ 2
Think of a class as something what is organizing your data. Usually you have it in other files beyond the places where you're coding the logic. So it should be implemented and designed to make your logic as short and readable as possible. Therefore you need e.g. one or more useful constructors to avoid setting members from the outside. And it's of course much better to provide a method which organizes the output for you instead of organizing it where the more relevant logic should take place.
1st Jul 2020, 2:31 AM
Sandra Meyer
Sandra Meyer - avatar
+ 2
Have to, that's even worse than... And should definitely not be recommended by a beginners book...
1st Jul 2020, 2:32 AM
Sandra Meyer
Sandra Meyer - avatar
+ 2
Thanks very much Sandra...I really appreciate🙂🙂
1st Jul 2020, 2:33 AM
Osazuwa Lawson
Osazuwa Lawson - avatar
+ 1
Your problems are not caused by print method. You're calling the methods without braces, which are mandatory. Without braces you can only access to visible fields (members) of a class.
1st Jul 2020, 2:02 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
There are some more issues... You need to put the stuff you want to be executed within main method within curly braces as code block. And calling the methods will not work as you obviously expect. You should make them either returning the requested values to use them within a print call or change from one println in main method to several.
1st Jul 2020, 2:06 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Thanks very much... I'll make the appropriate changes I really appreciate 🙂🙂
1st Jul 2020, 2:09 AM
Osazuwa Lawson
Osazuwa Lawson - avatar
+ 1
Here you can take a look, how the clean implementation would be: https://code.sololearn.com/cbgc7t96zTSj/?ref=app
1st Jul 2020, 2:18 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Ohh I see...I printed this code without using method b4...So I decided to use the method format..then I encountered this error This code is the one without method https://code.sololearn.com/cMdChR19SCrD/?ref=app
1st Jul 2020, 2:22 AM
Osazuwa Lawson
Osazuwa Lawson - avatar
+ 1
Please read some stuff about classes. What you've got there is more a data structure like C struct, but not an object. To instantiate an object, it needs a construction (what you are calling with keyword "new <name>). Without any constructor implementation, this cannot work.
1st Jul 2020, 2:24 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
For real? Then I have to check my library to delete it if it's there...
1st Jul 2020, 2:27 AM
Sandra Meyer
Sandra Meyer - avatar
0
It's quite better to use getter-methods and to encapsulate some logic. To display an object, it's completely unnecessary to do this from the outside field by field. This information (how a complete summary of a product looks like and which information it contains), should be part of the corresponding class and not clutter the program logic.
2nd Jul 2020, 6:43 PM
Sandra Meyer
Sandra Meyer - avatar