Results 1 to 2 of 2
  1. #1
    Joshsmith is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default Class example not compiling Runner class and Swimmer class

    Here is an example that my teacher posted up. However, when I run the code. The runner and swimmer classes do not compile because it cannot find the value Event. Did the teacher forget to post a class for Event, or am I not understanding something?

    Any help would be appreciated.

    Thanks


    Code is below:



    Java Code:
    public class Athlete {
    	private String name;
    	private String gnum;
    	private double gpa;
    	protected String [] valid;
    
    	public String getName() {return name;}
    	public String getGnum() {return gnum;}
    	public double getGPA() {return gpa;}
    	public  String [] getValid() {return valid;}
    
    	public  String validList()
    	{
    		String list ="";
    		for (int i = 0; i < valid.length; i++)
    			list += valid[i]+", ";
    		return list;
    	}
    
    	public void setName(String n) {name = n;}
    	public boolean setGnumber(String g)
    	{
    		if (Character.toUpperCase(g.charAt(0)) != 'G' || g.length()!=9)
    			return false;
    		for (int i = 1; i < g.length(); i++)
    			if (!Character.isDigit(g.charAt(i)))
    				return false;
    		gnum = g;
    		return true;
    	}
    
    	public boolean setGPA( double g)
    	{
    		if (g < 1.76 || g > 4.0)
    			return false;
    		gpa = g;
    		return true;
    	}
    
    	public String info()
    	{
    		return "Name:  "+name+"\nGNumber:  "+gnum;
    	}
    
    	public String toString() {return "";}
    
    }

    Java Code:
    public class FootballPlayer extends Athlete {
    
    	String position;
    
    	String [] positions ()
    	{
    		String [] p = {"offense", "defense", "special teams"};
    		return p;
    	}
    
    	FootballPlayer()
    	{
    		valid = positions();
    	}
    
    	public String getPosition () {return position;}
    
    	public boolean setPosition (String p)
    	{
    		for (int i = 0; i < valid.length ; i++)
    			if (p.equalsIgnoreCase(valid[i]))
    			{
    				position = p;
    				return true;
    			}
    		return false;
    	}
    
    	public String toString()
    	{
    		return "Sport:  Football\n"+info()+"\nPosition:  "+position;
    	}
    
    
    }


    Java Code:
    public class Runner extends Athlete {
    
    	Event[] events;
    	int numEvents;
    
    	String [] trackEvents ()
    	{
    		String [] t = {"55Meter", "300Meter", "400Meter" };
    		return t;
    	}
    
    	public Runner()
    	{
    		valid = trackEvents();
    		events = new Event[3];
    		numEvents = 0;
    	}
    
    	public int getNumEvents() {return numEvents;}
    
    	public String getEventName(int pos) {return events[pos].getName();}
    
    	public int getTime (String stroke) {
    
    		return events[findEvent(stroke)].getTime();
    
    	}
    
    	private int findEvent(String event)
    	{
    		for (int i = 0; i < events.length; i++)
    			if (event.equalsIgnoreCase(events[i].getName()))
    				return i;
    		return -1;
    	}
    
    
    	public boolean setEvent (String event, int t)
    
    	{
    		 if (t < 0 || t > 300)
    		 	return false;
    		 events[numEvents] = new Event();
    		 events[numEvents].setName(event);
    		 events[numEvents++].setTime(t);
    		 return true;
    
    	}
    	public String toString()
    		{
    			String runner = "Sport:  Track\n";
    			runner +=  info()+"\n";
    			for (int i = 0; i < numEvents; i++)
    				runner += "   "+events[i].getName()+":  "+events[i].getTime()+" seconds\n";
    			return runner;
    	}
    
    
    }



    Java Code:
    public class Swimmer extends Athlete {
    
    	Event[] events;
    
    	String [] swimEvents ()
    	{
    		String [] s = {"backstroke", "breaststroke", "butterfly", "freestyle"};
    		return s;
    	}
    
    	Swimmer()
    	{
    		valid = swimEvents();
    		events = new Event[4];
    		for (int i = 0; i < valid.length; i++)
    		{	events[i] = new Event();
    			events[i].setName(valid[i] );
    		}
    
    	}
    
    	private int findStroke(String stroke)
    	{
    		for (int i = 0; i < events.length; i++)
    			if (stroke.equalsIgnoreCase(events[i].getName()))
    				return i;
    		return -1;
    	}
    
    
    	public int getTime (String stroke) {
    
    		return events[findStroke(stroke)].getTime();
    
    	}
    
    	public boolean setTime (String stroke, int t)
    	{
    		if (t < 0 || t > 100)
    			return false;
    
    		 events[findStroke(stroke)].setTime(t);
    		 return true;
    
    	}
    
    	public String toString()
    	{
    		String swimmer = "Sport:  Swimming\n";
    		swimmer +=  info()+"\n";
    		for (int i = 0; i < events.length; i++)
    			swimmer += "   "+events[i].getName()+":  "+events[i].getTime()+" seconds\n";
    		return swimmer;
    	}
    
    
    }




    Java Code:
    import javax.swing.JOptionPane;
    
    public class MasonAthletes {
    
    	public static void main (String [] args)
    	{
    		Runner oneRunner;
    		Swimmer oneSwimmer;
    		FootballPlayer oneFoot;
    
    		oneRunner = getARunner();
    		oneSwimmer = getASwimmer();
    		oneFoot = getAFoot();
    
    		athletes (oneRunner, oneSwimmer, oneFoot);
    	}
    
    	private static void athletes (Runner r, Swimmer s, FootballPlayer f)
    	{
    		JOptionPane.showMessageDialog(null, r.toString() + s.toString() + f.toString());
    	}
    
    	private static boolean isValid(String [] valid, String choice)
    	{
    		for (int i = 0; i < valid.length; i++)
    			if (choice.equalsIgnoreCase(valid[i]))
    				return true;
    		return false;
    	}
    
    	private static FootballPlayer getAFoot()
    	{
    		FootballPlayer oneGuy = new FootballPlayer();
    		String position;
    		String gnum;
    		double gpa;
    
    		oneGuy.setName(JOptionPane.showInputDialog("Football Athlete's name:"));
    		do {
    			position = JOptionPane.showInputDialog("Football Player's position ("+oneGuy.validList()+"):");
    		} while (!isValid(oneGuy.getValid(), position));
    		oneGuy.setPosition(position);
    
    		do {
    			gpa = Double.parseDouble(JOptionPane.showInputDialog("Athlete's GPA :  "));
    		}while (!oneGuy.setGPA(gpa));
    
    		do {
    			gnum = JOptionPane.showInputDialog("Enter "+ oneGuy.getName() + "'s GNumber (a G followed by 8 digits:");
    		}while(!oneGuy.setGnumber(gnum));
    
    		return oneGuy;
    	}
    
    	private static Swimmer getASwimmer()
    		{
    			Swimmer oneGuy = new Swimmer();
    
    			int time;
    			String gnum;
    			double gpa;
    			String [] strokes = oneGuy.getValid();
    
    			oneGuy.setName(JOptionPane.showInputDialog("Swimmer Athlete's name:"));
    
    
    			do {
    				gpa = Double.parseDouble(JOptionPane.showInputDialog("Athlete's GPA:  "));
    			}while (!oneGuy.setGPA(gpa));
    
    			do {
    				gnum = JOptionPane.showInputDialog("Enter "+ oneGuy.getName() + "'s GNumber:");
    			}while(!oneGuy.setGnumber(gnum));
    
    
    			for (int i = 0; i < strokes.length; i++)
    				do {
    					time = Integer.parseInt(JOptionPane.showInputDialog("Enter "+oneGuy.getName()+"'s time in the "+ strokes[i]));
    				} while (!oneGuy.setTime( strokes[i], time));
    
    			return oneGuy;
    	}
    	private static Runner getARunner()
    			{
    				Runner oneGuy = new Runner();
    
    				int time;
    				String gnum;
    				double gpa;
    				String [] events = oneGuy.getValid();
    				String event;
    
    				oneGuy.setName(JOptionPane.showInputDialog("Track Athlete's name:"));
    
    
    				do {
    					gpa =  Double.parseDouble(JOptionPane.showInputDialog("Athlete's GPA:  "));
    				}while (!oneGuy.setGPA(gpa));
    
    				do {
    					gnum = JOptionPane.showInputDialog("Enter "+ oneGuy.getName() + "'s GNumber:");
    				}while(!oneGuy.setGnumber(gnum));
    
    
    				do {
    
    					do {
    						event = JOptionPane.showInputDialog("Enter event name ("+oneGuy.validList() +") for: "+oneGuy.getName() );
    					} while (!isValid(oneGuy.getValid(), event));
    
    
    					do {
    						time = Integer.parseInt(JOptionPane.showInputDialog("Enter "+oneGuy.getName()+"'s time in the "+ event));
    					} while (!oneGuy.setEvent (event, time));
    
    				}while (JOptionPane.showConfirmDialog(null,"Another Event?")==JOptionPane.YES_OPTION  && oneGuy.getNumEvents() < events.length);
    
    
    
    				return oneGuy;
    	}
    }

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Class example not compiling Runner class and Swimmer class

    what line of code is the error, what is the error message?

Similar Threads

  1. Class is not compiling!
    By bluej in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 06:26 AM
  2. compiling .java into .class
    By khp in forum Advanced Java
    Replies: 4
    Last Post: 06-29-2009, 03:52 PM
  3. not compiling a copy of class -
    By itaipee in forum Eclipse
    Replies: 0
    Last Post: 01-07-2009, 10:47 AM
  4. Compiling a single class
    By ROSOBORONEXPORTCORP in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 01:27 PM
  5. Compiling a class in a package from cmd
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 11:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •