Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using this student class public class Student { //Instance variables //Each student object has a name and three test scores private String name; //Student name

image text in transcribed
Using this student class
public class Student {
//Instance variables
//Each student object has a name and three test scores
private String name; //Student name
private int test1; //Score on test 1
private int test2; //Score on test 2
private int test3; //Score on test 3
// Default constructor -- initialize name to the empty string and
// the test scores to zero.
public Student(){
name = "";
test1 = 0;
test2 = 0;
test3 = 0;
}
// Additional constructor -- initialize the name and test scores
// to the values provided.
public Student(String nm, int t1, int t2, int t3){
name = nm;
test1 = t1;
test2 = t2;
test3 = t3;
}
// Additional constructor -- initialize the name and test scores
// to match those in the parameter s.
public Student(Student s){
name = s.name;
test1 = s.test1;
test2 = s.test2;
test3 = s.test3;
}
//Other methods
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){
//Retrieve score i
if (i == 1) return test1;
else if (i == 2) return test2;
else return test3;
}
public int getAverage(){
//Compute and return the average
int average;
average = (int) Math.round((test1 + test2 + test3) / 3.0);
return average;
}
public int getHighScore(){
//Determine and return the highest score
int highScore;
highScore = test1;
if (test2 > highScore) highScore = test2;
if (test3 > highScore) highScore = test3;
return highScore;
}
public String toString(){
//Construct and return a string representation of the student
String str;
str = "Name: " + name + " " + // " " denotes a newline
"Test 1: " + test1 + " " +
"Test 2: " + test2 + " " +
"Test 3: " + test3 + " " +
"Average: " + getAverage();
return str;
}
}
in java
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) 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 out- side the range from 0 to 100. Thus, sample error messages might be "SORRY: name required" I and "SORRY: must have 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions