Array Lists

How to add functions to Array List

Page 1 of 1

3 Replies - 1152 Views - Last Post: 05 May 2009 - 05:23 PM Rate Topic: -----

#1 suzi1811  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-February 09

Array Lists

Post icon  Posted 05 May 2009 - 07:25 AM

Hi this is the question I'm trying to answer but am having a problem understanding and doing though I have looked at notes I was given and looked up ArrayLists online:-
"Task 3: ArrayLists

Produce a class design using UML and write java code for an instantiable class called Competitors which should contain an ArrayList suitable for managing information about all the contestants and their performance in a Competition.

Task 3.1: Include the following functionality to the Competitors class
Add a contestant
Remove a contestant
Clear the Arraylist
Check to see if the Arraylist is empty (meaning no contestants have been entered in the competition)
Display information from Task 2 for all the contestants in the tournament
Ascertain the overall winner of the tournament based on best performance
Ascertain the contestant with the shortest best performance throw distance of the tournament
Ascertain the average throw distance of the tournament
Ascertain the number of contestants in the tournament
Note:
You will need to include exception handling.
Validate all keyboard input."
The Code I have done so far is as follows:-
import java.util.ArrayList;
import java.util.*;
public class Competitors {
	public static void main(String[] args){
		//declare an array list of type contestant
		// size of array list can change dynamically
		Scanner keyboard = new Scanner(System.in);
		
		ArrayList<Contestant> team = new ArrayList<Contestant>(cap);
		
		Competitors c1 = new Competitors(); //create an instance of the object c1
		Competitors c2 = new Competitors(); //create an instance of the object c2
		
		//add elements to the end of arraylist team
		team.add(c1); //instance of Competitors (c1) added to end of arraylist team
		team.add(c2); //instance of Competitors (c2) added to end of arraylist team
		team.add(new Competitors());
		team.add(new Competitors()); //new instance added to arraylist team
		team.remove();
		team.clear();
		team.contains(all);
				
		int i = 0; // used to show position of arraylist elements
		
		//display contents of arraylist team
		for (Competitors m : team) {//for each Person in the ArrayList team
			System.out.printf("%-4d %-8s %-10s %-3d %n", i, m.getFirstname(), m.getSurname(), m.getDateOfBirth());//print firstname, surname and dob
			i++;//increment position
		}//end for

		//insert new Competitors object at position 0
		team.add(2, new Competitors());
		System.out.println();
		System.out.println();
		//print each Competitors object in ArrayList team
		//this time position not shown
		for (Competitors m : team) {
			System.out.printf("%-8s %-10s %3d %n", m.getFirstname(), m.getSurname(), m.getDateOfBirth())

		Contestant.display();		
	} //end main method
 
} //end public class Competitors


If anyone can help me out I'd really appreciate it.
Thanks Suzan

Is This A Good Question/Topic? 0
  • +

Replies To: Array Lists

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Array Lists

Posted 05 May 2009 - 07:33 AM

and your question is??

This post has been edited by mostyfriedman: 05 May 2009 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: Array Lists

Posted 05 May 2009 - 08:29 AM

Competitors c1 = new Competitors(); //create an instance of the object c1
Competitors c2 = new Competitors(); //create an instance of the object c2


Why are you creating these when all it has is the main you're currently running?

Also:
team.remove();
		team.clear();
		team.contains(all);

Why are you doing this?
removes() removes the last element
then clear() clears the whole array,
and why are you trying to find "all" if it is all empty and what is "all"?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Lists

Posted 05 May 2009 - 05:23 PM

And your ArrayList should be in the class Competitor not in the main() method
The main() method should just be to unit test competitor

class Competitors {

	 ArrayList<Contestant> team; 

	 // Constructor
	Competitors {
	   team = new ArrayList<Contestant>();
	}

	// to add a Contestant
	void add(Contestant x) {
		 team.add(x);
	}
	// display all Contestants
	void list() {
		for(Contestant c : team) {
			 System.out.println(c);
		}
	}

	// now to test the whole stuff
	public static void main(String[] arg) {
		Competitor cop = new Competitors();
		... scanner stuff

		Constestant c = new Contestant(......
		cop.add(c);
		// list the whole thing
		cop.list();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1