Question

In: Computer Science

Write a complete Java program, including import statements, variable declarations and initializations as needed, for the...

Write a complete Java program, including import statements, variable declarations and initializations as needed, for the following problem. DO NOT USE ARRAYS.

The program will read from the keyboard a series of records, each record containing a city ID number (integer), morning temperature (integer) and evening temperature (integer). See below for sample input.

Sample input:

123 72 79

157 80 100

103 76 56

9999

For each city ID read in, compute the average of the two temperatures. Print a heading and below it the city ID, morning temperature, evening temperature and the average temperature (one decimal place) with columns lined up as shown below in the sample output using printf. Continue reading records until the city id is 9999.

Sample output:

City id morning evening average 123 72 79 75.5

When all records have been read in, print (a) the total number of records read in, (b) the lowest AM temperature and the associated city ID and (c) the highest average temperature (one decimal point) and the associated city ID. Don’t print the numbers by themselves (123 72); include a description to explain what the number represents. For example,

City 123 has the lowest morning temperature of 72

If the user entered 9999 as the first city, the program should print “No records read in”.

Solutions

Expert Solution

Program Code [JAVA]

import java.util.Scanner;

public class City {

    public static void main(String[] args) {

        // Object of Scanner class to take input

        Scanner scan = new Scanner(System.in);

        // Declaring All required variables

        int cityID, morningTemp, eveningTemp, totalRecords = 0;
        double avgTemp;
        int lowestAMTemp = 0, lowestTempCity = 0;
        double highestAvgTemp = 0.0;
        int highestAvgCity = 0;

        // Taking input from user until he enters 9999

        while(true) {

            cityID = scan.nextInt();

            if(cityID == 9999) {

                // Checking if it is first record to read

                if(totalRecords == 0)
                    System.out.println("No records to read in");
                break;
            }

            morningTemp = scan.nextInt();
            eveningTemp = scan.nextInt();

            if (totalRecords == 0) {
                lowestTempCity = cityID;
                lowestAMTemp = morningTemp;
            }

            // Calculating Average temperature

            avgTemp = (morningTemp + eveningTemp) / 2.0;

            // Identifying lowest AM adn highest Avg

            if(morningTemp < lowestAMTemp) {
                lowestAMTemp = morningTemp;
                lowestTempCity = cityID;
            }

            if(avgTemp > highestAvgTemp) {
                highestAvgTemp = avgTemp;
                highestAvgCity = cityID;
            }

            // Printing number

            System.out.printf("City id morning evening average %d %d %d %.1f", cityID, morningTemp, eveningTemp, avgTemp);

            totalRecords++;
        }

        System.out.println("\nTotal number of records: " + totalRecords);
        System.out.println("City " + lowestTempCity + " has the lowest morning temperature of " + lowestAMTemp);
        System.out.printf("City %s has the highest average temperature of %.1f",highestAvgCity, highestAvgTemp);
    }
}

Sample Output:-

-------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count = 0; double listPrice; String...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at the top and at least one more good comment later in the program), to process data for the registrar as follows: NOTE: Include prompts. Send all output to the screen. 1. Read in the id number of a student, the number of credits the student has, and the student’s grade point average (this is a number like 3.25). Print the original data right after...
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT