Consider the following code fragment Write a toString method

Consider the following code fragment Write a toString method that produces a String in the form \": / mmHG\", where are replaced with instance variables. Write an equals method that returns true if the names arc equal, the systolic bp numbers are equal, and the diastolic bp numbers are equal. The method should return false otherwise.

Solution

public class BloodPressure {

   private String name;
   private int systolic;
   private int diastolic;

   /**
   * @param name
   * @param systolic
   * @param diastolic
   */
   public BloodPressure(String name, int systolic, int diastolic) {

       this.name = name;
       this.systolic = systolic;
       this.diastolic = diastolic;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @return the systolic
   */
   public int getSystolic() {
       return systolic;
   }

   /**
   * @return the diastolic
   */
   public int getDiastolic() {
       return diastolic;
   }

   /*
   * Problem 4 (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return getName() + \": \" + getSystolic() / getDiastolic() + \"mmHG\";
   }

   /*
   * Problem 5 (non-Javadoc)
   *
   * @see java.lang.Object#equals(java.lang.Object)
   */
   @Override
   public boolean equals(Object obj) {
       if (obj instanceof BloodPressure) {
           BloodPressure bloodPressure = (BloodPressure) obj;
           if ((bloodPressure.getName().equals(getName())
                   && (bloodPressure.getDiastolic() == getDiastolic()) && (bloodPressure
                       .getSystolic() == getSystolic())))
               return true;
           else
               return false;
       } else

           return false;
   }
}

 Consider the following code fragment Write a toString method that produces a String in the form \
 Consider the following code fragment Write a toString method that produces a String in the form \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site