2

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 I already have a Student class in a different class?

Like I said I am a complete beginner when it come to Java, so if this could be explained in the simplest terms possible that would be great! Thanks!

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
cef27
  • 25
  • 3
  • Just do the same as your Main class, etc. Same thing. Create a new class called Student_Testing. There is no special "Testing" class. – OldProgrammer Feb 08 '16 at 21:58
  • Thanks, I created it just like the others but I don't understand how to create a student object. And I'm not sure what value I'm supposed to be be setting using my mutator. (I only got this far because our professor provided us the Main and Student class lol) – cef27 Feb 08 '16 at 22:01
  • 1
    I think that they expect you to rename your class Main to Student_Testing, that's all. You also need to use your setters and after that your getters to check that they are doing their job right. – Celine NOEL Feb 08 '16 at 22:09
  • 1
    It sounds like you're extremely new, not just to Java, but to object oriented programming in general. You may want to read up on the general concepts of what classes and objects are, first. Some of the answers to [this question](http://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances) may help. – Douglas Feb 08 '16 at 22:13
  • I agree with Celine here. You could literally rename Main to Student_Testing. And that would be your answer. – OneCricketeer Feb 08 '16 at 22:27

1 Answers1

1
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
Krimson
  • 7,386
  • 11
  • 60
  • 97