Question

Write a program in java (consists of three files) that uses class-objects to calculate temperature in...

Write a program in java (consists of three files) that uses class-objects to calculate temperature in Fahrenheit and Centigrade. That is, there are at least three classes. The problem is to write a three classes (3) that calculate the temperature either in Fahrenheit or Celsius. This action should be repeatable. This demonstrates use of a menu to give user a choice. Use of proper syntax for named constants, variables, reference variables, constructors, getter and setter and display methods is expected.

Algorithm(s) :

F = (XC x 1.8) + 32

C = (F – 32) / 1.8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can do this conversion in single class itself or using inheritance concept. But as per mentioned question you are asking getters(), setters() all . so, for your understanding created three classes. Fahrenheit - convert temp to Fahrenheit same with centigrade & two driver program to use above mentioned functionality.

Fahrenheit.java


public class Fahrenheit { //class creation with public access modifier access everywhere

   public double convertTempToFahrenheit(double tempInCentigrade) //method definition
   {
       return (tempInCentigrade*1.8)+32; //conversion
   }

}

Centigrade.java


public class Centigrade {
  
  
   public double convertTempToCentigrate(double tempInFahrenheit) //method definition input fahrenheit
   {
       return (tempInFahrenheit-32)/1.8; //return fahrenheit -> centigrade convertion
   }

}

TempDriver.java

import java.util.Scanner;

public class TempDriver {
   double temp;
   public static void main(String ar[])
   {
       Scanner scannerObj=new Scanner(System.in); //scanner instance creation -- for user input
       TempDriver tempDriverObj=new TempDriver(); //instance creation all three classes
       Fahrenheit fahrenHeitObj=new Fahrenheit();
       Centigrade centigradeObj=new Centigrade();
       int userChoice;
       while(true) //Asking user n times
       {
           System.out.println("1. convertTempToCentigrade \t 2. convertTempToFahrenheit \t 3. EXIT"); //Menu display
           System.out.println("Enter Choice:");
           userChoice=scannerObj.nextInt(); //input user choice
           switch(userChoice)   
           {
           case 1:
           {
               System.out.println("Enter Temperature in Fahrenheit:"); //Asking user temp in Fahrenheit
               tempDriverObj.setTemp(scannerObj.nextDouble()); //setting method to set temp based on input
           double centiTemp   =centigradeObj.convertTempToCentigrate(tempDriverObj.getTemp()); //invoking method using object
               tempDriverObj.displayTempConvetion(tempDriverObj.getTemp(), centiTemp); //display temp after convertion
               break;
           }
           case 2:
      
           {
               System.out.println("Enter Temperature in Centigrade:");
               tempDriverObj.setTemp(scannerObj.nextDouble());
           double fahrenheitTemp   =fahrenHeitObj.convertTempToFahrenheit(tempDriverObj.getTemp());
               tempDriverObj.displayTempConvetion(fahrenheitTemp,tempDriverObj.getTemp());
               break;
          
           }
           case 3: //exit
               return;
          
           }
          
          
       }
   }
   public double getTemp() {
       return temp; //getter for temp
   }
   public void setTemp(double temp) { //setting provided value to temp
       this.temp = temp;
   }
   public void displayTempConvetion(double tempInFahrenheit,double tempInCenti)
   {
       System.out.println("\t\t -- Temp Convertion Details -- \t\t"); //displaying temp
       System.out.println("\t\t -- Temp in Fahrenheit :"+tempInFahrenheit);
       System.out.println("\t\t -- Temp in Centigrade :"+tempInCenti);
      
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a program in java (consists of three files) that uses class-objects to calculate temperature in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • Java Write a Temperature class that has two private instance variables: • degrees: a double that...

    Java Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have - four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set...

  • Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit....

    Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating- point number for the temperature and a character for the scale, eitherでfor Celsius or 'F' for fahrenheit. The class should have Four constructors: one for the number of degrees, one for the scale, one for both the degrees and scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and celsius if no...

  • write program in java 5. Falling Distance When an object is falling because of gravity, the...

    write program in java 5. Falling Distance When an object is falling because of gravity, the following formula can be used to determine the distance the object falls in a specific time period: d = 1/2 gta The variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the amount of time, in seconds, that the object has been falling. Write a method named fallingDistance that accepts an object's falling time...

  • FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice...

    FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom functionc_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a...

  • Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The...

    Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a value-returning...

  • Write a program named FahrenheitToCelsius that accepts a temperature in Fahrenheit from a user and converts...

    Write a program named FahrenheitToCelsius that accepts a temperature in Fahrenheit from a user and converts it to Celsius by subtracting 32 from the Fahrenheit value and multiplying the result by 5/9. Display both values to one decimal place. For example, if 88.5 degrees is input, the output would be: 88.5 F is 31.4 C Answer in C# (Posting incomplete code i have so far that wont run correctly.) using System; using static System.Console; class FahrenheitToCelsius { static void Main()...

  • IN JAVA…Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be...

    IN JAVA…Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be able to enter a Celsius temperature, click a button, and then see the equiva-lent Fahrenheit temperature. Use the following formula to make the conversion: F = (9/5)C + 32 F is the Fahrenheit temperature and C is the Celsius temperature. Instead of only converting from Celsius to Fahrenheit, also convert from Fahrenheit to Celsius depending on the user's choice. Some hints: Use JTextField and...

  • Java please The kelvin is the base unit of temperature in the International System of Units...

    Java please The kelvin is the base unit of temperature in the International System of Units (SI), having the unit symbol K. It is named after the Belfast-born, Glasgow University engineer and physicist William Thomson, 1st Baron Kelvin (1824–1907). It uses absolute zero as its null point. The Celsius scale, also known as the centigrade scale, is a temperature scale used by the International System of Units (SI). The Celsius scale is based on 0 °C for the freezing point...

  • Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student...

    Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student and “PostGraduate” that inherits form “Graduate”. Write the class definition for the abstract class “Student”. The class definition should include private instance variables of type String to hold the student’s first name, a string for his/her major and an int to hold the number of units taken. Getter and setter methods for each of the variables should be included in the class definition. Also...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT