13 Replies - 1509 Views - Last Post: 17 November 2014 - 12:04 AM Rate Topic: -----

#1 Preacher2012   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 07:39 PM

Hello, I apologize in advance for such a long post, I just want to be thorough so you can get an idea of exactly what it is that I am assigned to do. So here is the assignment:

In this lab you will write a program to keep track of Olympic Athletes. Each athlete represents their home country, and may compete in several events in the Olympics. In each event the athlete has a chance to win a gold medal (if the athlete comes in first), a silver medal (if the athlete comes in second), or a bronze medal (if the athlete comes in third).

The program will use an Athlete object to hold the information about an athlete. The Athlete class will contain the following information for each athlete: the athlete’s name, country, event, and their final rank in the event (finished 1st, 2nd, 3rd, 4th, etc...). Athletes who compete in more than one event will have more than one Athlete object. Athlete will need methods to create and print these objects. Athlete will also need to implement the Comparable Interface and have a compareTo method that will be able to compare Athlete objects appropriately.

The class OlympicAthletes will hold the information about all athletes that competed in a given Olympic. An OlympicAthletes object will contain an ordered list of Athlete objects. The Athletes will be ordered by country, and by name within country.

The class OlympicAthletes will need member functions for the following:

• Add an Athlete
• Delete an Athlete (disqualified, failed a drug test.)
• Print all Athletes in the List
• Print a particular athlete: given the name and country of the athlete, print all the entries for that athlete.
• Print an event: given the name of the event, print all athletes that competed in that event.
• Print medal winners by country: given the name of the country, print all the athletes from that country that won medals, including the name of the medal. (Gold, silver, or bronze.)
• Print medal winners by event: given the name of the event, print the medal winners for that event, including the name of the medal. (Gold, silver, or bronze.)
• Print medal winners by medal: given the name of the medal, print all athletes that won that medal.

Your main program will create an OlympicAthletes object for the Summer 2012 games that were held in London or the Winter 2014 games that were held in Sochi. The data for the Athletes will be read from a file. Your file must use the format below with the data for each athlete entered in this order: name, country, event, and rank. Each of these on a separate line. You can assume that the data in the input file is valid, and your program should read until end of file (do not use a sentinel). For example a very small data file would look like:

Matthew Grevers
USA
100M BACKSTROKE MEN
1
Nick Thoman
USA
100M BACKSTROKE MEN
2
Ryosuke Irie
Japan
100M BACKSTROKE MEN
3
Xiaoxia LI
China
TABLE TENNIS, SINGLES WOMEN
1
Ning DING
China
TABLE TENNIS, SINGLES WOMEN
2

You file should have at least 20 athletes. You can obtain Olympic information by going to www.Olympics.com or go to your favorite search engine and perform a search on the Summer 2012 games or the Winter 2014 games.

Your driver (console) program should read the input file and store the data in your OlympicAthletes object. Then main should give the user a menu so they can print all athletes, look up athletes, look up events, look up medal winners (in the 3 ways described above), add athletes and delete athletes. Let the user continue until they want to quit. If the user gives an invalid response to the menu, give an error message and continue.

Once you get this application working then use Java serialization to save the ordered list of Athlete objects for later use, possibly as input to another run of your application.

You can use the Golf application on pages 418 – 421 in your text and the Song List application on pages 451 – 458 in your text as references when designing and writing your application.
You will be developing three classes in addition to using one of the ordered list classes presented in the text. The three classes are Athlete, OlympicAthletes, and your driver (console) class.
I have also attached the assignment sheet, in case this means to much scrolling lol.
Here is what I have so far:
ListInterface.java
//----------------------------------------------------------------------------
// ListInterface.java            by Dale/Joyce/Weems                 Chapter 6
//
// The lists are unbounded and allow duplicate elements, but do not allow
// null elements. As a general precondition, null elements are not passed as
// arguments to any of the methods.
//
// The list has a special property called the current position - the position
// of the next element to be accessed by getNext during an iteration through
// the list. Only reset and getNext affect the current position.
//----------------------------------------------------------------------------
public interface ListInterface<T>
{
  int size();
  // Returns the number of elements on this list.

  void add(T element);
  // Adds element to this list.

  boolean remove (T element);
  // Removes an element e from this list such that e.equals(element)
  // and returns true; if no such element exists, returns false.

  boolean contains (T element);
  // Returns true if this list contains an element e such that
  // e.equals(element); otherwise, returns false.

  T get(T element);
  // Returns an element e from this list such that e.equals(element);
  // if no such element exists, returns null.

  String toString();
  // Returns a nicely formatted string that represents this list.

  void reset();
  // Initializes current position for an iteration through this list,
  // to the first element on this list.

  T getNext();
  // Preconditions: The list is not empty
  //                The list has been reset
  //                The list has not been modified since the most recent reset
  //
  // Returns the element at the current position on this list.
  // If the current position is the last element, then it advances the value
  // of the current position to the first element; otherwise, it advances
  // the value of the current position to the next element.
}


ArrayUnsortedList.java
//----------------------------------------------------------------------------
// ArrayUnsortedList.java         by Dale/Joyce/Weems                Chapter 6
//
// Implements the ListInterface using an array.
//
// Null elements are not permitted on a list.
//
// Two constructors are provided: one that creates a list of a default
// original capacity, and one that allows the calling program to specify the
// original capacity.
//----------------------------------------------------------------------------
public class ArrayUnsortedList<T> implements ListInterface<T>
{
  protected final int DEFCAP = 100; // default capacity
  protected int origCap;            // original capacity
  protected T[] list;               // array to hold this list’s elements
  protected int numElements = 0;    // number of elements in this list
  protected int currentPos;         // current position for iteration

  // set by find method
  protected boolean found;  // true if element found, otherwise false
  protected int location;   // indicates location of element if found

  public ArrayUnsortedList()
  {
    list = (T[]) new Object[DEFCAP];
    origCap = DEFCAP;
  }

  public ArrayUnsortedList(int origCap)
  {
    list = (T[]) new Object[origCap];
    this.origCap = origCap;
  }

  protected void enlarge()
  // Increments the capacity of the list by an amount
  // equal to the original capacity.
  {
    // Create the larger array.
    T[] larger = (T[]) new Object[list.length + origCap];

    // Copy the contents from the smaller array into the larger array.
    for (int i = 0; i < numElements; i++)
    {
      larger[i] = list[i];
    }

    // Reassign list reference.
    list = larger;
  }

  protected void find(T target)
  // Searches list for an occurence of an element e such that
  // e.equals(target). If successful, sets instance variables
  // found to true and location to the array index of e. If
  // not successful, sets found to false.
  {
    location = 0;
    found = false;

    while (location < numElements)
    {
      if (list[location].equals(target))
      {
        found = true;
        return;
      }
      else
        location++;
    }
  }

  public void add(T element)
  // Adds element to this list.
  {
    if (numElements == list.length)
      enlarge();
    list[numElements] = element;
    numElements++;
  }

  public boolean remove (T element)
  // Removes an element e from this list such that e.equals(element)
  // and returns true; if no such element exists, returns false.
  {
    find(element);
    if (found)
    {
      list[location] = list[numElements - 1];
      list[numElements - 1] = null;
      numElements--;
    }
    return found;
  }

  public int size()
  // Returns the number of elements on this list.
  {
    return numElements;
  }

  public boolean contains (T element)
  // Returns true if this list contains an element e such that
  // e.equals(element); otherwise, returns false.
  {
    find(element);
    return found;
  }

  public T get(T element)
  // Returns an element e from this list such that e.equals(element);
  // if no such element exists, returns null.
  {
    find(element);
    if (found)
      return list[location];
    else
      return null;
  }

  public String toString()
  // Returns a nicely formatted string that represents this list.
  {
    String listString = "";
    for (int i = 0; i < numElements; i++)
      listString = listString + "" + list[i] + "\n";
    return listString;
  }

  public void reset()
  // Initializes current position for an iteration through this list,
  // to the first element on this list.
  {
    currentPos  = 0;
  }

  public T getNext()
  // Preconditions: The list is not empty
  //                The list has been reset
  //                The list has not been modified since the most recent reset
  //
  // Returns the element at the current position on this list.
  // If the current position is the last element, it advances the value
  // of the current position to the first element; otherwise, it advances
  // the value of the current position to the next element.
  {
    T next = list[currentPos];
    if (currentPos == (numElements - 1))
      currentPos = 0;
    else
      currentPos++;
    return next;
  }
}


ArraySortedList3.java
//----------------------------------------------------------------------------
// ArraySortedList3.java          by Dale/Joyce/Weems                Chapter 6
//
// Implements the ListInterface using an array. It is kept in increasing order
// as defined by the compareTo method of the added elements. Only Comparable 
// elements may be added to a list.
//
// Null elements are not permitted on a list.
//
// Two constructors are provided: one that creates a list of a default
// original capacity, and one that allows the calling program to specify the 
// original capacity.
//----------------------------------------------------------------------------
public class ArraySortedList3<T> extends ArrayUnsortedList<T> 
                                 implements ListInterface<T>
{

  public ArraySortedList3() 
  {
    super();
  }

  public ArraySortedList3(int origCap) 
  {
    super(origCap);
  }

  protected void recFind(Comparable target, int fromLocation, int toLocation)
  // Searches list between fromLocation and toLocation
  // for an occurrence of an element e such that
  // target.equals(e). If successful, sets instance variables
  // found to true and location to the array index of e. If
  // not successful, sets found to false.
  {
    if (fromLocation > toLocation)          // Base case 1
      found = false;
    else
    {
      int compareResult;
      location = (fromLocation + toLocation) / 2;
      compareResult = target.compareTo(list[location]);

      if (compareResult == 0)              // Base case 2
        found = true;
      else if (compareResult < 0)      
        // target is less than element at location
        recFind (target, fromLocation, location - 1);
      else                           
        // target is greater than element at location
        recFind (target, location + 1, toLocation);
    }
  }
  
  protected void find(T target)
  // Searches list for an occurrence of an element e such that
  // target.equals(e). If successful, sets instance variables
  // found to true and location to the array index of e. If
  // not successful, sets found to false.
  {
    Comparable targetElement = (Comparable)target;
    found = false;
    recFind(targetElement, 0, numElements - 1);
  }

 public void add(T element)
  // Precondition:  element is Comparable.
  //  
  // Adds element to this list.
  {
    T listElement;      
    int location = 0;
 
    if (numElements == list.length)
      enlarge();

    while (location < numElements)
    {
      listElement = (T)list[location];
      if (((Comparable)listElement).compareTo(element) < 0)  // list element < add element
        location++;
      else
        break;   // list element >= add element
    }
    
    for (int index = numElements; index > location; index--)
      list[index] = list[index - 1];

    list[location] = element;
    numElements++;
  }

  public boolean remove (T element)
  // Removes an element e from this list such that e.equals(element)
  // and returns true; if no such element exists, returns false.
  {
    find(element);    
    if (found)
    {
      for (int i = location; i <= numElements - 2; i++)
        list[i] = list[i+1];
      list[numElements - 1] = null;
      numElements--;  
    }
    return found;
  }
}


Athlete.java
public class Athlete implements Comparable<Athlete>
{
	//variables needed
	protected String name;
	protected String country;
	protected String event;
	protected int rank;

	//default constructor
	public Athlete()
	{
		name = "";
		country = "";
		event = "";
		rank = 0;
	}

	//overloaded constructor
	public Athlete(String name, String country, String event, int rank)
	{
		this.name = name;
		this.country = country;
		this.event = event;
		this.rank = rank;
	}

	//setName method
	public void setName(String name)
	{
		this.name = name;
	}

	//getName method
	public String getName()
	{
		return name;
	}

	//setCountry method
	public void setCountry(String country)
	{
		this.country = country;
	}

	//getCountry method
	public String getCountry()
	{
		return country;
	}

	//setEvent method
	public void setEvent(String event)
	{
		this.event = event;
	}

	//getEvent method
	public String getEvent()
	{
		return event;
	}

	//setRank method
	public void setRank(int rank)
	{
		this.rank = rank;
	}

	//getRank method
	public int getRank()
	{
		return rank;
	}

	//compareTo method
	public int compareTo(Athlete other)
	{
		if(this.rank > other.rank)
			return -1;
		else
			if(this.rank == other.rank)
				return 0;
			else
				return +1;
	}

	//toString method
	public String toString()
	{
		String str = "";

		str += "Name: " + name + "\nCountry: " + country + "\nEvent: " + event + "\nRank: " + rank + "\n";

		return str;
	}
}


OlympicAthletes.java
import java.util.*;

public class OlympicAthletes<Athlete>
{
	//create list to store athletes
	ListInterface<Athlete> Athletes = new ArraySortedList3<Athlete>();

	//addAthlete method
	public void addAthlete(Athlete newAthlete)
	{
		Athletes.add(newAthlete);
	}

	//deleteAthlete method
	public void deleteAthlete(Athlete delAthlete)
	{
		if(Athletes.contains(delAthlete))
		{
			Athletes.remove(delAthlete);
		}
	}

	//printParticularAthlete given game and country of the athlete
	public String printParticularAthlete(String name, String country)
	{
		String str = "";
		for(int i = 0; i < Athletes.size(); i++)
		{
			if(Athletes.contains((Athlete)name))
			{
				str += "true";
			}
		}
		return str;
	}
}


I'm having trouble with the printParticularAthlete method in OlympicAthletes. I keep getting this error:
Exception in thread "main" java.lang.ClassCastException: Athlete cannot be cast
to java.lang.String
at java.lang.String.compareTo(String.java:108)
at ArraySortedList3.recFind(ArraySortedList3.java:41)
at ArraySortedList3.find(ArraySortedList3.java:62)
at ArrayUnsortedList.contains(ArrayUnsortedList.java:107)
at OlympicAthletes.printParticularAthlete(OlympicAthletes.java:29)
at TestOlyAths.main(TestOlyAths.java:33)

How can I fix this method to fix that error? Thank you in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Olympic athletes classes, having trouble with some methods

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12333
  • View blog
  • Posts: 45,441
  • Joined: 27-December 08

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 07:42 PM

This is your problem:
if(Athletes.contains((Athlete)name))



Note that name is a String, not an Athlete. You will have to loop through the List and check each Athlete's name.
Was This Post Helpful? 0
  • +
  • -

#3 Preacher2012   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 08:01 PM

How do loop through like that?
I tried this:
public Athlete printParticularAthlete(String name, String country)
	{
		Athlete tempAth;
		for(int i = 0; i < Athletes.size(); i++)
		{
			tempAth = Athletes[i];
			if(tempAth.getName() == name && tempAth.getCountry() == country)
			{
				return tempAth;
			}
			else
				tempAth = Athletes.getNext();
		}
	}


and I get these errors:

F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:29: error: array required, but ListInterface<Athlete> found
tempAth = Athletes[i];
^
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:30: error: cannot find symbol
if(tempAth.getName() == name && tempAth.getCountry() == country)
^
symbol: method getName()
location: variable tempAth of type Athlete
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:30: error: cannot find symbol
if(tempAth.getName() == name && tempAth.getCountry() == country)
^
symbol: method getCountry()
location: variable tempAth of type Athlete
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
3 errors

Tool completed with exit code 1

This post has been edited by Preacher2012: 16 November 2014 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12333
  • View blog
  • Posts: 45,441
  • Joined: 27-December 08

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 08:06 PM

Quote

F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:29: error: array required, but ListInterface<Athlete> found
tempAth = Athletes[i];
^

This isn't an array. It looks like there is a getNext() method as part of the List interface. I'd use that.

Quote

F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:30: error: cannot find symbol
if(tempAth.getName() == name && tempAth.getCountry() == country)
^

Are you sure tempAth is an Athlete? Also, don't compare Strings using the == operator. It compares their locations in memory. Use the String equals() method instead.
Was This Post Helpful? 0
  • +
  • -

#5 Preacher2012   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 08:18 PM

I edited the class:
public class OlympicAthletes
{
	//create list to store athletes
	ListInterface Athletes = new ArraySortedList3();

	//addAthlete method
	public void addAthlete(Athlete newAthlete)
	{
		Athletes.add(newAthlete);
	}

	//deleteAthlete method
	public void deleteAthlete(Athlete delAthlete)
	{
		if(Athletes.contains(delAthlete))
		{
			Athletes.remove(delAthlete);
		}
	}

	//printParticularAthlete given game and country of the athlete
	public Athlete printParticularAthlete(String name, String country)
	{
		Athlete tempAth = new Athlete();
		for(int i = 0; i < Athletes.size(); i++)
		{
			tempAth = (Athlete)Athletes[i];
			if(tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
			{
				return tempAth;
			}
			else
				tempAth = (Athlete)Athletes.getNext();
		}
	}
}


and now I get this:
F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:29: error: array required, but ListInterface found
tempAth = (Athlete)Athletes[i];
^
Note: F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Tool completed with exit code 1
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12333
  • View blog
  • Posts: 45,441
  • Joined: 27-December 08

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 08:20 PM

Quote

F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:29: error: array required, but ListInterface found
tempAth = (Athlete)Athletes[i];

Already explained this.

Quote

Note: F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

You are getting this warning because you removed the generics. The generics were there for a reason- a good reason. I don't see why you removed them.

Rather than throwing code at this or randomly changing things, think about what you are doing and take a good look at the tools you have.
Was This Post Helpful? 0
  • +
  • -

#7 Preacher2012   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 08:33 PM

ok I fixed the tempAth = (Athlete)Athletes[i]; and added the generics back Heres the method:
	//printParticularAthlete given game and country of the athlete
	public Athlete printParticularAthlete(String name, String country)
	{
		Athlete tempAth;
		for(int i = 0; i < Athletes.size(); i++)
		{
			tempAth = Athletes.getNext();
			if(tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
			{
				return tempAth;
			}
			else
				tempAth = (Athlete)Athletes.getNext();
		}
	}


and now I get these two errors:
F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:30: error: cannot find symbol
if(tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
^
symbol: method getName()
location: variable tempAth of type Athlete
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:30: error: cannot find symbol
if(tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
^
symbol: method getCountry()
location: variable tempAth of type Athlete
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
2 errors

Tool completed with exit code 1

tempAth an object of the class athlete so shouldnt it be able to get its name and country?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12333
  • View blog
  • Posts: 45,441
  • Joined: 27-December 08

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 08:44 PM

I took the time to compile your code: http://ideone.com/wJpdyo

I did not encounter the errors you did. So I think this means you are using a different Athlete class than the one provided in your OP.
Was This Post Helpful? 0
  • +
  • -

#9 Preacher2012   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 08:54 PM

I didnt change the athlete class. Its the same one in the original post:
public class Athlete implements Comparable<Athlete>
{
	//variables needed
	protected String name;
	protected String country;
	protected String event;
	protected int rank;

	//default constructor
	public Athlete()
	{
		name = "";
		country = "";
		event = "";
		rank = 0;
	}

	//overloaded constructor
	public Athlete(String name, String country, String event, int rank)
	{
		this.name = name;
		this.country = country;
		this.event = event;
		this.rank = rank;
	}

	//setName method
	public void setName(String name)
	{
		this.name = name;
	}

	//getName method
	public String getName()
	{
		return name;
	}

	//setCountry method
	public void setCountry(String country)
	{
		this.country = country;
	}

	//getCountry method
	public String getCountry()
	{
		return country;
	}

	//setEvent method
	public void setEvent(String event)
	{
		this.event = event;
	}

	//getEvent method
	public String getEvent()
	{
		return event;
	}

	//setRank method
	public void setRank(int rank)
	{
		this.rank = rank;
	}

	//getRank method
	public int getRank()
	{
		return rank;
	}

	//compareTo method
	public int compareTo(Athlete other)
	{
		if(this.rank > other.rank)
			return -1;
		else
			if(this.rank == other.rank)
				return 0;
			else
				return +1;
	}

	//toString method
	public String toString()
	{
		String str = "";

		str += "Name: " + name + "\nCountry: " + country + "\nEvent: " + event + "\nRank: " + rank + "\n";

		return str;
	}
}


and this is the OlympicAthletes that I am currently trying to fix:
import java.util.*;

public class OlympicAthletes<Athlete>
{
	//create list to store athletes
	ListInterface<Athlete> Athletes = new ArraySortedList3<Athlete>();

	//addAthlete method
	public void addAthlete(Athlete newAthlete)
	{
		Athletes.add(newAthlete);
	}

	//deleteAthlete method
	public void deleteAthlete(Athlete delAthlete)
	{
		if(Athletes.contains(delAthlete))
		{
			Athletes.remove(delAthlete);
		}
	}

	//printParticularAthlete given game and country of the athlete
	public Athlete printParticularAthlete(String name, String country)
	{
		Athlete tempAth;
		for(int i = 0; i < Athletes.size(); i++)
		{
			tempAth = Athletes.getNext();
			if(tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
			{
				return tempAth;
			}
			else
				tempAth = (Athlete)Athletes.getNext();
		}
	}


with those two errors:
F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:30: error: cannot find symbol
if(tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
^
symbol: method getName()
location: variable tempAth of type Athlete
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
F:\CSC236FlemingJava\Labs\Lab5\OlympicAthletes.java:30: error: cannot find symbol
if(tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
^
symbol: method getCountry()
location: variable tempAth of type Athlete
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
2 errors

Tool completed with exit code 1

I want the printParticularAthlete to print the athlete if the name and country match.
I don't know why I'm getting those errors, I've been stuck on that for a while.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12333
  • View blog
  • Posts: 45,441
  • Joined: 27-December 08

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 08:56 PM

Quote

I don't know why I'm getting those errors, I've been stuck on that for a while.

Like I said (and showed you)- I compiled your code and didn't encounter those errors, so you clearly aren't using the classes you think you are.

It might be worth starting a new project on your IDE and plug the classes in appropriately.
Was This Post Helpful? 0
  • +
  • -

#11 Preacher2012   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 09:27 PM

I made a new folder, I copied the code for all the classes and pasted it to create whole new .java files in the new folder. I also made a testAthlete class to test that the Athlete class in the new folder was being used and it compiles fine. Here are the classes in the new folder:
ListInterface.java
//----------------------------------------------------------------------------
// ListInterface.java            by Dale/Joyce/Weems                 Chapter 6
//
// The lists are unbounded and allow duplicate elements, but do not allow
// null elements. As a general precondition, null elements are not passed as
// arguments to any of the methods.
//
// The list has a special property called the current position - the position
// of the next element to be accessed by getNext during an iteration through
// the list. Only reset and getNext affect the current position.
//----------------------------------------------------------------------------
public interface ListInterface<T>
{
  int size();
  // Returns the number of elements on this list.

  void add(T element);
  // Adds element to this list.

  boolean remove (T element);
  // Removes an element e from this list such that e.equals(element)
  // and returns true; if no such element exists, returns false.

  boolean contains (T element);
  // Returns true if this list contains an element e such that
  // e.equals(element); otherwise, returns false.

  T get(T element);
  // Returns an element e from this list such that e.equals(element);
  // if no such element exists, returns null.

  String toString();
  // Returns a nicely formatted string that represents this list.

  void reset();
  // Initializes current position for an iteration through this list,
  // to the first element on this list.

  T getNext();
  // Preconditions: The list is not empty
  //                The list has been reset
  //                The list has not been modified since the most recent reset
  //
  // Returns the element at the current position on this list.
  // If the current position is the last element, then it advances the value
  // of the current position to the first element; otherwise, it advances
  // the value of the current position to the next element.
}


ArrayUnsortedList.java
//----------------------------------------------------------------------------
// ArrayUnsortedList.java         by Dale/Joyce/Weems                Chapter 6
//
// Implements the ListInterface using an array.
//
// Null elements are not permitted on a list.
//
// Two constructors are provided: one that creates a list of a default
// original capacity, and one that allows the calling program to specify the
// original capacity.
//----------------------------------------------------------------------------
public class ArrayUnsortedList<T> implements ListInterface<T>
{
  protected final int DEFCAP = 100; // default capacity
  protected int origCap;            // original capacity
  protected T[] list;               // array to hold this list’s elements
  protected int numElements = 0;    // number of elements in this list
  protected int currentPos;         // current position for iteration

  // set by find method
  protected boolean found;  // true if element found, otherwise false
  protected int location;   // indicates location of element if found

  public ArrayUnsortedList()
  {
    list = (T[]) new Object[DEFCAP];
    origCap = DEFCAP;
  }

  public ArrayUnsortedList(int origCap)
  {
    list = (T[]) new Object[origCap];
    this.origCap = origCap;
  }

  protected void enlarge()
  // Increments the capacity of the list by an amount
  // equal to the original capacity.
  {
    // Create the larger array.
    T[] larger = (T[]) new Object[list.length + origCap];

    // Copy the contents from the smaller array into the larger array.
    for (int i = 0; i < numElements; i++)
    {
      larger[i] = list[i];
    }

    // Reassign list reference.
    list = larger;
  }

  protected void find(T target)
  // Searches list for an occurence of an element e such that
  // e.equals(target). If successful, sets instance variables
  // found to true and location to the array index of e. If
  // not successful, sets found to false.
  {
    location = 0;
    found = false;

    while (location < numElements)
    {
      if (list[location].equals(target))
      {
        found = true;
        return;
      }
      else
        location++;
    }
  }

  public void add(T element)
  // Adds element to this list.
  {
    if (numElements == list.length)
      enlarge();
    list[numElements] = element;
    numElements++;
  }

  public boolean remove (T element)
  // Removes an element e from this list such that e.equals(element)
  // and returns true; if no such element exists, returns false.
  {
    find(element);
    if (found)
    {
      list[location] = list[numElements - 1];
      list[numElements - 1] = null;
      numElements--;
    }
    return found;
  }

  public int size()
  // Returns the number of elements on this list.
  {
    return numElements;
  }

  public boolean contains (T element)
  // Returns true if this list contains an element e such that
  // e.equals(element); otherwise, returns false.
  {
    find(element);
    return found;
  }

  public T get(T element)
  // Returns an element e from this list such that e.equals(element);
  // if no such element exists, returns null.
  {
    find(element);
    if (found)
      return list[location];
    else
      return null;
  }

  public String toString()
  // Returns a nicely formatted string that represents this list.
  {
    String listString = "";
    for (int i = 0; i < numElements; i++)
      listString = listString + "" + list[i] + "\n";
    return listString;
  }

  public void reset()
  // Initializes current position for an iteration through this list,
  // to the first element on this list.
  {
    currentPos  = 0;
  }

  public T getNext()
  // Preconditions: The list is not empty
  //                The list has been reset
  //                The list has not been modified since the most recent reset
  //
  // Returns the element at the current position on this list.
  // If the current position is the last element, it advances the value
  // of the current position to the first element; otherwise, it advances
  // the value of the current position to the next element.
  {
    T next = list[currentPos];
    if (currentPos == (numElements - 1))
      currentPos = 0;
    else
      currentPos++;
    return next;
  }
}


ArraySortedList3.java
//----------------------------------------------------------------------------
// ArraySortedList3.java          by Dale/Joyce/Weems                Chapter 6
//
// Implements the ListInterface using an array. It is kept in increasing order
// as defined by the compareTo method of the added elements. Only Comparable 
// elements may be added to a list.
//
// Null elements are not permitted on a list.
//
// Two constructors are provided: one that creates a list of a default
// original capacity, and one that allows the calling program to specify the 
// original capacity.
//----------------------------------------------------------------------------
public class ArraySortedList3<T> extends ArrayUnsortedList<T> 
                                 implements ListInterface<T>
{

  public ArraySortedList3() 
  {
    super();
  }

  public ArraySortedList3(int origCap) 
  {
    super(origCap);
  }

  protected void recFind(Comparable target, int fromLocation, int toLocation)
  // Searches list between fromLocation and toLocation
  // for an occurrence of an element e such that
  // target.equals(e). If successful, sets instance variables
  // found to true and location to the array index of e. If
  // not successful, sets found to false.
  {
    if (fromLocation > toLocation)          // Base case 1
      found = false;
    else
    {
      int compareResult;
      location = (fromLocation + toLocation) / 2;
      compareResult = target.compareTo(list[location]);

      if (compareResult == 0)              // Base case 2
        found = true;
      else if (compareResult < 0)      
        // target is less than element at location
        recFind (target, fromLocation, location - 1);
      else                           
        // target is greater than element at location
        recFind (target, location + 1, toLocation);
    }
  }
  
  protected void find(T target)
  // Searches list for an occurrence of an element e such that
  // target.equals(e). If successful, sets instance variables
  // found to true and location to the array index of e. If
  // not successful, sets found to false.
  {
    Comparable targetElement = (Comparable)target;
    found = false;
    recFind(targetElement, 0, numElements - 1);
  }

 public void add(T element)
  // Precondition:  element is Comparable.
  //  
  // Adds element to this list.
  {
    T listElement;      
    int location = 0;
 
    if (numElements == list.length)
      enlarge();

    while (location < numElements)
    {
      listElement = (T)list[location];
      if (((Comparable)listElement).compareTo(element) < 0)  // list element < add element
        location++;
      else
        break;   // list element >= add element
    }
    
    for (int index = numElements; index > location; index--)
      list[index] = list[index - 1];

    list[location] = element;
    numElements++;
  }

  public boolean remove (T element)
  // Removes an element e from this list such that e.equals(element)
  // and returns true; if no such element exists, returns false.
  {
    find(element);    
    if (found)
    {
      for (int i = location; i <= numElements - 2; i++)
        list[i] = list[i+1];
      list[numElements - 1] = null;
      numElements--;  
    }
    return found;
  }
}


Athlete.java (In the new folder with the rest of these in the same folder)
public class Athlete implements Comparable<Athlete>
{
	//variables needed
	protected String name;
	protected String country;
	protected String event;
	protected int rank;

	//default constructor
	public Athlete()
	{
		name = "";
		country = "";
		event = "";
		rank = 0;
	}

	//overloaded constructor
	public Athlete(String name, String country, String event, int rank)
	{
		this.name = name;
		this.country = country;
		this.event = event;
		this.rank = rank;
	}

	//setName method
	public void setName(String name)
	{
		this.name = name;
	}

	//getName method
	public String getName()
	{
		return name;
	}

	//setCountry method
	public void setCountry(String country)
	{
		this.country = country;
	}

	//getCountry method
	public String getCountry()
	{
		return country;
	}

	//setEvent method
	public void setEvent(String event)
	{
		this.event = event;
	}

	//getEvent method
	public String getEvent()
	{
		return event;
	}

	//setRank method
	public void setRank(int rank)
	{
		this.rank = rank;
	}

	//getRank method
	public int getRank()
	{
		return rank;
	}

	//compareTo method
	public int compareTo(Athlete other)
	{
		if(this.rank > other.rank)
			return -1;
		else
			if(this.rank == other.rank)
				return 0;
			else
				return +1;
	}

	//toString method
	public String toString()
	{
		String str = "";

		str += "Name: " + name + "\nCountry: " + country + "\nEvent: " + event + "\nRank: " + rank + "\n";

		return str;
	}
}


OlympicAthletes.java (in the same new folder!)
public class OlympicAthletes<Athlete>
{
	//create list to store athletes
	ListInterface<Athlete> Athletes = new ArraySortedList3<Athlete>();

	//addAthlete method
	public void addAthlete(Athlete newAthlete)
	{
		Athletes.add(newAthlete);
	}

	//deleteAthlete method
	public void deleteAthlete(Athlete delAthlete)
	{
		if(Athletes.contains(delAthlete))
		{
			Athletes.remove(delAthlete);
		}
	}

	//printParticularAthlete given game and country of the athlete
	public Athlete printParticularAthlete(String name, String country)
	{
		Athlete tempAth;
		for(int i = 0; i < Athletes.size(); i++)
		{
			tempAth = (Athlete)Athletes.getNext();
			if((Athlete)tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
			{
				return tempAth;
			}
			else
				tempAth = (Athlete)Athletes.getNext();
		}
	}
}


TestAthlete.java (again, same new folder and it compiles and runs.)
public class TestAthlete
{
	public static void main(String[] args)
	{
		//create athlete objects
		Athlete kevin = new Athlete("Kevin Patel", "USA", "Swimming", 3);

		//Athlete john = new Athlete("John Wick", "USA", "Swimming", 1);
		Athlete john = new Athlete();
		john.setName("John Wick");
		john.setCountry("USA");
		john.setEvent("Swimming");
		john.setRank(1);

		Athlete michael = new Athlete("Michael Phelps", "USA", "Swimming", 2);

		//Athlete roger = new Athlete("Roger Miller", "USA", "Swimming", 2);
		Athlete roger = new Athlete();
		roger.setName("Roger Miller");
		roger.setCountry("USA");
		roger.setEvent("Swimming");
		roger.setRank(2);

		System.out.println(kevin.toString());
		System.out.println("Kevin vs John: " + kevin.compareTo(john));
		System.out.println("Kevin vs Michael: " + kevin.compareTo(michael));
		System.out.println("Kevin vs Roger: " + kevin.compareTo(roger) + "\n");


		System.out.println(john.toString());
		System.out.println("John vs Kevin: " + john.compareTo(kevin));
		System.out.println("John vs Michael: " + john.compareTo(michael));
		System.out.println("John vs Roger: " + john.compareTo(roger) + "\n");

		System.out.println(michael.toString());
		System.out.println("Michael vs Kevin: " + michael.compareTo(kevin));
		System.out.println("Michael vs John: " + michael.compareTo(john));
		System.out.println("Michael vs Roger: " + michael.compareTo(roger) + "\n");

		System.out.println(roger.toString());
		System.out.println("Roger vs Kevin: " + roger.compareTo(kevin));
		System.out.println("Roger vs John: " + roger.compareTo(john));
		System.out.println("Roger vs Michael: " + roger.compareTo(michael) + "\n");
	}
}


I also made one to test OlympicAthletes called TestOlyAths.java
TestOlyAths.java
public class TestOlyAths
{
	public static void main(String[] args)
	{
		//create athlete objects
		Athlete kevin = new Athlete("Kevin Patel", "USA", "Swimming", 3);

		//Athlete john = new Athlete("John Wick", "USA", "Swimming", 1);
		Athlete john = new Athlete();
		john.setName("John Wick");
		john.setCountry("USA");
		john.setEvent("Swimming");
		john.setRank(1);

		Athlete michael = new Athlete("Michael Phelps", "USA", "Swimming", 2);

		//Athlete roger = new Athlete("Roger Miller", "USA", "Swimming", 2);
		Athlete roger = new Athlete();
		roger.setName("Roger Miller");
		roger.setCountry("USA");
		roger.setEvent("Swimming");
		roger.setRank(2);

		OlympicAthletes allAthletes = new OlympicAthletes();
		allAthletes.addAthlete(kevin);
		allAthletes.addAthlete(john);
		allAthletes.addAthlete(michael);
		allAthletes.addAthlete(roger);
		System.out.println(allAthletes.toString());
		allAthletes.deleteAthlete(john);
		System.out.println(allAthletes.printParticularAthlete("john", "USA"));
	}
}


OK so, TestAthlete.java compiles and runs without any errors showing but, When I compile either OlympicAthletes.java or TestOlyAths.java, they both show these two errors:

F:\CSC236FlemingJava\Labs\Lab5Olympics\OlympicAthletes.java:28: error: cannot find symbol
if((Athlete)tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
^
symbol: method getName()
location: variable tempAth of type Athlete
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
F:\CSC236FlemingJava\Labs\Lab5Olympics\OlympicAthletes.java:28: error: cannot find symbol
if((Athlete)tempAth.getName().equals(name) && tempAth.getCountry().equals(country))
^
symbol: method getCountry()
location: variable tempAth of type Athlete
where Athlete is a type-variable:
Athlete extends Object declared in class OlympicAthletes
2 errors

Tool completed with exit code 1

I did what you suggested, but I still don't know why I'm getting those two errors, I'm really confused now.
The error is definitely in the printParticularAthlete method, because when comment out that method, the class compiled without any errors showing, Its the method itself causing the errors, I'm still trying to figure out why

This post has been edited by Preacher2012: 16 November 2014 - 09:38 PM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12333
  • View blog
  • Posts: 45,441
  • Joined: 27-December 08

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 10:25 PM

I went ahead and set up your code. The issue is that in your class OlympicAthlete<Athlete>, it is reading the Athlete as a generic type holder, rather than your class Athlete. If you get rid of the generic declaration in your class definition for OlympicAthlete: (so change class OlympicAthlete<Athlete> to class OlympicAthlete).


And remove the cast here:
if((Athlete)tempAth.getName().equals(name) && tempAth.getCountry().equals(country))



You should be good to go, minus a couple of errors I think you should be able to correct.
Was This Post Helpful? 0
  • +
  • -

#13 Preacher2012   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Olympic athletes classes, having trouble with some methods

Posted 16 November 2014 - 11:09 PM

That worked! Now I have to print all of the athletes that competed in an event. Heres the printAnEvent method:
	//printAnEvent method, given name of the, prints all athletes that competed in the event
	public Athlete printAnEvent(String event)
	{
		Athlete tempAth = new Athlete();
		for(int i = 0; i < Athletes.size(); i++)
		{
			tempAth = (Athlete)Athletes.getNext();
			if(tempAth.getEvent().equals(event))
			{
				return tempAth;
			}
			tempAth = (Athlete)Athletes.getNext();
		}
		return tempAth;
	}


Heres the testOlyAths.java where I'm testing this method:
public class TestOlyAths
{
	public static void main(String[] args)
	{
		//create athlete objects
		Athlete kevin = new Athlete("Kevin Patel", "USA", "Swimming", 3);

		//Athlete john = new Athlete("John Wick", "USA", "Swimming", 1);
		Athlete john = new Athlete();
		john.setName("John Wick");
		john.setCountry("USA");
		john.setEvent("Swimming");
		john.setRank(1);

		Athlete michael = new Athlete("Michael Phelps", "USA", "Swimming", 2);

		//Athlete roger = new Athlete("Roger Miller", "USA", "Swimming", 2);
		Athlete roger = new Athlete();
		roger.setName("Roger Miller");
		roger.setCountry("USA");
		roger.setEvent("Martial arts");
		roger.setRank(2);

		OlympicAthletes allAthletes = new OlympicAthletes();
		allAthletes.addAthlete(kevin);
		allAthletes.addAthlete(john);
		allAthletes.addAthlete(michael);
		allAthletes.addAthlete(roger);
		//System.out.println(allAthletes.printAllAthletes());
		//allAthletes.deleteAthlete(john);
		//System.out.println(allAthletes.printParticularAthlete("john", "USA"));
		System.out.println(allAthletes.printAnEvent("Swimming"));
	}
}


As you can see, I have 3 athletes competing in swimming, yet when I run this, it only prints the first one (kevin). I also need it to print out john and michael which also competed in swimming.

This post has been edited by Preacher2012: 17 November 2014 - 12:03 AM

Was This Post Helpful? 0
  • +
  • -

#14 Preacher2012   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Olympic athletes classes, having trouble with some methods

Posted 17 November 2014 - 12:04 AM

I coded the method two ways:
	//printAnEvent method, given name of the event, prints all athletes that competed in the event
	public Athlete printAnEvent(String event)
	{
		/*Athlete tempAth = new Athlete();
		for(int i = 0; i < Athletes.size(); i++)
		{
			tempAth = (Athlete)Athletes.getNext();
			if(tempAth.getEvent().equals(event))
			{
				return tempAth;
			}
			tempAth = (Athlete)Athletes.getNext();
		}
		return tempAth;*/
		Athlete tempAth = new Athlete();
		for(int i = 0; i < Athletes.size(); i++)
		{
			tempAth = (Athlete)Athletes.getNext();
			if(tempAth.getEvent().equals(event))
			{
				return tempAth;
			}
			else
			{
				tempAth = (Athlete)Athletes.getNext();
			}
		}
		return tempAth;
	}


TestOlyAths.java
public class TestOlyAths
{
	public static void main(String[] args)
	{
		//create athlete objects
		Athlete kevin = new Athlete("Kevin Patel", "USA", "Swimming", 3);

		//Athlete john = new Athlete("John Wick", "USA", "Swimming", 1);
		Athlete john = new Athlete();
		john.setName("John Wick");
		john.setCountry("USA");
		john.setEvent("Swimming");
		john.setRank(1);

		Athlete michael = new Athlete("Michael Phelps", "USA", "Swimming", 2);

		//Athlete roger = new Athlete("Roger Miller", "USA", "Swimming", 2);
		Athlete roger = new Athlete();
		roger.setName("Roger Miller");
		roger.setCountry("USA");
		roger.setEvent("Martial arts");
		roger.setRank(2);

		OlympicAthletes allAthletes = new OlympicAthletes();
		allAthletes.addAthlete(kevin);
		allAthletes.addAthlete(john);
		allAthletes.addAthlete(michael);
		allAthletes.addAthlete(roger);
		//System.out.println(allAthletes.printAllAthletes());
		//allAthletes.deleteAthlete(john);
		//System.out.println(allAthletes.printParticularAthlete("john", "USA"));
		System.out.println(allAthletes.printAnEvent("Swimming"));
	}
}


but both way, It only prints out the first one it finds which is kevin, it wont print john and michael which also competed in swimming, how can i get this to work?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1