Question

In: Computer Science

Sorry there is no more information i could provide.Please give each step's code in two class....

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.

Solutions

Expert Solution

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();
    }
}

==============================================================


Related Solutions

I'M SORRY BUT I COULD NOT FIND ENGLISH ON THE SUBJECT. THIS IS FOR ENGLISH NOT...
I'M SORRY BUT I COULD NOT FIND ENGLISH ON THE SUBJECT. THIS IS FOR ENGLISH NOT FOR PSYCHOLOGY. P.S. IT'S DUE IN 2 HOURS. an argument essay on "are women equal to men?"
Question1 if possible, could anyone do with explanation For each of the following code snippets, give...
Question1 if possible, could anyone do with explanation For each of the following code snippets, give both of the following: a. Give the overall T(n) run me analysis expression for the code. b. Describe the worst case running me of the code snippet in Big‑Oh notation. 1.for (int i=0;i<n;i++) { for (int j=0;j<n/2;j++) { sum++;} } 2.void silly(int n,int x,int y) { for (int i=0;i<n/2;i++) { if (x<y) { for (int j=0;j<100*n*n;j++) { cout<< "j="<<j<<endl;}} else {cout <<"i="<<i<<endl;}}} 3.void silly(int...
I am really sorry but these are related to each other, please and please show me...
I am really sorry but these are related to each other, please and please show me all the steps with clear hand writing. thanks in advance _____________ (a) Calculate the electrical conductivity of copper if it is given that the metal has 8.5 x 1022 conduction electrons per cm3 and the mobility of a conduction electron is 35 cm2.V-1.s-1. answer , units (b) In an electronics project you need to construct your own inductor. The instructions for making this inductor...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
How can the following code be corrected? Give at least two good answers. 1 public class...
How can the following code be corrected? Give at least two good answers. 1 public class H2ClassC { 2   H2ClassC (int a) {} 3 } // end class H2ClassC 4 5 class H2ClassD extends H2ClassC{ 6 } // end class H2ClassD
PLEASE GIVE THE FULL DETAILS OF THE STEPS ON EACH QUESTIONS SO I COULD FOLLOW UP...
PLEASE GIVE THE FULL DETAILS OF THE STEPS ON EACH QUESTIONS SO I COULD FOLLOW UP YOUR WHOLE EXPLANATIONS. PLEASE WRITE THE STEPS ON SOLVING THIS EXAMPLE EX) STEP 1, STEP 2, ETC HANDWRITING IS OKAY AS LONG AS IT IS READABLE. IF YOU HAVE USED THE EQUATIONS OR CONCEPTS, PLEASE STATE IT CLEARLY WHICH ONE YOU HAVE YOU USED SO I COULD FULLY UNDERSTAND THE DETAILS OF THE STEPS YOU HAVE DONE. PLEASE DO NOT OMIT THE DETAILS OF...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard (int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT