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

Java question: Feel free to add additional instance values and methods, but your

ID: 3583557 • Letter: J

Question

Java question: Feel free to add additional instance values and methods, but your clone object must have instance variables firstName and lastName. The firstName and lastName instance variables will hold your first and last names. You will demonstrate your understanding of encapsulation by using the private specifier to restrict access to your instance variables. Add accessor methods, mutator methoda, and an introduction() method. The introduction() method will introduce yourself to the virtual world by displaying a greeting, your first and last name, and anything else you would want to say. It must include extensive, clear commenting.

Explanation / Answer

Solution:

part a)
{
private String firstName,lastName;
public MyClone(String f, String l)
{
firstName=f;
lastName=l;
}
public void print()
{
System.out.println("First Name is = "+firstName);
System.out.println("Last Name is = "+lastName);
}
}
class testClone
{
public static void main(String args[])
{
MyClone o = new MyClone("aaa","bbb");
//o.firstName="ccc"; Cant access this member of class here since it is private
o.print();
}
}
part b)
import java.util.Scanner;
public class StringCannedMessage {
public static void main(String[] args) {
String stringArr[] = { "I love Java the programming language!",
"I love Java the drink!",
"The JRE consists of the Java Virtual Machine (JVM)",
"Java was created by a team led by James Gosling",
"Java is Easy to Use", "Java is Reliability", "Java is Secure",
"Java is Platform Independent", "Java is Object Oriented",
"Java is High level language" };
String selectedMessage = shoutOutCannedMessage(stringArr);
System.out.println("You Selected :" + selectedMessage);
}
/**
* method to return selected message
*
* @param stringArr
* @return
*/
public static String shoutOutCannedMessage(String stringArr[]) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
for (int i = 0; i < stringArr.length; i++) {
System.out.println((i + 1) + ". " + stringArr[i]);
}
System.out.print("Select Message :");
int n = scanner.nextInt();
if (n > 0 && n <= 10)
return stringArr[n - 1];
else
return "Invalie Message Selection";
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
}
SAMPLE OUTPUT:
1. I love Java the programming language!
2. I love Java the drink!
3. The JRE consists of the Java Virtual Machine (JVM)
4. Java was created by a team led by James Gosling
5. Java is Easy to Use
6. Java is Reliability
7. Java is Secure
8. Java is Platform Independent
9. Java is Object Oriented
10. Java is High level language
Select Message :9
You Selected :Java is Object Oriented

1. I love Java the programming language!
2. I love Java the drink!
3. The JRE consists of the Java Virtual Machine (JVM)
4. Java was created by a team led by James Gosling
5. Java is Easy to Use
6. Java is Reliability
7. Java is Secure
8. Java is Platform Independent
9. Java is Object Oriented
10. Java is High level language
Select Message :11
You Selected :Invalie Message Selection
1. I love Java the programming language!
2. I love Java the drink!
3. The JRE consists of the Java Virtual Machine (JVM)
4. Java was created by a team led by James Gosling
5. Java is Easy to Use
6. Java is Reliability
7. Java is Secure
8. Java is Platform Independent
9. Java is Object Oriented
10. Java is High level language
Select Message :0
You Selected :Invalid Message Selection

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote