Question

In: Computer Science

Of Temperature class Arrays are a very powerful data structure with which you must become very...

Of Temperature class

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 an 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.

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 also 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.

Here is a demo class:

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("\nHere are the temperatures in the three arrays:");
printTemperatureArray(temperatureArrayOne);
printTemperatureArray(temperatureArrayTwo);
printTemperatureArray(temperatureArrayThree);
System.out.println("\nAn array with the largest values taken from the\n"+
"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("\nAn array with the largest values taken from the\n"+
"3 arrays has " + arrayWithLargestValues.length + " elements.");
System.out.println("This solution knew the largest size of the three arrays:");
printTemperatureArray(arrayWithLargestValues);
}//main



The output expected:

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

Temperature class:

// Temperature.java
import java.util.Scanner;
public class Temperature {
//C = Celcius, F = Fahrenheir, K = Kelvin
private double temp;
private char scale;
public Temperature() {
set(0, 'C');
}
public Temperature(double temp, char scale) {
//this.temp = temp;
//this.scale = scale;
set(temp, scale);
}
public Temperature toCesius() {
Temperature ret_temp = new Temperature();
double t = 0;
switch (this.scale)
{
case 'C':
ret_temp = this;
break;
case 'F':
t = (this.temp - 32) * 5/9;
ret_temp.set(t, 'C');
break;
case 'K':
t = this.temp - 273.15;
ret_temp.set(t,'C');
break;
}
return ret_temp;
  
}
public Temperature toKelvin() {
Temperature ret_temp = new Temperature();
double t = 0;
  
switch (this.scale)
{
case 'K':
ret_temp = this;
break;
case 'C':
t = this.temp + 273.15;
ret_temp.set(t, 'K');
break;
case 'F':
t = (this.temp - 32) * 5/9;
ret_temp.set(t,'K');
break;
}
return ret_temp;
}
public Temperature toFahrenheit() {
Temperature ret_temp = new Temperature();
double t = 0;
  
switch (this.scale)
{
case 'F':
ret_temp = this;
break;
case 'C':
t = (this.temp * 9/5) + 32;
ret_temp.set(t, 'F');
break;
case 'K':
t = (this.temp - 273.15) * 9/5 + 32;
ret_temp.set(t,'F');
break;
}
return ret_temp;
}
public Temperature divide(double temp) {
Temperature ret_temp = new Temperature();
  
ret_temp.set(this.temp / temp, this.scale);
return ret_temp;

}
public Temperature multiply(Temperature temp) {
Temperature ret_temp = new Temperature();
Temperature other_temp = new Temperature();
//Convert input temp to the same scale with the this
switch (this.scale){
case 'C':
other_temp = temp.toCesius();
break;
case 'F':
other_temp = temp.toFahrenheit();
break;
case 'K':
other_temp = temp.toKelvin();
break;
}
ret_temp.set(this.temp * other_temp.getTemp(), this.scale);
return ret_temp;

}
public Temperature subtract(Temperature temp) {
Temperature other_temp = new Temperature();
Temperature ret_temp = new Temperature();
//Convert input temp to the same scale with the this
switch (this.scale){
case 'C':
other_temp = temp.toCesius();
break;
case 'F':
other_temp = temp.toFahrenheit();
break;
case 'K':
other_temp = temp.toKelvin();
break;
}
ret_temp.set(this.temp - other_temp.getTemp(), this.scale);
return ret_temp;

}
public Temperature add(Temperature temp) {
Temperature other_temp = new Temperature();
Temperature ret_temp = new Temperature();
//Convert input temp to the same scale with the this
switch (this.scale){
case 'C':
other_temp = temp.toCesius();
break;
case 'F':
other_temp = temp.toFahrenheit();
break;
case 'K':
other_temp = temp.toKelvin();
break;
}
  
ret_temp.set(this.temp + other_temp.getTemp(), this.scale);
return ret_temp;

}
public boolean equals(Temperature temp){
if (this.scale == temp.getScale()){
return (this.temp == temp.getTemp());
}
else{
Temperature other_temp = new Temperature();
//Convert input temp to the same scale with the this
switch (this.scale){
case 'C':
other_temp = temp.toCesius();
break;
case 'F':
other_temp = temp.toFahrenheit();
break;
case 'K':
other_temp = temp.toKelvin();
break;
}
  
return (this.temp == other_temp.getTemp());
}
  
}
public boolean greaterThan(Temperature temp) {
if (this.scale == temp.getScale()){
return (this.temp > temp.getTemp());
}
else{
Temperature other_temp = new Temperature();
//Convert input temp to the same scale with the this
if (this.scale == 'C'){
//convert to C
other_temp = temp.toCesius();
}
else if (this.scale == 'F') {
//convert to F
other_temp = temp.toFahrenheit();
}else {
//convert to K
other_temp = temp.toKelvin();
}
return (this.temp > other_temp.getTemp());
}
}
  
public String toString() {
String str = "Temperature: " + this.temp + " Scale: " + Character.toString(this.scale);
return str;
}
public double getTemp(){
return this.temp;
}
public double getScale(){
return this.scale;
}
public void read() {
String str = toString();
System.out.println(str);
}
private void set(double temp, char scale){
switch (scale){
case 'C':
//check the range
if(temp < -273.15){
System.exit(0);
}
break;
case 'F':
if (temp < -459.67){
System.exit(0);
}
break;
case 'K':
if (temp < 0){
System.exit(0);
}
break;
}
this.temp = temp;
this.scale = scale;
}
public static void main(String[] args){
  
System.out.print ("Enter a temperature: ");
Scanner scan = new Scanner (System.in);
double userTemp = scan.nextDouble();
System.out.print ("Enter a scale (C/F/K): ");
String userScale = scan.next();
char scale;
if (userScale.equalsIgnoreCase("C")) {
scale = 'C';
}
else if (userScale.equalsIgnoreCase("K")) {
scale = 'K';
}
else
scale = 'F';
Temperature t1 = new Temperature (userTemp, scale);
t1.read();
Temperature t2 = t1.toCesius();
t2.read();
  
Temperature t3 = t1.toKelvin();
t3.read();
  
Temperature t4 = t1.toFahrenheit();
t4.read();
  
boolean isEqual = t1.equals(t2);
  
Temperature t5 = t1.add(t3);
String str5 = t5.toString();
}
}

Solutions

Expert Solution

Math.java

import java.util.Scanner;

public class Math {
private static Scanner in = new Scanner(System.in);
public static void readValues(double[] array)
{
// the inbuilt property of array is length that returns the size of the array
int arrayLength = array.length;
  
  
for(int i = 0 ; i < arrayLength ; i++)
{
System.out.print("Enter element " + (i+1) + ": ");
array[i] = in.nextDouble();
  
in.nextLine(); // clear buffer
}
System.out.println("--------------------");
  
}
  
public static double averageTemperature(double[] array)
{
double sum = 0;
for(int i = 0 ; i < array.length ; i++)
sum += array[i];
  
return (sum/array.length);
}
  
public static void printTemperatures(double[] array) {
System.out.println("Array contains: ");
for(int i = 0 ; i < array.length ; i++)
System.out.println("Temperature " + (i+1) + ": "+array[i]);
  
System.out.println("--------------------");
}
  
private static double sumOfArray(double[] array)
{
double sum = 0;
for(int i = 0 ; i < array.length ; i++)
sum += array[i];
  
return sum;
}
  
private static double returnArray(double[] array, double[] array1, double[] array2, int choice)
{

  
// choice = 1, maximum size
// choice = 2, largest array
  
if(choice == 1)
{
int size1 = array.length;
int size2 = array1.length;
int size3 = array2.length;
  
if(size1 > size2 && size1 > size3)
return size1;
else if(size2 > size1 && size2 > size3)
return size2;
else
return size3;
}
else if(choice == 2)
{
// compute largest on the basis of sum
double sum1 = 0, sum2 = 0, sum3 = 0;
sum1 = sumOfArray(array);
sum2 = sumOfArray(array1);
sum3 = sumOfArray(array2);
  
if(sum1 > sum2 && sum1 > sum3)
return sum1;
else if(sum2 > sum1 && sum2 > sum3)
return sum2;
else
return sum3;
  
}

return -1;
}
  
public static double returnLargestArrayOrSize(int choice, double[] array, double[] array1, double[] array2)
{
  
// choice = 1, maximum size
// choice = 2, largest array
double retVal = returnArray(array, array1, array2, choice);
return retVal;
}
  
private static double maximumValue(double num1, double num2, double num3)
{
if(num1 > num2 && num1 > num2)
return num1;
else if(num2 > num3 && num2 > num1)
return num2;
else
return num3;
}
  
public static double[] maximumArray(double[] array, double[] array1, double[] array2)
{
int size1 = array.length;
int size2 = array1.length;
int size3 = array2.length;
int maxSize = 0;
  
if(size1 > size2 && size1 > size3)
maxSize = size1;
else if(size2 > size1 && size2 > size3)
maxSize = size2;
else
maxSize = size3;
double maxArray[] = new double[maxSize];
  
// compute the largest element from each array
for(int i = 0 ; i < maxSize ; i++)
{
// check whether the current index is less than size of each array
double num1 = 0, num2 = 0, num3 = 0;
if(i < size1)
num1 = array[i];
  
if(i < size2)
num2 = array1[i];
  
if(i < size3)
num3 = array2[i];
  
// if size of array is less than the current loop varaible, in that case
// value is assumed to be 0;
maxArray[i] = maximumValue(num1, num2, num3);
}
  
return maxArray;
  
}
}

ArrayDemo.java

import java.util.Random;

public class ArrayDemo {
public static void main(String[] args) {
Random random = new Random();
  
// create 3 array of random size
// random.nextInt(6) will generate a random number between 0 to 5
  
double array[] = new double[random.nextInt(6)];
double array1[] = new double[random.nextInt(6)];
double array2[] = new double[random.nextInt(6)];
  
// read values in integer array
System.out.println("Array 1: \n");
Math.readValues(array);
System.out.println("Array 2: \n");
Math.readValues(array1);
System.out.println("Array 3: \n");
Math.readValues(array2);
  
double averageTemp = Math.averageTemperature(array);
double averageTemp1 = Math.averageTemperature(array1);
double averageTemp2 = Math.averageTemperature(array2);
  
// print array
Math.printTemperatures(array);
Math.printTemperatures(array1);
Math.printTemperatures(array2);
  
double max = Math.returnLargestArrayOrSize(1, array, array1, array2);
System.out.println("Maximum size: " + max);
  
max = Math.returnLargestArrayOrSize(2, array, array1, array2);
System.out.println("Largest array(on sum basis): " + max);
  
System.out.println("\nAverage temperature of array 1: " + averageTemp);
System.out.println("\nAverage temperature of array 2: " + averageTemp1);
System.out.println("\nAverage temperature of array 3: " + averageTemp2);
  
}
}

Output:


Related Solutions

As seen during our class, the WBS can be a very powerful tool to define clearly...
As seen during our class, the WBS can be a very powerful tool to define clearly what is the scope of your project. I propose you to choose a real and simple project from your professional or personal life and perform the following activities: •Define what is your project (it can be anything you want) and what is the product, service or result expected from it •Document the requirements of the product or service created by your project •Then represent...
Photovoltaic (PV) arrays have become very popular in on-grid and off-grid systems due to their simplicity,...
Photovoltaic (PV) arrays have become very popular in on-grid and off-grid systems due to their simplicity, low emissions and low levelised costs of electricity. PV arrays are connected to grid systems via power electric converters and typically have embedded maximum power point tracking algorithms. Describe how a PV system with maximum power point tracking may be modelled in a dynamic simulation package.
#data structure 1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named...
#data structure 1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list. 2 test program checks that a newly constructed priority queue is empty o checks that a queue with one item in it is not empty o checks that items are correctly entered that would go at the front of the queue o checks that items are correctly entered that would go at the...
A very common application of arrays is computing statistics on lists of numbers. Below you will...
A very common application of arrays is computing statistics on lists of numbers. Below you will find a template of a program that uses four user-defined methods: input an array of numbers, compute the average the array, find the largest number in the array, and find the smallest number in the array. Enter the following program into C# (ignore the line numbers) and replace the lines marked with // *** with the appropriate C# code (may require more than one...
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {    ...
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {     public A first;     public B second;     public Pair(A a, B b)     {         first = a;         second = b;     }     public void setFirst(A a)     {         first = a;     }     public A getFirst()     {         return first;     }     public void setSecond(B b)     {         second = b;     }     public...
Write a very general sort method that can sort any type of data arrays/lists. For example,...
Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support...
public class AddValueToArray { // You must define the addValueTo method, which will add // a...
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
The following balance sheet is for a local partnership in which the partners have become very...
The following balance sheet is for a local partnership in which the partners have become very unhappy with each other. Cash $ 41,000 Liabilities $ 31,000 Land 135,000 Adams, capital 84,000 Building 125,000 Baker, capital 30,000 Carvil, capital 62,000 Dobbs, capital 94,000 Total assets $ 301,000 Total liabilities and capital $ 301,000 To avoid more conflict, the partners have decided to cease operations and sell all assets. Using this information, answer the following questions. Each question should be viewed as...
The following balance sheet is for a local partnership in which the partners have become very...
The following balance sheet is for a local partnership in which the partners have become very unhappy with each other. Cash $ 43,000 Liabilities $ 33,000 Land 145,000 Adams, capital 89,000 Building 135,000 Baker, capital 36,000 Carvil, capital 66,000 Dobbs, capital 99,000 Total assets $ 323,000 Total liabilities and capital $ 323,000 To avoid more conflict, the partners have decided to cease operations and sell all assets. Using this information, answer the following questions. Each question should be viewed as...
The following balance sheet is for a local partnership in which the partners have become very...
The following balance sheet is for a local partnership in which the partners have become very unhappy with each other.   Cash $ 61,000   Liabilities $ 51,000   Land 235,000   Adams, capital 156,500   Building 225,000   Baker, capital 45,000   Carvil, capital 102,000   Dobbs, capital 166,500        Total assets $521,000        Total liabilities and capital $521,000 To avoid more conflict, the partners have decided to cease operations and sell all assets. Using this information, answer the following questions. Each question should be viewed as an independent...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT