Temperature Recording Program: Java Implementation

Verified

Added on  2023/04/25

|7
|1100
|494
Practical Assignment
AI Summary
This Java program is designed to record and analyze the average daily temperature for each day of the week. The program utilizes Java's ArrayList data structure to store days of the week and their corresponding temperatures. Users can input a specific day to retrieve its temperature or request the week's temperature statistics, including the average weekly temperature. The program's pseudo-code, source code, and output screenshots are provided, along with reflection questions addressing the program's purpose, algorithm, inputs, outputs, and potential errors. The program effectively handles user input, calculates the average weekly temperature, and demonstrates object-oriented programming principles. The conclusion emphasizes Java's suitability for such applications and the importance of testing and error handling in software development. The program demonstrates effective use of loops and conditional statements to process and display data.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Running head: TEMPERATURE RECORDING PROGRAM
Temperature Recording Program
Name of the Student:
Name of the Author:
Author note:
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
2
TEMPERATURE RECORDING PROGRAM
Table of Contents
Introduction................................................................................................................................3
Pseudo Code...............................................................................................................................3
Source Code...............................................................................................................................4
Output Screenshots.....................................................................................................................5
Reflection Questions..................................................................................................................6
Conclusion..................................................................................................................................7
Document Page
3
TEMPERATURE RECORDING PROGRAM
Introduction
The program is meant to record the average daily temperature for each of the days of
a week. The program has been coded using the Java programming language. In this program,
the days are stored in an ArrayList data structure in java. The data type of this ArrayList is set
as String. Furthermore, another Double data-type ArrayList is used to store the respective
temperature for each day. Both arrays are set to be of capacity of 7. The user is asked to
search for the temperature of a particular day or the temperature stats for the whole week.
The weekly stat also provides the week’s average temperature, having calculated the same
from the program’s dataset.
Pseudo Code
Step-1. Initialize String arrayList for days
Step-2. Initialize String arrayList for temperature
Step-3. Add all days from Monday to Sunday into days array
Step-4. Add all temperatures as per respective days from Monday to Sunday into
temp array
Step-5. Display menu
Step-6. Take user input of choice
Step-7. Set Flag=0, sum=0
Step-8. Check if choice is Week, if yes goto next step, else goto
Step-9. For each temperature data in Temp array follow the below two steps 10 and 11
Step-10. Add the temperature to Sum
Step-11. Display the temperature and the corresponding day from the Days array.
Step-12. Divide Sum by 7 to get average and display the average weekly temperature
and goto Exit step.
Document Page
4
TEMPERATURE RECORDING PROGRAM
Step-13. For each day data in Days array follow the below step
Step-14. Check if day entered is equal to the day from array, if yes display Day and
corresponding temperature from temp array, set flag to 1 and break from loop.
Step-15. If flag is set at 0, display input error.
Step-16. Exit.
Source Code
import java.util.ArrayList;
import java.util.Scanner;
/*
* Keep day and temperature records of a week.
* Calculate weekly average and display day and temperature as per user's choice.
*/
public class Temperature {
public static void main(String[] args) {
ArrayList<String> days = new ArrayList<>(7);
ArrayList<Double> temp = new ArrayList<>(7);
//add all days from Monday to Sunday into days array
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
days.add("Sunday");
//add all temperatures as per respective days from Monday to Sunday into
temp array
temp.add(25.5);
temp.add(24.0);
temp.add(25.5);
temp.add(27.5);
temp.add(30.5);
temp.add(20.0);
temp.add(22.5);
Scanner sc = new Scanner(System.in);
System.out.print("Enter a day between Monday to Sunday.\nEnter Week to
get weekly stats.\nEnter now... ");
String dayCh = sc.nextLine();
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
5
TEMPERATURE RECORDING PROGRAM
int flag=0;
double sum=0;
//If user enters week as the choice
if(dayCh.equalsIgnoreCase("week")) {
System.out.println();
//iterate through all temperature values
for(double t : temp) {
sum+=t; //add all the temperatures
System.out.println("Day: "+days.get(temp.indexOf(t))+"\t|
Temperature: "+t);
}
System.out.println("\nWeek's average temperature: "+(sum/7));
//calculate and display weekly average temperature
}
else {
System.out.println();
for(String d : days) {
//if the day in the array matches the input choice
if(d.equalsIgnoreCase(dayCh)) {
System.out.println("Day: "+d+"\t| Temperature:
"+temp.get(days.indexOf(d))); //display the day and temperature
flag=1; //set flag and exit loop
break;
}
}
//if flag is still not set, it suggestes that the day or choice entered was
invalid
if(flag==0) {
System.out.println("Invalid Day Entered!!");
}
}
}
}
Output Screenshots
Normal Day Entry
Document Page
6
TEMPERATURE RECORDING PROGRAM
Week Entry
Wrong Entry
Reflection Questions
Project Purpose: To store and receive the temperature stats of respective days of a week and
also the weekly stats like average weekly temperature.
Algorithm Used: The program used the loop and conditional statements of java to iterate
through the days and temperature arrayList data structures. The user’s entry is searched in the
Days array and successful searching, the respective temperature data is fetched and displayed
as output. In case the user enters ‘week’, the program iterates the temp array and calculates
the sum of all data in. Later it is divided by 7 to get the week’s average.
Program Inputs: Program Calculates: This calculates the average temperature of a week.
Document Page
7
TEMPERATURE RECORDING PROGRAM
Program Outputs: The temperature of respective days or the weekly average as well.
Program Errors: The program has no errors. It even handles erroneous inputs of the user.
Conclusion
It can hence be concluded that Java is quite the language to be used in case of
developing programs like this. It is an Object Oriented Programming language and its object
oriented features are really helpful when it comes to developing solutions for real life
problems. Also, testing is a very crucial phase in program and application development as it
helps to spot the various lags and bugs in a program and hence help in fixing them, as
required. It is also necessary to skilfully handle user’s erroneous inputs and ensure that the
program does noit crash.
chevron_up_icon
1 out of 7
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]