Hello, I was wondering if anybody can help me write data fields for an instrument(Guitar) and the number of strings it has (6) and an array of string names representing string names (E,A,D,G,B,E). I have my code done for tuning and playing not sure how to do the other part:

public class StringedInstrument {
	private boolean isTuned;
	private boolean isPlaying;
	public StringedInstrument() {
		isTuned = false;
		isPlaying = false; }

	public void tune() {
		isTuned = !isTuned; }

	public void play() {
		isPlaying = true; }

	public void pause() {
		is Playing = false; }
}

Well, you could start with extending the class?

public class Guitar extends StringedInstrument {
  ...
}

Then think about how to implement a guitar object.

Interesting concept. not sure where you are going with it. The solution hfx642
added seems ok to start.
I would like to mention that while you are just getting started with this program.
I have found that if you write several classes to follow you should maintain proper
Code syntax conventions. Meaning that the method names and the variable names
should always maintain the same pattern.

Example:

public StringedInstrument() {
}

should be

stringedInstument(){
}

Where the method starts with lower case and any subsequent word should be upper case.

Same thing with :

String String_Names [] = {"E", "A", "D", "G", "B", "E"};
String string_Names [] = {"E", "A", "D", "G", "B", "E"};
//better yet
String stringNames [] = {"E", "A", "D", "G", "B", "E"};

Also, I am thinking that

you mean:

public void tune() {
		isTuned = !isTuned; 
}

to be:

public void tune() {
		isTuned = false;
 }

or maybe you are testing the variable's value ?

public void tune() {
		//isTuned = !isTuned; 

if(isTuned){

//will do this if isTuned is true.
}
if(!isTuned){

//will do this if isTuned is false.
}

}

As far as my conventions are concerned I always use boolean Variables

boolean bTuned=true;

your method would be more powerfull if it were called and did its thing and retured
the result too.

public boolean tune() {
		//isTuned = !isTuned; 
boolean bTuned=true;

if(isTuned){

//will do this if isTuned is true.
//does it change bTuned to false?

}
if(!isTuned){

//will do this if isTuned is false.
//does it change bTuned to false?

}

return bTuned;
}

good luck


http://geosoft.no/development/javastyle.html#Naming Conventions

Edited 5 Years Ago by ceyezumma: add link

This article has been dead for over six months. Start a new discussion instead.