• 0

[java] Need Some Help With This Student Class


Question

public class SKstudent
{
	private String name;    
	private int test1;    
	private int test2;    
	private int test3;   
	public SKstudent()

	{     
  name = "";       
  test1 = 0;       
  test2 = 0;       
  test3 = 0;   
	}   

	public SKstudent(SKstudent s)
	{       
	this.name = s.name;    
	this.test1 = s.test1;      
	this.test2 = s.test2;      
	this.test3 = s.test3;   
	}   

	public void setName(String n)    
	{       
	this.name = n; 
	}   

	public void setScore(int i, int score)   
	{    
  switch(i)       
  {           
  case 1: test1 = score; break;            
  case 2: test2 = score; break;            
  case 3: test3 = score; break;        
  }  
	}  

	public int getScore(int i)    
	{       
	int x = 0;        
	switch(i)        
  {            
  case 1: x = test1; break;            
  case 2: x = test2; break;            
  case 3: x = test3; break;      
        }
	return x;    
	}
    
	public int getAverage()    
	{        
	int average;        
	average = (int)(Math.round(test1 + test2 + test3)/3.0);        
	return average;    
	}    

	public int getHighScore()    
	{        
	int highScore = test1;        
	if(test2>highScore)            
	highScore = test2;    
	if(test3>highScore)         
	highScore = test3;     
	return highScore;
	}

	public String toString()   
	{        
	String str;        
	str = "Name:     " + name + "\n" +     
	"Test 1:         " + test1 + "\n" +       
	"Test 2:         " + test2 + "\n" +       
	"Test 3:         " + test3 + "\n" +       
	"Average:        " + getAverage();
       
	return str;
   
	}
}

the above is a class created.

now how do i use that class to do this

Create two students

Enter the name and (3) test grades for each student from the keyboard

Output the students information to the screen (using toString() )

Print to the screen the name of the student with the highest test score and the test score

Print to the screen the name of the student with the highest average and the average

...?

anyhelp would be appreciated

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Here is a start of some of the code.

SKstudent[] Students = new SKstudent[2]; // Creates 2 students in an array

int temp;

for (int i=0; i=1; i++) {

// Get the name with input functions

Students.setName(name); // set the name

for (int j=1; j=3; j++) {

temp = Students.getScore(j); // get the jth test score

Students.setScore(j, temp); // set the jth test score

}

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.