5

I'm in a beginning programming class, and a lot of this had made sense to me up until this point, where we've started working with methods and I'm not entirely sure I understand the "static," "void," and "return" statements.

For this assignment in particular, I thought I had it all figured out, but it says it "can not find symbol histogram" on the line in the main method, although I'm clearly returning it from another method. Anyone able to help me out?

Assignment: "You see that you may need histograms often in writing your programs so you decide for this program to use your program 310a2 Histograms. You may add to this program or use it as a class. You will also write a class (or method) that will generate random number in various ranges. You may want to have the asterisks represent different values (1, 100, or 1000 units). You may also wish to use a character other than the asterisk such as the $ to represent the units of your graph. Run the program sufficient number of times to illustrate the programs various abilities.

Statements Required: output, loop control, decision making, class (optional), methods.

Sample Output:

Sales for October

Day Daily Sales Graph

2 37081 *************************************

3 28355 ****************************

4 39158 ***************************************

5 24904 ************************

6 28879 ****************************

7 13348 ************* "

Here's what I have:

import java.util.Random;

public class prog310t
{ 
  public static int randInt(int randomNum) //determines the random value for the day
  {   
    Random rand = new Random();

    randomNum = rand.nextInt((40000 - 1000) + 1) + 10000;

    return randomNum;
  }

  public String histogram (int randomNum) //creates the histogram string
  {
    String histogram = "";
    int roundedRandom = (randomNum/1000);
    int ceiling = roundedRandom;
    for (int k = 1; k < ceiling; k++)
    {
      histogram = histogram + "*";
    }

    return histogram;   
  }

  public void main(String[] Args)
  {
    System.out.println("Sales for October\n");

    System.out.println("Day        Daily          Sales Graph");

    for (int k = 2; k < 31; k++)
    {
      if (k == 8 || k == 15 || k == 22 || k == 29)
      {
        k++;
      }

      System.out.print(k + "         ");

      int randomNum = 0;

      randInt(randomNum);

      System.out.print(randomNum + "       ");

      histogram (randomNum);

      System.out.print(histogram + "\n");
    }
  }
}

Edit: Thanks to you guys, now I've figured out what static means. Now I have a new problem; the program runs, but histogram is returning as empty. Can someone help me understand why? New Code:

import java.util.Random;

public class prog310t
{ 
  public static int randInt(int randomNum) //determines the random value for the day
  {   
    Random rand = new Random();

    randomNum = rand.nextInt((40000 - 1000) + 1) + 10000;

    return randomNum;
  }

  public static String histogram (int marketValue) //creates the histogram string
  {
    String histogram = "";
    int roundedRandom = (marketValue/1000);
    int ceiling = roundedRandom;
    for (int k = 1; k < ceiling; k++)
    {
      histogram = histogram + "*";
    }

    return histogram;   
  }

  public static void main(String[] Args)
  {
    System.out.println("Sales for October\n");

    System.out.println("Day        Daily          Sales Graph");

    for (int k = 2; k < 31; k++)
    {
      if (k == 8 || k == 15 || k == 22 || k == 29)
      {
        k++;
      }

      System.out.print(k + "         ");

      int randomNum = 0;

      int marketValue = randInt(randomNum);

      System.out.print(marketValue + "       ");

      String newHistogram = histogram (randomNum);

      System.out.print(newHistogram + "\n");
    }
  }


}
6
  • 3
    For starters, I would suggestion changing the name of the method to getHistogram to avoid confusing it with the variable. Commented Jan 16, 2015 at 16:18
  • 1
    main() needs to be static. Consequently histogram() needs to be static so that main can call it. Commented Jan 16, 2015 at 16:18
  • Code snippets are for javascript. Just mark your code block using the {} (Code Sample) button in the toolbar of the editor. Commented Jan 16, 2015 at 17:02
  • What values of marketValue does it return an empty string for? It should any time the value is below 1000, certainly. Commented Jan 16, 2015 at 17:04
  • 1
    Here is a good resource for understanding static methods introcs.cs.princeton.edu/java/21function Commented Jan 16, 2015 at 18:20

3 Answers 3

4

You're correct that your issues are rooted in not understanding static. There are many resources on this, but suffice to say here that something static belongs to a Class whereas something that isn't static belogns to a specific instance. That means that

public class A{
    public static int b;

    public int x;
    public int doStuff(){
        return x;
    }

    public static void main(String[] args){
        System.out.println(b); //Valid. Who's b? A (the class we are in)'s b.
        System.out.println(x); //Error. Who's x? no instance provided, so we don't know.
        doStuff(); //Error. Who are we calling doStuff() on? Which instance?

        A a = new A();
        System.out.println(a.x); //Valid. Who's x? a (an instance of A)'s x.
    }
}

So related to that your method histogram isn't static, so you need an instance to call it. You shouldn't need an instance though; just make the method static:

Change public String histogram(int randomNum) to public static String histogram(int randomNum).

With that done, the line histogram(randomNum); becomes valid. However, you'll still get an error on System.out.print(histogram + "\n");, because histogram as defined here is a function, not a variable. This is related to the return statement. When something says return x (for any value of x), it is saying to terminate the current method call and yield the value x to whoever called the method.

For example, consider the expression 2 + 3. If you were to say int x = 2 + 3, you would expect x to have value 5 afterwards. Now consider a method:

public static int plus(int a, int b){
    return a + b;
}

And the statement: int x = plus(2, 3);. Same here, we would expect x to have value 5 afterwards. The computation is done, and whoever is waiting on that value (of type int) receives and uses the value however a single value of that type would be used in place of it. For example:

int x = plus(plus(1,2),plus(3,plus(4,1)); -> x has value 11.

Back to your example: you need to assign a variable to the String value returned from histogram(randomNum);, as such:

Change histogram(randomNum) to String s = histogram(randomNum).

This will make it all compile, but you'll hit one final roadblock: The thing won't run! This is because a runnable main method needs to be static. So change your main method to have the signature:

public static void main(String[] args){...}

Then hit the green button!

Sign up to request clarification or add additional context in comments.

Comments

2

For starters your main method should be static:

public static void main(String[] Args)

Instance methods can not be called without an instance of the class they belong to where static methods can be called without an instance. So if you want to call your other methods inside the main method they must also be static unless you create an object of type prog310t then use the object to call the methods example:

public static void main(String[] Args)
{
     prog310t test = new prog310t();
     test.histogram(1);
}

But in your case you probably want to do:

public static String histogram (int randomNum)

public static void main(String[] Args)
{
     histogram(1);
}

Also you are not catching the return of histogram() method in your main method you should do like this:

  System.out.print(histogram(randomNum) + "\n");

Or

String test = histogram(randomNum);
System.out.print(test + "\n");

Static methods are part of a class and can be called without an instance but instance methods can only be called from an instance example:

public class Test
{
    public static void main(String[] args)
    {
        getNothingStatic();// this is ok
        getNothing(); // THIS IS NOT OK IT WON'T WORK NEEDS AN INSTANCE
        Test test = new Test();
        test.getNothing(); // this is ok
        getString(); // this is ok but you are not capturing the return value
        String myString = getString(); // now the return string is stored in myString for later use
    }
    public void getNothing()
    {
    }
    public static void getNothingStatic()
    {
    }
    public static String getString()
    {
        return "hello";
    }
}

Void means the method is not returning anything it is just doing some processing. You can return primitive or Object types in place of void but in your method you must specify a return if you don't use void.

3 Comments

And why the downvote please say why you shouldn't downvote and not say why?
who knows why the down votes as this explains most of op's issues
It looks like you missed a static in the code just after "But in your case you probably want to do:"
0

Before calling histogrom (randomNum) you need to either make histogram static or declare the object that has histogram as a method

e.g

prog310t myClass = new prog310t();

myClass.histogram()

2 Comments

No reason to downvote. It's certainly offers a solution to the root of his problem (he's getting an error when running his main method that histogram is unknown)
" I thought I had it all figured out, but it says it "can not find symbol histogram" on the line in the main method" -- That's straight from his question....I think that's one of the many places he's stuck and I'm trying to help him out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.