In: Computer Science
Sorry there is no more information i could provide.Please give each step's code in two class.
This question is about the java program and it conclude six step.Please show every step's code in two class and answer the question :"How many tests should the testStudent method now contain" "Do not forget to modify your testStudent method to test the new code. "and please explain why?Thank you!
Step1:
Create a class Student that contains the following things:
a private integer ID number;
a constructor that takes as argument an ID number and uses this
ID number to initialize the object's ID number;
a public method getID that takes zero argument and returns the ID
number of the student;
a public static method testStudent that contains tests for your
class.
Here is the corresponding UML diagram (remember that + means public
and - means private):
+--------------------------+
| Student |
+--------------------------+
| - ID: int |
+--------------------------+
| + Student(int ID) |
| + getID(): int |
| + testStudent(): void |
+--------------------------+
The Student class does not have a setID method because the ID
number of a student never changes.
How many tests should the testStudent method contain?
Once you have written the Student class, you can test it by adding
the following code in a new class called Start in the same
project:
public class Start {
public static void main(String[] args) {
Student.testStudent();
}
}
This code calls the static testStudent method of the Student class,
which should then run all your tests.
What is the problem if you make the ID instance variable
public?
Step 2
Modify the constructor of the Student class so that negative ID
numbers are not allowed when creating a Student object. If the
constructor is given a negative ID number as argument then the
constructor should use 0 for the ID number.
Modify your testStudent method to test the new code. How many tests
should the testStudent method now contain?
Step 3
Add to your Student class a string representing the name of the
student. The constructor for the class should now take the name of
the student as an extra argument. Also add to your class two
methods getName and setName to get and change the name of a student
(a student is allowed to change name).
Here is the corresponding UML diagram:
+--------------------------------+
| Student |
+--------------------------------+
| - ID: int |
| - name: String |
+--------------------------------+
| + Student(int ID, String name) |
| + getID(): int |
| + getName(): String |
| + setName(String name): void |
| + testStudent(): void |
+--------------------------------+
Note: we have not talked much about the String type in class yet.
For now just use it like you use any other type (like int or
float).
Do not forget to modify your testStudent method to test the new
code. In Java you can use == to compare constant strings.
Step 4
Add to your Student class a character representing the grade of
the student. The default grade when a student is created is 'A'.
Also add to your class two methods getGrade and setGrade to get and
change the grade of a student.
Here is the corresponding UML diagram:
+--------------------------------+
| Student |
+--------------------------------+
| - ID: int |
| - name: String |
| - grade: char |
+--------------------------------+
| + Student(int ID, String name) |
| + getID(): int |
| + getName(): String |
| + setName(String name): void |
| + getGrade(): char |
| + setGrade(char grade): void |
| + testStudent(): void |
+--------------------------------+
Do not forget to modify your testStudent method to test the new
code. In Java you can use == to compare characters.
Step5
Add a new constructor to your Student class that takes three
arguments as input: an ID number, a name, and an initial grade.
Using this second constructor it becomes possible to create Student
objects with an initial grade which is different from 'A'.
Here is the corresponding UML diagram:
+--------------------------------------------+
| Student |
+--------------------------------------------+
| - ID: int |
| - name: String |
| - grade: char |
+--------------------------------------------+
| + Student(int ID, String name) |
| + Student(int ID, String name, char grade) |
| + getID(): int |
| + getName(): String |
| + setName(String name): void |
| + getGrade(): char |
| + setGrade(char grade): void |
| + testStudent(): void |
+--------------------------------------------+
Do not forget to modify your testStudent method to test the new
code.
Step 6
Add to your Student class a boolean indicating whether the
student is currently sleeping in the lab or not. When a student is
created, the student is awake. Also add to your class three
methods:
one method isSleeping that returns a boolean indicating whether
the student is currently sleeping or not.
one method goToSleep that makes the student fall asleep. When a
student falls asleep, the grade of the student decreases by one
letter grade ('A' becomes 'B', 'B' becomes 'C', 'C' becomes 'D',
'D' becomes 'F', 'F' stays an 'F', and any other grade becomes 'F'
too).
one method wakeUp that makes the student wake up. The grade of a
student does not go up when the student wakes up.
Here is the corresponding UML diagram:
+--------------------------------------------+
| Student |
+--------------------------------------------+
| - ID: int |
| - name: String |
| - grade: char |
| - sleeping: boolean |
+--------------------------------------------+
| + Student(int ID, String name) |
| + Student(int ID, String name, char grade) |
| + getID(): int |
| + getName(): String |
| + setName(String name): void |
| + getGrade(): char |
| + setGrade(char grade): void |
| + isSleeping(): boolean |
| + goToSleep(): void |
| + wakeUp(): void |
| + testStudent(): void |
+--------------------------------------------+
Do not forget to modify your testStudent method to test the new
code.
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks =========================================================================== public class Student { private int ID;//a private integer ID number; // Step 3 private String name; // Step 4 private char grade; // Step 6 private boolean sleeping; public Student(int ID) { this.ID = ID; //Step 2 if (this.ID <= 0) this.ID = 0; } public Student(int id, String name) { this.ID = ID; //Step 2 if (this.ID <= 0) this.ID = 0; this.name = name; } // Step 5 public Student(int ID, String name, char grade) { this.ID = ID; //Step 2 if (this.ID <= 0) this.ID = 0; this.name = name; this.grade = grade; } public int getID() { return ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } // Step 4 public char getGrade() { return grade; } public void setGrade(char grade) { this.grade = grade; } // Step 6 public boolean isSleeping() { return sleeping; } // Step 6 public void goToSleep() { sleeping = true; } // step 6 public void wakeUp() { sleeping = false; } public static void testStudent() { Student aStudent = new Student(123); System.out.println("Testing the ID is getting saved correctly."); System.out.println("Student ID: "+aStudent.getID()); System.out.println("Expected: 123"); if (aStudent.getID() == 123) { System.out.println("Test Case 1 passed."); } else { System.out.println("Test Case 1 failed."); } System.out.println("Testing negative ID is not getting assigned."); Student anotherStudent = new Student(-10); System.out.println("Student ID: "+anotherStudent.getID()); System.out.println("Expected: 0"); if (anotherStudent.getID() == 0) { System.out.println("Test Case 2 passed."); } else { System.out.println("Test Case 2 failed."); } System.out.println("Testing name is getting saved correctly."); Student studentB = new Student(90, "John"); System.out.println("Student Name: "+studentB.getName()); System.out.println("Expected: John"); if (studentB.getName() == "John") { System.out.println("Test Case 3 passed."); } else { System.out.println("Test Case 3 failed."); } System.out.println("Testing grade is getting saved correctly."); studentB.setGrade('A'); System.out.println("Student Grade: "+studentB.getGrade()); System.out.println("Expected: A"); if (studentB.getGrade() == 'A') { System.out.println("Test Case 4 passed."); } else { System.out.println("Test Case 4 failed."); } System.out.println("Testing constructor that accepts grade."); Student studentC = new Student(45, "Peter", 'F'); System.out.println("Student Grade: "+studentC.getGrade()); System.out.println("Expected: F"); if (studentC.getGrade() == 'F') { System.out.println("Test Case 5 passed."); } else { System.out.println("Test Case 5 failed."); } System.out.println("Testing student can be made to sleep and awake."); System.out.println("Student going to sleep now."); studentC.goToSleep(); System.out.println("Student Sleeping: "+studentC.isSleeping()); System.out.println("Expected: true"); if (studentC.isSleeping()) { System.out.println("Test Case 6 passed."); } else { System.out.println("Test Case 6 failed."); } studentC.wakeUp(); System.out.println("Student waken up."); System.out.println("Student Sleeping: "+studentC.isSleeping()); System.out.println("Expected: false"); if (!studentC.isSleeping()) { System.out.println("Test Case 7 passed."); } else { System.out.println("Test Case 7 failed."); } } }
==============================================================
public class Start { public static void main(String[] args) { Student.testStudent(); } }
==============================================================