|
| | #1 (permalink) |
| Banned Join Date: Nov 2004 Location: Georgia
Posts: 281
| 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:
|
| | |
| | #2 (permalink) |
| Banned Join Date: Nov 2004 Location: Georgia
Posts: 281
| 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. |
| | |
| | #3 (permalink) |
| Never Go Full Retard Join Date: May 2002 Location: Hell
Posts: 5,669
| 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. |
| | |
| | #4 (permalink) | |
| Banned Join Date: Nov 2004 Location: Georgia
Posts: 281
| Quote:
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! | |
| | |
| | #5 (permalink) |
| Banned Join Date: Nov 2004 Location: Georgia
Posts: 281
| 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. |
| | |
| | #6 (permalink) |
| Never Go Full Retard Join Date: May 2002 Location: Hell
Posts: 5,669
| 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:
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:
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. |
| | |
| | #7 (permalink) |
| Banned Join Date: Nov 2004 Location: Georgia
Posts: 281
| 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. ![]() |
| | |
| | #8 (permalink) |
| Registered User Join Date: Feb 2002 Location: Georgia
Posts: 333
| 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:
this will sort the quizes for names Code:
gah something along these lines should work...
__________________ Archimonde |
| | |
| | #9 (permalink) | ||
| Never Go Full Retard Join Date: May 2002 Location: Hell
Posts: 5,669
| Quote:
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:
Last edited by Vorph : 04-19-2006 at 04:51 PM. | ||
| | |
| | #10 (permalink) |
| Banned Join Date: Nov 2004 Location: Georgia
Posts: 281
| 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 |
| | |
| | #11 (permalink) | |
| Banned Join Date: Nov 2004 Location: Georgia
Posts: 281
| Quote:
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. | |
| | |
| | #12 (permalink) |
| Registered User Join Date: Apr 2003 Location: Atlanta
Posts: 266
| 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. |
| | |
| | #13 (permalink) | |
| Banned Join Date: Nov 2004 Location: Georgia
Posts: 281
| Quote:
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. | |
| | |
| | #15 (permalink) | |
| Registered User Join Date: Apr 2003 Location: Atlanta
Posts: 266
| Quote:
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. | |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |