• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A method to accumulate a total

 
De Huaste
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello CR! First timer here. Looking for help getting my code to accumulate a total rather than just getting the last number.

This is a snippet of the main method...

public class PickingBerries
{
public static void main(String [] args)
{
System.out.println("Out in the forest, some friends were picking blackberries...");

Bucket alan = new Bucket(); // Alan has an empty bucket
Bucket lisa = new Bucket(); // Lisa has an empty bucket
Bucket ian = new Bucket();

System.out.println("Alan...");

alan.report(); // Prints to console: "This bucket has 0 berries."
alan.pick(10); // 10 in bucket now
alan.pick(2); // now up to 12
alan.report(); // Prints to console: "This bucket has 12 berries."

This is a snippet of the second class with the pick method. As it stands, when I run the program I'm only getting the last number picked (2) instead of the total (12).

class Bucket
{
//keeps tracks of the number of blackberries
//that a person has picked and placed in their bucket

private static int bucket;
private boolean status = true;

public void pick(int berries)
{
bucket =+ berries;
}

Help is appreciated
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of


Try


Also, please use code tags when posting code
 
De Huaste
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aj Prieto wrote:Instead of


Try


Also, please use code tags when posting code


Thank you very much. And will do
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
De Huaste wrote:
Aj Prieto wrote:Instead of


Try


Also, please use code tags when posting code


Thank you very much. And will do


Much more important than solving this particular issue is the question: Do you understand the difference, and why one does what you want but not the other?
 
De Huaste
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
De Huaste wrote:
Aj Prieto wrote:Instead of


Try


Also, please use code tags when posting code


Thank you very much. And will do


Much more important than solving this particular issue is the question: Do you understand the difference, and why one does what you want but not the other?


I agree and to be honest, I need to re-read my assignment operators. But if you're offering wisdom, I'll certainly listen.
 
Carey Brown
Bartender
Posts: 1685
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private static int bucket;

I don't think you want that to be static. Each instance should have its own bucket.
And a nit: seeing as how your class is named 'Bucket' you may want to rename this to something else, like 'count'. This won't have any impact on how it works but it might make reading it a little clearer.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
De Huaste wrote:
I agree and to be honest, I need to re-read my assignment operators. But if you're offering wisdom, I'll certainly listen.




Just as you might write on paper for an algebra problem, X = +Y. That is, where X = -Y is "X equals negative Y", similarly X = +Y is "X = positive Y", which of course is the same as "X = Y". Same thing in Java


 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: . . . X = +Y is "X = positive Y", which of course is the same as "X = Y". . . .
Actually the unary + operator is promotion, not positivity. This came up a few weeks ago, and nobody could think of a situation when they would actually use that operator.
 
De Huaste
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all! Campbell and Carey, points noted. Jeff, this makes perfect sense. Moving on to bigger and more interesting problems ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic