Question

In: Computer Science

Define a class to represent a Temperature. Your class should contain Instance variable int fahrenheit A...

Define a class to represent a Temperature. Your class should contain

  • Instance variable int fahrenheit
  • A parameterized constructor;
  • Member method to calculate the equivalent temperature in Celsius unit toCelsius() with decimal value.
    • double toCelsius()
    • Conversion formula: celsius= (fahrenheit - 32) * 5/9
  • Method definition to override the equals() method
    • boolean equals(Object obj)

Solutions

Expert Solution

The code for the given problem statement is given below. Since programming language is not mentioned so Java is used to implement. All the steps are given in comments and output is given. It has class Temperature which has two methods. toCelsius is used to convert Fahrenheit to celcius and returns Celsius value and equals compares two objects and returns true if both objects are equal.

//Code starts here

//This class Temperature has two methods : Convert Fahernheit to Celcius and to compare Objects
public class Temperature
{
   int fahrenheit; //Instance variable to store Fahernheit temperature
public Temperature(int fahrenheit){ //parameterized constructor
this.fahrenheit = fahrenheit;
}
public double toCelsius(){ //Member method to calculate the equivalent temperature in Celsius unit with decimal value.
double celsius= (((double)this.fahrenheit - 32) * 5/9)/100 * 100; //Conversion formula from Fahernheit to Celsius
return celsius; //return Celsius temperature
}
public boolean equals(Object obj){ //Method definition to override the equals() method. It compares objects
if(obj == this) //If object is compared with itself return true
return true;
if(!(obj instanceof Temperature)) // If given object is not instance of Temperature class return false
return false;
Temperature temperature = (Temperature) obj; // Otherwise convert object to temperature object and compare
return (fahrenheit == temperature.fahrenheit); //return true if fahrenheit temperature are same otherwise false
}
   public static void main(String[] args) {
       Temperature t = new Temperature(43); //First object of Temperature class
       System.out.println("42 in Fahrenheit is equal to : " + t.toCelsius() + " Celsius"); //Call toCelsius method to convert Fahernheit to Celsius
       Temperature t2 = new Temperature(42); //Second object of Temperature class
       System.out.println("Are objects equal?");
       System.out.println(t.equals(t2)); //Call equals method to compare two objects
   }
}

//Code ends here

Output


Related Solutions

Write a C++ program test.cpp. The class should contain a protected int member variable var, which...
Write a C++ program test.cpp. The class should contain a protected int member variable var, which is initialized with an integer value between 1 and 50 in a constructor that takes an integer parameter. The class should contain a public member function called play that should print out a sequence of integers as a result of iteratively applying a math function f to the member variable var. The function f is defined as f(x) = (3x+1)/2 if x is odd...
(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
In Java The Order class should have:  Seven instance variables: the order number (an int),...
In Java The Order class should have:  Seven instance variables: the order number (an int), the Customer who made the order, the Restaurant that receives the order, the FoodApp through which the order was placed, a list of food items ordered (stored as an array of FoodItem), the total number of items ordered (an int), and the total price of the order (a double).  A class constant to set the maximum number of items that can be ordered...
Define a class for the student record. The class should instance variables for Quizzes, Midterm, Final...
Define a class for the student record. The class should instance variables for Quizzes, Midterm, Final and total score for the course and final letter grade. The class should have input and output methods. The input method should not ask for the final numeric grade, nor should it ask for final letter grade. The classes should have methods to compute the overall numeric grade and the final letter grade. These two methods will be void methods that set the appropriate...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
Write a Temperature class that will hold a temperature in Fahrenheit, and provide methods to get...
Write a Temperature class that will hold a temperature in Fahrenheit, and provide methods to get the temperature in Fahrenheit, Celsius and Kelvin. The class should have the following field: ftemp – A double that hold a Fahrenheit temperature. The class should have the following methods: Constructor – The constructor accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field. setFahrenheit – The setFahrenheit method accepts a Fahrenheit temperature (as a double) and stores it in...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have a constructor Sentence(int size), where size determines the length of the sentence field of a given Sentence. The class should have an instance method public boolean isValid() that determines the validity of a sentence according to the rules detailed below. Also create a public nested class Word, with instance variables String value and Type type. Within this class, you must create: A public enum...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT