Calculating average weekly temperature – Java Program

What it does:

A program to find the average of weekly temperature. It will prompt the user to enter a temperature for each day of the week. Where at the end of the program it will find out there average. It will also prompt the user to see if they would like to use the program again entering different temperatures.

Code:


public class WeeklyTemperatures
{
   //Variables
   static double monTemp, tueTemp, wedTemp, thurTemp, friTemp, satTemp, sunTemp;
   static Scanner keyboard = new Scanner(System.in);
   static DecimalFormat df = new DecimalFormat("00.00");


   public static void menu()
   {
      System.out.println("Welcome to the temperature calculator\n");

      //Prompting user for the temperature each day
      System.out.println("Please enter Monday's temperature. ");
      monTemp = keyboard.nextDouble();

      System.out.println("Please enter Tuesday's temperature. ");
      tueTemp = keyboard.nextDouble();

      System.out.println("Please enter Wednesday's temperature. ");
      wedTemp = keyboard.nextDouble();

      System.out.println("Please enter Thursday's temperature. ");
      thurTemp = keyboard.nextDouble();

      System.out.println("Please enter Friday's temperature. ");
      friTemp = keyboard.nextDouble();

      System.out.println("Please enter Saturday's temperature. ");
      satTemp = keyboard.nextDouble();

      System.out.println("Please enter Sunday's temperature. ");
      sunTemp = keyboard.nextDouble();

   }//menu

   public static void dailyAverageTemp()
   {
      int day;

      System.out.println("\nTo see the average daily temperature, please enter the number " +
              "for the day of the week you wish to view.");

      System.out.println("1. Monday");
      System.out.println("2. Tuesday");
      System.out.println("3. Wednesday");
      System.out.println("4. Thursday");
      System.out.println("5. Friday");
      System.out.println("6. Saturday");
      System.out.println("7. Sunday\n");
      day = keyboard.nextInt();

      switch (day)
      {

         case 1: System.out.println("Monday's average temperature was "+(df.format(monTemp))+"C");
               break;
         case 2: System.out.println("Tuesday's average temperature was "+(df.format(tueTemp))+"C");
               break;
         case 3: System.out.println("Wednesday's average temperature was "+(df.format(wedTemp))+"C");
               break;
         case 4: System.out.println("Thursday's average temperature was "+(df.format(thurTemp))+"C");
               break;
         case 5: System.out.println("Friday's average temperature was "+(df.format(friTemp))+"C");
               break;
         case 6: System.out.println("Saturday's average temperature was "+(df.format(satTemp))+"C");
               break;
         case 7: System.out.println("Sunday's average temperature was "+(df.format(sunTemp))+"C");

      }//switch

   }//averageDailyTemp

   public static void calculatingAverageWeeklyTemp()
   {
      double sumOfTemps;
      double averageWeeklyTemp;

      //Adding all the temperatures for each day together.
      sumOfTemps = monTemp+tueTemp+wedTemp+thurTemp+friTemp+satTemp+sunTemp;

      //Dividing the sum of all temperatures by 7 to find the average weekly temp.
      averageWeeklyTemp = sumOfTemps/7;

      System.out.println("\nThe average weekly temperature was "+(df.format(averageWeeklyTemp))+"C");


   }//calculatingAverage

   public static void exit()
   {

      int choice;

      System.out.println("\nWould you like to exit?");
      System.out.println("1. Yes");
      System.out.println("2. No");
      choice = keyboard.nextInt();

      if(choice ==1)
      {
         System.out.println("\nThank-you for using this system!");
         System.exit(0);
      }//if

      else
      {
         System.out.println("\nYou will now be redirected to the main menu");
         menu();
      }//else

   }//exit

   public static void main(String[] args)
   {
      menu();
      dailyAverageTemp();
      calculatingAverageWeeklyTemp();
      exit();   
}//main 
}//class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s