Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Another Program Problem
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aziz




PostPosted: Wed Jul 19, 2006 9:01 am   Post subject: (No subject)

OneOffDriveByPoster wrote:
r.3volved wrote:
so you can iterate through the array in a for loop from 0 to the amount of values it contains and then divide by the array elements +1 for average

The point is, you don't want ANY hard coded values in your code


...and the initial size of your array (which I still do not see) is not a hard coded value?


ArrayList comes in handy here Smile
Sponsor
Sponsor
Sponsor
sponsor
r.3volved




PostPosted: Wed Jul 19, 2006 11:25 am   Post subject: (No subject)

ArrayList = http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

If you're a noob and don't understand arraylists then set your array to the maximum number of classes that a student can take (instansiating them all to 0) and identify if the value != 0 as you parse through the array.

Don't criticize just because you don't understand
Aziz




PostPosted: Wed Jul 19, 2006 4:09 pm   Post subject: (No subject)

Or, define the array as

code:
int[] array;


and when you add a new value:

Java:
newArray = new int[array.length+1]
for (int i = 0; i < array.length - 1; i++) {
  newArray[i] = array[i];
}
array[newArray.length + 1] = newValue;


Although, please, for the love of learning, and ease, learn to use ArrayList. There's even a tutorial here on it (if not ArrayList, then Vector, which is similar)

To make an ArrayList:

Java:
ArrayList<Object> array;

array = new ArrayList(0);

value = readLine();

array.add(value);


Please, look at the tutorial. Don't stop at what you know. You take on projects not only to make something, but to learn in the process.
wtd




PostPosted: Wed Jul 19, 2006 6:26 pm   Post subject: (No subject)

When you declare a:

code:
ArrayList<Object>


You make baby bunny rabbits cry. Declaring an ArrayList this way throws out all of the benefits generics provide.
OneOffDriveByPoster




PostPosted: Wed Jul 19, 2006 7:02 pm   Post subject: (No subject)

r.3volved wrote:
Don't criticize just because you don't understand


I understand that the program does not need an array. Do you--or do you not--understand?
[Gandalf]




PostPosted: Wed Jul 19, 2006 7:18 pm   Post subject: (No subject)

r.3volved, you originally said array, not ArrayList. That said, I believe OneOffDriveByPoster was pointing out your mistake, ie:
You say: Use an array, avoid hard coded values.
He says: Arrays are hard coded.

The general problem, though, is not what I tried to clear up above. It is that you do not need to store the value of each mark in an array. You can instead just add the mark to the total when it is entered and find the average by dividing the total by the amount of marks. Don't complicate things when you don't need to.
OneOffDriveByPoster




PostPosted: Wed Jul 19, 2006 7:22 pm   Post subject: (No subject)

[Gandalf] wrote:
You can instead just add the mark to the total when it is entered and find the average by dividing the total by the amount of marks. Don't complicate things when you don't need to.


Thank you. Now I think this thread can rest in peace. :D
wtd




PostPosted: Wed Jul 19, 2006 7:34 pm   Post subject: (No subject)

[Gandalf] wrote:
The general problem, though, is not what I tried to clear up above. It is that you do not need to store the value of each mark in an array. You can instead just add the mark to the total when it is entered and find the average by dividing the total by the amount of marks.


Let's put it this way...

Do it both ways. They both have educational value.
Sponsor
Sponsor
Sponsor
sponsor
Aziz




PostPosted: Thu Jul 20, 2006 7:47 am   Post subject: (No subject)

wtd wrote:
When you declare a:

code:
ArrayList<Object>


You make baby bunny rabbits cry. Declaring an ArrayList this way throws out all of the benefits generics provide.


Clarification to avoid confusion: I said <Object> as in <PlaceAnyObjectHere>. I didn't use <E> because most likely that would confuse him. In layman's terms, if you want an array of, say, Strings, it would be ArrayList<String>. Okay, that's it then. I'm done with this topic.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 24 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: