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
Ho Sun Lee
2,003 PointsAnother 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
Gloria Dwomoh
13,116 PointsHey 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.
Ho Sun Lee
2,003 PointsYeah, 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!
Gloria Dwomoh
13,116 PointsThe 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.
Leo Zhang
3,389 PointsYou need to complete the code in the student class before you declare them in the main class. So here are the steps,
- declare three variables, student name, social security number and GPA. These are the properties of a student object.
- 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.
- complete setter and getter methods. The setter allows you to set the particular value. And the getter returns the value of a particular variable.
- complete toString() method, it should return the values of all three variables in a string format
- complete the boolean method, so later it can be called to compare with two student objects.
- then go to main method, follow the hints there! Hope this helps! Cheers!
Ho Sun Lee
2,003 PointsHo Sun Lee
2,003 PointsHi 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
Instead of what I actually put, which was
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!
Gloria Dwomoh
13,116 PointsGloria Dwomoh
13,116 PointsI 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!
Ho Sun Lee
2,003 PointsHo Sun Lee
2,003 PointsHi 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;
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 :
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.