Write a class named Initials that stores three char values, one for the first, m
ID: 3925678 • Letter: W
Question
Write a class named Initials that stores three char values, one for the first, middle, and last initials of a person. The class should have the following constructors and methods
A three argument constructor that could be used to create an Initials object like this (For, perhaps, Franklin Delano Roosevelt) :
A setter for each initial
x.setFirst(‘M’);
x.setMiddle(‘J’);
x.setLast(‘F’);
And a toString method that return the three initials, each followed by a period (‘.’) and a space.
x.toString() would return “M. J. F.”
Explanation / Answer
Initials.java
public class Initials {
private char first;
private char middle;
private char last ;
public Initials(char f, char m, char l){
this.first = f;
this.middle = m;
this.last = l;
}
public char getFirst() {
return first;
}
public void setFirst(char first) {
this.first = first;
}
public char getMiddle() {
return middle;
}
public void setMiddle(char middle) {
this.middle = middle;
}
public char getLast() {
return last;
}
public void setLast(char last) {
this.last = last;
}
public String toString(){
return first+". "+middle+". "+last;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.