Question
Inheritance Code 1) modify Sport as follows add the following method and make the class abstract: public abstract double computeCalories(); implement the Comparable interface, using
Inheritance Code
1) modify Sport as follows add the following method and make the class abstract: public abstract double computeCalories(); implement the Comparable interface, using calories to compare objects: public int compareTo (Sport s) 2) create a TeamSport class that extends Sport and contains the following: two instances variables players: number of people on team time: minutes of play two constructors no argument: default values of ice hockey, 544, 6, 60 argument: user-supplied values for activity, calories, players, time two getters two setters toString() that prints values of instance variables activity hockey calories 544 players 6 time 60 override the equals() method from Object and have it return true if both TeamSport objects have the same number of players and minutes of play: public boolean equals (Object o) override the abstract method from Sport: public double computeCalories() using the following formula: calories = (minutes played + overtime) * calories / 60 where overtime = random number from 0 to 5 and each overtime is 5 m Spring 2018 COMP 1050 Lab 5 Bolotin 3) create an IndividualSport class that extends Sport and contains the following: instances variable enjoyability (1 = chore to do, 2 = indifferent, 3 = love) two constructors no argument: default value of 1 for enjoyability argument: user-supplied values for activity, calories, enjoyability getter setter toString() that prints values of instance variables activity skiing calories 654 enjoy 1 override the abstract method from Sport: public double computeCalories() using the following formula: calories = minutes played * calories / 60 where time is based on enjoyability (1 = 15 minutes, 2 = half hour, 3 = hour) Then compile and run SporProgram2.java. Your output should be similar to this Sport s0 is skating and it burns 392 calories The calories for s0 is being changed to 572 for a faster rate Sport s0 now burns 572 calories per hour Sport s3 is skiing and it burns 654 calories Doing s0 or s3 will not burn the same number of calories Sport s7 is sledding and it burns 572 calories Doing s0 or s7 will burn the same number of calories Spring 2018 COMP 1050 Lab 5 Bolotin There are 8 Sport objects Here are the properties of the Sport objects: s0 activity skating calories 572 enjoyability 1 s1 activity skiing calories 654 enjoyability 3 s2 activity snowboarding calories 429 enjoyability 1 s3 activity skiing calories 654 enjoyability 2 s4 activity ice hockey calories 544 players 6 time 60 s5 activity curling calories 327 players 3 time 60 s6 activity ice hockey calories 544 players 6 time 60 s7 activity sledding calories 572 enjoyability 2 Spring 2018 COMP 1050 Lab 5 Bolotin Does team sport s4 equal s5? no Does s4 equal s6? yes s6's time is being changed to 48 minutes Does s4 equal s6? no Seeing how sports compare caloriewise: s1 compared to s4: s1 burns more s6 compared to s7: s7 burns more s0 compared to s7: both burn the same A typical workout session for each sport: skating 572 skiing 163 snowboarding 429 skiing 327 ice hockey 680 curling 408 ice hockey 661 sledding 286
Code so far:
* SportProgram2.java * * driver program for lab 5 (inheritance) */ public class SportProgram2 { public static void main (String [] args) { // create sport objects Sport s0 = new IndividualSport(); Sport s1 = new IndividualSport("skiing", 654, 3); Sport s2 = new IndividualSport("snowboarding", 429, 1); Sport s3 = new IndividualSport("skiing", 654, 2); Sport s4 = new TeamSport(); Sport s5 = new TeamSport("curling", 327, 3, 150); Sport s6 = new TeamSport(); Sport s7 = new IndividualSport("sledding", 572, 2); // use getters and setters System.out.println(" Sport s0 is " + s0.getActivity() + " and it burns " + s0.getCalories() + " calories"); System.out.println("The calories for s0 is being changed to 572 for a " + "faster rate"); s0.setCalories(572); System.out.println("Sport s0 now burns " + s0.getCalories() + " calories per hour"); // compare calories burned for different sports System.out.println(" Sport s3 is " + s3.getActivity() + " and it burns " + s3.getCalories() + " calories"); 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 s7 is " + s7.getActivity() + " and it burns " + s7.getCalories() + " calories"); value = (s0.sameCaloriesExpended(s7)) ? "" : "not "; System.out.println("Doing s0 or s7 will " + value + "burn the same number of calories "); // use static variable to get number of objects System.out.println("There are " + Sport.sportObjects() + " Sport objects"); /* inheritance-related test code */ // print properties using subclass versions of toString() System.out.println("Here are the properties of the Sport objects: "); Sport [] sports = {s0, s1, s2, s3, s4, s5, s6, s7};; for (int i = 0; i < sports.length; i++) { System.out.println("s" + i + " " + sports[i] + " "); } // compare sports objects using overridden equals() method System.out.println("Does team sport s4 equal s5? " + (s4.equals(s5) ? "yes" : "no")); System.out.println("Does s4 equal s6? " + (s4.equals(s6) ? "yes" : "no")); System.out.println("s6's time is being changed to 48 minutes"); ((TeamSport)s6).setTime(48); // accessing method from subclass System.out.println("Does s4 equal s6? " + (s4.equals(s6) ? "yes" : "no")); // compare calories burned for each sport using Comparable interface System.out.println(" Seeing how sports compare caloriewise: "); System.out.print("s1 compared to s4: "); int result = s1.compareTo(s4); System.out.println(result == 1 ? "s1 burns more" : result == 0 ? "both burn the same" : "s4 burns more"); System.out.print("s6 compared to s7: "); result = s6.compareTo(s7); System.out.println(result == 1 ? "s6 burns more" : result == 0 ? "both burn the same" : "s7 burns more"); System.out.print("s0 compared to s7: "); result = s0.compareTo(s7); System.out.println(result == 1 ? "s0 burns more" : result == 0 ? "both burn the same" : "s7 burns more"); // compute calories of a workout using overridden superclass method System.out.println(" A typical workout session for each sport: "); for (Sport s : sports) { System.out.printf("%12s %5.0f ", s.getActivity(), s.computeCalories()); } System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started