Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

school assignment question

Thread view: 
Paul Fedorenko - 16 Mar 2005 04:19 GMT
We're looking at arrays in my Java class at the moment.  Mainly, we've been
building, sorting and searching arrays of int values, which is pretty
straightforward.  But for part of the assignment that I have, I need an
array of 20 entries, with each entry comprising of a string and 2 ints.

So...  I figure I need to make a class with the data in it, plus the methods
for adding the data into the array, searching the array and printing the
results.  my problem is that I have no idea of where to start putting this
beast together.  Would a 2-dim array do it?

All I really need is a nudge in teh right direction, and I'll be able to
figure the rest out on my own.
Chris Smith - 16 Mar 2005 04:41 GMT
> We're looking at arrays in my Java class at the moment.  Mainly, we've been
> building, sorting and searching arrays of int values, which is pretty
[quoted text clipped - 5 lines]
> results.  my problem is that I have no idea of where to start putting this
> beast together.  Would a 2-dim array do it?

You need a class to hold a string and two ints.  The class does not need
to have any code related to the searching, printing, or any other
operation on the array.  In fact, the class should not be aware of the
existence of an array.  It should just manage that string, and those two
ints.

Definitely don't use a two-dimensional array.  Although it could be made
to work, it's a very bad idea.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Paul Fedorenko - 16 Mar 2005 05:02 GMT
> You need a class to hold a string and two ints.  The class does not need
> to have any code related to the searching, printing, or any other
> operation on the array.  In fact, the class should not be aware of the
> existence of an array.  It should just manage that string, and those two
> ints.

So something like:

   public class StudentInfo
   {
        String name;
        int ID;
        int status;
   }

And then another class with the methods needed to assign data to those
variables?  I'm guessing I'd be best off setting up the array itself in
main()?

> Definitely don't use a two-dimensional array.  Although it could be made
> to work, it's a very bad idea.

We've only done 1 and 2-dim arrays.  I think I'm completely out of my league
with this one
John - 16 Mar 2005 06:25 GMT
Usually, the methods that assign values the properties are part of the
same class as the
properties. If you write something like StudentInfo you should add the
access methods as well:

public class StudentInfo {
    private String name;
    private int ID;
    private int status;

    public StudentInfo(aname,aID, astatus) {
        name=aname;
        ID=aID;
        astatus=status;
    }

    public void setName(String aname) {
        name=aname;
    }

    public String getName() {
        return name;
    }

    ...
}

To create an array of 20 objects that are instances of the above class do:

StudentInfo []siarr=new StudentInfo[20];

Now, your array elements are not initialized... You will have to add the
values:
for(int i=0;i<20;i++)
    siarr=new StudentInfo(.....,.....,......);

>>You need a class to hold a string and two ints.  The class does not need
>>to have any code related to the searching, printing, or any other
[quoted text clipped - 20 lines]
> We've only done 1 and 2-dim arrays.  I think I'm completely out of my league
> with this one
Paul Fedorenko - 16 Mar 2005 17:19 GMT
> Usually, the methods that assign values the properties are part of the
> same class as the
> properties. If you write something like StudentInfo you should add the
> access methods as well:

Thanks very much for the reply.  I think you just did half the assignment
for me, which I wasn't expecting, but I think there's still enough for me to
work with on my own, so that's ok.  <grin>

Definitely a nudge in the right direction
John - 17 Mar 2005 04:21 GMT
I'm glad that helped. However, this thing should be tought in class not
necessarily given as an
assignment. It's true that it takes practice to find out things on your
own, but sometimes it's
just a waste of time. Anyway, keep practicing :-)).

>>Usually, the methods that assign values the properties are part of the
>>same class as the
[quoted text clipped - 6 lines]
>
> Definitely a nudge in the right direction
blmblm@myrealbox.com - 16 Mar 2005 20:18 GMT
>Usually, the methods that assign values the properties are part of the
>same class as the
>properties. If you write something like StudentInfo you should add the
>access methods as well:
>
>public class StudentInfo {

[ snip ]

>To create an array of 20 objects that are instances of the above class do:
>
>StudentInfo []siarr=new StudentInfo[20];

Nitpick:  This doesn't create an array of objects; it creates an array
of *references to objects*.  (You probably know this, but the OP might
be confused.)  Initially all the references are null.  To actually
create the objects, you need to ....

>Now, your array elements are not initialized... You will have to add the
>values:
>for(int i=0;i<20;i++)
>    siarr=new StudentInfo(.....,.....,......);

This actually creates the desired objects.

[ snip ]

Hoping to steer the OP away from a common beginner misunderstanding ....

| B. L. Massingill
| ObDisclaimer:  I don't speak for my employers; they return the favor.
Paul Fedorenko - 16 Mar 2005 23:36 GMT
Here's what I have so far.  The compiler keeps spitting out errors
regardless of what I do to fix it, so what I'm posting is my most
recent attempt.  I'm still not completely comfortable with the whole
objects and classes thing, so that'll probably show up quite nicely in
the code I'm posting here.

Obviously, I don't want anyone to do this assignment for me, and I
don't want any of you to assume that.  More likely, I just need to be
kicked and yelled at a few times to force me to do things properly.
________________________________________________________

public class SearchStudents
{
    public static void main(String[] args)
    {
    String name1;
       double ID;
       int status;

       FillArray();
    }

    static void FillArray()
    {
          StudentInfo[] siarray = new StudentInfo[20];

       for (int i = 0; i < 20; i++)
       {
         name1 = getName1(i);
         ID = 100000 * Math.random();
         status = 2 * Math.random();
         siarray = new StudentInfo(name, ID, status);
       }
    }

    static String getName1(int j)
    {
        String[] name = { //list of 20 names here };
        return name[j];
    }
}

public class StudentInfo
{
    private String name;
    private int ID;
    private int status;

    public StudentInfo(arrName, arrID, arrStatus)
    {
    name = arrName;
    ID = arrID;
    status = arrStatus
    }
}
Bjorn Abelli - 17 Mar 2005 01:10 GMT
"Paul Fedorenko" wrote...

> Here's what I have so far.  The compiler keeps spitting out errors
> regardless of what I do to fix it, so what I'm posting is my most
> recent attempt.

Well, looking at the code, those "fixes" seem most likely to be only
"guesses", and lack of some understanding of fundamental things like
"variable scope", "method signatures", etc.

I'll try to give you some hints:

> public class SearchStudents
> {
>     public static void main(String[] args)
>     {

What are you attempting to use these variables for?
And from where can you reach them?

>        String name1;
>        double ID;
[quoted text clipped - 5 lines]
>     static void FillArray()
>     {

Maybe the inner workings of "FillArray" will meet the requirements of your
assignment (?), but possibly the assignment will need to be expanded
somewhat in the future, so you can reach "siarray" from somewhere else as
well?

>       StudentInfo[] siarray = new StudentInfo[20];
>
>        for (int i = 0; i < 20; i++)
>        {

Where did you define variable "name1"?
How can you reach it from here?

>          name1 = getName1(i);

Where did you define variable "ID"?
How can you reach it from here?

>          ID = 100000 * Math.random();

Where did you define variable "status"?
How can you reach it from here?
Can the result of the calculation be implicitly cast to that type?

>          status = 2 * Math.random();

To what element of the array do you want to assign the StudentInfo?
Where did the variable "name" come from?

>          siarray = new StudentInfo(name, ID, status);
>        }
>     }
>
>     static String getName1(int j)
>     {

As the array of names will be the same each time, maybe you'd look into the
chapter on "constants"?

>         String[] name = { //list of 20 names here };
>         return name[j];
[quoted text clipped - 3 lines]
> public class StudentInfo
> {

You will possibly need to add some methods to get these values as well...

>     private String name;
>     private int ID;
>     private int status;

Maybe you should look into the chapter on "method signatures"?

>     public StudentInfo(arrName, arrID, arrStatus)
>     {
[quoted text clipped - 3 lines]
>     }
> }

There are a lot more to be said about this, but I hope these hints can help
you come a bit further on your own.

// Bjorn A
Paul Fedorenko - 17 Mar 2005 05:45 GMT
> Well, looking at the code, those "fixes" seem most likely to be only
> "guesses", and lack of some understanding of fundamental things like
> "variable scope", "method signatures", etc.

Those are definitely among my problems, yes.  Part of me wishes that I could
just define the variables straight-up at the beginning of the function that
uses them.  It'd make life a whole lot easier, not having to worry about
things like private, static, public and protected.  I think that's what's
throwing me the most.  Well...  That, and trying to get the array put
together in the first place.

> I'll try to give you some hints:
>
[quoted text clipped - 9 lines]
>>        double ID;
>>        int status;

They're supposed to be used by the functions in the class where they're
declared, and are meant to be passed into the array once they contain the
appropriate information.  I've also tried declaring them in the FillArray()
function, where they're actually being used, but that only caused me more
problems.  I figured they were better off where they are now, since I get
less errors that way...

No?

> There are a lot more to be said about this, but I hope these hints can
> help you come a bit further on your own.

I think I may have gotten the problems straightened out, except for the
following:

   C:\Documents and Settings\paul\My
Documents\Programming\Java\Lesson7\SearchStudents.java:16: ';' expected
             siarray = new StudentInfo[i](name1, ID, status);

I've got a semi-colon at the end of the line, and all the lines surrounding
the one in question end with a semi-colon too.  As far as I can tell, the
syntax seems right otherwise.
Bjorn Abelli - 17 Mar 2005 07:47 GMT
"Paul Fedorenko" wrote...

> "Bjorn Abelli" wrote...

>>> public class SearchStudents
>>> {
[quoted text clipped - 16 lines]
>
> No?

No!

This is a question of "variable scope".

A variable only "exist" within a specific context.

As they were defined only in the method "main", they couldn't be reached
from within the method "FillArray".

The simple rule is to define variables where they are being used, in this
case *inside* FillArray, as that's the *only* place they're being used. If
they had been used in other methods as well, then you could have declared
them on a more "global" level, e.g. as "static" variables.

>    C:\Documents and Settings\paul\My
> Documents\Programming\Java\Lesson7\SearchStudents.java:16: ';' expected
[quoted text clipped - 3 lines]
> surrounding the one in question end with a semi-colon too.  As far as I
> can tell, the syntax seems right otherwise.

As I recall, there *was* a semicolon missing in the constructor of
StudentInfo...

// Bjorn A
Lāʻie Techie - 21 Mar 2005 07:29 GMT
> I think I may have gotten the problems straightened out, except for the
> following:
[quoted text clipped - 6 lines]
> surrounding the one in question end with a semi-colon too.  As far as I
> can tell, the syntax seems right otherwise.

Since siarray is an array of StudentInfo references, you can only assign
an array to it.  If you want to assign individual elements, you must tell
the index.

StudentInfo []siarray = new StudentInfo[20]; // creates an array

You reference each element of the array using square brackets:

siarray[0] = new StudentInfo(); // assigns a reference to element 0
StudentInfo si = siarray[1]; // retrieves the second element (index 1)

HTH,
La'ie Techie
John - 17 Mar 2005 05:01 GMT
True, forgot to specify that.

>>Usually, the methods that assign values the properties are part of the
>>same class as the
[quoted text clipped - 24 lines]
>
> Hoping to steer the OP away from a common beginner misunderstanding ....


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.