Question

In: Computer Science

IN JAVA Create a program that asks the user to input the day, high and low...

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.

Solutions

Expert Solution

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 !





Related Solutions

IN JAVA Create a program that asks the user to input the day, high and low...
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...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Create a program that asks the user to input three numbers and computes their sum. This...
Create a program that asks the user to input three numbers and computes their sum. This sounds simple, but there's a constraint. You should only use two variables and use combined statements. You can use the output below as a guide. Please enter the first number: 4 Please enter the second number: 2 Please enter the third number: 9 The sum of the three numbers is: 15.
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
In Java:Implement a program that repeatedly asks the user to input apositive integer and...
In Java:Implement a program that repeatedly asks the user to input a positive integer and outputs the factorial of that input integer. Do not use recursion, solution should use stack.
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT