-8

So as part of my IT project I have to do a method class. I already did this but am finding trouble to complete it. When i checked the notes i found something about the get and set method. Can anyone help me pls by showing how the first one is made or just by telling what i should do?

public class MethodClassProject 
{  

Integer Id ;
String Brand ;
String Price ;
String Size ;
String Quantity ;
String Code ;
String Color ;
String Style ;

void setId (int userId)
{
    Id = userId ;
}
void setBrand (String userBrand)
{
    Brand = userBrand ;
}
blex
  • 24,941
  • 5
  • 39
  • 72
  • 3
    What do you think is required here? If it requires `getters` and `setters`, what do you think they are? How do you think they look? Where should they be used? Now that you have that information handy, we can help you clarify misconceptions, but certainly not write your code – Kon Mar 04 '15 at 15:29
  • What's exactly your problem? How create a getter/setter for an attribute? By the way, according to java naming conventions, variables should start with a lowercase letter. – Albert Mar 04 '15 at 15:34
  • 1
    I would suggest starting by reading up on what "Java Access Modifiers" are since you seem to be omitting them, and I guess **only after that** the concept of `getters` and `setters` could be understood. – Ceiling Gecko Mar 04 '15 at 15:34
  • Most confusing class name award goes to... – Ceiling Gecko Mar 04 '15 at 15:36
  • You say the get and set method. You make the methods using the return keyword. – CaffeineToCode Mar 04 '15 at 15:59

2 Answers2

0

Read up on the return keyword. An example of getters and setters methods can be found at How do getters and setters work?. A setter method is a method which sets a variable in your class. Getter methods give the variable to whatever calls the method.

Getters:

In a main method:

String s = MethodClassProject.getBrand();

Would set String s to whatever you the brand name is.

Setters:

MethodProjectClass.setBrand("Diet Coke");

Would set the brand name to Diet Coke.

Together:

class MainWrapper {
    public static void main(String[] args) {
        MethodProjectClass.setBrand("Diet Coke");
        System.out.println(MethodProjectClass.getBrand()); // Prints Diet Coke
    }
}

I would also recommend simply making your variables public if you want the variable to be set without anything changing the input value.

I will leave the actual getter methods in the class to you, otherwise you will not learn.

Community
  • 1
  • 1
CaffeineToCode
  • 830
  • 3
  • 13
  • 25
0

You were definitely on the right track, but here's a few suggestions:

  • Variable names in java should start with a lowercase letter
  • Keep an eye on your indentation. While it's not really important to the compiler, it makes it very difficult for humans to read when the indentation isn't right.
  • Watch your braces, there's a closing brace missing in the code you provided.
  • Look up how to return items
  • Look up the this keyword in Java, as well