Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Only. 1. Design and implement a Temperature class able to handle temperatur

ID: 3887382 • Letter: J

Question

Java Only.

1.   Design and implement a Temperature class able to handle temperatures in either Fahrenheit or
     Celsius. Include appropriate constructors (including a copy constructor), getters and setters, toString,
     and belowFreezing methods. (5 pts.)

2.   For your Temperature class (from problem 1), implement an appropriate equals method. Provide examples to demonstrate the use of your equals method. (3 pts.)

3.   Give example use of objects of type Temperature (from problem 1) for each of the three means of
       object access discussed in class (and given in the lecture slides). Include sufficient code to
       demonstrate its use. (3 pts.) The three means of object access are

1. By Composition (object contains a data member which itself is an object)

2. By Parameter Passing (passing of an object to a method of a given object)

3. By Local Declaration (object declared within a method of a given object)

4.   Give sufficient code to demonstrate how a class can be designed such that it maintains how many
       instances of itself exist at any given point in time. (3 pts.)

5. Assume that there is a Java interface named Measurable. The only method in the interface is method
       getMeasure(), which returns a string describing an object’s measurement (if of type Measurement).
       Assume that there are three classes of type Measureable: Rectangle (with measurement height and
       width, in inches), Package (with measurement of weight in lbs and ounces), and WindSpeed (with
       measurement in miles per hour). Give sufficient code to demonstrate the use of polymorphism
       involving objects of each of these three types. (6 pts.)

6.   Declare an interface named Sized, containing only one method, getSize, that returns a string. Assume
       that there exist a number of classes declared of type Sized in a particular application, specifically,
       StudentRecords (where size is the number of records), SemesterSchedule (where size it the number
       of credits taking), Department (where size is the number of faculty in a particular department), and
       Building (where size is the total number of square feet). (10 pts.)

       Also declare a checked exception named UnsizedException.

       Create a class named Measurements that contains just one static method named getSize that takes         as an argument an object of any type (and returns a string type) and does the following:

                  If the object is of type String or StringBuffer, then returns a string of the form “16 characters”

                  If the object is of type Sized, and also of type

                                    StudentRecords, then returns a string of the form “ ”

                                    SemesterSchedule, then returns a string of the form “16 credits”

                                    Department, then returns a string of the form “28 faculty members”

                                    Building, then returns a string of the form “8560 square feet”

                  Otherwise, an UnsizedException is thrown.

Explanation / Answer

Below see the code for Temperature class. and along with attached the Demo class, which is going to use the methods of Temprature class.

package temprature;

public class Temprature

{

public final String celsius="CELSIUS";

public final String fahreignheit="FAHREIGNHEIT";

//to variable will hold the number of instances created till now.

// as it is a static variable it will hold the data between function calls.

public static int instancesCount=0;

private double tempInCels;

private double tempInFr;

public Temprature()

{

// increment the value by 1 when new object is created

instancesCount++;

}

public Temprature(double temp,String mesurementUnit)

{

if(celsius.equals(mesurementUnit))

{

setTempInCels(temp);

}

else if(fahreignheit.equals(mesurementUnit))

{

setTempInFr(temp);

}

instancesCount++;

//increment the value by 1 when new object is created

}

public double getTempInCels() {

return tempInCels;

}

public void setTempInCels(double tempInCels) {

this.tempInCels = tempInCels;

double fahrenheit = ((tempInCels * 9)/5) + 32;

tempInFr=fahrenheit;

}

public double getTempInFr() {

return tempInFr;

}

public void setTempInFr(double tempInFr) {

this.tempInFr = tempInFr;

double celsius=(tempInFr - 32.0) * (5.0 / 9.0);

tempInCels=celsius;

}

public String toString()

{

return "Temperature in Celsius is: "+tempInCels+" and Temperature in Fahrenheit is: "+tempInFr;

}

public boolean isBelowFreezing()

{

if(tempInCels<0.0)

{

return true;

}

return false;

}

@Override

public boolean equals(Object obj)

{

Temprature temp=(Temprature)obj;

if(temp.getTempInCels()==this.getTempInCels())

{

return true;

}

return false;

}

}

Now Attached the Demo class for usage example purpose.

package temprature;

public class Demo

{

public static void main(String args[])

{

Temprature temprature=new Temprature(68,"FAHREIGNHEIT");

Temprature temprature1=new Temprature(68,"FAHREIGNHEIT");

//Example of usage by local Declaration it is decalared locally and used locally.

if(temprature.equals(temprature1))

{

System.out.println("Tempratures are equals");

}

//Example of usage using a parameter passing.

printTempDetails(temprature);

//Example code for printing number of instances class has

System.out.println("No of instances: "+Temprature.instancesCount);

}

public static void printTempDetails(Temprature temp)

{

System.out.println(temp.toString());

}

}

Thanks and do provide me the feedback.

if you like the ans.

Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote