Tuesday, June 9, 2015
Project 3 / getSetInstanceVariables
/*
1. Add methods to “set” and “get” the instance variables in the Person class. These would consist of: getName, getAge, getGender, setName, setAge, and setGender.
2. Add methods to “set” and “get” the instance variables in the Student class. These would consist of: getIdNum, getGPA, setIdNum, and setGPA.
*/
package gettersAndSettersForInstanceVariables;
public class MainActivity {
public static void main(String[] args)
{
Person p = new Person();
p.setName("Bpn");
p.setAge(22);
p.setGender("Male");
System.out.println("Name is: "+p.getName());
System.out.println("Age is: "+p.getAge());
System.out.println("Gender is: "+p.getGender());
}
}
----------------------------------------------------------
package gettersAndSettersForInstanceVariables;
public class Person
{
private String name;
private int age;
private String gender;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
}
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment