i've just started to learn java yesterday from some books and am doing pretty good i guess i started doing some exercise but i came to this question where it asks me to create a class of student with methods and attributes so i know what is class n methods but i dont know how to do it any guideline?thank you
2 replies to this topic
#1
Posted 20 March 2011 - 04:01 AM
|
|
|
#2
Posted 20 March 2011 - 04:35 AM
Example:
public class ClassName{
private int attribute1; //attribute
private String attribute2; //attribute
public ClassName(){ //constructor
attribute1 = 2;
attribute2 = "word";
}
public void printAll(){ //method
System.out.println(attribute1 + " " + attribute2);
}
public int getAttribute1(){ //method
return attribute1;
}
public void setAttribute2(String attribute2){ //method
this.attribute2 = attribute2;
}
}
#3
Posted 21 March 2011 - 12:05 AM
The syntax to do it is above, but if you are struggling on what exactly they should do/hold then it's best to think as real-world as possible (unless you are given exact attributes to create). So you should create attributes to hold things age, student number, name etc etc.
It is then best to have these as private attributes (so outside classes cannot tamper with them in a way which is out of your control), so you should create set and get methods as above. Get which returns the attribute, and set which changes an attribute to one given as a parameter. :)
You then add any methods that you need to, to carry out anything else you might need from a student object.
It is then best to have these as private attributes (so outside classes cannot tamper with them in a way which is out of your control), so you should create set and get methods as above. Get which returns the attribute, and set which changes an attribute to one given as a parameter. :)
You then add any methods that you need to, to carry out anything else you might need from a student object.
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn
"Things don’t have to change the world to be important.” - Steve Jobs
"Things don’t have to change the world to be important.” - Steve Jobs
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









