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
HEAT+MASS TRANSFER:FUND.+APPL.
Degarmo's Materials And Processes In Manufacturing
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (8th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
- 4. def modify_data(x, my_list): X = X + 1 my_list.append(x) print(f"Inside the function: x = {x}, my_list = {my_list}") num = 5 numbers = [1, 2, 3] modify_data(num, numbers) print(f"Outside the function: num = {num}, my_list = {numbers}") Classe Classe that lin Thus, A pro is ref inter Ever dict The The output: Inside the function:? Outside the function:?arrow_forwardpython Tasks 5 • Task 1: Building a Library Management system. Write a Book class and a function to filter books by publication year. • Task 2: Create a Person class with name and age attributes, and calculate the average age of a list of people Task 3: Building a Movie Collection system. Each movie has a title, a genre, and a rating. Write a function to filter movies based on a minimum rating. ⚫ Task 4: Find Young Animals. Create an Animal class with name, species, and age attributes, and track the animals' ages to know which ones are still young. • Task 5(homework): In a store's inventory system, you want to apply discounts to products and filter those with prices above a specified amount. 27/04/1446arrow_forwardOf the five primary components of an information system (hardware, software, data, people, process), which do you think is the most important to the success of a business organization? Part A - Define each primary component of the information system. Part B - Include your perspective on why your selection is most important. Part C - Provide an example from your personal experience to support your answer.arrow_forward
- Management Information Systemsarrow_forwardQ2/find the transfer function C/R for the system shown in the figure Re དarrow_forwardPlease original work select a topic related to architectures or infrastructures (Data Lakehouse Architecture). Discussing how you would implement your chosen topic in a data warehouse project Please cite in text references and add weblinksarrow_forward
- Please original work What topic would be related to architectures or infrastructures. How you would implement your chosen topic in a data warehouse project. Please cite in text references and add weblinksarrow_forwardWhat is cloud computing and why do we use it? Give one of your friends with your answer.arrow_forwardWhat are triggers and how do you invoke them on demand? Give one reference with your answer.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