Create a method that returns a String representation of random musical notes with random time duration for those notes?

I need the playGuitar method to return a string representation of 16 random notes of random duration. The output should look like: A(2), G(3), B(0.5), C(1), C(1), D(0.25), …].

Also, how do I get the Color data field to be displayed as blue or red and not RGB values in my Guitar Constructor?

I do not want to use arrays.

import java.awt.Color;
import java.util.Random;


public class Guitar {
    private int numStrings = 6;
    private double guitarLength = 28.2;
    private String guitarManufacturer = "Gibson";
    private Color guitarColor = Color.red;


    Guitar(){
        int numStrings = 6;
        double guitarLength = 28.2;
        String guitarManufacturer = "Gibson";
        Color guitarColor = Color.red;
    }

    Guitar(int specStrings, double specLength, String specManufacturer, Color specColor){
        numStrings = specStrings;
        guitarLength = specLength;
        guitarManufacturer = specManufacturer;
        guitarColor = specColor;
        //String s1 = Color.toString(specColor);
    }

    public String playGuitar(String str3){
        Random rnd = new Random();

        for (int iCount = 0; iCount<16 ; iCount++){
        char a = (char) (rnd.nextInt(7) + 'A');
        double b = (double) (rnd.nextInt(5)+1);
        String str1 = Double.toString(b);
        String str2 = Character.toString(a);
        //System.out.println(a);
        //System.out.println(str1);
        str3 = str2+"("+str1+")";
        continue;
        //System.out.println(str2+"("+str1+")");
        //System.out.println(str3);
        //String str4 = String.toString(str3);
        //System.out.println(str4);
            }
        return str3;
    }

    public String toString(){
        return numStrings+" "+guitarLength+" "+guitarManufacturer+" "+guitarColor;
    }
    public int getNumStrings (){
        return numStrings;
    }
    public double getGuitarLength(){
        return guitarLength;
    }
    public String getGuitarManufacturer(){
        return guitarManufacturer;
    }
    public Color getGuitarColor(){

        return guitarColor;
    } 
}

Upvotes: 1

Views: 118

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51515

I fixed the problems with your Guitar class and ran the following test of the playGuitar method. I manually formatted the output to fit in the answer.

F(2.75), G(2.50), G(0.75), A(0.25), D(1.75), G(0.75), 
F(0.50), C(2.75), A(2.50), A(1.00), A(2.25), E(1.50), 
D(2.50), G(1.25), A(0.50), G(1.00)

The major changes I made are;

  1. I added a main method do I could test the Guitar class.

  2. I fixed your empty constructor to use the instance variables.

  3. I changed the guitar color to be a String since you wanted to display "red" or 'blue". The Color class is used to draw with actual colors using the Graphics or Graphics2D classes.

  4. I fixed the multitude of problems with your playGuitar class.

Here's the code. I hope you learn something by reading the code.

import java.util.Random;

public class Guitar {
    public static void main(String[] args) {
        Guitar guitar = new Guitar();
        System.out.println(guitar.playGuitar());
    }

    private int numStrings;
    private double guitarLength;
    private String guitarManufacturer;
    private String guitarColor;

    Guitar() {
        numStrings = 6;
        guitarLength = 28.2;
        guitarManufacturer = "Gibson";
        guitarColor = "red";
    }

    Guitar(int specStrings, double specLength,
            String specManufacturer, String specColor) {
        numStrings = specStrings;
        guitarLength = specLength;
        guitarManufacturer = specManufacturer;
        guitarColor = specColor;
    }

    public String playGuitar() {
        Random rnd = new Random();
        String str3 = "";

        for (int iCount = 0; iCount < 16; iCount++) {
            char a = (char) (rnd.nextInt(7) + 'A');
            double b = 0.25d * (rnd.nextInt(11) + 1);
            String str1 = String.format("%.2f", b);
            String str2 = Character.toString(a);
            str3 += str2 + "(" + str1 + ")";
            if (iCount < 15) {
                str3 += ", ";
            }
        }

        return str3;
    }

    @Override
    public String toString() {
        return numStrings + " " + guitarLength + " " +
                guitarManufacturer + " " + guitarColor;
    }

    public int getNumStrings() {
        return numStrings;
    }

    public double getGuitarLength() {
        return guitarLength;
    }

    public String getGuitarManufacturer() {
        return guitarManufacturer;
    }

    public String getGuitarColor() {
        return guitarColor;
    }

}

Upvotes: 1

Related Questions