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

Java Inheritance Program. Please write code:

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

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

// 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!