package com.codegym.task.task04.task0402;
/*
Price of apples
*/
public class Solution {
public static void main(String[] args) {
Apple apple = new Apple();
apple.addPrice(50);
Apple apple2 = new Apple();
apple2.addPrice(100);
System.out.println("The cost of apples is " + Apple.applePrice);
}
public static class Apple {
public static int applePrice = 0;
public static void addPrice(int appl) {
applePrice=applePrice+appl;//write your code here
}
}
}
here is the solution worked for me
Under discussion
Comments (12)
- Popular
- New
- Old
You must be signed in to leave a comment
hidden #11162976
7 November 2022, 21:58
The problem is that those exercises become from "easy" to very hard. I don't think they were intended for learning. Just for guessing and searching, the answers online. In this way there is "no way" to learn anything :D
+1
Thomas
22 May 2019, 19:40
Is my thinking straight ?? this is sooooo much work to add 2 apple prices together under the hood !! ---
looks to me like 2 apples are constructed -- apple & apple2 a) a function ( method thing ) is created to be able to reuse it on each apple to set a price field / variable & persist the value ?? a.1 ( however in the method -- the function -- applePrice Adds together apple + apple2 price(s) *plural* to get a TOTAL invoice eh ? applePrice(s) is really appleTotalPrices kinda then ..... b) we pass - call - invoke addPrice method -- where it's Parameter is defined as a summation formula inside applePrice b.1) pass in applePrice variable value ( argument ) to set price on the object ?? c) the applePrice Parameter defined in body of the method is a SUM of the applePrice value from 1st call (argument) AND 2nd call (argument) d) use dot notation to extract / bind the variable value with the correct Object to do something with it -- locate the data in an Apple field ? ... hmmm confused not binding to apple or apple2 vs Apple ?! ... will one of you genius people clarify ( or offer feedback ) if this is the program flow ? Thanks Much
0
Guadalupe Gagnon
22 May 2019, 20:51
This tasks is designed to introduce the concept of static variables, which are shared over every single object of the class. Copy and paste this code in when you pass verification and then run it. Try to read the output and go line by line and follow it in the code. It should help you see the code in action and figure out what is going on:
+2
Thomas
22 May 2019, 21:29
Thanks GG ! This is exactly the Step x Step help I was looking for ! THANKS --- It is key that I understand this wayyy better ... I blew it off & went to javascript a few years ago haha -- but need to be better at Java for Android Mobile / Salesforce too ! >> This community is The Best I have ever been involved in ...
This breakdown from Class / Main & Method should help other's as well ............
+2
Guadalupe Gagnon
23 May 2019, 01:30
This is how I solve a lot of tasks, I create these "step by step" output lines so that I can see what is going on. Practice implementing this yourself because it really is way faster and more efficient than trying to walk through the logic in your head. You will start to see unexpected results and be able to pin point exactly what is wrong with your code and make the changes. I've been doing this since I was level 4 to 6 or so and have improved my methods quite considerably. Now I program it to have an on/off switch for all my debugging output lines so that with one change I can shut off every line of code that would fail the task instead of commenting them out or deleting them.
+2
Thomas
23 May 2019, 18:49
I like the embedded debugging Logic here and providing plain speak Output ... I am used to #Javascript rapidly interpreting with tons of debugging Tools
+1
Guadalupe Gagnon
23 May 2019, 19:13
I haven't had a chance yet to check javascript out.
Like i said, this method I have built up over the months. When I first started doing it I would just have 1 or 2 output lines that would be:
// some code
sys.out(apple.applePrice)
// some code
system.out(apple.applePrice)
and my output would be:
50
150
and then I had to go and figure out how that happened. Over the months I started to output a lot more data, and also format it cleanly so it was easy to read. A wall of text can be very cumbersome, but add a few lines of just '=' repeated, and some indentation and it becomes 10 times easier to read and understand.
0
Guadalupe Gagnon
14 January 2019, 16:07
should just be:
0
Sourav Kumar Rana
19 January 2019, 11:35
but this worked
0
William Leininger
3 February 2019, 03:49
This doesn't make sense. It provides an error instead.
com/codegym/task/task04/task0402/Solution.java:31: error: cannot find symbol
applePrice = appl;
^
symbol: variable appl
location: class com.codegym.task.task04.task0402.Solution.Apple
The whole thing makes no sense as when I do Sourav's it returns 150 however it looks like it wasn't going with whatever codegym was confusingly trying to teach. I often find I can't figure these convoluted lessons (pull out my hair & quit 9/10 times) without the help section. I got the below to work as it was holding the variable in the class and not in each individual object - i don't know if that was intended lesson.
+1
Guadalupe Gagnon
4 February 2019, 14:27
I was wrong with my above post. This happens when an OP doesn't use the attach code. I didn't read the task requirements before posting and saw the addPrice method as sort of a constructor. The addPrice method is supposed to add the passed in value to the current value of applePrice.
I'm surprised this post is still up as you aren't supposed to post solutions.
About the error that you are receiving, i would have to see the code that you had that was throwing the error.
0
Thomas
22 May 2019, 19:43
See my post please here for comment .... THX
0