'F' → Fullscreen
new keyword.
In this task, you will create a simple
Student class with a few instance
variables and one method. Then you will create two student objects and
call their method.
Step 1: Create a file named Student.java and type the following code:
public class Student{ String name; int gradeLevel;
void introduce() { System.out.println("Hi, I'm " + name + " and I'm in grade " + gradeLevel + "."); }} Step 2: Create a file named
Program.java and type the following
code:
public class Program{ public static void main(String[] args) { Student s1 = new Student(); s1.name = "Alex"; s1.gradeLevel = 10;
Student s2 = new Student(); s2.name = "Jordan"; s2.gradeLevel = 11;
s1.introduce(); s2.introduce(); }} Step 3: Run the program. Make sure you understand how
the objects are created and how the introduce()
method uses the instance variables.
Now you will extend the Student
class to store a GPA and add new behavior based on that data.
Step 1: Update your Student.java to match the code below:
public class Student{ String name; int gradeLevel; double gpa;
void introduce() { System.out.println("Hi, I'm " + name + " and I'm in grade " + gradeLevel + "."); }
void showGpa() { System.out.println(name + "'s GPA is " + gpa + "."); }
void checkHonorRoll() { if (gpa >= 3.5) { System.out.println(name + " made the honor roll!"); } else { System.out.println(name + " did not make the honor roll this time."); } }} Step 2: Replace the code in
Program.java with the following:
public class Program{ public static void main(String[] args) { Student s1 = new Student(); s1.name = "Alex"; s1.gradeLevel = 10; s1.gpa = 3.8;
Student s2 = new Student(); s2.name = "Jordan"; s2.gradeLevel = 11; s2.gpa = 3.2;
s1.introduce(); s1.showGpa(); s1.checkHonorRoll();
System.out.println();
s2.introduce(); s2.showGpa(); s2.checkHonorRoll(); }}Step 3: Run the program. Observe how each student's instance variables are used to determine whether they made the honor roll.
Challenge: Add a third student of your choice with a different GPA and test your methods again.
Your program output should look similar to the sample output below (exact wording or spacing may vary slightly).
Hi, I'm Alex and I'm in grade 10.Alex's GPA is 3.8.Alex made the honor roll!
Hi, I'm Jordan and I'm in grade 11.Jordan's GPA is 3.2.Jordan did not make the honor roll this time.
You may write your reflection answers as comments at the bottom of
your Program.java file.
Student example in your answer.
Student class?
Submit your completed Student.java,
Program.java, and your
reflection answers to the appropriate dropbox.