There are two parts to this lab, one involving an integer array and one involvin
ID: 3629641 • Letter: T
Question
There are two parts to this lab, one involving an integer array and one involving a character array.1. Create a class called Weather and include in it an instance variable named temperature that is an array of 5 integers.
2. Create a static method calcAverage that takes an integer array as input and returns the average temperature (double) of the array. The definition of a static method begins public static …
3. Create a static method printArray that takes an integer array as input and prints all the values in it.
4. In a main method, ask the user to input 5 temperature values; store these in the integer array. Then invoke the printArray method and the calcAverage method. Also, print the average.
Assuming you have written everything correctly, your output should look like this:
Welcome to the Average Temperature Program
Please enter 5 temperatures.
Enter temperature: 10
Enter temperature: 25
Enter temperature: 48
Enter temperature: 87
Enter temperature: 63
Average temperature: 46.6
Temperatures in the array: 10 25 48 87 63
Thank you for using this program, goodbye.
Create a class called Backward and include in it an instance variable named charsIn that is an array of 80 characters and an instance variable charsOut that is also an array of 80 characters.
Create a regular (non-static) method reverse that takes whatever is stored in the instance variable charsIn and stores the exact reverse of it in charsOut.
In a main method, ask the user to enter up to 80 characters, store them in charsIn, invoke reverse(), and then print whatever is in charsOut.
Assuming you have written everything correctly, your output should look like this:
Welcome to the Talking Backward Program
Please enter from 1 to 80 characters.
> This is a test of reverse.
The reverse is
> .esrever fo tset a si sihT
Goodbye.
Explanation / Answer
import java.util.*;
class Weather
{
public static double calcAverage(int[] a)
{
double sum=0;
for(int i=0; i<5; i++)
sum = sum + a[i];
return sum/5;
}
public static void printArray(int[] a)
{
for(int i=0; i<5; i++)
{
System.out.print(a[i]);
System.out.print(" ");
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Average Temperature Program");
System.out.println("Please enter 5 temperatures.");
int[] a = new int[5];
for(int i=0; i<5;i++)
{
System.out.println("Enter temperature: ");
a[i] = in.nextInt();
}
System.out.println(" Average temperature: " +calcAverage(a) );
System.out.print("Temperatures in the array: ");
printArray(a);
System.out.println("");
System.out.println("Thank you for using this program, goodbye. ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.