954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Extending ArrayList of integer

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 :S

hell04
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
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.

import java.util.ArrayList;

public class Sum extends ArrayList {

ArrayList num = new ArrayList();

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 :S


Code tags:
[code=JAVA]
// paste code here
[/code]

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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;

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 :(

hell04
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
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;

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 ?

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 
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:

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

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/tutorial/java/javaOO/thiskey.html

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

So VernonDozier should I in my Sum() method replace num with 'this'?

hell04
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
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?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Ok after modifying I have the following code;

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!

hell04
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Suggest you remove the main(...) in MyArrayList in case you're running that one by mistake?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Don't worry I worked it out :) Thanks to everyone for their help

hell04
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

You have written your program well and I would just like to revise a little bit. I now this thread was written in 2009 but I will still send this just for the new once looking for an answer similar to your problem.

import java.util.ArrayList;

public class sum {

//I added a main function so that you will be able to run it and see
//results
public static void main(String [] args){

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

// I have replaced int from Integer or double if you want to maintain
// Double
// and i took off the sum class that you made.
// you do not need it because you are not calling it anyways

int subtotal = 0;
for (int d : num) {

subtotal += d;
}
System.out.print(subtotal);
}
}

tiny7415
Light Poster
25 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You