0

I have to pass the values that are in my Guitar constructor and then pass them to the generateSong method. There is one catch. The generate song method can not take parameters in because since the values are in the same class, they should be able to be able to take in the values from the constructor.

When I try to implement the values in that method, the values are not able to be accessed. How can I fix this?

public Guitar(int chord, int numOfStrings)  {
        //pass these values to the generateSong method
        System.out.println("Guitar () generated a guitar with: " + numOfStrings + "." + "Song length is " +chord);
        // declare the array for which the song is stored 
        // store the values of the last row i (highest index of the array)
        double[] max = null;
        Guitar.generateSong();
        // start the generate song method 
        public static void generateSong () {
            double [] [] song = new double [numOfStrings] [chord];
            double[] max;
            int findmax =0;
            int sum =0;
            for (int i =0; i<song.length; i++) {
                for (int j =0; j<song.length; j++ ) {
                    sum = (int) (sum +song[i][j]);
                }
                for (int e=0; e<song.length; e++) {
                    if (song[e]!=song[findmax] ) {
                        max[e] =e;
                    }
                    for(int k=0; k<song.length; k++) {
                        for (int l=0; k<song[k].length; k++)
                            // assign note values to the array
                            song [k] [l] = 27.5 + (Math.random()* 4186); 
                        // print out the proper value 
                        System.out.printf("8.2%f", song); 
                        for( int m=0; m<max.length; m++)
                            max[m]= 1 +( Math.random() *3);
                        System.out.printf("8.2%f", max);
                    }
                }
            }
        }

The values int chord and int numOfStrings come from command line parameters in the main method and are passed to this method through the following object:

Guitar guitar = new Guitar (numOfStrings, chord); 

1 Answer 1

2

So, there are two syntax errors and one misunderstanding of the requirement. Let's start with the misunderstanding.

The generate song method can not take parameters in because since the values are in the same class, they should be able to be able to take in the values from the constructor.

This is only accomplishable via fields. You need to set your constructor up so that fields for those values are created, which can then be used everywhere else in this class.

In this scenario...

public class Guitar {
    private int chord;
    private int numberOfStrings;

    public Guitar(final int chord, final int numberOfStrings) {
        this.chord = chord;
        this.numberOfStrings = numberOfStrings;
    }
 }

Second, you can't define methods inside of other methods. Move the definition of your generateSong method out of the constructor.

Third, don't make the generateSong method static! You create an instance of these to use, and you use that instance inside of main to perform the behavior you want.

// in main()
Guitar guitar = new Guitar(4, 10);
guitar.generateSong();

As an addendum, you need to be sure that you parse the integers you get from the command line, as those values are always going to be coming in as String. I leave this as an exercise for the reader.

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

3 Comments

Second, you can't define methods inside of other methods. Disagree. You can. Anonymous, lambdas are functions too.
@ssc: A lambda is a function (or better yet, a very thin veneer over anonymous classes). It's not a method.
My bad. I often use these terms interchangeably but I understood and got your point. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.