Extending ArrayList of integer

Thread Solved

Join Date: Jul 2009
Posts: 8
Reputation: hell04 is an unknown quantity at this point 
Solved Threads: 0
hell04 hell04 is offline Offline
Newbie Poster

Extending ArrayList of integer

 
0
  #1
Aug 28th, 2009
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Extending ArrayList of integer

 
0
  #2
Aug 29th, 2009
Originally Posted by hell04 View 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.

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]



  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: hell04 is an unknown quantity at this point 
Solved Threads: 0
hell04 hell04 is offline Offline
Newbie Poster

Re: Extending ArrayList of integer

 
0
  #3
Aug 29th, 2009
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;
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Extending ArrayList of integer

 
0
  #4
Aug 29th, 2009
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Extending ArrayList of integer

 
0
  #5
Aug 29th, 2009
Originally Posted by stephen84s View Post
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:

  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).


  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: hell04 is an unknown quantity at this point 
Solved Threads: 0
hell04 hell04 is offline Offline
Newbie Poster

Re: Extending ArrayList of integer

 
0
  #6
Aug 30th, 2009
So VernonDozier should I in my Sum() method replace num with 'this'?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Extending ArrayList of integer

 
0
  #7
Aug 30th, 2009
Originally Posted by hell04 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: hell04 is an unknown quantity at this point 
Solved Threads: 0
hell04 hell04 is offline Offline
Newbie Poster

Re: Extending ArrayList of integer

 
0
  #8
Aug 31st, 2009
Ok after modifying I have the following code;
  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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Extending ArrayList of integer

 
0
  #9
Aug 31st, 2009
Suggest you remove the main(...) in MyArrayList in case you're running that one by mistake?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: hell04 is an unknown quantity at this point 
Solved Threads: 0
hell04 hell04 is offline Offline
Newbie Poster

Re: Extending ArrayList of integer

 
0
  #10
Aug 31st, 2009
Don't worry I worked it out Thanks to everyone for their help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC