JAVA. Write a class named FullName containing: Two instance variables named give
ID: 3572485 • Letter: J
Question
JAVA. Write a class named FullName containing: Two instance variables named given and family of type String . A constructor that accepts two String parameters . The value of the first is used to initialize the value of given and the value of the second is used to initialize family. A method named toString that accepts no parameters . toString returns a String consisting of the value of family, followed by a comma and a space followed by the value of given .
This is what I have:
if (FullName.indexOf(“, “) != -1) {
lastName = FullName.substring(0, FullName.indexOf(“, “));
firstName = FullName.substring(FullName.indexOf(“, “) + 2, FullName.length());
} else {
firstName = FullName.substring(0, FullName.indexOf(” “));
lastName = FullName.substring(FullName.indexOf(” “) + 1, FullName.length());
}
Error:
More Hints:
You almost certainly should be using: "
You almost certainly should be using: String
You almost certainly should be using: class
• We think you might want to consider using: this
Explanation / Answer
Note: Are you satisfied with this.If you need any changes just comment.I need what the exact output you need in some what eloborate manner.Thank you.
____________
FullName.java
public class FullName {
//Declaring instance variables
private String given;
private String family;
//Parameterized constructor
public FullName(String given, String family) {
this.given = given;
this.family = family;
}
/* toString () method is used to display
* the co0ntents of an object inside it
*/
@Override
public String toString() {
return given+", "+family;
}
}
_______________
Test.java
public class Test {
public static void main(String[] args) {
//Creating the object for FullName class by passing the inputs
FullName fn=new FullName("Kane","Williams");
//Displaying the output
System.out.println(fn.toString());
}
}
________________
output:
Williams, Kane
_____Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.