Question 4: Write the Name class needed by the following program in order to pro
ID: 3732512 • Letter: Q
Question
Question 4: Write the Name class needed by the following program in order to produce the output shown. Recall that s.substring(start,end) can be used to extract a portion of the String s class NameTest t public static void main (String args) Name n1 = new Name ("Narges", "Norouz"); Name n2new Name ("Jane", "Programmer") System.out.printin (n1): /7 prints "Norouzi, Narges" System.out.println (n2) // prints "Programmer, Jane" System.out.println (nl.initials) // prints "NN" System.out.printin (n2.initialso):/7 prints "JP"Explanation / Answer
NameTest.java
public class NameTest {
public static void main(String[] args) {
Name n1 = new Name("Narges", "Norouzi");
Name n2 = new Name("Jane", "Prohrammer");
System.out.println(n1);
System.out.println(n2);
System.out.println(n1.initials());
System.out.println(n2.initials());
}
}
Name.java
public class Name {
private String firstName, lastName;
public Name(String firstName, String lastName) {
this.firstName=firstName;
this.lastName=lastName;
}
public String toString() {
return lastName+", "+firstName;
}
public String initials() {
return firstName.substring(0,1)+lastName.substring(0,1);
}
}
Output:
Norouzi, Narges
Prohrammer, Jane
NN
JP
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.