SlideShare a Scribd company logo
In JAVA
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:
a. Method getData: This method reads and stores the data in the two-dimensional array.
b. Method averageHigh: This method calculates and returns the average high temperature of the
year.
c. Method averageLow: This method calculates and returns the average low temperature of the
year.
d. Method indexHighTemp: This method returns the index of the highest temperature in the
array.
e. Method indexLowTemp: This method returns the index of the lowest temperature in the array.
(These methods must all have the appropriate parameters.)
Solution
CODE
package test;
import java.util.Scanner;
public class Temperatures {
//declaring temperatures as a static array
public static double[][] temperatures = new double[12][2];
public static void main(String[] args) {
getData();
System.out.println("Average high is : "+averageHigh());
System.out.println("Average low is : "+averageLow());
System.out.println("Month of highest temperature is : "+indexHighTemp());
System.out.println("Month of lowest temperature is : "+indexLowTemp());
}
//to get data of temperatures for all months from user
public static void getData() {
System.out.println("Enter details");
Scanner in = new Scanner(System.in);
for (int i = 0; i < 12; i++) {
System.out.println("Enter highest temperature for month " + (i + 1));
temperatures[i][0] = in.nextDouble();
System.out.println("Enter lowest temperature for month " + (i + 1));
temperatures[i][1] = in.nextDouble();
}
}
//to get avegare of highest temperatures of all months
public static double averageHigh() {
double avg = 0;
for (int i = 0; i < 12; i++) {
avg += temperatures[i][0];
}
return avg / 12;
}
//to get sverage of lowest temperatures of all months
public static double averageLow() {
double avg = 0;
for (int i = 0; i < 12; i++) {
avg += temperatures[i][1];
}
return avg / 12;
}
//to get index of highest tempeartures of all months
public static int indexHighTemp() {
int index = 0;
double max = Double.MIN_VALUE;
for (int i = 0; i < 12; i++)
if (temperatures[i][0] > max)
{
index = i;
max =temperatures[i][0];
}
return index+1;
}
//to get index of lowest tempeartures of all months
public static int indexLowTemp() {
int index = 0;
double min = Double.MAX_VALUE;
for (int i = 0; i < 12; i++)
if (temperatures[i][1] < min)
{
index = i;
min = temperatures[i][1];
}
return index+1;
}
}
SAMPLE OUTPUT
Enter details
Enter highest temperature for month 1
60
Enter lowest temperature for month 1
50
Enter highest temperature for month 2
50
Enter lowest temperature for month 2
40
Enter highest temperature for month 3
40
Enter lowest temperature for month 3
30
Enter highest temperature for month 4
30
Enter lowest temperature for month 4
20
Enter highest temperature for month 5
20
Enter lowest temperature for month 5
10
Enter highest temperature for month 6
20
Enter lowest temperature for month 6
10
Enter highest temperature for month 7
30
Enter lowest temperature for month 7
20
Enter highest temperature for month 8
40
Enter lowest temperature for month 8
30
Enter highest temperature for month 9
50
Enter lowest temperature for month 9
40
Enter highest temperature for month 10
60
Enter lowest temperature for month 10
50
Enter highest temperature for month 11
70
Enter lowest temperature for month 11
60
Enter highest temperature for month 12
80
Enter lowest temperature for month 12
70
Average high is : 45.833333333333336
Average low is : 35.833333333333336
Index of highest temperature is : 11
Index of lowest temperature is : 4

More Related Content

PPTX
Building Java Programas
PPT
ch07-arrays.ppt
PPT
02.softwareengr.ppt
PDF
Write a Java class to represent a temperature. The class has a single.pdf
PDF
Progr3
DOCX
Dti2143 lab sheet 6
DOCX
Dti2143 lab sheet 6
PDF
Assignment Details There is a .h file on Moodle that provides a defi.pdf
Building Java Programas
ch07-arrays.ppt
02.softwareengr.ppt
Write a Java class to represent a temperature. The class has a single.pdf
Progr3
Dti2143 lab sheet 6
Dti2143 lab sheet 6
Assignment Details There is a .h file on Moodle that provides a defi.pdf

Similar to In JAVA Write a program that uses a two-dimensional array to sto.pdf (18)

PDF
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
PPTX
Section1-Functiiiiiiiiiiiiiooooooooooooooooions.pptx
PPTX
Program Design
PDF
PART 6: FROM GEO INTO YOUR REPORT
PDF
Program 1 (Practicing an example of function using call by referenc.pdf
PDF
Java conceptual learning material
DOCX
C programming Lab 2
DOC
C important questions
PDF
Write a method called uniqueNumbers that takes an int array as param.pdf
PDF
public class Interest{double interest(double rate, double amount){.pdf
PPT
PDF
TIME SERIES ANALYSIS USING ARIMA MODEL FOR FORECASTING IN R (PRACTICAL)
PDF
An object of class StatCalc can be used to compute several simp.pdf
PDF
Using the code below Part 1 Write a static method called L.pdf
PPT
Dynamically Evolving Systems: Cluster Analysis Using Time
PDF
Assignment Java Programming 2
PDF
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
PDF
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
Section1-Functiiiiiiiiiiiiiooooooooooooooooions.pptx
Program Design
PART 6: FROM GEO INTO YOUR REPORT
Program 1 (Practicing an example of function using call by referenc.pdf
Java conceptual learning material
C programming Lab 2
C important questions
Write a method called uniqueNumbers that takes an int array as param.pdf
public class Interest{double interest(double rate, double amount){.pdf
TIME SERIES ANALYSIS USING ARIMA MODEL FOR FORECASTING IN R (PRACTICAL)
An object of class StatCalc can be used to compute several simp.pdf
Using the code below Part 1 Write a static method called L.pdf
Dynamically Evolving Systems: Cluster Analysis Using Time
Assignment Java Programming 2
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
Ad

More from info309708 (20)

PDF
Pluto has been hard to measure from Earth because of its atmosphere. .pdf
PDF
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
PDF
Modern Database Management 11th Edition by Jeffrey A. HofferUse th.pdf
PDF
Let f X Y be a function.True or FalseA sufficient condition for f .pdf
PDF
Its your third week on the job at Panache Inc. Last week, you made.pdf
PDF
How do CCMI model help with the improvements and comparison of the p.pdf
PDF
How would you implement a node classs and an edge class to create .pdf
PDF
How do you evaluate your own global mind set levelsSolutionAN.pdf
PDF
Here are two datasetsDataset A 64 65 66 68 70 71 72Dataset B .pdf
PDF
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
PDF
For this homework, you will develop a class called VendingMachine th.pdf
PDF
Discuss what is SSH and the advantages and disadvantages of using it.pdf
PDF
Describe the differences between the three major physical connection .pdf
PDF
Create a student record management system using linked list and queu.pdf
PDF
Coca-Cola companyStrategic Goals and Objectivesi. Objectives are.pdf
PDF
C programming. Answer question only in C code Ninth Deletion with B.pdf
PDF
Biology LabThe poisonous wastes of diptheria germs are called (A).pdf
PDF
Aside from the expansion of industrial capitalism, what factors affe.pdf
PDF
Yates (2009) notes that unions have a purpose broader than serving t.pdf
PDF
write a C program for blinking light using function make sure it.pdf
Pluto has been hard to measure from Earth because of its atmosphere. .pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
Modern Database Management 11th Edition by Jeffrey A. HofferUse th.pdf
Let f X Y be a function.True or FalseA sufficient condition for f .pdf
Its your third week on the job at Panache Inc. Last week, you made.pdf
How do CCMI model help with the improvements and comparison of the p.pdf
How would you implement a node classs and an edge class to create .pdf
How do you evaluate your own global mind set levelsSolutionAN.pdf
Here are two datasetsDataset A 64 65 66 68 70 71 72Dataset B .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
For this homework, you will develop a class called VendingMachine th.pdf
Discuss what is SSH and the advantages and disadvantages of using it.pdf
Describe the differences between the three major physical connection .pdf
Create a student record management system using linked list and queu.pdf
Coca-Cola companyStrategic Goals and Objectivesi. Objectives are.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdf
Biology LabThe poisonous wastes of diptheria germs are called (A).pdf
Aside from the expansion of industrial capitalism, what factors affe.pdf
Yates (2009) notes that unions have a purpose broader than serving t.pdf
write a C program for blinking light using function make sure it.pdf
Ad

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Presentation on HIE in infants and its manifestations
Institutional Correction lecture only . . .
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
GDM (1) (1).pptx small presentation for students
Module 4: Burden of Disease Tutorial Slides S2 2025
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Final Presentation General Medicine 03-08-2024.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Supply Chain Operations Speaking Notes -ICLT Program
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Chinmaya Tiranga quiz Grand Finale.pdf
Presentation on HIE in infants and its manifestations

In JAVA Write a program that uses a two-dimensional array to sto.pdf

  • 1. In JAVA 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: a. Method getData: This method reads and stores the data in the two-dimensional array. b. Method averageHigh: This method calculates and returns the average high temperature of the year. c. Method averageLow: This method calculates and returns the average low temperature of the year. d. Method indexHighTemp: This method returns the index of the highest temperature in the array. e. Method indexLowTemp: This method returns the index of the lowest temperature in the array. (These methods must all have the appropriate parameters.) Solution CODE package test; import java.util.Scanner; public class Temperatures { //declaring temperatures as a static array public static double[][] temperatures = new double[12][2]; public static void main(String[] args) { getData(); System.out.println("Average high is : "+averageHigh()); System.out.println("Average low is : "+averageLow()); System.out.println("Month of highest temperature is : "+indexHighTemp()); System.out.println("Month of lowest temperature is : "+indexLowTemp()); } //to get data of temperatures for all months from user public static void getData() { System.out.println("Enter details");
  • 2. Scanner in = new Scanner(System.in); for (int i = 0; i < 12; i++) { System.out.println("Enter highest temperature for month " + (i + 1)); temperatures[i][0] = in.nextDouble(); System.out.println("Enter lowest temperature for month " + (i + 1)); temperatures[i][1] = in.nextDouble(); } } //to get avegare of highest temperatures of all months public static double averageHigh() { double avg = 0; for (int i = 0; i < 12; i++) { avg += temperatures[i][0]; } return avg / 12; } //to get sverage of lowest temperatures of all months public static double averageLow() { double avg = 0; for (int i = 0; i < 12; i++) { avg += temperatures[i][1]; } return avg / 12; } //to get index of highest tempeartures of all months public static int indexHighTemp() { int index = 0; double max = Double.MIN_VALUE; for (int i = 0; i < 12; i++) if (temperatures[i][0] > max) { index = i; max =temperatures[i][0]; } return index+1; }
  • 3. //to get index of lowest tempeartures of all months public static int indexLowTemp() { int index = 0; double min = Double.MAX_VALUE; for (int i = 0; i < 12; i++) if (temperatures[i][1] < min) { index = i; min = temperatures[i][1]; } return index+1; } } SAMPLE OUTPUT Enter details Enter highest temperature for month 1 60 Enter lowest temperature for month 1 50 Enter highest temperature for month 2 50 Enter lowest temperature for month 2 40 Enter highest temperature for month 3 40 Enter lowest temperature for month 3 30 Enter highest temperature for month 4 30 Enter lowest temperature for month 4 20 Enter highest temperature for month 5 20 Enter lowest temperature for month 5 10
  • 4. Enter highest temperature for month 6 20 Enter lowest temperature for month 6 10 Enter highest temperature for month 7 30 Enter lowest temperature for month 7 20 Enter highest temperature for month 8 40 Enter lowest temperature for month 8 30 Enter highest temperature for month 9 50 Enter lowest temperature for month 9 40 Enter highest temperature for month 10 60 Enter lowest temperature for month 10 50 Enter highest temperature for month 11 70 Enter lowest temperature for month 11 60 Enter highest temperature for month 12 80 Enter lowest temperature for month 12 70 Average high is : 45.833333333333336 Average low is : 35.833333333333336 Index of highest temperature is : 11 Index of lowest temperature is : 4