aspose file tools*
The moose likes Beginning Java and the fly likes A method to accumulate a total Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A method to accumulate a total" Watch "A method to accumulate a total" New topic
Author

A method to accumulate a total

De Huaste
Greenhorn

Joined: Nov 18, 2012
Posts: 11
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

Joined: Sep 28, 2012
Posts: 75

Instead of


Try


Also, please use code tags when posting code


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
De Huaste
Greenhorn

Joined: Nov 18, 2012
Posts: 11
Aj Prieto wrote:Instead of


Try


Also, please use code tags when posting code


Thank you very much. And will do
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 18, 2012
Posts: 11
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
Ranch Hand

Joined: Nov 19, 2001
Posts: 209

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.


Sent from my IBM 360 mainframe
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Nov 18, 2012
Posts: 11
Thank you all! Campbell and Carey, points noted. Jeff, this makes perfect sense. Moving on to bigger and more interesting problems ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A method to accumulate a total