Question

In: Computer Science

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 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 threee convesion methods all return a Temperature. Include a least two constructore: a default and a explicit. Use a private helper called set() that takes the parameters of the constructor and tests for appropiate values for each possible scale. This private set() method can be used to guarantee temperrature valuese are in the proper range. The subtract() and divide() methods can call the constructor to return a temperature in a legal range. A switch statement should be used throughtout this class when choosong between "C", "K", and "F". Absolute zero for Kelvin is 0, for Fahrenheit -459.67, and fro Calvin -273.15. Your program must guarantee this absolute value is not violated. For the equal() method consider changing the this temperature and the parameter temperature to the same scale and then testing the degree value for equality.

Driver:

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 temperatrure is " + tempAve); System.out.print("Subtracting " + temp2 + " from " + temp4 +" gives " ); temp4 = temp4.subtract(temp2); System.out.println(temp4); } }

Then the output should look like this:

Solutions

Expert Solution

// Temperature.java
class Temperature
{

public double temp;
public char temperatureScale;
//Empty constructor
public Temperature(){}
  
// Constructor
public Temperature(double temp, char temperatureScale)
{
this.temp=temp;
this.temperatureScale=temperatureScale;
}
  
// Method to convert the given temperature into kelvin
public Temperature toKelvin()
{
double conv_Temp;

switch(temperatureScale)
{
case 'C' :
conv_Temp=(temp+273.15);
Temperature t = new Temperature(conv_Temp, 'K');
return t;
case 'F' :
conv_Temp=(double)((temp-32)*5.0/9.0+273.15);
t = new Temperature(conv_Temp, 'K');
return t;
}

if(temp < 0)
{
System.out.println("K cannot be < 0");
System.exit(0);
}
return this;
}

// Method to convert the given temperature into Celsius
public Temperature toCelsius()
{
double conv_Temp;
switch(temperatureScale)
{
case 'K' :
conv_Temp=temp-273.15;
Temperature t = new Temperature(conv_Temp, 'C');
return t;
case 'F' :
conv_Temp=(double)((temp-32)*5.0/9.0);
t = new Temperature(conv_Temp, 'C');
return t;
}

if(temp < -273.15)
{
System.out.println("C cannot be < -273.15");
System.exit(0);
}
return this;
}

// Method to convert the given temperature into Fahrenheit
public Temperature toFahrenheit()
{
double conv_Temp;

switch(temperatureScale)
{
case 'K' :
conv_Temp=(double)(temp -273.15)*9.0/5.0+32;
Temperature t = new Temperature(conv_Temp, 'F');
return t;
case 'C' :
conv_Temp=(double)(temp*9.0/5.0 +32);
t = new Temperature(conv_Temp, 'F');
return t;
}

if(temp < -459.67)
{
System.out.println("F cannot be < -459.67");
System.exit(0);
}
return this;
}

// Method to add the temperature values
public Temperature add(Temperature n)
{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();
return new Temperature(temp1.temp+temp2.temp, 'K');
}

// Method to subtract the temperature values
public Temperature subtract(Temperature n)
{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();
return new Temperature(temp1.temp-temp2.temp, 'K');
}

// Method to multiply the temperature values
public Temperature multiply(Temperature n)
{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();
return new Temperature(temp1.temp*temp2.temp, 'K');
}

// Method to divide the temperature values
public Temperature divide(double d)
{
Temperature temp1 = this.toKelvin();
double new_temp=(double)(temp1.temp/d);
return new Temperature(new_temp, 'K');
}

// Method to compare the temperature values
public boolean equals(Temperature n)
{
return this.temp==n.temp&&this.temperatureScale==n.temperatureScale;
}

// Method to convert the temperature values into a string
public String toString()
{
return "" + temp + " " + temperatureScale ;
}

// Method to read the temperature values
public void read()
{
Temperature[] array = new Temperature[5];
for (int i = 0; i < array.length; i++)
{
array[i] = new Temperature();
}

}
}

// 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 + " 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);
System.out.print("Subtracting " + temp2 + " from " + temp4 +" gives " );
temp4 = temp4.subtract(temp2);
System.out.println(temp4);
  
}
  
}

/*
output:

122.0 F to Celcius is 50.0 C
Temp1 is 100.0 C
Temp1 to Kalvin is 373.15 K
These two temperature are not equal
tempAve is 50.0 C
temp1 is 373.15 K
temp2 is 122.0 F
temp3 is 32.0 F
temp4 is 100.0 C
The average temperature is 333.15 K
Subtracting 122.0 F from 100.0 C gives 50.0 K

*/


Related Solutions

Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
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.
please write a java code, one for method and another for the Demo to: -Compute the...
please write a java code, one for method and another for the Demo to: -Compute the average age of all female students. -Compute the least amount of credits completed among males. Store the three arrays in the demo file. Print the results within the demo file. String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"}; int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21,...
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 Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
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...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in...
For My Programming Lab - Java: 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 holds 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 set Fahrenheit method accepts...
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT