cef27 February 2016

Creating a testing class

I am extremely new to Java so forgive me if I am not asking this question correctly. I have an assignment that is asking me:

"Test your accessors and mutators in a class called Student_Testing. First create the Student object, then set the value using your mutator, and then print the value to the screen. Repeat for each of the variables. Copy and paste the entire Student_Testing class."

So I currently have a Main class that is this:

public class Main {

    public static void main(String[] args) {
        Student student1 = new Student();
        Student student2 = new Student("Joe", 123);

        int id = student1.getStudentID();
        String name = student1.getName();
        System.out.println("ID 1: " + id);
        System.out.println("Name 1: " + name);

        int id2 = student2.getStudentID();
        String name2 = student2.getName();
        System.out.println("ID 2: " + id2);
        System.out.println("Name 2: " + name2);
    }
}

And I have have a class called Student which is this:

public class Student {

    private String name;
    private int student_id;
    private double balance;

    public Student() {
        name = "";
        student_id = 0;
        balance = 0.0;
    }

    public Student(String input_name, int id) {
        name = input_name;
        student_id = id;
    }

    public String getName() {
        return name;
    }

    public int getStudentID() {
        return student_id;
    }

    public void setStudentID(int number) {
        student_id = number;
    }

    public void deposit(double amount) {
        balance = balance + amount;
    }
}

I have no clue how I am supposed to make the Student_Testing class and to create the object Student. I get errors every time.

Is the Student_Testing class created just like the other classes I have? And how am I supposed to create a new object Student when

Answers


Krimson February 2016

public class Main {

    public static void main(String[] args) {
        Student_Testing.test();
    }
}

public class Student_Testing {

    public static void test(){
        Student student1 = new Student();
        Student student2 = new Student("Joe", 123);
        int id = student1.getStudentID();
        String name = student1.getName();
        System.out.println("ID 1: " + id);
        System.out.println("Name 1: " + name);
        int id2 = student2.getStudentID();
        String name2 = student2.getName();
        System.out.println("ID 2: " + id2);
        System.out.println("Name 2: " + name2);
    }
}

public class Student {
    //student class stuff...
}

Here we created a new class called Student_Testing. In this class, we created a static method called test(). The stuff inside the test() function is exactly the same as it was in your original code.

Note the similarity between Student_Testing and your Main class?

We can now call this test method from your main function by simply doing Student_Testing.test();


Since you are still a beginner, it's important that you ask any follow up questions.
I highly encourage you to read through this and understand it thoroughly

Post Status

Asked in February 2016
Viewed 3,416 times
Voted 5
Answered 1 times

Search




Leave an answer