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

Recommended Answers

All 10 Replies

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.

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

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 ?

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

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

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?

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!

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

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

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<Integer> num = new ArrayList<Integer>();
    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);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.