In: Computer Science
Write a program that uses an array of high temperatures for your hometown from last week (Sunday – Saturday). Write methods to calculate and return the lowest high temperature (minimum) and a method to calculate and return the highest high temperature (maximum) in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods.
Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10)
Your output should be formatted in such a way that it makes sense for what a user would want to see. Remember to output a description of what you are showing the user - not just values on a screen.
Submit all your .java files, .class files and screenshots of your code and output.
Source Code in Java:
class Temperatures
{
static int max(int temps[]) //method to calculate and return
maximum of an array
{
int max=temps[0]; //temporary maximum
for(int i=1;i<temps.length;i++)
{
if(temps[i]>max)
max=temps[i];
}
return max; //returning maximum
}
static int min(int temps[]) //method to calculate and return
minimum of an array
{
int min=temps[0]; //temporary minimum
for(int i=1;i<temps.length;i++)
{
if(temps[i]<min)
min=temps[i];
}
return min; //returning minimum
}
static int[] increase(int temps[]) //method to increase every
element of an array by 10 and return it
{
int incTemps[]=new int[temps.length]; //creating new array
for(int i=0;i<temps.length;i++)
incTemps[i]=temps[i]+10; //filling new array with increased
values
return incTemps; //returning new array
}
public static void main(String args[])
{
//testing the functions
int temps[]={29,33,34,35,35,32,29};
//outputs
System.out.println("Lowest high temperature of the week is
"+min(temps));
System.out.println("Highest high temperature of the week is
"+max(temps));
temps=increase(temps);
System.out.print("Temperatures after increasing: ");
for(int i=0;i<temps.length;i++)
System.out.print(temps[i]+" ");
}
}
Output: