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

Java Inheritance Program. Please write code: These are the codes that I wrote fr

ID: 3797974 • Letter: J

Question

Java Inheritance Program. Please write code:

These are the codes that I wrote from the previous assignment, since this assignment is a build up from the previous assignment:

public class Sport {
// Instance variables
   private String activity;
   private int calories;
//2 constructors
private static int count = 0;
public Sport() {
count++;
}
Sport(Sport s){
this.activity = s.activity;
this.calories = s.calories;
count++;
}
// GETTERS & SETTERS
public Sport(String a, int c) {
activity = a;
calories = c;
count++;
}
public String getActivity() {
return activity;
}
public void setActivity(String activity) {
this.activity = activity;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
this.calories = calories;
}
//TOSTRING METHOD
public String toString(){
return "Activity: "+ activity +" Calories: " + calories;
}
public static int sportObjects(){
return count;
}
public boolean sameCaloriesExpended(Sport s){
if(this.calories == s.calories){
return true;
}
else{
return false;
}
}
}

public class SportProgram
{
public static void main (String [] args)
{
// create sport objects
Sport s0 = new Sport("walking", 654);
Sport s1 = new Sport("swimming", 874);
Sport s2 = new Sport("running", 1022);
Sport s3 = new Sport("snowshoeing", 654);
Sport s4 = new Sport("basketball", 654);
Sport s5 = new Sport(s2);

       // use getters and setters
       System.out.println(" Sport s0 is " + s0.getActivity());
       System.out.println("Doing sport s0 burns " + s0.getCalories() +
           " calories");
   System.out.println("The calories for s0 is being changed to 515 for a "
           + "slower walking rate");
       s0.setCalories(515);
       System.out.println("Sport s0 now burns " + s0.getCalories() +
           " calories per hour");

       // use static variable to get number of objects
   System.out.println(" There are " + Sport.sportObjects()
           + " Sport objects");

       // print properties of the sport objects
   System.out.println("Here are the properties of all the Sport objects:");
       Sport [] sports = {s0, s1, s2, s3, s4, s5};

       for (int i = 0; i < sports.length; i++)
       {
           System.out.println("s" + i + " " + sports[i] + " ");
       }

       // compare calories burned for different sports
   System.out.println(" Sport s3 is " + s3.getActivity());
       String value = (s0.sameCaloriesExpended(s3)) ? "" : "not ";
   System.out.println("Doing s0 or s3 will " + value
           + "burn the same number of calories");   
   System.out.println("Sport s4 is " + s4.getActivity());
       value = (s3.sameCaloriesExpended(s4)) ? "" : "not ";
   System.out.println("Doing s3 or s4 will " + value
           + "burn the same number of calories ");   
   }
}

inheritance Create a TeamSport class that extends Sport and contains the following: two instances variables players: number of players on team time: number of minutes of player two constructors no argument: default values of basketball, 654, 5, 40 argument: user-supplied values two getters two setters copy constructor toString0 that prints values of instance variables activity basketball calories 654 players 5 time 40 equals() method that returns true if both TeamSport objects have the same number of players and minutes of play: public boolean equals (Object o) Make the Sport class abstract and add the following abstract method to it: public abstract double computeCalories0; Then implement the method in TeamSport: public double computeCalories0 using the following formula: calories burned calories portion of hour number of overtimes portion of hour where number of overtimes is a randomly choose a number between from 0 to 5

Explanation / Answer

Sports.java [This is also modified as per requirement ]

public abstract class Sport {

   // Instance variables

      private String activity;

      private int calories;

   //2 constructors

   private static int count = 0;

   public Sport() {

   count++;

   }

   Sport(Sport s){

   this.activity = s.activity;

   this.calories = s.calories;

   count++;

   }

   // GETTERS & SETTERS

   public Sport(String a, int c) {

   activity = a;

   calories = c;

   count++;

   }

   public String getActivity() {

   return activity;

   }

   public void setActivity(String activity) {

   this.activity = activity;

   }

   public int getCalories() {

   return calories;

   }

   public void setCalories(int calories) {

   this.calories = calories;

   }

   //TOSTRING METHOD

   public String toString(){

   return "Activity: "+ activity +" Calories: " + calories;

   }

   public static int sportObjects(){

   return count;

   }

   public boolean sameCaloriesExpended(Sport s){

   if(this.calories == s.calories){

   return true;

   }

   else{

   return false;

   }

   }

   public abstract double computeCalories();

   }

     



=====================================================================================
TeamSport.java

import java.util.Random;

public class TeamSport extends Sport {

   int players, time;

   public int getPlayers() {

       return players;

   }

   public void setPlayers(int players) {

       this.players = players;

   }

   public int getTime() {

       return time;

   }

   public void setTime(int time) {

       this.time = time;

   }

   public TeamSport() {

       super("basketball", 654);

       this.players = 5;

       this.time = 40;

   }

   TeamSport(TeamSport s) {

       this.players = s.players;

       this.time = s.time;

   }

   public String toString() {

       return "activities" + " " + this.getActivity() + " " + "Calories" + this.getCalories() + " " + "Players"

               + this.getPlayers() + " " + "Time " + this.getTime();

   }

   public boolean equals(Object o) {

       TeamSport ts = (TeamSport) o;

       if (this.players == ts.players && this.time == ts.time)

           return true;

       return false;

   }

   @Override

   public double computeCalories() {

       // TODO Auto-generated method stub

       Random r = new Random();

       int num = r.nextInt(5);

       double caloriesburned = this.getCalories() * 1.0 * this.getTime() / 60 + num * 1.0 * this.getTime() / 60;

       return caloriesburned;

   }

}
================================================================================


Thanks, let me know if there is any concern.

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