Identify the following in the FirstClassOOPS class: Constructor(s) Mutator Metho
ID: 3871425 • Letter: I
Question
Identify the following in the FirstClassOOPS class:
Constructor(s)
Mutator Methods (Set methods)
Acessor Methods (Get methods)
Name of methods that returns values
Name the methods that do not return any values
List the methods that have parameters.
FirstClassOOPS
public class FirstClassOOPS {
// Declare Variables
private String sName;
private int sage;
//Create default constructor
public FirstClassOOPS()
{
sName="None";
sage = 18;
}
//Create a constructor
public FirstClassOOPS(String name, int age)
{
//initialize variables
sName = name;
sage = age;
}
//Mutator Method = Method that changes the name
public void setName(String name)
{
sName = name;
}
// Mutator Method = Method that changes the age
public void setAge(int age)
{
sage = age;
}
//Getter Method = Method that returns the value of Name
public String getName()
{
return sName;
}
//Getter Method = Method that returns the value of age
public int getAge()
{
return sage;
}
//To String Method = returns the name and age
public String toString()
{
String message = "My name is "+sName+". My age is "+sage+".";
return message;
}
}
ClassClient below.
public class ClassClient {
public static void main(String[] args)
{
//Create a FirstClassOOPS variable with name = John Smith and age = 62
FirstClassOOPS g = new FirstClassOOPS();
String outPutString = g.toString();
System.out.println(outPutString);
// The output should be
// My name is John Smith.
// My age is 62.
//
}
}
Explanation / Answer
In above, There are two constructor, one is default and another one is parametrized constructor.
public FirstClassOOPS()
{
sName="None";
sage = 18;
}
public FirstClassOOPS(String name, int age)
{
sName = name;
sage = age;
}
{
sName = name;
}
{
sage = age;
}
{
return sName;
}
{
return sage;
}
{
return sName;
}
It's return type is string and it returns string type value.
{
return sage;
}
It's return type is int and it returns int type value.
{
String message = "My name is "+sName+". My age is "+sage+".";
return message;
}
It's return type is string and it returns string type value.
public void setName(String name)
{
sName = name;
}
It's return type is void. So, It does not return any value.
{
sage = age;
}
It's return type is void. So, It does not return any value.
public void setName(String name)
{
sName = name;
}
In above, there is setName method which have one parameter of String type named name.
{
sage = age;
}
In above, there is setAge method which have one parameter of int type named age.