Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Another java question!

Here is another problem I am stuck with, I am still having a bit of trouble with the object references and arguments.

Here is the problem:

package studentclient;

    public class StudentClient{

    public static void main( String [] args ){
    /* Declare two object references of type Student s1 and s2 and 
    instantiate two Student objects passing three arguments to the 
    constructor for the class. Use different values for each class 
    object */

and here is what I have to get started:

public static void main(String[] args) {
    Student s1;
    Student s2;

     s1 = new Student (String, String, double);
     s2 = new Student (String, String, double);
}

When it asks me to instantiate two Student objects passing three arguments, what exactly do I have to do? And why? Am I even on the right track? I think I did everything they asked me so far in this step, but I'm not sure.

Also, if anyone can help me get started on this second part:

/* Output the name, social security number and GPA of the student from object reference s1 using the appropriate accessor methods to obtain the data */

// Your code here

Again, thanks in advance!

4 Answers

Hey there,

I think you didn't really understand the question. When you create a new object you don't pass on the datatype but the arguments the objects accept. The question you are having says...

" Output the name, social security number and GPA of the student from object reference s1 using the appropriate accessor methods to obtain the data "

So I assume the 3 arguments it wants is... a name, social security number in a form of a string and the GPA as double.

What you wrote is...

 public static void main(String[] args) {
    Student s1;
    Student s2;

     s1 = new Student (String, String, double);
     s2 = new Student (String, String, double);
}

passing the data types as arguments, but what it wants is that you pass actual arguments. So you are suppose to do this

public static void main(String[] args) {
    Student s1;
    Student s2;

     s1 = new Student ("Maria", "733 - 274H - 03", 3.7);
     s2 = new Student ("Paul", "47H - 1264 - 56", 3.7);
}

Or to make things shorter

public static void main(String[] args) {

     Student s1 = new Student ("Maria", "733 - 274H - 03", 3.7);
     Student s2 = new Student ("Paul", "47H - 1264 - 56", 3.7);
}

Now the 2nd part where it says you should output something you have to create methods (if those methods don't exist) that return the names and GPA to the system output.

Hi Gloria, thank you for your help!

The first part of the question didn't ask for the ssn, gpa, or name, which is why I was probably confused. So when the problem asked:

/* Declare two object references of type Student s1 and s2 and instantiate two Student objects passing three arguments to the constructor for the class. Use different values for each class object */

I could have just put

public static void main(String[] args) {

    Student s1 = new Student ("Maria", "733 - 274H - 03", 3.7);
    Student s2 = new Student ("Paul", "47H - 1264 - 56", 3.7);
}

Instead of what I actually put, which was

public static void main(String[] args) {
    Student s1;
    Student s2;

    s1 = new Student (String, String, double);
    s2 = new Student (String, String, double);
}

I mean, since this is supposed to be a step by step type of thing, it would have been nice if the problem had mentioned the ssn, gpa, and name from the get-go (is there something I'm missing here?).

Anyways, thanks so much for you help!

I saw it being mentioned down the road. I don't know where you got this problem from so I can't really say if it is written in a logical step or not, but i'm glad I helped. You are welcome!

Hi Gloria,

This is the whole problem here.

In the StudentClient application class main method code the instructions to perform the tasks indicated in the remarks : package studentclient;

public class StudentClient{

    public static void main( String [] args ){

        /* Declare two object references of type Student s1 and s2 
            and instantiate two Student objects passing three arguments 
            to the constructor for the class. Use different values for each class object */

        // Your code here

        /* Output the name, social security number and GPA of the 
            student from object reference s1 using the appropriate accessor 
            methods to obtain the data */

       // Your code here

        /* Output the name, social security number and GPA of the 
            student from object reference s2 using the toString method 
            to return the data */

        // Your code here

        /* Using the equals method and a selection control structure 
            (if statement), compare objects s1 and s2 and output an appropriate 
            message indicating if the objects are equal */

        // Your code here

        /* Using the appropriate mutator methods on student object s2, 
            change the name, social security number and GPA to the same 
            values as in object s1. Use the set methods. */

        // Your code here

        /* Again, using the equals method and a selection control structure 
           (if statement), compare objects s1 and s2 and output an appropriate 
            message indicating if the objects are equal */

        // Your code here
    }
}

In the Student service class code the instructions to define your Student service class structure with the appropriate constructors, mutator methods, accessor methods as well as a toString and an equals method :

public class Student {
     /* Declare three instance variables to represent the student name, 
        social security number and GPA */

    // Your code here

    /* Overloaded constructor method: Allows client to set beginning 
        values for name, ssn, and gpa. This constructor takes three parameters 
        and calls mutator methods to validate new values */

    public Student( String newName, String newSsn, double newGpa ) {
        // Your code here
    }

    /* getName accessor method */

    public String getName( ) {
    // Your code here
}
    /* setName mutator method */

    public void setName( String newName ) { 
        // Your code here
    } 
    /* getSsn accessor method */

    public String getSsn( ) {
        // Your code here
    }
    /* setSSN mutator method */

    public void setSsn( String newSsn ) { 
        // Your code here
    }
    /* getGpa accessor method */

    public double getGpa( ) { 
        // Your code here
    }
    /* setGpa mutator method: Allows client to set value of gpa and 
        prints an error message if new value is either less than 0 or greater 
        than 4.0. setGpa does not change the value of gpa if newGpa is 
        negative or greater than 4.0 */

    public void setGpa( double newGpa ) {
        // Your code here
    }
    /* toString method returns student name, social security number and GPA */

    public String toString( ) {
        // Your code here
    }

    /* equals method returns boolean compares two Student objects 
        for the same field values returns a boolean, true if this object has 
        the same field value as the parameter object */

    public boolean equals( Object o ) {
        // Your code here
    }
}

This is the whole thing. It's really long, and I'm having some trouble trying to solve this one. Just so you can get the context.

So I tried to use s1.getName(), s1.getSsn(), and s1.getGpa() to output the strings and double, but I couldn't get it to work. Is there something I'm doing wrong? I can send you my "attempt" if you want.

Yeah, my professor puts up extra credit assignments weekly on Blackboard, and since I'm having trouble with java, I thought I'd do all the extra credit work so I can get myself up to speed.

He's not really the greatest at writing down the steps, because a lot of us have to hit him up on the discussion board to ask him what he meant.

Thank you again!

The methods are empty so you have to fill them in with code or else you won't get the output you want. For example to output the things it asks you have to put a System.out in each of the functions you call to output the argument that was passed into the method.

You need to complete the code in the student class before you declare them in the main class. So here are the steps,

  1. declare three variables, student name, social security number and GPA. These are the properties of a student object.
  2. complete the student constructor so later you can create an instance of a student in the main class with the value you assign for a student. Now, the parameters needs to match to the properties that you created above.
  3. complete setter and getter methods. The setter allows you to set the particular value. And the getter returns the value of a particular variable.
  4. complete toString() method, it should return the values of all three variables in a string format
  5. complete the boolean method, so later it can be called to compare with two student objects.
  6. then go to main method, follow the hints there! Hope this helps! Cheers!