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 MyProgrammingLab with Pearson eText -- Access Card Package (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)
- Write a Java Program that allows the user to enter an arbitrary number of integer grades from the keyboard. Each grade should be added to an ArrayList called grades. Stop inputting grades when -1 is entered. The program should then iterate through the ArrayList, calculate the average grade, and output it along with the minimum and the maximum grade value.arrow_forwardWrite 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_forwardWrite a java program ( One class with a main method ) to do the following : 1. Prompt the user to enter an integer number n . 2. Declare an array named list of n integer numbers . 3. Prompt the user to fill the array . 4. Find and print the smallest odd number in the array .arrow_forward
- java Finish the method called printUniqueNumbers() that will take anint [] and print out all of the unique numbers in the array. A number is unique when it is only found in the array one time. This is a void method with an int [] parameter public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter the length of array!!!"); int size = scan.nextInt(); int[] nums = new int[size]; for(int i=0; i < size; i++){ nums[i] = scan.nextInt();arrow_forwardComplete the following piece of java code, so that it could convert date to Julian number and Julian number to date. public class JulianNumber { public static int toJulianNumber( int day, int month, int year ); // returns an array of integers for day, month, and year public static int[] fromJulianNumber( int julianNumber ); }arrow_forwardFOR JAVA Write a Java class named Book, which has a name, an author and a pub- lisher. This class must also have a static field (an array with length 100) which holds names of all constructed books, and a static method which returns the number of constructed books.arrow_forward
- Write a Java Program to Accept the Marks of a Student into an Array and find Total Marks and Average. Enter the number of subjects and then enter marks of students in all those subjects. Sample run: Enter no. of subject:5 Enter marks out of 100: 86 89 91 82 78 Sum: 426 Average: 85arrow_forwardWrite a java program named Largest that creates an array of 10 integer numbers. Then it finds and prints out the largest number. You must type your name and student number above the class declaration using a comment.arrow_forwardIn java Implement a public static double[] ageMetrics(Patient[] patients) method that returns an array of length 3 with the following three values (starting at index 0): The age of the youngest patient The age of the oldest patient The mean age of all patientsarrow_forward
- java Create a method called populate() that will take an int argument and print an array that is populated/filled with numbers starting from 1 to the given number. This is a void method with an int parameter Scanner in = new Scanner(System.in); int n = in.nextInt(); populate(n); populate(3) output: [1,2,3]arrow_forwardWrite a program in JAVA to input the number of elements in an array, also input the values in the array from the console. Replace all those elements which are even and greater than 5, with -99. Print the final array. Constraints: The array should initially not contain -99. The value of the number of elements should not be greater than 50.arrow_forwardWrite a program in Java that reads integers, one per line, and displays their sum. Also, display all the numbers read, each with an annotation giving its percentage contribution to the sum. Use a method that takes the entire array as one argument and returns the sum of the numbers in the array. (Hint: Ask the user for the number of integers to be entered, create an array of that length, and then fill the array with the integers read.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education