0

I have two separate files named Guitar and TestGuitar trying to execute code to get the number of strings, guitar length, make, color and then 16 randomly selected musical notes and durations. I can get my code to compile and execute but cannot figure out how to have it print the information in my playGuitar() section of the Guitar code. Any ideas or input on how to move forward would be very helpful and appreciated. This is the first code for Guitar.java

/*
* File: Guitar.java
* Author: 
* Date: Feb 8, 2020
* Purpose: 
*/

import java.util.Random;

public class Guitar {    
    private int numStrings;
    private double guitarLength;
    private String guitarManufacturer;
    private String guitarColor;

    //Constructor
    public Guitar(int numStrings, double guitarLength, String guitarManufacturer, String guitarColor) {
    this.numStrings = numStrings;
    this.guitarLength = guitarLength;
    this.guitarManufacturer = guitarManufacturer;
    this.guitarColor = guitarColor;
    }

    // Default Constructor
    public Guitar() { 
    this.numStrings = 6;
    this.guitarLength = 28.2;
    this.guitarManufacturer = "Gibson";
    this.guitarColor = "Red";
    }

    // Setter methods 
    // setnumStrings
    public void setnumStrings(int numStrings) {
    this.numStrings = numStrings;
    }
    // setguitarLength
   public void setguitarLength(double guitarLength)  {  
    this.guitarLength = guitarLength;
    }
    // setguitarManufacturer
   public void setguitarManufacturer(String guitarManufacturer)  {  
    this.guitarManufacturer = guitarManufacturer;
    }
    // setguitarColor
   public void setguitarColor(String guitarColor)  {    
    this.guitarColor = guitarColor;
    }

   // Getter methods
   // getnumStrings
    public int getnumStrings() {
    return numStrings;
    }    
    // getguitarLength
    public double getguitarLength() {
    return guitarLength;
    }
    // getguitarManufacturer
    public String getguitarManufacturer() {
    return guitarManufacturer;
    }
    // getguitarColor
    public String getguitarColor() {
    return guitarColor;
    }

    // playGuitar method for 16 randomly selected musical notes
    public String playGuitar() {
String play = "[";
//arrays containing possible notes and duration
char[] notes = {'A','B','C','D','E','F','G'};
double[] duration = {0.25,0.5,1,2,4};
//integers works as index for the arrays above
int a;
int b;
//initiate a random number
Random rn = new Random();
//for loop to select 16 randomly selected musical notes of random duration
for (int k=0;k<16;k++) {
    a = rn.nextInt(7);
    b = rn.nextInt(5);
    play = play + notes[a] + "(" + String.valueOf(duration[b]) + ")";
    if (k!=15) { 
        play = play + ",";
    }
}
play = play + ']';
System.out.println(play);
return play; 
}

    // toString method
    public String toString() {
    return "toString(): (numStrings = " + numStrings + ", guitarLength = " + 
    guitarLength + ", guitarManufacturer = " + guitarManufacturer + 
    ", guitarColor = " + guitarColor + ")";
}
    }

This is the second part of code TestGuitar

/*
* File: TestGuitar.java
* Author: 
* Date: Feb 8, 2020
* Purpose: 
*/
import java.util.Random;

public class TestGuitar extends Guitar{ 
public TestGuitar(int numStrings, double guitarLength, String guitarManufacturer, String guitarColor) {
super(numStrings, guitarLength, guitarManufacturer, guitarColor);
} 
public static void main(String[] args) {
Guitar one = new Guitar(7, 30.2, "Fender", "Black");
System.out.println(one.toString());
System.out.println("getNumStrings(): " + one.getnumStrings());
System.out.println("getGuitarLength(): " + one.getguitarLength());
System.out.println("getGuitarManufacturer(): " + one.getguitarManufacturer());
System.out.println("getGuitarColor(): " + one.getguitarColor());
}


}
3
  • Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Please see: Why is “Can someone help me?” not an actual question? Commented Feb 9, 2020 at 19:11
  • What do you want to print out and where are you stuck? Commented Feb 9, 2020 at 19:16
  • I am trying to get the public String playGuitar() information to print out, I can't really figure out how to get it to print when I run the code. Im not even 100% sure if that array is executing properly. Commented Feb 9, 2020 at 19:22

1 Answer 1

1

Maybe i didnt understand your question but, why dont you try

System.out.println(one.playGuitar());
Sign up to request clarification or add additional context in comments.

3 Comments

That is actually exactly what I needed, for some reason I didn't think to try that specific line. I was trying one.play, play, and everything else along those lines. Thanks!
Can you confirm the answer please?
Absolutely, sorry first time using this to ask for help.

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.