Question

In: Computer Science

JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...

JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements.

Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and divide(double). These four methods all return a temperature equaled to the respective operation. The returned type is that of the this. Note that the this value is not changed in these operations. Two boolean methods equals(Temperature), and greaterThan(Temperature) will return true if the this is greater than the parameter. Your class should include a read() method and a toString() method. Remember methods add, subtract, multiply, divide and the three conversion methods all return a Temperature. Include at least two constructors: a default constructor and an explicit constructor.   You must use a private helper method called set() that takes the parameters of the constructor and tests for appropriate values for each possible scale. The set method is a void method.   This private set() method can be used to guarantee temperature values are in the proper range. The add(), subtract(), multiply(), and divide() methods can call the constructor which calls the set() method. The set method will check the degree value and if it is in the proper range a new Temperature will be made to be returned by the add() method, subtract() method, multiply() method and divide() method. A switch statement should be used throughout this class when choosing between “C”, “F”, and “K”. Absolute zero for Kelvin is 0, for Fahrenheit -459.67, and for Celsius -273.15. Your program must guarantee this absolute value is not violated. For the equals() method consider changing the this temperature and the parameter temperature to the same scale and then testing the degree value for equality

public class TemperatureDemoWithoutArrays
{
public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
int x;
Temperature temp1 = new Temperature(100.0, 'C');
Temperature temp2 = new Temperature(122, 'F');
Temperature temp3 = new Temperature(32.0, 'F');
Temperature temp4 = new Temperature(100.0, 'C');
Temperature tempAve = new Temperature(50.0, 'C');
System.out.println(temp2 + " to Celcius is " +
temp2.toCelsius());
System.out.println("Temp1 is " + temp1);
temp1 = temp1.toKelvin();
System.out.println("Temp1 to Kalvin is " + temp1);
if (temp2.equals(tempAve))
{
System.out.println("These two temperatures are equal");
}
else
{
System.out.println("These two temperature are not equal");
}
System.out.println("tempAve is " + tempAve);
System.out.println("temp1 is " + temp1);
System.out.println("temp2 is " + temp2);
System.out.println("temp3 is " + temp3);
System.out.println("temp4 is " + temp4);
tempAve = tempAve.add(temp1);
tempAve = tempAve.add(temp2);
tempAve = tempAve.add(temp3);
tempAve = tempAve.add(temp4);
tempAve = tempAve.divide(5);
System.out.println("The average temperature is " + tempAve);
temp2 = new Temperature(150.0, 'k');
temp4 = new Temperature(100.0, 'c');
System.out.print("Subtracting " + temp2 + " from " + temp4 +" gives " );
temp4 = temp4.subtract(temp2);
System.out.println(temp4);
}
}

Solutions

Expert Solution

There are many problems in your testing class. I have implemented exactly as required.

Code:

Temperature.java


public class Temperature {
   private double temperature;
   private char type;
   public Temperature(){
       temperature=0;
       type='C';
   }
  
   public Temperature(double temperature,char type){
       this.type=type;
       set(temperature);
   }
  
   public double getTemperature(){
       return temperature;
   }
  
   public char getType(){
       return type;
   }

   private void set(double temperature){
       double temp;
       switch(this.type){
       case 'C':
           if(temperature<-273.15){
               System.out.println("Celcius temperature should not be less than 273.15.\nSetting to minimum value.");
               temp=-273.15;
           }
               else{
                   temp=temperature;  
               }
           break;
       case 'F':
           if(temperature<-459.67){
               System.out.println("Fahrenheit temperature should not be less than 459.67.\nSetting to minimum value.");
               temp=-459.67;
           }
               else{
                   temp=temperature;  
               }
           break;
       case 'K':
           if(temperature<0){
               System.out.println("Kelvin temperature should not be less than 0.\nSetting to minimum value.");
               temp=0;
           }
               else{
                   temp=temperature;  
               }
           break;
       }
       this.temperature=temperature;
   }
   public Temperature toCelsius(){
       double temp=this.temperature;
       switch(this.type){
       case 'C':
           temp=this.temperature;
           break;
       case 'F':
           temp=(this.temperature-32)*(5/9.0);
           break;
       case 'K':
           temp=(this.temperature-273.15);
           break;
       }
       Temperature obj=new Temperature(temp,'C');
       return obj;
      
   }
   public Temperature toFahrenheit(){
       double temp=this.temperature;
       switch(this.type){
       case 'F':
           temp=this.temperature;
           break;
       case 'C':
           temp=(this.temperature*1.8)+32;
           break;
       case 'K':
           temp=(this.temperature*9/5-459.67);
           break;
       }
       Temperature obj=new Temperature(temp,'F');
       return obj;
   }

   public Temperature toKelvin(){
       double temp=this.temperature;
       switch(this.type){
       case 'K':
           temp=this.temperature;
           break;
       case 'C':
           temp=(this.temperature+273.15);
           break;
       case 'F':
           temp=(this.temperature+459.67)*5/9.0;
           break;
       }
       Temperature obj=new Temperature(temp,'F');
       return obj;
   }
  
   public Temperature add(Temperature obj){
       double temp=this.temperature;
       switch(this.type){
       case 'K':
           temp=this.temperature+obj.toKelvin().getTemperature();
           break;
       case 'C':
           temp=this.temperature+obj.toCelsius().getTemperature();
           break;
       case 'F':
           temp=this.temperature+obj.toFahrenheit().getTemperature();
           break;
       }
       Temperature obj2=new Temperature(temp,this.type);
       return obj2;
   }
  
   public Temperature subtract(Temperature obj){
       double temp=this.temperature;
       switch(this.type){
       case 'K':
           temp=this.temperature-obj.toKelvin().getTemperature();
           break;
       case 'C':
           temp=this.temperature-obj.toCelsius().getTemperature();
           break;
       case 'F':
           temp=this.temperature-obj.toFahrenheit().getTemperature();
           break;
       }
       Temperature obj2=new Temperature(temp,this.type);
       return obj2;
   }
  
   public Temperature multiply(Temperature obj){
       double temp=this.temperature;
       switch(this.type){
       case 'K':
           temp=this.temperature*obj.toKelvin().getTemperature();
           break;
       case 'C':
           temp=this.temperature*obj.toCelsius().getTemperature();
           break;
       case 'F':
           temp=this.temperature*obj.toFahrenheit().getTemperature();
           break;
       }
       Temperature obj2=new Temperature(temp,this.type);
       return obj2;
   }
  
   public Temperature divide(double d){
       double temp=this.temperature/d;
       Temperature obj2=new Temperature(temp,this.getType());
       return obj2;
   }
  
  
   public boolean equals(Temperature obj){
       switch(this.type){
       case 'K':
           return (this.temperature==obj.toKelvin().getTemperature());
       case 'C':
           return (this.temperature==obj.toCelsius().getTemperature());
       case 'F':
           return (this.temperature==obj.toFahrenheit().getTemperature());
       }
       return true;
   }
  
   public boolean greaterThan(Temperature obj){
       switch(this.type){
       case 'K':
           return (this.temperature>=obj.toKelvin().getTemperature());
       case 'C':
           return (this.temperature>=obj.toCelsius().getTemperature());
       case 'F':
           return (this.temperature>=obj.toFahrenheit().getTemperature());
       }
       return true;
   }
}

TemperatureDemoWithoutArrays.java


public class TemperatureDemoWithoutArrays{
   public static final int ARRAY_SIZE = 5;
   public static void main(String[] args)
   {
   int x;
   Temperature temp1 = new Temperature(100.0, 'C');
   Temperature temp2 = new Temperature(122, 'F');
   Temperature temp3 = new Temperature(32.0, 'F');
   Temperature temp4 = new Temperature(100.0, 'C');
   Temperature tempAve = new Temperature(50.0, 'C');
   System.out.println(temp2.getTemperature() + " to Celcius is " +
   temp2.toCelsius().getTemperature());
   System.out.println("Temp1 is " + temp1.getTemperature());
   temp1 = temp1.toKelvin();
   System.out.println("Temp1 to Kalvin is " + temp1.getTemperature());
   if (temp2.equals(tempAve))
   {
   System.out.println("These two temperatures are equal");
   }
   else
   {
   System.out.println("These two temperature are not equal");
   }
   System.out.println("tempAve is " + tempAve.getTemperature());
   System.out.println("temp1 is " + temp1.getTemperature());
   System.out.println("temp2 is " + temp2.getTemperature());
   System.out.println("temp3 is " + temp3.getTemperature());
   System.out.println("temp4 is " + temp4.getTemperature());
   tempAve = tempAve.add(temp1);
   tempAve = tempAve.add(temp2);
   tempAve = tempAve.add(temp3);
   tempAve = tempAve.add(temp4);
   tempAve = tempAve.divide(5);
   System.out.println("The average temperature is " + tempAve.getTemperature());
   temp2 = new Temperature(150.0, 'K');
   temp4 = new Temperature(100.0, 'C');
   System.out.print("Subtracting " + temp2.getTemperature() + "K from " + temp4.getTemperature() +"C gives " );
   temp4 = temp4.subtract(temp2);
   System.out.println(temp4.getTemperature()+"C");
   }
}

Output:


Related Solutions

JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
write a java code, please do not use method and demo Consider a four digit number...
write a java code, please do not use method and demo Consider a four digit number such as 6587. Split it at two digits, as 65 and 87. Sum of 65 and 87 is 152. Sum of the digits of 152 is 8. Given the starting and ending four digit numbers and a given target number such as 10, write a program to compute and print the number of instances where the sum of digits equals the target.
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
I need pure html and css code for a demo website that sells stuff online (example...
I need pure html and css code for a demo website that sells stuff online (example amazon). I need a webpage that is about a specific product (shows price, image, description and etc etc.) Use of CSS is must. Use of Javascript would be an asset. Urgently needed.
pls, I need Matlab code for, OFDM modulation (Matlab demo by at least 4 carriers)
pls, I need Matlab code for, OFDM modulation (Matlab demo by at least 4 carriers)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT