These are the instructions for the program I wrote:
A student object should validate its own data. The client runs this method, called validateData(), with a student object, as follows:
String result = student.validateData();
if(result == null)
<use the student>
else
System.out.println(result);
If the student's data are valid, the method returns the value null; otherwise, the method returns a string representing an error message that describes the error in the data. The client can then examine this result and take the appropriate action.
A student's name is invalid if it is an empty string. A student's test score is invalid if it lies outside the range from 0 to 100. Thus, sample error message might be
"Sorry : name required"
and
"Sorry: must have 0 <= test score <=100"
This is my Student class:
public class Student { private static String name; private static int test1; private int test2; private int test3; public Student(){ name = ""; test1 = 0; test2 = 0; test3 = 0; } public void setName (String nm){ name = nm; } public static String getName(){ return name; } public void setScore (int i, int 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; if (test2 > highScore) highScore = test2; if (test3 > highScore) highScore = test3; return highScore; } public String toString(){ String str; str = "Name : " + name + "\n" + "Test1: " + test1 + "\n" + "Test2: " + test2 + "\n" + "Test3: " + test3 + "\n" + "Average: " + getAverage(); return str; } public String validateData() { String message = ""; if(name == "") message = "sorry please enter a name"; if(test1<0 || test1>100) message += "Sorry - Test 1 is not in between 0 and 100 \n"; if(test2<0 || test2>100) message += "Sorry - Test 2 not in between 0 and 100\n"; if(test3<0 || test3>100) message += "Sorry - Test 3 not in between 0 and 100"; return message; } }
And this is my ValidateData class:
import java.util.Scanner; public class ValidateData { public static void main(String[] args) { Scanner reader = new Scanner(System.in); Student student = new Student(); String name; System.out.println("What is the students name? " ); name = reader.nextLine(); student.setName(name); System.out.println("Test #1 score: " ); student.setScore(1, reader.nextInt()); System.out.println("Test #2 score: " ); student.setScore(2, reader.nextInt()); System.out.println("Test #3 score: " ); student.setScore(3, reader.nextInt()); String result = student.validateData(); if(result==null) System.out.println("All is OK\n" ); else System.out.println(result+"\n" ); } }
The program works fine with the exception that it does not prompt the user if the "name" is missing in case the name wasn't entered. The program also does not display the average of the scores and the coding for that is inserted into the Student class. The program does, however prompt the user that the scores entered are not in range if they are >100.
Any help with the syntax of these codes would be greatly appreciated,
Thank you.