The following critical elements will be addressed in this submission: MyClone ac
ID: 3573529 • Letter: T
Question
The following critical elements will be addressed in this submission: MyClone accessors and mutators: You will demonstrate your understanding of encapsulation by creating accessor and mutator methods for all instance variables of the MyClone class. MyClone constructor: You should have at least one constructor created that initializes all instance variables. 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. Partial class diagram for the MyClone object: Create an additional class: Create a class of your choice. Your programmer-defined class will have at a minimum two (2) instance variables and one (1) method. You will demonstrate your understanding of encapsulation by declaring the instance variables as private and by creating accessors and mutators for each instance variable. Constructor: You will implement at least one constructor that initializes all instance variables. Document your code.Explanation / Answer
MyClone.java
public class MyClone {
//Declaring instance variables
private String firstName;
private String lastName;
//Parameterized constructor
public MyClone(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//Setters and getters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//This method will display the message
public void introducion()
{
System.out.println("Welcome "+getFirstName()+" "+getLastName()+"!");
}
}
___________________
TestClass.java
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
//Declaring variables
String fname,lname;
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the firstname entered by the user
System.out.print("Enter firstname :");
fname=sc.next();
//Getting the lastname entered by the user
System.out.print("Enter lastname :");
lname=sc.next();
//Creating the MyClone class object by passing the inputs as arguments
MyClone mc=new MyClone(fname, lname);
//Calling the introduction() method on the MyClone class object
mc.introducion();
}
}
_____________________
Output:
Enter firstname :Kane
Enter lastname :Williams
Welcome Kane Williams!
______________
This is the class of User choice:
Rectangle.java
public class Rectangle {
private int length;
private int breadth;
public Rectangle(int length, int breadth) {
this.length = length;
this.breadth = breadth;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getBreadth() {
return breadth;
}
public void setBreadth(int breadth) {
this.breadth = breadth;
}
public void calArea() {
System.out.println("The Area of the Rectangle is :" + getLength()* getBreadth() + " sq mts");
}
}
___________________
DriverClass.java
import java.util.Scanner;
public class DriverClass {
public static void main(String[] args) {
//Declaring variables
int length,breadth;
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the length entered by the user
System.out.print("Enter length (in meters):");
length=sc.nextInt();
//Getting the breadth entered by the user
System.out.print("Enter breadth (in meters):");
breadth=sc.nextInt();
//Creating the Rectangle class object by passing the inputs as arguments
Rectangle rect=new Rectangle(length, breadth);
//Calling the calArea() method on the MyClone class object
rect.calArea();
}
}
_________________
Output:
Enter length (in meters):6
Enter breadth (in meters):8
The Area of the Rectangle is :48 sq mts
_________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.