user3359778
user3359778

Reputation: 37

Get and sets in Java (Newbie programmer here)

I'm trying to create a guitar program. I have a guitar amp class that has a sound object I'm trying to access in my pickup class. My goal is to set the sound property in my pickup class the same as my sound property in the GuitarAmp class so I can then set all the sound properties of my strings. I'm not really sure how to go about doing this and the articles I've read about gets and sets in Java have been no help. I have my two classes listed below, any help with the get and set would be appreciated.

public class GuitarAmp
{

    // This is what I want to get from this class
    public GuitarAmpSound sound;

    public GuitarAmp(GuitarAmpSound sound, int volume, int distortSetting) {
        sound = new GuitarAmpSound();
        sound.setVolume(64);
        sound.setDistortSetting(GuitarAmpSound.JAZZ);
    }

    public void changeVolume(int newVolume)
    {
        sound.setVolume(newVolume);
    }
}

Here is the pickup class.

public class GuitarPickup {

    public GuitarAmpSound pickupSound;
    public void Connect(GuitarString strings[], GuitarAmp amp)
    {

        pickupSound = new GuitarAmpSound();

        //This is where I need to set it

        for(int i = 1; i <= 6; i++)
        {
        strings[i].setSound(pickupSound);
        }
    }


 }

Upvotes: 2

Views: 198

Answers (4)

pecks
pecks

Reputation: 340

Conceptually, there is little point in saving the same Sound value to each GuitarString.

Upvotes: 0

ZaoTaoBao
ZaoTaoBao

Reputation: 2615

it's really complicated understand what you really wanna..take a look some change..but I think you need to reestructure the objects. what you wanna do? can you explain better?

public class GuitarAmp {

//This is what I want to get from this class
public GuitarAmpSound sound;

--> the sound you pass before call the constructor
sound.setVoolume(x);
sound.setDistortSetting(Y)
so you pass the object sound with the attributes full of information


public GuitarAmp(GuitarAmpSound sound){

this.sound = sound;

}

public void changeVolume(int newVolume){
  this.sound.setVolume(newVolume);
}
}

Here is the pickup class.

public class GuitarPickup {

public GuitarAmpSound pickupSound;
public void Connect(GuitarString strings[], GuitarAmp amp)
{

    pickupSound = new GuitarAmpSound();

    //This is where I need to set it

   for(int i = 0; i<strings.length; i++)
    {
    amp.setSound(strings[i].getSound());
    }
}

}

Upvotes: 0

ReeCube
ReeCube

Reputation: 2597

Your code makes no sense, replace the GuitarAmp class by this:

public class GuitarAmp {

   //This is what I want to get from this class
   private GuitarAmpSound sound;

   public GuitarAmp() {
      sound = new GuitarAmpSound();
      sound.setVolume(64);
      sound.setDistortSetting(GuitarAmpSound.JAZZ);
   }

   public void changeVolume(int newVolume){
      sound.setVolume(newVolume);
   }

   public GuitarAmpSound getSound() {
      return sound;
   }

   public void setSound(Sound sound) {
      this.sound = sound;
   }
}

for get and set the rule is simple:

public YourClassName getYourObjectName() {
   return yourObjectName;
}

public void setYourObjectName(YourClassName yourObjectName) {
   this.yourObjectName = yourObjectName;
}

Upvotes: 1

You need to declare a field (a variable belonging to a particular instance of a class) to hold each piece of information for that object. The pickupSound field on GuitarPickup is an example.

It is strong convention in Java to use the same name for the field as for the getters and setters. In the case of your volume, for example, the relevant part of the code would look like this:

public class GuitarAmpSound {
    private int sound = 0;

    public void setSound(int sound) {
        this.sound = sound; // "this.sound" means the sound field, not the parameter
    }

    public int getSound() {
        return sound; // or this.sound
    }
}

If you want to implement the necessary code for your for loop, your GuitarString class needs a GuitarAmpSound field of its own named sound and a corresponding getter and setter.

As a note, the conditions in your for loop have a number of problems. Arrays in Java are zero-based (so the strings on a 6-string guitar would go from 0 to 5), and you should never hard-code array sizes in a loop but should use strings.length instead. Finally, Java has a more convenient syntax if you just want to retrieve each element in an array (or collection):

for(GuitarString string : strings) {
    string.setSound(pickupSound);
}

Upvotes: 1

Related Questions