Concept explainers
Write a
“NumberAboveAverage” class
Program Plan:
“NumberAboveAverage.java”:
- • Define “NumberAboveAverage” class.
- ○ Define main function.
- ■ Declare an array “temperature” in “double” base type.
- ■ Create an object for scanner class.
- ■ Display prompt statement.
- ■ Read ten temperature from user using “for” loop.
- • Display prompt statement for each temperature.
- • Read temperature one by one from user.
- ■ Initializes total temperature “total_temp” to “0.0”.
- ■ Compute sum of all temperature using “for” loop.
- • The sum is computed by using “total_temp += temperature[i];”
- ■ The average temperature is computed by using “total_temp/10” and it is stored to a variable “tempAverage”.
- ■ Display average temperature.
- ■ Initializes the day count “dayCount” to “0”.
- ■ Using “for” loop, compute which day has above average.
- • If temperature is greater than average temperature, then increment the day count.
- • Display the day which has above average temperature.
- ■ Finally display total number of days has above temperature.
- ○ Define main function.
The below java program is used to counts the number of days that the temperature is above the average of temperature.
Explanation of Solution
Program:
Filename: “NumberAboveAverage.java”
//Import required package
import java.util.Scanner;
//Define "NumberAboveAverage" class
public class NumberAboveAverage
{
//Define main function
public static void main(String[] args)
{
//Declare an array for temperature
double[] temperature = new double[10];
//Create object for scanner class
Scanner reader = new Scanner(System.in);
//Display prompt statement
System.out.println("Please enter the values of ten temperature");
//Read ten temperatures from user using "for" loop
for(int i = 0; i < 10; i++)
{
//Prompt statement for temperature
System.out.print("Enter temperature for Day " + i + " is: ");
//Read temperature one by one from user
temperature[i] = reader.nextDouble();
}
//Initializes total temperature to "0".
double total_temp = 0.0;
//Compute sum of all temperature using "for" loop
for(int i = 0; i < 10; i++)
{
total_temp += temperature[i];
}
//Compute the average temperature to "0"
double tempAverage = total_temp/10;
//Display average temperature
System.out.println("The average temperature is: " + tempAverage);
//Initializes the day count to "0"
int dayCount = 0;
//Using "for" loop, compute which day has above average
for(int i = 0; i < 10; i++)
{
/* If temperature is greater than average temperature, then */
if( temperature[i] > tempAverage)
{
//Increment the day count
dayCount++;
//Display the day which has above average temperature
System.out.println("Day " + i + " had temperature " + temperature[i] + " which was above average");
}
}
//Finally display total number of days has above temperature
System.out.println("The number of days with a temperature above average is: " + dayCount);
}
}
Output:
Please enter the values of ten temperature
Enter temperature for Day 0 is: 10
Enter temperature for Day 1 is: 40
Enter temperature for Day 2 is: 15
Enter temperature for Day 3 is: 80
Enter temperature for Day 4 is: 42
Enter temperature for Day 5 is: 28
Enter temperature for Day 6 is: 48
Enter temperature for Day 7 is: 12
Enter temperature for Day 8 is: 30
Enter temperature for Day 9 is: 84
The average temperature is: 38.9
Day 1 had temperature 40.0 which was above average
Day 3 had temperature 80.0 which was above average
Day 4 had temperature 42.0 which was above average
Day 6 had temperature 48.0 which was above average
Day 9 had temperature 84.0 which was above average
The number of days with a temperature above average is: 5
Want to see more full solutions like this?
Chapter 7 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++: Early Objects (9th Edition)
Database Concepts (7th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with C++: Early Objects
Problem Solving with C++ (9th Edition)
Starting Out with Java: Early Objects (6th Edition)
- In JAVAarrow_forwardFor Beginners Java: Write a program that ask the user to enter a list of positive scores until the user enters a negative score to terminate the input. You need to store these scores in an array. You can assume the maximum number of scores the user may enter is 50. However, you need to keep track of the actual number of scores entered. Write 2 methods: 1. calculateAverage(): this method takes the list of scores and return the average score. 2. countPerfectScores(): this method takes the list of scores and return the number of perfect scores (100). The main program reads the input and calls these methods and print the results. Please make sure you write a comment line to document what your method does.arrow_forwardJava Programarrow_forward
- Write a Java program named Findletter that asks the user to enter 10 letters and store them in an array. Then it should ask the user to enter a letter (char value) to search for it. Then, if the entered letter is found, the program should display the message "The letter is in the array", otherwise it should display the message "The letter is not in the array".arrow_forward2. A store owner wants the total sales for 2020. Write a program that will load an array with 12 months of sales. Use iomanip to make a nice-looking chart that shows the monthly sales, the month with the highest sales, lowest sales, and average for the year sales. January ……………. $50,000 February…………… $60,000 March ……………….$80,000 April ………………..$106,000 May …………………..$50,000 June ………………..$190,000 July ………………….$900,000 August …………… $102,000 September ………..$70,000 October …………..$700,000 November………… $90,000 December …………$90,000 Total Sales for the year 2020 was ………………….………$ The month with highest sales was ****** at ………..$ The month with lowest sales was ****** at ………...$ The average of sales for the year was ……………………$ C++arrow_forward1. Write a program in a class EvensOddsAvg that counts the integers that are below the average. Read fifteen integers from the keyboard and place them in an array. Compute the sum and count of the evens and the odds separately. Find the average of the integers, and count and display the integers which are below average. Please Java Codearrow_forward
- Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods: Method getData: This method reads and stores the data in the two-dimensional array. Method averageHigh: This method calculates and returns the average high temperature of the year. Method averageLow: This method calculates and returns the average low temperature of the year. Method indexHighTemp: This method returns the index of the highest temperature in the array. Method indexLowTemp: This method returns the index of the lowest temperature in the array.arrow_forwardIn Java, create a program that gets user input to create an array of books. The length of the array should be determined by how many books the user enters. You may ask the user how many books they will enter. After all books have been entered by the user, invoke a method to display the contents of the array in a table. Each book will have a title, author, cover, pages, copyright (year), read time (float).arrow_forwardJAVAWrite a program that searches person's name and display all matched name. For example, if a user inputs the letter "Davis", the program searches all name which includes "Davis" in an array. Then, display all matched names on the screen. You must declare a single dimensional array which has all people’s name. Use methods of string classarrow_forward
- QUESTION 2 Write a Java program to input a single dimensional integer array of size 8. Pass the array to a method named countBigNumbers that return integer. In this method, count the array elements that are bigger than 50 and place them in another array. Display the returned count in main method. Display the two arrays by invoking a method named displayArray. 2 Sample inputs and their outputs are shown below: Input 8 numbers: 5 60 8 99 1 100 7 51 Count of numbers bigger than 50 is 4 Array1 is 5 60 100 51 66 Array2 is 60 99 100 51 Input 8 numbers: 80 19 91 150 37 23 50 49 Count of numbers bigger than 50 is 3 Arrayl is 80 19 91 100 37 23 50 49 Array2 is 80 91 100arrow_forwardcomputer programmingarrow_forwardDon't copy from other sources.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning