Create a Sport class that contains the following: • two instances variables acti
ID: 3785802 • Letter: C
Question
Create a Sport class that contains the following: • two instances variables activity: name of the sport calories: calories burned per hour • two constructors no argument: default values of walking and 654 two argument: user-supplied values • two getters • two setters • copy constructor • toString method that prints values of instance variables activity walking calories 654 • static variable sportObjects to count Sport objects created • static method sportObjects() that returns the number of objects created • instance method sameCaloriesExpended() that returns true if two activities burn same number of calories, false otherwise Next create five objects in SportProgram with the following values: s0 walking 654 s1 swimming 817 s2 running 1022 s3 snowshoeing 654 s4 basketball 654 Then create a Sport object s5 for running that is just like s2. Next compile and run SportProgram. Your output should look like this:
Sport s0 is walking Doing sport s0 burns 654 calories The calories for s0 is being changed to 515 for a slower walking rate Sport s0 now burns 515 calories per hour There are 6 Sport objects Here are the properties of all the Sport objects: s0 activity walking calories 515 s1 activity swimming calories 817 s2 activity running calories 1022 s3 activity snowshoeing calories 654 s4 activity basketball calories 654 s5 activity running calories 1022 Sport s3 is snowshoeing Doing s0 or s3 will not burn the same number of calories Sport s4 is basketball Doing s3 or s4 will burn the same number of calories
** This assignment has to do with objects. This is the provided driver program:
Explanation / Answer
SportProgram.java
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 ");
}
}
Sport.java
public class Sport {
private String activity;
private int calories;
private static int count = 0;
public Sport() {
count++;
}
Sport(Sport s){
this.activity = s.activity;
this.calories = s.calories;
count++;
}
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;
}
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;
}
}
}
Output:
Sport s0 is walking
Doing sport s0 burns 654 calories
The calories for s0 is being changed to 515 for a slower walking rate
Sport s0 now burns 515 calories per hour
There are 6 Sport objects
Here are the properties of all the Sport objects:
s0
Activity: walking Calories: 515
s1
Activity: swimming Calories: 874
s2
Activity: running Calories: 1022
s3
Activity: snowshoeing Calories: 654
s4
Activity: basketball Calories: 654
s5
Activity: running Calories: 1022
Sport s3 is snowshoeing
Doing s0 or s3 will not burn the same number of calories
Sport s4 is basketball
Doing s3 or s4 will burn the same number of calories