Write a class Student
that represents, of course, a student. The attributes that a student should have (implemented as instance variables) are:
You will probably have 8 instance variables to represent these attributes.
You will also implement these methods:
setupValues()
: similar to a method in the Person
class we wrote, this will ask the user for values for the instance variables for a Student
object (using the Scanner
class).printInfo()
: when this method is called on a Student
object it will print out all its information (the attributes).getName()
: this method simply returns the name of the student.getAverage()
: when this method is called on a Student
object it will return the average percentage grade of all three courses.After implementing the class, test it by creating Student
objects in a separate test class’s main()
method, and calling methods on these objects. For example:
// create objects for
Student s1 = new Student();
Student s1 = new Student();
// setup attribute values
s1.setupValues();
s2.setupValues();
// print out student info
s1.printInfo();
s2.printInfo();
// print out the average grades
System.out.println("Average grade for " + s1.getName() + " is " + s1.getAverage());
System.out.println("Average grade for " + s2.getName() + " is " + s2.getAverage());
import java.util.Scanner;
public class Student {
String name;
int id;
String class1;
int grade1;
String class2;
int grade2;
String class3;
int grade3;
void setupValues(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the student's name.");
name = keyboard.nextLine();
System.out.println("Enter the student's ID.");
idnumber = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter the student's classes.");
class1 = keyboard.nextLine();
class2 = keyboard.nextLine();
class3 = keyboard.nextLine();
System.out.println("Enter the student's grades.");
grade1 = keyboard.nextInt();
grade2 = keyboard.nextInt();
grade3 = keyboard.nextInt();
}
void printInfo(){
System.out.println("Student name: " + name);
System.out.println("Student ID: " + idnumber);
System.out.println("Student's classes: " + class1 + ", " + class2 + ", " + class3 + ".");
System.out.println("Student grades: " + grade1 + ", " + grade2 + ", " + grade3 + ".");
}
String getName(){
return name;
}
int getAverage(){
return (grade1 + grade2 + grade3)/3;
}
}