I want to find the average score of a LinkedList. My code is

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bowlinggame;

import java.util.LinkedList;
import java.util.TreeSet;

public class BowlingGame {   
    
    public static void main(String[] args) 
    {
        TreeSet <String>PlayerNames = new TreeSet<String>();
        PlayerNames.add("Steve");
        PlayerNames.add("James");
        PlayerNames.add("Bob");

        LinkedList <Integer>SteveScore = new LinkedList<Integer>();
        SteveScore.add(205);
        SteveScore.add(240);//the average of these
        SteveScore.add(189);
        LinkedList <String>SteveDate = new LinkedList<String>();
        SteveDate.add("07/12/2009");
        SteveDate.add("08/01/2009");
        SteveDate.add("02/27/2010");
        
        LinkedList <Integer>JamesScore = new LinkedList<Integer>();
        JamesScore.add(265);
        JamesScore.add(220);The average of these
        JamesScore.add(280);
        LinkedList <String>JamesDate = new LinkedList<String>();
        JamesDate.add("05/24/2009");
        JamesDate.add("08/07/2009");
        JamesDate.add("09/18/2010");
             
        LinkedList <Integer>BobScore = new LinkedList<Integer>();
        BobScore.add(205);
        BobScore.add(230);// the average of these
        BobScore.add(193);
        LinkedList <String>BobDate = new LinkedList<String>();
        BobDate.add("09/12/2009");
        BobDate.add("06/03/2010");
        BobDate.add("02/14/2011");
        
        System.out.println("Steve played " + SteveScore.size() + " games and the" + " last score he got was " + SteveScore.getLast() + " and that was on " + SteveDate.getLast());
        System.out.println("James played " + JamesScore.size() + " games and the" + " last score he got was " + JamesScore.getLast() + " and that was on " + JamesDate.getLast());
        System.out.println("Bob played " + BobScore.size() + " games and the" + " last score he got was " + BobScore.getLast() + " and that was on " + BobDate.getLast());
    }
}

Recommended Answers

All 3 Replies

I assume you have the normal definition for average: the sum / count

What is your problem doing this?

I assume you have the normal definition for average: the sum / count

What is your problem doing this?

Well I can't do it because it is inside a linkedList. How would I call all the elements inside the list. I know it is the sum / count but how would I sum it

You could always use an Iterator to add them up and get the average

float Steveavg = 0;
        ListIterator<Integer> it = SteveScore.listIterator(SteveScore.size());
        while (it.hasPrevious()) 
        {
            float sum = it.previous();
            Steveavg += sum/SteveScore.size();
        }

For the rest you could just change the name

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.