So when I run my code I get a bug where it doesn't display the name for the person when displaying the scores: ![Screen_Shot_2017-10-
Here is my code, I have been stumped for several hours:
import java.util.Scanner ;
public class StudentApp
{
public static void main(String []args)
{
Student student1 = new Student() ;
Student student2 = new Student() ;
Scanner reader = new Scanner(System.in) ;
String name ;
int score ;
System.out.println("Enter the first student's name: ") ;
name = reader.nextLine() ;
student1.setName(name) ;
for(int i =1; i<=3 ; i++)
{
System.out.print("Enter the student's score: ") ;
score = reader.nextInt() ;
student1.setScore(i, score) ;
}
reader.nextLine() ;
System.out.print("Enter the second student's name: ") ;
name = reader.nextLine() ;
student2.setName(name) ;
for(int i =1 ; i<=3 ; i++)
{
System.out.print("Enter the student's score: ") ;
score = reader.nextInt() ;
student2.setScore(i, score) ;
}
System.out.println(student1 ) ;
System.out.println(student2) ;
if (student1.getHighscore() > student2.getHighscore())
{
name = student1.getName ();
score = student2.getHighscore() ;
}
else
{
name = student2.getName() ;
score = student2.getHighscore() ;
}
System.out.println(name + " has the highest score: " + score) ;
if(student1.getAverage() > student2.getAverage()){
name = student1.getName() ;
score = student1.getAverage() ;
}
else{
name = student2.getName() ;
score = student2.getAverage() ;
}
System.out.println(name + " has the highest average score: " + score) ;
}
}
public class Student
{
private String name ;
private int test1 ;
private int test2 ;
private int test3 ;
public Student(){
this("", 0, 0 ,0 ) ;
}
public Student(String nm, int t1, int t2, int t3)
{
name = nm;
test1 = t1;
test2 = t2;
test3 = t3;
}
public void setName (String nm)
{ // Set a student's name
name = nm;
}
public String getName ()
{ // Get a student's name
return name;
}
public void setScore (int i, int score){
// Set test i to score
if (i==1) test1 = score;
else if (i == 2) test2 = score;
else test3 = score;
}
public int getScore(int i ){
if (i==1) return test1 ;
else if (i==2) return test2 ;
else return test3 ;
}
public int getAverage(){
int average ;
average = (int) Math.round((test1 + test2 + test3) / 3.0) ;
return average ;
}
public int getHighscore(){
int highScore ;
highScore = test1 ;
highScore = test1 ;
if(test2> highScore) highScore = test2 ;
if(test3> highScore) highScore = test3 ;
return highScore ;
}
public Student(Student s){
this(s.name, s.test1, s.test2, s.test3);
}
public String toString(){
String str ;
str = "Name: " + "\n" +
"Test 1: " + test1 + "\n" +
"Test 2: " + test2 + "\n" +
"Test 3: " + test3 + "\n" +
"Average : " + getAverage() ;
return str ;
}
}