Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The instructions were : Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods

The instructions were:

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 in these conversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(double). These four methods all return a temperature equaled 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 three convesion methods all return a Temperature. Include a least two constructors: a default and an explicit. Use a private helper called set() that takes the parameters of the constructor and tests for appropriate values for each possible scale. This private set() method can be used to guarantee temperature values 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 throughout this class when choosing between "C", "K", and "F". Absolute zero for Kelvin is 0, for Fahrenheit -459.67, and for 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.

Given the Demo class

public class TemperatureDemoWithoutArray

{

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);

}

}

My answer was:

import java.util.Scanner;

public class Temperature {

public static Scanner keyboard = new Scanner(System.in);

private double degree;

private char scale;

public Temperature() {} // default constructor

public Temperature(double other, char unit) {

set(other, unit);

}

public void read() {

System.out.println("Please enter the VacuumBottle for the given array, like ( 3 44.00 C/F/K). ");

this.degree = keyboard.nextDouble();

Scanner keyboard= new Scanner(System.in);

System.out.println("Now enter the Scale C,F, or K.");

this.scale = keyboard.nextLine().toUpperCase().charAt(0);

}

public double getDegree(){

return this.degree;

}

public char getScale(){

return this.scale;

}

public void setDegree(double x){

this.degree=x;

}

public void setScale(char y){

this.scale=y;

}

public void set(double temp, char unit) { // takes a double num temp and unit for checking

this.scale = unit;

switch (this.scale) {

case 'K': {

if (temp < 0) {

System.out.println("Invalid entery! kelvin cannot be less than \"0\" degree");

System.exit(0);

} else {

this.degree = temp;

}

break;

}

case 'F': {

if (temp < -459.67) {

System.out.println("Invalid entery! degree Faheranheit cannot be less than \"-459.67\" degree");

System.exit(0);

} else {

this.degree = temp;

}

break;

}

case 'C': {

if (temp < -273.15) {

System.out.println("Invalid entery! Celciuse cannot be less than \"-459.67\" degree");

System.exit(0);

} else {

this.degree = temp;

break;

}

}

}

}

public Temperature toCelsius() {

Temperature temptoCelsius = new Temperature();

switch (this.scale) {

case 'C':

temptoCelsius.degree = this.degree;

temptoCelsius.scale='C';

break;

case 'K':

temptoCelsius.degree = this.degree - 273.15;

temptoCelsius.scale='K';

break;

case 'F':

temptoCelsius.degree = (this.degree - 32) / 1.8;

temptoCelsius.scale='F';

break;

}

return temptoCelsius;

}

public Temperature toKelvin() {

Temperature temptoKelvin = new Temperature();

switch (this.scale) {

case 'K':

temptoKelvin.degree = this.degree;

temptoKelvin.scale='K';

break;

case 'C':

temptoKelvin.degree = this.degree + 273.15;

temptoKelvin.scale='C';

break;

case 'F':

temptoKelvin.degree = (this.degree + 459.67) / 1.8;

temptoKelvin.scale='F';

break;

}

return temptoKelvin;

}

public Temperature toFahrenheit() {

Temperature temptoFahrenheit = new Temperature();

switch (this.scale) {

case 'F':

temptoFahrenheit.degree = this.degree;

temptoFahrenheit.scale='F';

break;

case 'C':

temptoFahrenheit.degree = (1.8 * this.degree + 32);

temptoFahrenheit.scale='C';

break;

case 'K':

temptoFahrenheit.degree = (1.8 * this.degree - 459.67);

temptoFahrenheit.scale='K';

break;

}

return temptoFahrenheit;

}

public Temperature add(Temperature other) {

Temperature sum = new Temperature();

switch (this.scale) {

case 'C':

sum.degree = this.degree + other.toCelsius().degree;

sum.scale='C';

break;

case 'K':

sum.degree = this.degree + other.toKelvin().degree;

sum.scale='K';

break;

case 'F':

sum.degree = this.degree + other.toFahrenheit().degree;

sum.scale='F';

break;

}

return sum;

}

public Temperature subtract(Temperature other) {

Temperature sub = new Temperature();

switch (this.scale) {

case 'C':

sub.degree = this.degree - other.toCelsius().degree;

sub.scale='C';

break;

case 'K':

sub.degree = this.degree - other.toKelvin().degree;

sub.scale='K';

break;

case 'F':

sub.degree = this.degree - other.toFahrenheit().degree;

sub.scale='F';

break;

}

sub.set(sub.degree, this.scale);

return sub;

}

public Temperature multiply(Temperature other) {

Temperature multiply = new Temperature();

switch (this.scale) {

case 'C':

multiply.degree = this.degree * other.toCelsius().degree;

multiply.scale='C';

break;

case 'K':

multiply.degree = this.degree * other.toKelvin().degree;

multiply.scale='K';

break;

case 'F':

multiply.degree = this.degree * other.toFahrenheit().degree;

multiply.scale='F';

break;

}

return multiply;

}

public Temperature divide(double other) {

Temperature divide = new Temperature();

divide.degree = this.degree / other;

divide.scale=this.scale;

divide.set(divide.degree, this.scale);

return divide;

}

public boolean equals(Temperature other) {

boolean answer = false;

switch (this.scale) {

case 'C': {

if (this.degree == other.toCelsius().degree||((this.degree-other.toCelsius().degree>(-0.01) && this.degree-other.toCelsius().degree<0.01))) {

answer = true;

}

break;

}

case 'F': {

if ((this.degree == other.toFahrenheit().degree)||((this.degree-other.toFahrenheit().degree>(-0.01))&&(this.degree-other.toFahrenheit().degree<(0.01)))) {

answer = true;

}

break;

}

case 'K': {

if ((this.degree == other.toFahrenheit().degree)||((this.degree-other.toKelvin().degree>(-0.01))&&(this.degree-other.toKelvin().degree)<0.01)) {

answer = true;

}

break;

}

}

return answer;

}

public boolean greaterThan(Temperature other) {

boolean answer = false;

switch (this.scale) {

case 'C':

if (this.degree > other.toCelsius().degree) {

answer = true;

}

break;

case 'F':

if (this.degree > other.toFahrenheit().degree) {

answer = true;

}

break;

case 'K':

if (this.degree > other.toKelvin().degree) {

answer = true;

}

break;

}

return answer;

}

public String toString() {

return " " + this.degree + " degree " + this.scale;

}

}

//************************************************

Now I have to do the array part: I need help on it.

DO THIS!

After the topic of arrays has been covered create arrays of class Temperature. Write a program that satisfies the next 8 requirements using Temperatures. Create a class similar to the Math class. Put the next 5 static methods in it. (These static methods could also be in the Demo class.) On my website are two example programs that demonstrate passing arrays and returning arrays: Passing Arrays using static methods and ChangeArgumentDemo. Use the random number generator as described on the bottom of page 412 to create 3 arrays of random sizes of 1 to 5 elements.

P 412: int die = (int)(6.0 * Math.random()) + 1;

Create a static void method that has an array parameter and is called 3 times to read in Temperature values for each array.

Create a static method that computes and returns the average Temperature for each array and is called 3 times.

Create a static method that prints the Temperatures of an array.

Create a static helper method that has 3 array parameters and either returns the largest array or the largest size.

Create a static method that returns an array of Temperatures that has the same number of elements as the largest of the three arrays. This method will have 3 array parameters and possibly an integer parameter. It determines the largest Temperature value from the three arrays at each index and creates a copy of this Temperature and stores it at that index of the new array. This array is then returned.

As this program is running it should generate user friendly text for input and output explaining what the program is doing. Create a set of value Temperatures the demonstrate that your program runs correctly. (My website gives sample output to follow.)

The new ArrayDemo given:

public class TemperatureDemoWithArrays 
{ 
 public static void main(String[] args) 
 { 
 int x; 
 Temperature average; 
 System.out.println("You will be asked to fill 3 randomly sized arrays."); 
 Temperature[] temperatureArrayOne; 
 Temperature[] temperatureArrayTwo; 
 Temperature[] temperatureArrayThree; 
 
 temperatureArrayOne = new Temperature[getRandomArraySize()]; 
 readTemperatureArray(temperatureArrayOne); 
 System.out.println("The values of temperature array one are "); 
 printTemperatureArray(temperatureArrayOne); 
 average = getAverage(temperatureArrayOne); 
 System.out.println("The average of temperature array one is " + average); 
 
 temperatureArrayTwo = new Temperature[getRandomArraySize()]; 
 readTemperatureArray(temperatureArrayTwo); 
 System.out.println("The values of temperature array two are "); 
 printTemperatureArray(temperatureArrayTwo); 
 average = getAverage(temperatureArrayTwo); 
 System.out.println("The average of temperature array two is " + average); 
 
 temperatureArrayThree = new Temperature[getRandomArraySize()]; 
 readTemperatureArray(temperatureArrayThree); 
 System.out.println("The values of temperature array three are "); 
 printTemperatureArray(temperatureArrayThree); 
 average = getAverage(temperatureArrayThree); 
 System.out.println("The average of temperature array three is " + average); 
 
 Temperature[] largest = getLargestArray(temperatureArrayOne, temperatureArrayTwo, 
 temperatureArrayThree); 
 Temperature[] arrayWithLargestValues1; 
 if(temperatureArrayOne == largest) 
 arrayWithLargestValues1 = createArrayWithLargestValues(largest, 
 temperatureArrayTwo, temperatureArrayThree); 
 else if(temperatureArrayTwo == largest) 
 arrayWithLargestValues1 = createArrayWithLargestValues(largest, 
 temperatureArrayOne, temperatureArrayThree); 
 else// fractionArrayThree is largest 
 arrayWithLargestValues1 = createArrayWithLargestValues(largest, 
 temperatureArrayOne, temperatureArrayTwo); 
 System.out.println(" Here are the temperatures in the three arrays:"); 
 printTemperatureArray(temperatureArrayOne); 
 printTemperatureArray(temperatureArrayTwo); 
 printTemperatureArray(temperatureArrayThree); 
 System.out.println(" An array with the largest values taken from the "+ 
 "3 arrays has " + arrayWithLargestValues1.length + " elements."); 
 System.out.println("This solution knew the largest array of the three arrays:"); 
 printTemperatureArray(arrayWithLargestValues1); 
//CODE THAT FOLLOWS IS A DIFFERENT ALGORITH THAT ALSO FINDS THE LARGEST ARRAY 
 int sizeOfLargestArray = getSizezOfLargestArray(temperatureArrayOne, temperatureArrayTwo, 
 temperatureArrayThree); 
 Temperature[] arrayWithLargestValues = createArrayWithLargestValues(sizeOfLargestArray, 
 temperatureArrayOne,temperatureArrayTwo, temperatureArrayThree); 
 System.out.println(" An array with the largest values taken from the "+ 
 "3 arrays has " + arrayWithLargestValues.length + " elements."); 
 System.out.println("This solution knew the largest size of the three arrays:"); 
 printTemperatureArray(arrayWithLargestValues); 
 }//main 

OutPut should look like:

You will be asked to fill 3 randomly sized arrays. 
 
This array has 1 temperature(s). 
please enter a temperature in this form 45 C 
300 c 
The values of temperature array one are 
300.0C 
The average of temperature array one is 300.0C 
 
This array has 4 temperature(s). 
please enter a temperature in this form 45 C 
122 f 
please enter a temperature in this form 45 C 
0 c 
please enter a temperature in this form 45 C 
100 c 
please enter a temperature in this form 45 C 
50 c 
The values of temperature array two are 
122.0F, 0.0C, 100.0C, 50.0C 
The average of temperature array two is 122.0F 
 
This array has 3 temperature(s). 
please enter a temperature in this form 45 C 
212 f 
please enter a temperature in this form 45 C 
50 c 
please enter a temperature in this form 45 C 
32 f 
The values of temperature array three are 
212.0F, 50.0C, 32.0F 
The average of temperature array three is 122.0F 
 
Here are the temperatures in the three arrays: 
300.0C 
122.0F, 0.0C, 100.0C, 50.0C 
212.0F, 50.0C, 32.0F 
 
An array with the largest values taken from the 
3 arrays has 4 elements. 
This solution knew the largest array of the three arrays: 
300.0C, 50.0C, 100.0C, 50.0C 
 
An array with the largest values taken from the 
3 arrays has 4 elements. 
This solution knew the largest size of the three arrays: 
300.0C, 50.0C, 100.0C, 50.0C 

Do the Array part!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago