I need help with the following problem. It is being programmed in Java at a very
ID: 3762575 • Letter: I
Question
I need help with the following problem. It is being programmed in Java at a very basic level.
1. MyClone accessors and mutators: You will demonstrate your understanding of encapsulation by creating accessor and mutator methods for all instance variables of the MyClone7 class.
2. MyClone constructor: You should have at least one constructor created that initializes all instance variables.
3. MyClone method introduction(): Create an introduction() method for your MyClone class. The introduction() method will introduce you to the virtual world by displaying a greeting, your first and last name, and anything else you would want to say.
4. Constructor: You will implement at least one constructor that initializes all instance variables.
Please comment in detail on code to aid in understanding, beginning level. Thanks,
Explanation / Answer
//MyClone7.java
public class MyClone7
{
//instance variables of MyClone7
private String firstName;
private String lastName;
private int age;
//parameter constructor of class MyClone7
public MyClone7(String firstName, String lastName, int age)
{
this.firstName=firstName;
this.lastName=lastName;
this.age=age;
}
/**Method that greets to the virtual world with name and age*/
public void introduction()
{
System.out.println("Hi virtual world!");
System.out.println("My name is "+firstName+" "+lastName);
System.out.println("My age is "+age);
}
//Mutator metods of instance variables
//Set firstname
public void setFName(String firstName)
{
this.firstName=firstName;
}
//Set last name
public void setLName(String lastName)
{
this.lastName=lastName;
}
//Set age
public void setAge(int age)
{
this.age=age;
}
//Accessor methods of instance variables
//Returns first name
public String getFName()
{
return firstName;
}
//Returns last name
public String getLName()
{
return lastName;
}
//Returns age
public int getAge()
{
return age;
}
}//end of the class MyClone7
------------------------------------------------------------------------------------------------------
/**The java program MyCloneDriver that creates an instance of
* MyClone7 class and calls introdcution method
* on MyClone7 object */
//MyCloneDriver.java
public class MyCloneDriver
{
public static void main(String[] args) {
//Create an instance of MyClone7 class
MyClone7 cloneObject=new MyClone7("John", "Sera", 25);
cloneObject.introduction();
}
}
------------------------------------------------------------------------------------------------------------------------
Sample Output:
Hi virtual world!
My name is John Sera
My age is 25