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

    Default Adding Arrays and Enhanced For Loop into program.

    Ok I have an established program already which I will add at the end (Ill truncate it down to one subclass for our sake)

    These are the objectives of the project I need to implement into the exsisting code and I am totally clueless on how to do that. Do I put the arrays inside the Athlete subclasses (i.e Runner) or into Athlete itself, or even into the command program.


    New Requirements
    Create an array of participants, including at least one of each type of participant
    Create a method to sort the array by participant name
    Print the information for each participant, including membership code, name, age, current points, and earned points.
    Create a method to calculate the total points for all participants in the system
    Print the total points earned by all participants to the screen.
    Class #1:
    class FitnessProjectCommand2 {
    public static void main(String[] args) {
    System.out.println("Fitness Contest Tracker");

    Runner runner1 = new Runner("John Doe", "112345", 45, 28.0);
    runner1.setshoetype("Nike");
    runner1.setAthletetype("Runner");

    runner1.memberinfo();

    }
    }
    Class 2:
    public class Athlete2 {
    private String membername;
    private String membernum;
    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 = currentpoints+memberage*1.5;

    }


    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());
    }

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


    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;
    }
    }
    Not looking for anyone to write the code for me, just looking for the nudge in the right direction. Thanks in advance. I missed alot of the early classes in Java so I am tryign to self teach myself and I just cant find alot of good stuff on using arrays for info like this.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Well, the instructions are pretty much a dead giveaway, that your array should not be part of your existing classes, but rather a new class, for instance:
    Java Code:
    public class Competition {
      Athlete[] participants;
      //rest of code
    }
    When dealing with organisation in OOP, always think in terms of what an object is supposed to do, that will give you clues as to when to add more functionality to your existing class, or when to make a new class to handle the new demands. Let's say the assignment is to make a super class CarComponent and your job is to make subclasses. When you make a Tire extends CarComponent class, does the tire need to know about fuel injection? No, that's the engines job. Similarly, when you need functionality for multiple objects of the same class, it's usually a better idea to make a <insertclassname>Manager class, instead of adding the group methods to your class definition. Usually, an object needs to only know it's specific job, and how to compare itself to other objects of the same class.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. enhanced for loop
    By billq in forum New To Java
    Replies: 1
    Last Post: 05-01-2010, 02:07 PM
  2. Enhanced For Loop
    By terahawks in forum New To Java
    Replies: 3
    Last Post: 04-16-2010, 08:46 AM
  3. Help for Enhanced for loop
    By jboy in forum New To Java
    Replies: 6
    Last Post: 09-13-2009, 06:45 AM
  4. Enhanced for loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM
  5. Enhanced For loop In Java
    By goldhouse in forum Advanced Java
    Replies: 1
    Last Post: 05-06-2007, 04:26 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
  •