Question
Part two Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must
Part two
Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where arrays can be used. After the topic of arrays has been covered create arrays of class Temperature. Write a program that satisfies the next 6 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.
1) Create a static void method that has an array parameter and is called 3 times to read in Temperature values for each array.
2) Create a static method that computes and returns the average Temperature for each array and is also called 3 times.
3) Create a static method that prints the Temperatures of an array.
4) Create a static helper method that has 3 array parameters and either returns the largest array or the largest size.
5) 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.
6) 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 test value Temperatures that demonstrate that your program runs correctly.
Here is the demo with arrays for part two
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
Here is my temperature class for part one
import java.util.Scanner; public class Temperature{ double temperature; char degree;
public Temperature(){} public Temperature(double temperature,char degree){ this.temperature = temperature; this.degree = degree; }
Temperature toCelsius(){ Temperature temp = new Temperature(); temp.degree='C'; if(degree == 'K' || degree =='k'){ temp.temperature = temperature - 273.15; }else if(degree == 'F' || degree =='f'){ temp.temperature = (temperature -32.0) *(5.0/9.0); }else{ temp.temperature = temperature; }
return temp; }
Temperature toKelvin(){ Temperature temp = new Temperature(); temp.degree='K'; if(degree == 'C' || degree =='c'){ temp.temperature = temperature + 273.15; }else if(degree == 'F' || degree =='f'){ temp.temperature = ((temperature - 32.0) *(5.0/9.0)) + 273.15; }else{ temp.temperature = temperature; } return temp; }
Temperature toFahrenheit(){ Temperature temp = new Temperature(); temp.degree='F'; if(degree == 'C' || degree =='c'){ temp.temperature = (temperature * (9.0/5.0)) + 32.0; }else if(degree == 'K' || degree =='k'){ temp.temperature = ((temperature - 273.15) * (9.0/5.0)) + 32.0; }else{ temp.temperature = temperature; } return temp; }
Temperature add(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); }
temp.temperature = this.temperature + temp.temperature; return temp; }
Temperature subtract(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); }
temp.temperature = this.temperature - temp.temperature; return temp;
}
Temperature multiply(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); }
temp.temperature = this.temperature * temp.temperature; return temp; }
Temperature divide(double d){ Temperature temp = new Temperature(); temp.temperature = this.temperature / d; temp.degree = this.degree; return temp; }
boolean equals(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); } if(temp.temperature == this.temperature){ return true; }else{ return false; } }
boolean greaterThan(Temperature t){ Temperature temp = new Temperature(); if(this.degree=='C'){ temp = t.toCelsius(); }else if(this.degree =='F'){ temp = t.toFahrenheit(); }else if(this.degree =='K'){ temp = t.toKelvin(); } if(this.temperature > temp.temperature){ return true; }else{ return false; } }
public void read(){ Scanner sc = new Scanner(System.in);
System.out.print("Enter Temperature: "); this.temperature = sc.nextDouble(); System.out.print("Enter Degree(C,F,K): "); sc.nextLine(); char d = sc.nextLine().charAt(0); if(d=='C' || d=='c' || d=='K' || d=='k'|| d=='F' || d=='f'){ this.degree = d; }else{ System.out.println("Wrong degree entered"); } }
public String toString(){ return temperature +" "+degree; }
}
Here is the 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
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started