package Lecture_7; public class Student implements Comparable<Object>{ Integer a
ID: 3928880 • Letter: P
Question
package Lecture_7;
public class Student implements Comparable<Object>{
Integer age;
String state;
String firstName;
String lastName;
public Student(Integer age, String state, String firstName, String lastName){
this.setAge(age);
this.setState(state);
this.setFirstName(firstName);
this.setLastName(lastName);
}
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age = age;
}
public String getState(){
return state;
}
public void setState(String state){
this.state = state;
}
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;
}
@Override
public int compareTo(Object o) {
Student nStudent = (Student)o;
return this.getAge().compareTo(nStudent.getAge());
}
@Override
public String toString(){
return String.format("%3d %10s",this.getAge(), this.getState(),this,getFirstName(),this.getLastName());
}
}
I am stuck in the last part of "return String.format("%3d %10s",this.getAge(), this.getState(),this,getFirstName(),this.getLastName())"
my output only gives me age and state, however I need my output to print age, state, FirstName, and LastName. What do I need to add in "%3d %10s"<---- in there so my program will print all 4 variables, please advise.
Explanation / Answer
You need to include more placeholders to print FirstName and LastName. Just add "%s %s" to the line you are trying to output.
Therefore, the line becomes,
return String.format ("%3d %10s %s %s", this.getAge(), this.getState(), this.getFirstName(), this.getLastName());
Therefore, the complete program is:
public class Student implements Comparable<Object>{
Integer age;
String state;
String firstName;
String lastName;
public Student(Integer age, String state, String firstName, String lastName){
this.setAge(age);
this.setState(state);
this.setFirstName(firstName);
this.setLastName(lastName);
}
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age = age;
}
public String getState(){
return state;
}
public void setState(String state){
this.state = state;
}
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;
}
@Override
public int compareTo(Object o) {
Student nStudent = (Student)o;
return this.getAge().compareTo(nStudent.getAge());
}
@Override
public String toString(){
return String.format("%3d %10s %s %s",this.getAge(), this.getState(),this,getFirstName(),this.getLastName());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.