Results 1 to 4 of 4
  1. #1
    vinyacam is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default Lost with subclasses

    My last project had me do a Fitness Contest Tracker. MY next one is create one with subclasses with differnt athlete types and to establish different points totals for the different types.

    Implement four subclass objects which extend the athlete class. Each of the four classes should add the particular variable needed to track the new item and should have a constructor which takes the arguments to instantiate the object, including the new item (shoes, bike, or goggles). Each subclass should also implement a method which overrides the base method for total earned points, which calculates the points as originally implemented and then multiples the point totals for each respective object as follows:
    Biker = base points * .90
    Runner = base points * 1.1
    Walker = base points * 1 (or just the base points)
    Swimmer = base points * 1.2
    Ive looked up everything online from polymorphism and its like and am just completely confused on how to implement it with the attached codes. If anyone could give me a hand, point me in the right direction etc I would be exteremly grateful.
    Attached Files Attached Files

  2. #2
    vinyacam is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Currently I have 3 classes that look like the following and I am stuck here:

    Class 1:
    class FitnessProjectCommand2 {
    public static void main(String[] args){
    Runner runner = new Runner("John Doe", "112345", "45", "28");

    }
    }
    Class 2:
    public Athlete2 (String name, int num, int age, double points, double tpoints ) {
    membername=name;
    membernum=num;
    memberage=age;
    currentpoints=points;
    totalpoints=tpoints;
    }

    public void setmembername(String name){
    membername=name;
    }

    public String getmembername(){
    return membername;
    }

    public void setmembernum(int num){
    membernum=num;
    }

    public int getmembernum(){
    return membernum;
    }

    public void setmemberage(int age){
    memberage=age;
    }

    public int getmemberage(){
    return memberage;
    }

    public void setcurrentpoints(double points){
    currentpoints=points;
    }

    public double getcurrentpoints(){
    return currentpoints;
    }

    public void settotalpoints(double tpoints){
    totalpoints=tpoints;
    }

    public double gettotalpoints(){
    return totalpoints;
    }

    public void memberinfo() {
    System.out.printf("Member name: %s\n", getmembername());
    System.out.printf("Membership Number: %s\n", getmembernum());
    System.out.printf("Member Age: %s\n", getmemberage());
    System.out.printf("Member's Current Points: %s\n", getcurrentpoints());
    System.out.printf("Member's Total Points: %s\n", gettotalpoints());
    }


    }
    Class 3:
    public class Runner extends Athlete2 {
    private String shoetype;
    private double basepoints;

    public Runner(String membername, int membernum, int memberage, double currentpoints){
    super(membername, membernum, memberage, currentpoints);
    setshoetype(shoetype);
    setbasepoints(basepoints);
    }

    public void setshoetype(String shoe){
    shoetype=shoe;
    }

    public String getshoetype(){
    return shoetype;
    }


    public void setbasepoints(double bpoints){
    basepoints=bpoints;
    }

    public double getbasepoints(){
    return basepoints;
    }

    Basically I need it to print out something that looks like this when I make the other 4 subclasses:
    ------------------------Fitness Challenge Results ------------------------
    Member Name: John Doe
    Member Number: 123456
    Member Age: 45
    Current Points: 36
    Athlete Type: Runner
    Shoe Type: Nike
    Total Points: 168

    Member Name: Jane Doe
    Member Number: 123457
    Member Age: 38
    Current Points: 29
    Athlete Type: Walker
    Shoe Type: Nike
    Total Points: 154


    Etc Etc. Thanks is advance for any help.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by vinyacam View Post
    the following and I am stuck here:

    your main errors are: wrong constructor declaration and invalid method calls. i correct your code so that you can run it and produce the output you want.

    Java Code:
    public class Athlete2 {
    	private String membername;
    	private String membernum;
    	// add athletetype plus getter and setter
    	private String athletetype;
    	private int memberage;
    	private double currentpoints;
    	private double totalpoints;
    
    	public Athlete2(String name, String num, int age, double points) {
    		membername = name;
    		membernum = num;
    		memberage = age;
    		currentpoints = points;
    		totalpoints = 0;
    	}
    
    	public void setmembername(String name) {
    		membername = name;
    	}
    
    	public String getmembername() {
    		return membername;
    	}
    
    	public void setmembernum(String num) {
    		membernum = num;
    	}
    
    	public String getmembernum() {
    		return membernum;
    	}
    
    	public void setmemberage(int age) {
    		memberage = age;
    	}
    
    	public int getmemberage() {
    		return memberage;
    	}
    
    	public void setcurrentpoints(double points) {
    		currentpoints = points;
    	}
    
    	public double getcurrentpoints() {
    		return currentpoints;
    	}
    	
    	public String getAthletetype() {
    		return athletetype;
    	}
    
    	public void setAthletetype(String athletetype) {
    		this.athletetype = athletetype;
    	}
    
    	public void settotalpoints(double tpoints) {
    		totalpoints = tpoints;
    	}
    
    	public double gettotalpoints() {
    		return totalpoints;
    	}
    
    	public void memberinfo() {
    		System.out.printf("Member name: %s\n", getmembername());
    		System.out.printf("Membership Number: %s\n", getmembernum());
    		System.out.printf("Member Age: %s\n", getmemberage());
    		System.out.printf("Member's Current Points: %s\n", getcurrentpoints());
    		System.out.printf("Athlete Type: %s\n", getAthletetype());
    		System.out.printf("Member's Total Points: %s\n\n", gettotalpoints());
    	}
    
    }
    Java Code:
    public class Runner extends Athlete2 {
    private String shoetype;
    private double basepoints;
    
    // change membernum to String
    public Runner(String membername, String membernum, int memberage, double currentpoints) {
    super(membername, membernum, memberage, currentpoints);
    setshoetype(shoetype);
    setbasepoints(basepoints);
    }
    
    public void setshoetype(String shoe){
    shoetype=shoe;
    }
    
    public String getshoetype(){
    return shoetype;
    }
    
    
    public void setbasepoints(double bpoints){
    basepoints=bpoints;
    }
    
    public double getbasepoints(){
    return basepoints;
    } 
    }

    Java Code:
    class FitnessProjectCommand2 {
    	public static void main(String[] args) {
    		Runner runner1 = new Runner("John Doe", "112345", 45, 28.0);
    		// if you don't pass this parameter to the constructor you
    		// have to call for each member the setter
    		runner1.setshoetype("Nike");
    		runner1.settotalpoints(168.0);
    		runner1.setAthletetype("Runner");
    
    		Runner runner2 = new Runner("Jane Doe", "112347", 38, 29.0);
    		runner2.setshoetype("Nike");
    		runner2.settotalpoints(168.0);
    		runner2.setAthletetype("Runner");
    
    		runner1.memberinfo();
    		runner2.memberinfo();
    	}
    }

    the output is:

    Member name: John Doe
    Membership Number: 112345
    Member Age: 45
    Member's Current Points: 28.0
    Athlete Type: Runner
    Member's Total Points: 168.0

    Member name: Jane Doe
    Membership Number: 112347
    Member Age: 38
    Member's Current Points: 29.0
    Athlete Type: Runner
    Member's Total Points: 168.0

    one improvement you could make is using a collection so that you can call a method addAthlet(Runner) and so on ... i didn't implement any calculation of points what you can easily add it in the gettotalpoints with simple if athletetype.equals("Runner") { calc} good luck.
    Last edited by j2me64; 05-09-2010 at 10:21 AM.

  4. #4
    vinyacam is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post

    i didn't implement any calculation of points what you can easily add it in the gettotalpoints with simple if athletetype.equals("Runner") { calc} good luck.
    So where/how/when do I insert this statement so I can multiply the current points by the values stated so I can get the total points to pop up proper for a runner/walker/swimmer/biker.

    I already cut and paste and fixed the Runner class for walker, swimmer, an biker. Just need to get this math in order now.

Similar Threads

  1. subclasses and inheritance - how's it all work?
    By 711groove in forum New To Java
    Replies: 14
    Last Post: 12-18-2009, 10:04 AM
  2. Generics and subclasses
    By Sven in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2008, 07:06 PM
  3. Main class and subclasses
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 11-19-2008, 07:16 PM
  4. help needed with methods in subclasses
    By uncopywritable in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 02:47 PM
  5. Hibernate subclasses
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 05:42 PM

Posting Permissions

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