This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes instantiate objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instantiate objects" Watch "instantiate objects" New topic

instantiate objects

Valarie Brandt

Joined: Oct 03, 2003
Posts: 24
I am very new to the Java programming world. I am in the process of taking my first class.
In our second project we have 2 classes.
One names Student which has 3 fields with get and set methods for each, a method to calculate student gpa and another method to display all variable except for gpa. Then we write a constructor to set default values for the three variable. I got that far and here is where I want to sit and cry(which I have done twice).
We create a second class ShowStudent(which I have done) and the the class is suppose to have a control level method. I am not sure what control level method is suppose to mean-is it the main method which is call processStudent()? processStudent() is suppose to call another method createStudent() which will instantiate two Student objects from the student class. Does that mean i call the method from student class that displays the student info(the fields that were declared)? How do i get the information from Student to communicate with ShowStudent?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Hi Valarie,
Welcome to JavaRanch!
Please don't cry -- it makes the Moose nervous.

It sounds like you've already done the hard part: writing the Student class. Now all you need to do is write the other class whose job is just to "show off" what Student can do. So you create a few Student objects. Then use the setXXX() methods you've written to adjust the state of the Students. Finally, use the Student.display() method to display the Student values, and then use the getGPA() method to fetch the GPA and print that, too.
Nothing hard here at all. If you run into trouble, you can always post some code for us to critique.

[Jess in Action][AskingGoodQuestions]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Valerie -
Welcome to JavaRanch!
We're all here to help each other, because we've all (*) run into stuff in Java and programming that's left us questioning our skills or our sanity. Or both. Be patient, maybe start looking at the stuff as a bunch of puzzles.
(*) I'm sure there are a few people here who never ran into something that seemed unsolvable to them, but if such folks exist, they're very very rare!

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Valarie Brandt

Joined: Oct 03, 2003
Posts: 24
How do I create the student objects ? thats where i am running into a wall.
My book says: supply a type, identifier, and allocate memory space for it.
These are the fields from Student:
public int studentId;
public int creditHours;
public double numberOfPoints;
I have to have a controll level method called processStudent(); so I start out with :
public class ShowStudent
public void processStudent();
(In this method I call another method-createStudent) I am not sure how
to do the call. Should it be nested?
public void createStudent(); In this method I use the set methods
from Student to assign values in the objects using the correct
method. I understand that i should use the set method from
Student which looks like:
public void setNumberOfPoints(double points)
numberOfPoints = points;
Do i call it Student.setNumberOfPoints or do I call it Student.Points?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Well, let's see. It looks as though perhaps you haven't written as much of the Student class as I imagined. Let me remind you of a few fundamentals.
First, to create an object -- any Object -- in Java, you use "new":

This is essentially a call to Student's constructor, so if the constructor needs arguments, you put them in the parentheses there.
Now, second, you've got this student in the variable s, and you want to use the setNumberOfPoints() method to affect that particular Student object -- so you invoke the method by name on that specific object, like this:

OK? Can you make program from there?
I agree. Here's the link:
subject: instantiate objects