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 Plus Mylab Programming With Pearson Etext -- Access Card Package (8th 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)
- What should the next three steps be in my machine based home security system after deployment and after the following current steps: Enhancing Security & Privacy Measures User Interface (UI) and Experience (UX) Improvement Machine Learning Model Refinement & Continuous Improvementarrow_forwardI am creating a machine learning home based security system, have completed initial deployment and in the following phases of the project: Expanding device compatibility and integration, preparing for cloud integration, and implementing system reduncancy and disaster recovery. What should the next three phases be?arrow_forwardHands-On Assignments Part II Assignment 1-5: Querying the DoGood Donor Database Review the DoGood Donor data by writing and running SQL statements to perform the following tasks: 1. List each donor who has made a pledge and indicated a single lump sum payment. Include first name, last name, pledge date, and pledge amount. 2. List each donor who has made a pledge and indicated monthly payments over one year. Include first name, last name, pledge date, and pledge amount. Also, display the monthly payment amount. (Equal monthly payments are made for all pledges paid in monthly payments.) 3. Display an unduplicated list of projects (ID and name) that have pledges committed. Don't display all projects defined; list only those that have pledges assigned. 4. Display the number of pledges made by each donor. Include the donor ID, first name, last name, and number of pledges. 5. Display all pledges made before March 8, 2012. Include all column data from the DD PLEDGE table.arrow_forward
- Write a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…arrow_forwardFind the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + day_of_week[i] + ': ')arrow_forwardFind the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': ')arrow_forward
- Find the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you 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 LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr