Using the following create the nessecary static methods to pass random arrays wi
ID: 664419 • Letter: U
Question
Using the following create the nessecary static methods to pass random arrays with the TempartureWithArrays and Temperature classes
--------------------------------------------------------------------------------------
public class TemperatureWithArrays
{
public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
int x;
Temperature temp1 = new Temperature(120.0, 'C');
Temperature temp2 = new Temperature(100, 'C');
Temperature temp3 = new Temperature(50.0, 'C');
Temperature temp4 = new Temperature(232.0, 'K');
Temperature tempAve = new Temperature(0.0, 'C');
Temperature[] tempArray = new Temperature[ARRAY_SIZE];//create pointer to array
Temperature t1;
for(x = 0; x < tempArray.length; x++)
{
t1 = new Temperature();
tempArray[x] = t1;// fill the array with Temperatures
}
System.out.println("Temp1 is " + temp1);
//temp1 = temp1.toKelvin();
temp2.toKelvin();
System.out.println("Temp1 to Kalvin is " + temp1);
if (temp1.equals(temp3))
{
System.out.println("These two temperatures are equal");
}
else
{
System.out.println("These two temperature are not equal");
}
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(4);
System.out.println("the average temperatrure is " + tempAve );
Temperature[] temperatureArrayOne;
Temperature[] temperatureArrayTwo;
Temperature[] temperatureArrayThree;
temperatureArrayOne = new Temperature[getRandomArraySize()];
readTemperatureArray(temperatureArrayOne);
printTemperatureArray(temperatureArrayOne);
t1 = getAverage(temperatureArrayOne);
System.out.println("the average of temperature array one is " + t1);
temperatureArrayTwo = new Temperature[getRandomArraySize()];
readTemperatureArray(temperatureArrayTwo);
printTemperatureArray(temperatureArrayTwo);
t1 = getAverage(temperatureArrayTwo);
System.out.println("the average of temperature array two is " + t1);
temperatureArrayThree = new Temperature[getRandomArraySize()];
readTemperatureArray(temperatureArrayThree);
printTemperatureArray(temperatureArrayThree);
t1 = getAverage(temperatureArrayThree);
System.out.println("the average of temperature array three is " + t1);
Temperature[] largest = getLargestArray(temperatureArrayOne, temperatureArrayTwo,
temperatureArrayThree);
Temperature[] arrayWithLargestValues;
if(temperatureArrayOne == largest)
arrayWithLargestValues = createArrayWithLargestValues(largest,
temperatureArrayTwo, temperatureArrayThree);
else if(temperatureArrayTwo == largest)
arrayWithLargestValues = createArrayWithLargestValues(largest,
temperatureArrayOne, temperatureArrayThree);
else// fractionArrayThree is largest
arrayWithLargestValues = createArrayWithLargestValues(largest,
temperatureArrayOne, temperatureArrayTwo);
System.out.println("An array with the largest values from the 3 arrays is");
printTemperatureArray(arrayWithLargestValues);
}
}
----------------------------------------------------------------------------------------------------
Temperature.java
// declaring headers
import java.io.*;
// declaring class Temperature
public class Temperature
{
// declaring local variables
public double kelv;
public double cels;
public double fahr;
public double temp;
public char scale;
// declaring function temperature
public Temperature(double temp, char scale)
{
// setting the temperature value
setTemperature(temp , scale);
}
// declaring function Switcherooski
public Temperature Switcherooski(Temperature t){
// checking the condition using switch case
switch (this.scale)
{
// checking case condition
case 'K':
// calling toKelvin function
t.toKelv();
break;
case 'C':
// calling celsius function
t.toCels();
break;
case 'F':
// calling fahrenheit
t.toFahr();
break;
default:
//displaying
System.out.println("temperature is Too hot or cold");
System.exit(0);
break;
}
return this;
}
// setting temperature
private void setTemperature(double temp, char scale) {
// indicate current object
this.temp = temp;
this.scale = scale;
}
// adding temperature value
public void add( Temperature t){
Temperature p = t.Switcherooski(t);
this.temp += p.temp;
}
// subtraction temperature value
public void subtract( Temperature t){
Temperature p = t.Switcherooski(t);
this.temp -= p.temp;
}
// multiply temperature value
public void multiply( Temperature t ){
Temperature p = t.Switcherooski(t);
this.temp *= p.temp;
}
// divide temperature value
public void divide( int x ){
this.temp = this.temp / x;
}
// checking boolean condition
public boolean equals(Temperature n) {
if( this.temp == n.temp ){
return false;
}else{
return true;
}
}
// checking the temperature whether its greater than or not
public boolean greaterThan(Temperature n) {
return this.temp > n.temp;
}
// converting value to string
public String toString(){
return ""+ this.temp;
}
// calculating kelvin value
public Temperature toKelv()
{
if( this.scale == 'C')
{
this.temp = (this.temp+273.15);
}
else if( this.scale == 'F')
{
this.temp = (((this.temp-32)*5)/9)+273.15;
}
return new Temperature(this.temp , 'K');
}
// calculating celsius value
public Temperature toCels()
{
if( this.scale == 'F')
{
this.temp =(this.temp - 32) / 1.8;
}
else if(this.scale == 'K')
{
this.temp = this.temp-273.15;
}
return new Temperature(this.temp , 'C');
}
// calculating fahrenheit value
public Temperature toFahr()
{
if( this.scale == 'C')
{
this.temp = ( this.temp *1.8)+32;
}
else if( this.scale == 'K')
{
this.temp = ((this.temp-273.15)*1.8)+32;
}
return new Temperature(this.temp , 'F');
}
// reading the value
public String read(){
return "" + this.temp +" " + this.scale;
}
}
Explanation / Answer
CONCEPT:
methods for temperature using arrays:
CODE:
public class wklyTemp
{
static double mntmp, tutmp, wdtmp, thrtmp, frtmp, sttmp, sntmp;
static Scanner kysn = new Scanner(System.in);
static DecimalFormat df = new DecimalFormat("00.00");
public static void mnu()
{
//ask user for temparature...
System.out.println(" Monday's temperature. ");
mntmp = kysn.nextDouble();
//ask user for temparature...
System.out.println(" Tuesday's temperature. ");
tutmp = kysn.nextDouble();
//ask user for temparature...
System.out.println("Wednesday's temperature. ");
wdtmp = kysn.nextDouble();
//ask user for temparature...
System.out.println(" Thursday's temperature. ");
thrtmp = kysn.nextDouble();
//ask user for temparature...
System.out.println("Friday's temperature. ");
frtmp = kysn.nextDouble();
//ask user for temparature...
System.out.println(" Saturday's temperature. ");
sttmp = kysn.nextDouble();
//ask user for temparature...
System.out.println("Sunday's temperature. ");
sntmp = kysn.nextDouble();
}//mnu
public static void datemp()
{
int day;
System.out.println(" please enter the number " +
"for the day of the week");
System.out.println("1. Monday");
System.out.println("2. Tuesday");
System.out.println("3. Wednesday");
System.out.println("4. Thursday");
System.out.println("5. Friday");
System.out.println("6. Saturday");
System.out.println("7. Sunday ");
day = kysn.nextInt();
switch (day)
{
case 1: System.out.println("Monday's average was "+(df.format(mntmp))+"C");
break;
case 2: System.out.println("Tuesday's average was "+(df.format(tutmp))+"C");
break;
case 3: System.out.println("Wednesday's average was "+(df.format(wdtmp))+"C");
break;
case 4: System.out.println("Thursday's average was "+(df.format(thrtmp))+"C");
break;
case 5: System.out.println("Friday's average was "+(df.format(frtmp))+"C");
break;
case 6: System.out.println("Saturday's average was "+(df.format(sttmp))+"C");
break;
case 7: System.out.println("Sunday's average was "+(df.format(sntmp))+"C");
}
}//averageDailyTemp
public static void calacTemp()
{
double sumOfTemps;
double averageWeeklyTemp;
sumOfTemps = mntmp+tutmp+wdtmp+thrtmp+frtmp+sttmp+sntmp;
averageWeeklyTemp = sumOfTemps/7;
System.out.println(" The average weekly temperature was "+(df.format(averageWeeklyTemp))+"C");
}
public static void exit()
{
int choice;
System.out.println(" Would you like to exit?");
System.out.println("1. Yes");
System.out.println("2. No");
choice = kysn.nextInt();
if(choice ==1)
{
System.out.println(" Thank-you for using this system!");
System.exit(0);
}//if
else
{
System.out.println(" You will now be redirected to the main mnu");
mnu();
}
}
public static void main(String[] args)
{
mnu();
datemp();
calacTemp();
exit();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.