Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a new Java Project called Midterm1. Add two packages to the src folder: p

ID: 672208 • Letter: C

Question

Create a new Java Project called Midterm1. Add two packages to the src folder: partl and part2 Package part1: (45 points) Athlete name String +Sport (name: String) +getName() String practice() toString() String BaseballPlayer SoccerPlayer battingAvg : double BaseballPlayer ( name position: String +SoccerPlayer(name : String, position: String) +getPosition) String +headBall) String, battingAvg : double) +getBattingAvg(): double In package partl declare 4 classes: Athlete, BaseballPlayer, SoccerPlayer and AthleteApp. Implement the classes based on the UML class diagram above. (AthleteApp includes the main method) Notice: the UML class diagram does not list overridden methods. I specify the expected behavior below and it is your job to decide whether a method needs to be orverridden or not . toString: should print the type of the class followed by a colon, a space, and the name practice: prints "practicing" (including a new line at the end) toString: should print the type of the class followed by a colon, a space, the name, and the batting average practice: prints "hitting the ball" (including a new line at the end) tostring: should print the type of the class followed by a colon, a space, and the name e.g. Athlete: Rafael Nadal .Class Basebal1Player: e.g. BaseballPlayer: Ted Williams battingAvg: 0.344 .Class SoccerPlayer e.g. SoccerPlayer Lionel Messi practice: prints "kicking the ball" (including a new line at the end) headBall: prints "A" followed by the position and " is heading the ball" (including a new line at the end) e.g. A forward is heading the ball In the main method do the following: Create an athlete named Rafael Nadal, a soccer player named Lionel Messi with position forward, and a baseball player named Ted Williams with a batting average of 0.344, and "Create an array of Athletes and initialize it with the athlete, the soccer player and the baseball player Loop through the array and print information about each of the athletes Make each athlete practice. If the Athlete happens to be a soccer player make him head the ball. Print a new line before advancing to the next element in the array Note: AthleteApp doesn't call all methods. Still, every field, ctor(constructor), method specified in the UML class diagram needs to be implemented. Also: make sure to avoid code repetition (expected output is on next page)

Explanation / Answer

package mani;

public class AthleteApp {

  
   public static void main(String[] args) {
       Athlete a=new Athlete("Rafael Nadal");
       SoccerPlayer s=new SoccerPlayer("Lionel Messi", "forward");
       BaseballPlayer b=new BaseballPlayer("Ted Williams", 0.344);
       Athlete[] ath=new Athlete[3];
       ath[0]=a;
       ath[1]=s;
       ath[2]=b;
       for(Athlete c:ath){
           System.out.println(c.toString());
           c.practice();
           if(c.sport.equalsIgnoreCase("soccer")){
               s.headBall();
           }
           System.out.println();
       }
      
      
      

   }

}

public class SoccerPlayer extends Athlete{
   String position;
public SoccerPlayer(String name,String position){
   super(name);
   this.position=position;
   super.Sport("soccer");
  
  
}
public String getPosition(){
   return position;
}
public void practice(){
   System.out.println("Kicking the ball");
}
public void headBall(){
   System.out.println(getPosition()+" is heading the ball");
}
public String toString(){
   return super.name+": "+super.getName();
}

}

package mani;
public class BaseballPlayer extends Athlete
{
   double battingAvg;
   public BaseballPlayer(String name,double battingAvg){
       super(name);
       this.battingAvg=battingAvg;
       super.Sport("Baseball");
   }
   public double getBattingAvg(){
       return battingAvg;
   }
   @Override
   public void practice(){
       System.out.println("hitting the ball");
   }
   public String toString(){
       return super.name+": "+super.getName()+" battingAvg: "+getBattingAvg();
   }
}

public class Athlete{
   String name;
   String sport="Athlete";
   public Athlete(String name2) {
       this.name=name2;
      
   }
   public void Sport(String name){
       this.sport=name;
      
   }
   public String getName(){
       return name;
      
   }
   public void practice(){
      
       System.out.println("practicing...");
   }
   public String toString(){
       return sport+": "+getName();
   }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote