943,987 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1976
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 28th, 2009
0

Extending ArrayList of integer

Expand Post »
Hi all,
I have the following task;
Extend an ArrayList of Integer by adding a method called 'sum', that returns the total sum of the elements of the list of integers. Such that each time the method is called sum up all the integers.

So I started by making the following code but it is not working! Any Help Please.
Java Syntax (Toggle Plain Text)
  1. import java.util.ArrayList;
  2.  
  3. public class Sum extends ArrayList<Integer> {
  4.  
  5. ArrayList<Integer> num = new ArrayList<Integer>();
  6.  
  7. num.add(1);
  8. num.add(13);
  9. num.add(24);
  10. num.add(65);
  11. num.add(67);
  12. num.add(89);
  13.  
  14. return (num);
  15.  
  16.  
  17. public Integer Sum() {
  18.  
  19. Double subtotal = 0.0;
  20. for (Double d : num) {
  21. subtotal += d;
  22.  
  23. }
  24.  
  25. }
  26.  
  27. }

Please Help
Last edited by ~s.o.s~; Aug 29th, 2009 at 10:57 am. Reason: Added code tags, please learn to use them.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hell04 is offline Offline
8 posts
since Jul 2009
Aug 29th, 2009
0

Re: Extending ArrayList of integer

Click to Expand / Collapse  Quote originally posted by hell04 ...
Hi all,
I have the following task;
Extend an ArrayList of Integer by adding a method called 'sum', that returns the total sum of the elements of the list of integers. Such that each time the method is called sum up all the integers.

So I started by making the following code but it is not working! Any Help Please.

import java.util.ArrayList;

public class Sum extends ArrayList<Integer> {

ArrayList<Integer> num = new ArrayList<Integer>();

num.add(1);
num.add(13);
num.add(24);
num.add(65);
num.add(67);
num.add(89);

return (num);


public Integer Sum() {

Double subtotal = 0.0;
for (Double d : num) {
subtotal += d;

}

}

}

Please Help

Code tags:


[code=JAVA]
// paste code here
[/code]



JAVA Syntax (Toggle Plain Text)
  1. import java.util.ArrayList;
  2.  
  3. public class Sum extends ArrayList<Integer> {
  4.  
  5. ArrayList<Integer> num = new ArrayList<Integer>();
  6.  
  7. num.add(1);
  8. num.add(13);
  9. num.add(24);
  10. num.add(65);
  11. num.add(67);
  12. num.add(89);
  13.  
  14. return (num);
  15.  
  16.  
  17. public Integer Sum() {
  18.  
  19. Double subtotal = 0.0;
  20. for (Double d : num) {
  21. subtotal += d;
  22.  
  23. }
  24.  
  25. }
  26.  
  27. }

Lines 5 through 14 just appear to be floating around. Are they part of a main function? Part of a constructor? Currently they're not part of anything.

Line 17 - The Sum function returns an Integer, but there's no "return" line, so you need to add one.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 29th, 2009
0

Re: Extending ArrayList of integer

Actually I have no clue of how to fix line 4 to 17 but I have fixed the Sum method and it looks something like;
JAVA Syntax (Toggle Plain Text)
  1. public Integer Sum() {
  2.  
  3. Integer subtotal = 0;
  4. for (Integer d : num) {
  5. subtotal += d;
  6. return subtotal;
  7. }
  8. }

But sum method is saying that it doesn't know what num is! Sorry I am new to this language and have no clue what I am doing
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hell04 is offline Offline
8 posts
since Jul 2009
Aug 29th, 2009
0

Re: Extending ArrayList of integer

Quote ...
Actually I have no clue of how to fix line 4 to 17 but I have fixed the Sum method and it looks something like;
Well I can see how this can be fixed, but I need to know your motive in writing lines 4 to 17, are they for testing your code like in a main as VernonDozier says??
Also why does your sum() method need to return an "Integer" object, would an "int" not be just fine ?
Last edited by stephen84s; Aug 29th, 2009 at 11:22 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Aug 29th, 2009
0

Re: Extending ArrayList of integer

Click to Expand / Collapse  Quote originally posted by stephen84s ...
Well I can see how this can be fixed, but I need to know your motive in writing lines 4 to 17, are they for testing your code like in a main as VernonDozier says??
Also why does your sum() method need to return an "Integer" object, would an "int" not be just fine ?

It's gotta be the Driver/Tester/main function code. Make it obvious and take it all the way out of your class and put it in another class:

JAVA Syntax (Toggle Plain Text)
  1. public class Driver
  2. {
  3. public static void main(String[] args)
  4. {
  5. MyArrayList num = new MyArrayList();
  6.  
  7. num.add(1);
  8. num.add(13);
  9. num.add(24);
  10. num.add(65);
  11. num.add(67);
  12. num.add(89);
  13.  
  14. int sum = num.Sum ();
  15. System.out.println (sum);
  16. }
  17. }

Sum is not a good name for this class. It should be a method in the class. I renamed the class MyArrayList (and I switched from Integer to int, like Stephen mentioned. Switch it back if you like).


JAVA Syntax (Toggle Plain Text)
  1. import java.util.ArrayList;
  2.  
  3. public class MyArrayList extends ArrayList<Integer>
  4. {
  5. public int Sum()
  6. {
  7. return 17;
  8. }
  9. }

It'll run as is, but obviously gives the wrong answer. Write the Sum function so it is correct. Don't use num unless you define it inside the Sum function. There is no connection between num in Driver's main function and what's in the Sum function.

You may want to use the keyword "this" in your function rather than num.

http://java.sun.com/docs/books/tutor...O/thiskey.html
Last edited by VernonDozier; Aug 29th, 2009 at 1:52 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 30th, 2009
0

Re: Extending ArrayList of integer

So VernonDozier should I in my Sum() method replace num with 'this'?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hell04 is offline Offline
8 posts
since Jul 2009
Aug 30th, 2009
0

Re: Extending ArrayList of integer

Click to Expand / Collapse  Quote originally posted by hell04 ...
So VernonDozier should I in my Sum() method replace num with 'this'?
When in doubt, try it out. Does it compile? Does it run? Does it give good results?
Last edited by VernonDozier; Aug 30th, 2009 at 11:20 pm. Reason: Fixed grammar/spelling.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 31st, 2009
0

Re: Extending ArrayList of integer

Ok after modifying I have the following code;
JAVA Syntax (Toggle Plain Text)
  1. import java.util.ArrayList;
  2.  
  3. public class MyArrayList extends ArrayList<Integer>{
  4.  
  5. public Integer Sum(){
  6.  
  7. Integer sum1 = 0;
  8.  
  9. for (Integer d : this) {
  10.  
  11. sum1 += d;
  12. }
  13. return sum1;
  14. }
  15.  
  16. public static void main (String args []){
  17.  
  18. }
  19. }
  20.  
  21. and
  22.  
  23. import java.util.ArrayList;
  24.  
  25. public class Driver{
  26.  
  27.  
  28.  
  29. public void main(String args[]){
  30.  
  31. MyArrayList num = new MyArrayList();
  32.  
  33. num.add(1);
  34. num.add(13);
  35. num.add(24);
  36. num.add(65);
  37. num.add(67);
  38. num.add(89);
  39. num.get(1);
  40. Integer sum = num.Sum();
  41.  
  42. System.out.println(sum);
  43.  
  44. }
  45.  
  46. }

It compiles but all I get is empty screen!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hell04 is offline Offline
8 posts
since Jul 2009
Aug 31st, 2009
0

Re: Extending ArrayList of integer

Suggest you remove the main(...) in MyArrayList in case you're running that one by mistake?
Featured Poster
Reputation Points: 1924
Solved Threads: 952
Posting Expert
JamesCherrill is online now Online
5,790 posts
since Apr 2008
Aug 31st, 2009
0

Re: Extending ArrayList of integer

Don't worry I worked it out Thanks to everyone for their help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hell04 is offline Offline
8 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Eclipse help
Next Thread in Java Forum Timeline: java inventory program help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC