Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 13 votes, 5.00 average. Display Modes
Old 04-18-2006, 07:33 PM   #1 (permalink)
Bunkertor 7
Banned
 
Bunkertor 7's Avatar
 
Join Date: Nov 2004
Location: Georgia
Posts: 281
+0 Internets
Send a message via AIM to Bunkertor 7
Thumbs up objects and arrays in Java

For my final project in Programing Principles I, we have to make a seemingly simple program that takes in 10 student names through the JOpitonPane window, assign each student 6 random test scores and then sort the student names, a test score (i.e. have the names and the scores correctly switched), and print the test scores.

I did this project last semester for my Intro. to Java class, but I didn't define more than one class (e.g. I didn't use objects). While I thought it would make this process more automated, it's becoming more and more complicated since I want to create an array of objects that will hold 1 string for the name and assign 6 random test scores (which I can do). The problem lies when I try to use this god awful method of swapping elements in arrays that my teacher showed the class how to do (he didn't show us how to swap elements in arrays of objects or how to use the included classes for array searching and sorting).

This code does compile without any errors, but the sorting methods print out way 10 times (instead of one). I'd like to stay away from two dimensional arrays and stick to objects, but I'm willing to try anything at this point.

Code:
import javax.swing.*; import java.util.Arrays; public class Tests { int temp2; public static void main (String[] args) { Student[] srecords = new Student[10]; for(int i=0; i<10; i++) srecords[i] = new Student(); for(int i=0; i=2; j--) for(int k=0; k 0) swap(a, k, k+1); System.out.println("--------------------"+ "\nAfter sorting,"); for(int i=0; i
Bunkertor 7 is offline   Reply With Quote
Old 04-18-2006, 07:38 PM   #2 (permalink)
Bunkertor 7
Banned
 
Bunkertor 7's Avatar
 
Join Date: Nov 2004
Location: Georgia
Posts: 281
+0 Internets
Send a message via AIM to Bunkertor 7
This vB code button is acting screwy, here's the urls to the files.

http://www.georgiasouthern.edu/~joxford1/Tests.java
http://www.georgiasouthern.edu/~joxford1/Student.java

Edit: I'm also about ready to slam my dick in the fridge door than continuing to work on this project. I really think that it couldn't be as painful as this.

Last edited by Bunkertor 7 : 04-18-2006 at 07:55 PM.
Bunkertor 7 is offline   Reply With Quote
Old 04-18-2006, 08:26 PM   #3 (permalink)
Vorph
-666 Internets
 
Vorph's Avatar
 
Join Date: May 2002
Location: Hell
Posts: 4,954
Your sorting routine seems weird to me. I don't have a java compiler (and haven't used java in 10 years, but it's similar enough to C# that I can read the code still) installed so I can't test it but:

1. Working on an array of Student objects inside of a Student object is definitely not kosher. The sorting() method should belong to the Tests object just like sort_names() does.
2. You're comparing temp2 values, not actual test scores. Again, just by reading the code, temp2 appears to just be a variable you store the quiz number they want sorted by. If it was my project, I would get rid of q1 through q6 and make int q[6]; instead. That way you could reference q[tempint] instead of using temp2 at all (which allows you to lose temp2 entirely as well).
3. Your loop at the end prints out the same thing 10 times because you use this instead of x[q].
__________________
  

Last edited by Vorph : 04-18-2006 at 08:28 PM.
Vorph is offline   Reply With Quote
Old 04-18-2006, 09:10 PM   #4 (permalink)
Bunkertor 7
Banned
 
Bunkertor 7's Avatar
 
Join Date: Nov 2004
Location: Georgia
Posts: 281
+0 Internets
Send a message via AIM to Bunkertor 7
Quote:
Originally Posted by Vorph
Your sorting routine seems weird to me. I don't have a java compiler (and haven't used java in 10 years, but it's similar enough to C# that I can read the code still) installed so I can't test it but:

1. Working on an array of Student objects inside of a Student object is definitely not kosher. The sorting() method should belong to the Tests object just like sort_names() does.
2. You're comparing temp2 values, not actual test scores. Again, just by reading the code, temp2 appears to just be a variable you store the quiz number they want sorted by. If it was my project, I would get rid of q1 through q6 and make int q[6]; instead. That way you could reference q[tempint] instead of using temp2 at all (which allows you to lose temp2 entirely as well).
3. Your loop at the end prints out the same thing 10 times because you use this instead of x[q].
I was experimenting with putting the sorting methods (except the printing ones) in my Tests class, but I was having trouble balancing out all the bullshit static vs. non-static references. The technique itself is derived from the only way he showed us how to sort elements in Java. Would it be possible to use predefined array sorting methods when dealing with objects?

I had NO CLUE how to store a temporary object holder for q1..q6 (and I couldn't use "this"), so that was what I was trying to do with my temp2 variable.

I would have thought q[x] would be a legal identifier for a variable, but I'll certainly try that.

I'm not going to say I suck at programming, as my teacher really didn't give us many examples pertaining to things like this, so it's basically been trial and error with whatever outside help I can find. Everything else up until he gave us a quick rundown on classes was easy stuff though.

I appreciate your help Vorph, thanks a heap!
Bunkertor 7 is offline   Reply With Quote
Old 04-19-2006, 08:47 AM   #5 (permalink)
Bunkertor 7
Banned
 
Bunkertor 7's Avatar
 
Join Date: Nov 2004
Location: Georgia
Posts: 281
+0 Internets
Send a message via AIM to Bunkertor 7
Yep, as I thought q[x] isn't a legal identifier in Java.

Also, why can't I make an array of objects? If I put the method in my main class, then that pretty much defeats the purpose of having objects.
Bunkertor 7 is offline   Reply With Quote
Old 04-19-2006, 09:32 AM   #6 (permalink)
Vorph
-666 Internets
 
Vorph's Avatar
 
Join Date: May 2002
Location: Hell
Posts: 4,954
An object's methods are meant to operate on one single instance of that object. Unless I've misunderstood the problem, you want to sort the array of objects based on one of the values inside the object, and you're not actually changing the individual objects at all just the array...so it doesn't belong inside the object itself. The quiz sorting method doesn't belong inside the object for the same reason that the name sorting one doesn't.

Now, if you needed a method which sorted an individual student's quizzes by the score he received on them, it would go inside the Student object. You see what I'm getting at?


I'm not sure what you mean by q[x] isn't a legal identifier though. If you're referring to the part where I said it's printing 10 times because of this instead of x[q], here's what I'm talking about (in the sorting() method of Student):

Code:
for(int q=0; q<10; q++) System.out.println(this.n+" "+this.temp2);
I'm not sure what the purpose of that loop is though, looks sort of like debugging code that should probably be removed.

I see what you tried to do with the temp2 property in the Student object, but it's not going to work. Reason being, Java lacks an eval() statement--it would work in something like JavaScript on a website but even though Java uses a VM to do its work, allowing for code that changes at runtime in a compiled environment would be a nightmare.

Unless I'm totally missing part of what the project is supposed to do, this is all I would say belongs in the Student class:

Code:
import javax.swing.*; import java.util.Arrays; class Student { String n=""; int q[6]; Student() { for(int i=0;i<6;i++) q[i] = (int)(Math.Random()*101); n = JOptionPane.showInputDialog("Enter the student's name:"); } public void printout() { System.out.println("------------------------------------------------"+ "\nInformation for Student: "+this.n); for(int i=0;i<6;i++) System.out.println("\nTest " + i+1 + " Score: " + this.q[i]); System.out.println("\n------------------------------------------------"); } }
When you want to access a particular test score for comparison, using the choice variable that they enter, it'd be something like
if (srecords[k].q[choice-1] > srecords[k+1].q[choice-1]) ... etc.

I agree with you that this does indeed make for a rather simplistic use of an object, but I can only assume that's because it's an introductory class.

PS-Using variables like n and q rather than, say, strName and intQuiz makes baby jesus cry.

Edit: minor typo in printout()
__________________
  

Last edited by Vorph : 04-19-2006 at 10:16 AM.
Vorph is offline   Reply With Quote
Old 04-19-2006, 04:00 PM   #7 (permalink)
Bunkertor 7
Banned
 
Bunkertor 7's Avatar
 
Join Date: Nov 2004
Location: Georgia
Posts: 281
+0 Internets
Send a message via AIM to Bunkertor 7
Are you trying to make an array of 6 integers with the q[6], because a single variable name with q[x] will cause a compile time error.

The way to define arrays in java is

datatype[] name = new datatype[x], where x is the number of elements you want. I think you want me to create an object that holds a string and an array of 6 integers. The problem is referencing between arrays of different objects (say between Student 1 and Student 3). The code you suggested,

"if (srecords[k].q[choice-1] > srecords[k+1].q[choice-1])"

looks like it would take care of this since I can swap elements between arrays nice and crisp (perhaps there's no real other way to do this in Java) .

I think understand what you're talking about with the object referencing; you're trying to convey that you should only have methods that operate on the object values either through instancing or whatnot.

I see where my error was in my printout method too.

The simple variable names just let me saved time when I was writing out the code, but that's just my preference.

Again, I thank you quite a bit for helping me with this tedious project.
Bunkertor 7 is offline   Reply With Quote
Old 04-19-2006, 04:37 PM   #8 (permalink)
Benito Fireslinger
Registered User
 
Join Date: Feb 2002
Location: Georgia
Posts: 333
+0 Internets
stick the student objects in an Object[] o = new Object[10]

and when you get them out of the array cast them back as Student objects by doing (Student)o[i];


you cant use Arrays.sort because you have not implemented Comparable so it cannot do natural order sorting, which is why you have to write your own (which needs a swap method).

Also never access variables directly always make get and set methods

To sort by quizes (make a method which does this)

Code:
public int[] getQuizes() { return q; } public String getName() { return name; } public Student sortQuizes(Student a) { int[] qTemp = a.getQuizes(); for(int i=qTemp.length-1; i >= 1; i--) { for(int j = 1; j < i; j++) { if ( qTemp[j-1] < qtemp[j] ) swap(a, j-1, j); } } } protected static void swap(int[] array, int i, int j) { int temp; temp = array[i]; array[i] = array[j]; array[j] = temp;; }//end swap

this will sort the quizes

for names

Code:
public Student[] sortNames(Student[] sTemp) { for(int i=sTemp.length-1; i >= 1; i--) { for(int j = 1; j < i; j++) { String n = sTemp[j-1].getName(); String n2 = sTemp[j].getName(); if ( n.compareTo(n2) > 0 ) swap(sTemp, j-1 , j); } } }


gah something along these lines should work...
__________________
Archimonde
Benito Fireslinger is offline   Reply With Quote
Old 04-19-2006, 04:47 PM   #9 (permalink)
Vorph
-666 Internets
 
Vorph's Avatar
 
Join Date: May 2002
Location: Hell
Posts: 4,954
Quote:
Originally Posted by Bunkertor 7
Are you trying to make an array of 6 integers with the q[6], because a single variable name with q[x] will cause a compile time error.

The way to define arrays in java is

datatype[] name = new datatype[x], where x is the number of elements you want.
Ahh, forgot about that. Like I said, it's been 10 years and I never really cared for Java anyway. It took me years to stop complaining about having to switch from plain old C to C++ (and lately C#). ^^

And also, like Benito mentioned, I should've put in property gets rather than accessing the properties directly... couldn't remember how Java did it and then I ended up forgetting to fix it. I think he covered the rest, at any rate.

Quote:
Again, I thank you quite a bit for helping me with this tedious project.
No problem.
__________________
  

Last edited by Vorph : 04-19-2006 at 04:51 PM.
Vorph is offline   Reply With Quote
Old 04-19-2006, 05:04 PM   #10 (permalink)
Bunkertor 7
Banned
 
Bunkertor 7's Avatar
 
Join Date: Nov 2004
Location: Georgia
Posts: 281
+0 Internets
Send a message via AIM to Bunkertor 7
FUCK, FINALLY GOT THIS SONBITCH!

Vorp's idea to use an array within each object helped me do the sorting methods with a breeze, so the array was technically never altered. However, the names and CORRECT scores kept coming up in different places (which, again, wasn't a problem) so I needed a way to fix that; here I just made a recycle method to take a copy I made of the original array and reassign all the elements. This damn simple concept took me far too long to code, but I thank everyone for their help.

Here are the urls again with the final project.

http://www.georgiasouthern.edu/~joxford1/Tests.java
http://www.georgiasouthern.edu/~joxford1/Student.java
Bunkertor 7 is offline   Reply With Quote
Old 04-19-2006, 05:28 PM   #11 (permalink)
Bunkertor 7
Banned
 
Bunkertor 7's Avatar
 
Join Date: Nov 2004
Location: Georgia
Posts: 281
+0 Internets
Send a message via AIM to Bunkertor 7
Quote:
Originally Posted by Vorph
Ahh, forgot about that. Like I said, it's been 10 years and I never really cared for Java anyway. It took me years to stop complaining about having to switch from plain old C to C++ (and lately C#). ^^
Is C# really worth using as opposed to C++ or even J++? Java was my first language since there's been a big push to orient CS programs around the country (at least here at Georgia Southern and Georgia Tech) towards Java. The only good thing about Java is that you don't have to rewrite code for different architectures, but that's about it.

I need to take the time and look at my C++ book I bought last semester; I also should take the time to find some information about Visual J# or something related that will run with Windows. (Linux is a whole different story now, and I'm not very experienced). Hopefully learning Java as my first language will help me with J#.

Last edited by Bunkertor 7 : 04-19-2006 at 05:33 PM.
Bunkertor 7 is offline   Reply With Quote
Old 04-19-2006, 05:37 PM   #12 (permalink)
Sanchek
Registered User
 
Join Date: Apr 2003
Location: Atlanta
Posts: 266
+0 Internets
Send a message via AIM to Sanchek
C# is Java for people who work in the real world.

As languages, they're really very similar. Understanding concepts is more important than worrying about the syntax of one language or another.
Sanchek is offline   Reply With Quote
Old 04-19-2006, 05:45 PM   #13 (permalink)
Bunkertor 7
Banned
 
Bunkertor 7's Avatar
 
Join Date: Nov 2004
Location: Georgia
Posts: 281
+0 Internets
Send a message via AIM to Bunkertor 7
Quote:
Originally Posted by Sanchek
C# is Java for people who work in the real world.

As languages, they're really very similar. Understanding concepts is more important than worrying about the syntax of one language or another.
So I shouldn't have trouble picking them up once I have all the concepts of Java down?

I'm in CS-I now and next semester I'll have CS-II which implements Java GUIs and the like; hopefully I can do as well in that class as I did this semester.
Bunkertor 7 is offline   Reply With Quote
Old 04-20-2006, 08:12 AM   #14 (permalink)
Benito Fireslinger
Registered User
 
Join Date: Feb 2002
Location: Georgia
Posts: 333
+0 Internets
Quote:
C# is Java for people who work in the real world.
... this is debatable.
__________________
Archimonde
Benito Fireslinger is offline   Reply With Quote
Old 04-20-2006, 08:45 AM   #15 (permalink)
Sanchek
Registered User
 
Join Date: Apr 2003
Location: Atlanta
Posts: 266
+0 Internets
Send a message via AIM to Sanchek
Quote:
Originally Posted by Bunkertor 7
So I shouldn't have trouble picking them up once I have all the concepts of Java down?

I'm in CS-I now and next semester I'll have CS-II which implements Java GUIs and the like; hopefully I can do as well in that class as I did this semester.
Yeah, you'll be fine. Don't look at it as learning the language as much as learning the OOD concepts and understanding how to implement them. That is far more valuable than the particulars of any language.

Read The Daily WTF for examples of the massive stupidity we run into on a daily basis, due to "programmers" that were little more than vocationally trained to implement certain design patterns in a certain language.

Though, the Java GUI stuff in your next class is probably a lame waste of time IMO.
Sanchek is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 03:49 PM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6