1

I have solved this all inside the main method, however the professor has asked we follow very specific guidelines. Here are the instructions:

  1. Create methods with the following signatures (don’t forget static):

    • private static boolean isFizz (int val) check if a multiple of 3.
    • private static boolean isBuzz(int val) check if a multiple of 5.
  2. Create a class member variable (this means it’s not inside a method, but is inside a class):

    • private static int counter
    • Note: you can initialize this when you declare it, or inside the main method.
  3. In the main method:
    1. Use the counter to iterate from 1 to 100.
    2. Use the two other methods you define to determine what to print.
      • Note that the methods should not print anything, they just return a boolean value.
    3. Your program should include at least one of each of the following:
      • branch control statement (like if).
      • loop.
public class Fizzy {

    //checking if a multiple of 3
    private static boolean isFizz(int i){
        if (i % 3 == 0){
            return true;
        }
        return false;
    }
    //checking if a multiple of 5
    private static boolean isFuzz(int i){
        if (i % 5 == 0){
            return true;
        }
        return false;
    }

    //professor wants a class here outside of main with a private static int.
    //But I get an error and I'm not sure what I need to do to fix it.
    //also, is this where my booleans need to be called?

    public class Counter {

       private static int counter(int x);

    }


    public static void main (String [] args){

        //I think I'm supposed to call something here?
        //I've tried Counter a = new Counter(); but it doesn't like it.
        //I've tried new booleans but also doesn't like it.

        /**
         * for loop to iterate i to 100
         */

        //counter is supposed to be iterated here. However I am not sure
        //how to exactly access counter from a separate class.

        for(counter; counter <= 100; ++counter){

             //if Statement to check if a multiple of 3 and 5.
            if (counter % 3 == 0 && counter % 5 == 0){
                System.out.println("FizzBuzz");
            }
            // else if statement to check if multiple of 3
            else if (isFizz == true){
                System.out.println("Fizz");
            }
            //else if statement to check if multiple of 5
            else if (isFuzz == true){
                System.out.println("Buzz");
            }
             //else just run the loop
            else {
                System.out.println(counter);
            }

        }
    }

    }
}

It is supposed to go like this:

1
2
Fizz
4
Buzz
Fizz
.
.
.
14
FizzBuzz
16

And so on.

cdlane
  • 40,441
  • 5
  • 32
  • 81
Sailanarmo
  • 1,139
  • 15
  • 41
  • Perhaps the first "very specific guideline" might be to give some example code that actually *compiles*? – paulsm4 Jan 22 '16 at 07:46
  • public class Counter : This should be another file.. or just remove "public ": See http://stackoverflow.com/questions/3578490/why-only-1-public-class-in-java-file – Jayan Jan 22 '16 at 07:47
  • 1
    isFuzz is a method, yet you use it like a member? Change this to isFuzz(counter), and same goes with isFizz(counter). This fizzfuzz one is just both returning true. So it would be isFizz(counter) && isFuzz(counter) – Arunav Sanyal Jan 22 '16 at 07:48
  • `private static int counter(int x);` should not have `(int x)`. That is likely where the error is coming from. Also, depending on what the program is meant to do, you may be right about where to call `isFizz` and `isBuzz`. It will most likely be inside your loop. – Calvin P. Jan 22 '16 at 07:48
  • "Create a class member variable" is a bit ambiguous: is it a member variable that is a class, or a member variable inside your class? From the points below I deduce it's the latter. – Mark Jeronimus Jan 22 '16 at 07:49
  • Shouldnt all the methods be inside class counter and the file called Counter.java – Arunav Sanyal Jan 22 '16 at 07:50
  • I am tempted to just write the whole thing and give it as an answer. But i wont since its a homework problem. All these comments together should logically lead to the answer. – Arunav Sanyal Jan 22 '16 at 07:52
  • @ArunavSanyal and that is what I was thinking myself, however, it says in the instructions "Don't forget Static." When I throw everything inside of the class Counter, I am thrown an error that says, "Inner Classes cannot have static declarations." – Sailanarmo Jan 22 '16 at 08:09

3 Answers3

2

Mostly as what others said, but with a slightly different flair: the fizzbuzz case is a composition of fizz and buzz, so there is no need to make it different: You can just extract the code to go to a new line and compose them together. See if you can understand this other approach:

for (int counter = 1; counter<=100; counter++) {
  // if it's fizz or buzz, we need to print not the number but a word
  if (isFizz(counter) || isBuzz(counter)) {
    //if it's fizz, we write fizz (note that fizzbuzz are fizz!)
    if (isFizz(counter)) {
      System.out.print("Fizz");
    }
    //if it's buzz, we write buzz (note that fizzbuzz are buzz!)
    if (isBuzz(counter)) {
      System.out.print("Buzz");
    }
  }
  //if it's neither fizz nor buzz, we need to print the number itself
  else {
    System.out.print(counter);
  }
  //once we wrote whatever we had to write, we go to a newline for the next one
  System.out.println();    
}
Diego Martinoia
  • 4,592
  • 1
  • 17
  • 36
-1

You have already written isFuzz and isFizz method, so why are you checking with % 3 and 5 again?

Instead, in the for loop, call your isFizz and isFuzz method with counter as argument. Store the return boolean value in variable.

Your code:

for(counter; counter <= 100; ++counter){

         //if Statement to check if a multiple of 3 and 5.
        if ( isFizz(counter) && isBuzz(counter) ){
            System.out.println("FizzBuzz");
        }
        // else if statement to check if multiple of 3
        else if ( isFuzz(counter) ) {
            System.out.println("Fizz");
        }
        else if( isBuzz(counter) )
            System.out.println("Buzz");
        else {
            System.out.println(counter);
        }

}

Edited: I'd left you to do it.

Mujtaba
  • 143
  • 1
  • 8
  • else if ( ){ not sure what you tried to show with that code, but you may want to edit it in a way it'll at least compile. – Stultuske Jan 22 '16 at 08:04
  • 1
    I am very confused on what this piece of code is trying to show here. – Sailanarmo Jan 22 '16 at 08:06
  • @Sailanarmo: he is right in saying you don't need the first check. You write more code then you should. your isFizz and isBuzz methods are longer than the actual code you need for it. – Stultuske Jan 22 '16 at 08:13
  • You didn't leave much to do.. This is the difficult portion of the exercise to do, and you've done it for him. He explicitly stated that he did *not* want his homework done for him. – Neil Jan 22 '16 at 09:41
  • Oooooh ok that makes a lot more sense now, sorry I didn't know you had left that for me to fill out. – Sailanarmo Jan 22 '16 at 16:47
-2

Here is a possible solution:

public class Fizzy {

    //a class *variable*, not a class
    private static int counter;

    //checking if a multiple of 3
    private static boolean isFizz(int i) {
        return i % 3 == 0; //no need for if
    }

    //checking if a multiple of 5
    private static boolean isBuzz(int i) {
        return i % 5 == 0; //no need for if
    }

    public static void main(String[] args) {
        for (counter = 1; counter <= 100; ++counter) {
            if (isFizz(counter) && isBuzz(counter)) { //reuse the methods
                System.out.println("FizzBuzz");
            } else if (isFizz(counter)) { //no need for "== true"
                System.out.println("Fizz");
            } else if (isBuzz(counter)) { //no need for "== true"
                System.out.println("Buzz");
            } else {
                System.out.println(counter);
            }
        }
    }
}

Usually I would just give pointers, but here you are so close to a solution that "giving pointers" is more or less the same as "show how it works".

Landei
  • 54,104
  • 13
  • 100
  • 195
  • OMG, now that I have re-read that sentence it all makes sense! I have been pulling my hair out trying to figure out why I even needed that class in the first place. And now all the answers down below also make sense. – Sailanarmo Jan 22 '16 at 08:23
  • 1
    Note: The OP's requirement was to *[...] to iterate from 1 to 100*. Thus, your `counter` in the for loop should actually be initialized to 1. – morido Jan 22 '16 at 08:52
  • 1
    Watch out though, this runs from 0 to 100. Also it would be a bit cleaner to avoid multiple calls to `ìsFizz` and `ìsBuzz` by assigning the resuls of the method calls to local variables in `main`. The real question @Sailanarmo should ask his teacher, however, is why the guidelines say to pass `counter` to `ìsFizz` and `ìsBuzz`. The counter already is a member variable and should not be passed around as a method parameter. Like so: `private static boolean isFizz() { return counter % 3 == 0; }` – Tobias Jan 22 '16 at 09:05
  • 2
    OP specifically asked to be pointed in the right direction. He did *not* want someone to do his homework for him. You did not help him, rather quite the contrary. – Neil Jan 22 '16 at 09:39