Related questions
Urgent Quick Question
Write an equals method for the StepsFitnessTracker class that tests whether the one object is equal to another object that is passed as an argument (Object otherObject). The Object superclass provides an equals method that tests if two object references are equal. Your new equals method should override the Object equals method, and should test the following: • Return true if the current (this) object, and otherObject are identical references (hint: you can use the == operator for this). • Returns false if otherObject is null. • Returns false if the current (this) object and otherObject have different classes (hint: you can use the getClass() method provided by the Object superclass, see the Java 8 API documentation for details). • Casts otherObject to a StepsFitnessTracker, and tests for equality of each instance variable1 , if the instance variables are the same the method should return true. Now write an equals method for the DistanceFitnessTracker and HeartRateFitnessTracker classes that: • Use the superclass equals method, and return false if this method fails. • Casts the parameter to either HeartRateFitnessTracker or DistanceFitnessTracker. • Tests for equality of the subclass instance fields. Please, use JUnit testing and write a test class named TestFTEquals to test that these new equals methods work properly. For inspiration, you can have a look at the test classes provided (TestFitnessTracker, TestDistanceFitnessTracker, TestStepsFitnessTracker, and TestHeartRateFitnessTracker) as they are already providing you some basic tests about how to test for equality of objects.
I already did the first part and added the equal method I need help to write the TESTFTEQUALS TEST PLease
public class HeartRateFitnessTracker extends FitnessTracker {
// Cumulative moving average HeartRate
HeartRate avgHeartRate;
// Number of heart rate measurements
int numMeasurements;
public HeartRateFitnessTracker(String modelName, HeartRate heartRate) {
super(modelName);
// Only one HeartRate to begin with; average is equal to single measurement
this.avgHeartRate = heartRate;
this.numMeasurements = 1;
}
public void addHeartRate(HeartRate heartRateToAdd) {
// Calculate cumulative moving average of heart rate
// See https://en.wikipedia.org/wiki/Moving_average
double newHR = heartRateToAdd.getValue();
double cmaHR = this.avgHeartRate.getValue();
double cmaNext = (newHR + (cmaHR * numMeasurements)) / (numMeasurements + 1);
this.avgHeartRate.setValue(cmaNext);
numMeasurements ++;
}
// Getter for average heart rate
public HeartRate getAvgHeartRate() {
return avgHeartRate;
}
public String toString() {
return "Heart Rate Tracker " + getModelName() +
"; Average Heart Rate: " + getAvgHeartRate().getValue() +
", for " + numMeasurements + " measurements";
}
@Override
public boolean equals(Object obj) {
// TODO Implement a method to check equality
if(this == obj) // it will check if both the references are refers to same object
return true;
if(obj == null || obj.getClass()!= this.getClass()) //it check the argument of the type HeartRateFitnessTracker by comparing the classes of the passed argument and this object.
return false;
HeartRateFitnessTracker HRFT=(HeartRateFitnessTracker)obj; // type casting of the argument.
return (HRFT.avgHeartRate == this.avgHeartRate);// comparing the state of argument with the state of 'this' Object.
}
}
public class DistanceFitnessTracker extends FitnessTracker{
private Distance totalDistance;
public DistanceFitnessTracker(String modelName, Distance distance) {
super(modelName);
this.totalDistance = distance ;
}
public Distance getDistance() {
return totalDistance;
}
public void addDistance(Distance distance) {
this.totalDistance.setValue(this.totalDistance.getValue() + distance.getValue());
}
public Distance getTotalDistance() {
return totalDistance;
}
public String toString() {
return "Distance Tracker " + getModelName() +
"; Total Distance: " + getTotalDistance().getValue();
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (!super.equals(obj)) return false;
DistanceFitnessTracker that = (DistanceFitnessTracker) obj;
if(this.getDistance() == null && that.getDistance() != null) return false;
if(this.getDistance() != null && that.getDistance() == null) return false;
if(that.getDistance() != this.getDistance()) return false;
return that.getDistance().getValue() == this.getDistance().getValue();
}
}
Step by stepSolved in 2 steps
- Goal 1: Update the Fractions Class ADD an implementation that takes an integer as a parameter. The functionality remains the same: Instead of multiplying/dividing the current object with another Fraction, it multiplies/divides with an integer. For example; a fraction 2/3 when multiplied by 4 becomes 8/3. Similarly, a fraction 2/3 when divided by 4 becomes 1/6. Goal 2: Update the Recipe Class Add a private member variable of type int to denote the serving size and initialize it to 1. Update the overloaded extraction operator (<<) to include the serving size. New Member functions to the Recipe Class: 1. Add four member functions get the value of each of the member variable of the Recipe class: getRecipeName(), getIngredientNames(), getIngredientQuantities(), getServingSize() They return the value of the respective member variables. 2. Add and implement a member function that scales the current recipe to a new serving size. The signature of this function is: void scaleRecipe(int...arrow_forwardTask 1:The first task is to make the maze. A class is created called Maze, in which a 2D array for the maze is declared. Your job is to implement the constructor according to the given javaDoc. When you completed this task, write a set of Junit test case to test your code.As you probably noticed, the maze is defined as a private variable. The second job for you is to write an accessor(getter) method that returns the maze. Other information about what is expected for this method is given in the starter code. public class PE1 { MazedogMaze; /** * This method sets up the maze using the given input argument * @param maze is a maze that is used to construct the dogMaze */ publicvoidsetup(String[][]maze){ /* insert your code here to create the dogMaze * using the input argument. */ }arrow_forwardSally and Harry implement two different compareTo methods for a classthey are working on together. Sally’s compareTo method returns −1, 0, or +1,depending on the relationship between two objects. Harry’s compareTo methodreturns −6, 0, or +3. Which method is suitable?arrow_forward
- mau Open Leathing inta... - The class has data members that can hold the name of your cube, length of one of the sides and the color of your cube. - The class has a constructor that accepts the name, length of one of the sides, and color as arguments and sets the data members to those values. - The class has the following methods to set the corresponding data member. setName(string newName) setSide(double newSide) setColor(string newColor) - The class has a method called getVolume() that returns the volume of the cube, calculated by cubing the length of the side. - The class has a method called volumelncrease(double newVolume) that receive the percent the volume should increase, and then the side to the corresponding value. i - For example, 2.5 indicates 2.5% increasing of the volume. So you take the current volume, increase it by 2.5%, and then find the cube root to calculate the new side length. You can use the built in function called cbrt, part of the math library, to find the cube...arrow_forwardThis class is used to store a time of day and output it.It also stores the current time of day and manipulates it. class Time{ // instance variables for an object to store a time of day: private int hours; private int minutes; // static Time object to store the current time of day, // defaults to midnight: private static Time curTime = new Time(); // Default Constructor // initialize time to 00:00 (midnight) public Time() { hours = minutes = 0; } // Constructor to parse a string hh:mm as the Time public Time(String hourColonMinute) { int colonIdx = hourColonMinute.indexOf(':'); hours = Integer.parseInt(hourColonMinute.substring(0, colonIdx)); minutes = Integer.parseInt(hourColonMinute.substring(colonIdx+1)); normalize(); } // initialize time using parameters for hour and minute public Time(int hr, int min) { hours = hr; minutes = min; normalize(); } // set current time to parameters public static void setCurTime(int hr, int min) {...arrow_forwardHello all! Need some help with this assignment. Need to create an array of 8 employees ( see image for details ) Thank you!arrow_forward
- tri1 and tri2 are instances of the Triangle class. Attributes height and base of both tri1 and tri2 are read from input. In the Triangle class, define instance method print_info() with self as the parameter to output the following in one line: 'Height: ' The value of attribute height ', base: ' The value of attribute basearrow_forwardAdd three methods to the Student class that compare twoStudent objects. One method (__eq__) should test for equality. A second method (__lt__) should test for less than. The third method (__ge__) should test for greater than or equal to. In each case, the method returns the result of the comparison of the two students’ names. Include a main function that tests all of the comparison operators. Note: The program should output in the following format: False: False True: True True: True False: False True: True True: True True: True True: True True: True True: True ks Add method to test for equality Add method to test for less than Add method to test for greater than or equal toarrow_forwardHelp me solve this in Java, don't make it to complicated.arrow_forward
- Write a method for the farmer class that allows a farmer object to pet all cows on the farm. Do not use arrays.arrow_forward3- Create a class named Player with a default name as "player", a default level as 0, and a default class type of "fighter". Feel free to add any other attributes you wish! Create an instance method in the Player class called attack that takes another player as a parameter. This function should compare the level of the two players, and return whether the player won, lost, or tied. Finally create a loop that allows the user to input a name and a class type for two players; the level of both players should be determined at random (between 1 and 99). Print out the name and attributes of each player, and have the first player attack the second. Print whether the player won, lost, or tied. Ask if the user wants to try again. (Python code)arrow_forwardIn java, can a polymorphic reference invoke a method that is only declared at the object's class level? If "yes", explain how.arrow_forward
- Text book imageComputer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONText book imageComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceText book imageNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Text book imageConcepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningText book imagePrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationText book imageSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY