| | |
Extending ArrayList of integer
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 8
Reputation:
Solved Threads: 0
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.
Please Help
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)
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
Last edited by ~s.o.s~; Aug 29th, 2009 at 10:57 am. Reason: Added code tags, please learn to use them.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
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)
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; } } }
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.
•
•
Join Date: Jul 2009
Posts: 8
Reputation:
Solved Threads: 0
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;
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
JAVA Syntax (Toggle Plain Text)
public Integer Sum() { Integer subtotal = 0; for (Integer d : num) { subtotal += d; return subtotal; } }
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
•
•
•
•
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;
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 ?"
"How to ask questions the smart way ?"
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
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)
public class Driver { public static void main(String[] args) { MyArrayList num = new MyArrayList(); num.add(1); num.add(13); num.add(24); num.add(65); num.add(67); num.add(89); int sum = num.Sum (); System.out.println (sum); } }
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)
import java.util.ArrayList; public class MyArrayList extends ArrayList<Integer> { public int Sum() { return 17; } }
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.
•
•
Join Date: Jul 2009
Posts: 8
Reputation:
Solved Threads: 0
Ok after modifying I have the following code;
It compiles but all I get is empty screen!
JAVA Syntax (Toggle Plain Text)
import java.util.ArrayList; public class MyArrayList extends ArrayList<Integer>{ public Integer Sum(){ Integer sum1 = 0; for (Integer d : this) { sum1 += d; } return sum1; } public static void main (String args []){ } } and import java.util.ArrayList; public class Driver{ public void main(String args[]){ MyArrayList num = new MyArrayList(); num.add(1); num.add(13); num.add(24); num.add(65); num.add(67); num.add(89); num.get(1); Integer sum = num.Sum(); System.out.println(sum); } }
It compiles but all I get is empty screen!
![]() |
Similar Threads
- Confuzzled in sorting arraylist... (Java)
- no luck with reading integers from file in arraylist (Java)
- ArrayList<MyClass>, help writing MyClass (Java)
- Arrays and ArrayList (Java)
- Assigning Array Values En Masse (Java)
- using boolean expression on an array (Java)
Other Threads in the Java Forum
- Previous Thread: Applet in browser
- Next Thread: Array help
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update windows working






Thanks to everyone for their help 