Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help To use a class, call the constructor. Below are two Java classes. Th

ID: 3577693 • Letter: P

Question

Please help

To use a class, call the constructor. Below are two Java classes. The name of the first is FirstClassOOPS. It defines variables, constructors, and methods. The second class is ClassClient. In ClassClient, the constructor for FirstClassOOPS is called. The constructor initializes the variable name to “John Smith” and age to 62. Then a method in FirstClassOOPSis used to print out the results below.

My name is John Smith.

My age is 62.

Part I:

Modify the code to make sure the print out is as follows.

My name is John Smith.

My age is 62.

Make a screenshot of the output and include it as an attachment to the Week 4 Assignment. Also, include the program code in a text file to be evaluated. Make sure you code is commented to receive full credit (see page 45).

Part II:

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.

//

}

}

Please submit your screenshots, answers and code in a file with your GID number.

Explanation / Answer

Note : Just call parameterized constructor in your class .Everything ready in your programme.

Note : save calss FirstClassOOPS.java ,ClientClass.java seperatly and compile ClientClass.java file and run ClientClass because ClientClass.java hava main method.

//Code Begins

//FirstClassOOPS.java

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.java
public class ClassClient {
public static void main(String[] args)
{
//Create a FirstClassOOPS variable with name = John Smith and age = 62
FirstClassOOPS g = new FirstClassOOPS("John Smith",62);

String outPutString = g.toString();
System.out.println(outPutString);
// The output should be
// My name is John Smith.
// My age is 62.
//
}
}

Output :


C:UsersAshokDesktopCheggjava>javac ClassClient.java

C:UsersAshokDesktopCheggjava>java ClassClient
My name is John Smith.
My age is 62.