In: Computer Science
IN JAVA
Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:
Day: (variable)
High: (variable)
Count High: (variable)
Total High: (variable)
Average High: (variable)
After the seven days and highs have been entered and displayed, use a For Loop to prompt the user for the days and the low temperatures. Inside the For Loop, the program will count, total, and average the lows. The following will be displayed:
Day: (variable)
Low: (variable)
Count Low: (variable)
Total Low: (variable)
Average Low: (variable)
Use a Do-While Loop to prompt the user to input the days, highs and lows. The program will count, total, and average the highs, and count, total, and average the lows. Display the following:
Day: (variable)
High: (variable)
Count High: (variable)
Total High: (variable)
Average High: (variable)
Day: (variable)
Low: (variable)
Count Low: (variable)
Total Low: (variable)
Average Low: (variable)
Prompt the user if they want to continue with another entry. If “Yes”, the DO-While repeats. If “No”, the program will end.
This question provides you a quite good insight into , various ways of applying loops and various kinds of loops.
I believe you are learning loops, and i am going to provide not just an answer this piece of code would definitely make your basics solid.
Now before jumping into the answer lets see, some quite insights we are getting from this question,like
1. how are you going to store days, low and high temperatures ?
Now its a basic usage of Data Structures, here we will go with HashMap, as i already told you, do not just solve the question i believe you must grasp DS and ALGO part.
Learn HASHMAPs from geeksforgeeks, and you will be a better developer.
2. how will exit from infinte loop, one which is asked in last scenario of DO WHILE.
Now , there are some assumptions,lets see them
1. user enters all 7 days of data , no duplicate data, test this program in that sense only.
2. temepratures are in degree, (you can play with it anytime)
NOTE : this code contains good usage of Naming Convention, you will learn a lot.
import java.util.HashMap;
import java.util.Scanner;
public class Temperature {
public static void main(String args[]){
display_temperature();
}
public static void display_temperature(){
HashMap<Integer,String> weekdays = new HashMap<>(){{
put(1,"monday");
put(2,"tuesday");
put(3,"wednesday");
put(4,"thursday");
put(5,"friday");
put(6,"saturday");
put(7,"sunday");
}};
Scanner sc = new Scanner(System.in);
HashMap<String,Float> highData = new HashMap<>();
int days = 0;
float average_high = 0.0f, total_high =0.0f;
while(days<=6){
System.out.println("Enter any day of the week ");
String day =sc.next();
System.out.println("Enter High temperature of this day ");
float high = sc.nextFloat();
total_high += high;
highData.put(day.toLowerCase(),high);
days+=1;
}
for(int i =0 ;i< 7;i++){
System.out.println("Day "+ weekdays.get(i+1));
System.out.println("high "+ highData.get( weekdays.get(i+1)));
}
System.out.println("Count High "+7);
System.out.println("Total High "+total_high);
System.out.println("Average High "+total_high / 7 );
float average_low =0.0f, total_low= 0.0f;
HashMap<String,Float> lowData = new HashMap<>();
for(days =0;days<=6;days++ ){
System.out.println("Enter any day of the week ");
String day =sc.next();
System.out.println("Enter Low temperature of this day ");
float low = sc.nextFloat();
total_low += low;
lowData.put(day.toLowerCase(),low);
}
for(int i =0 ;i< 7;i++){
System.out.println("Day "+ weekdays.get(i+1));
System.out.println("Low "+ lowData.get( weekdays.get(i+1)));
}
System.out.println("Count Low "+7);
System.out.println("Total Low "+total_low);
System.out.println("Average Low "+total_low / 7 );
while(true){
days =0;
average_high =0.0f;
average_low =0.0f;
total_high =0.0f;
total_low =0.0f;
highData = new HashMap<>();
lowData =new HashMap<>();
do{
System.out.println("Enter any day of the week ");
String day =sc.next();
System.out.println("Enter High temperature of this day ");
float high = sc.nextFloat();
total_high += high;
highData.put(day.toLowerCase(),high);
System.out.println("Enter Low temperature of this day ");
float low = sc.nextFloat();
total_low += low;
lowData.put(day.toLowerCase(),low);
}while(days<=6);
for(int i =0 ;i< 7;i++){
System.out.println("Day "+ weekdays.get(i+1));
System.out.println("Low "+ lowData.get( weekdays.get(i+1)));
System.out.println("high "+ highData.get( weekdays.get(i+1)));
}
System.out.println("Count Low "+7);
System.out.println("Total Low "+total_low);
System.out.println("Average Low "+total_low / 7 );
System.out.println("Count High "+7);
System.out.println("Total High "+total_high);
System.out.println("Average High "+total_high / 7 );
System.out.println("-------------------------------------------");
System.out.println("if you want to continue press 1 else 0");
int yes_or_no = sc.nextInt();
if(yes_or_no == 0 )
break;
}
}
}
this is a very nice, way of learning, i am happy you are
trying.
HAPPY LEARNING !