Question

In this assignment you will be implementing a weather forecaster. It involves writing 3 different classes plus a driver progrThe Season Class The first, shortest, and easiest class you should write is the Season class. This class represents a seasonThe Season Class, continued PLEASE NOTE: The Sample Interactions Tab Test Code would be entered into the Interactions tab iThe Temperature Class The next class you should write is the Temperature class. This class represents a temperature and shoulThe Temperature Class, continued Here are brief descriptions of all of the methods you should implement for the Temperature cThe Temperature Class, continued even more Method Description Sample Interactions Tab Test Code setScale(sc) Sets the scale;The Weather Class It depends on the Temperature and This class represents weather and should be saved in the file Weather.javThe Weather Class, continued Here are brief descriptions of all of the methods you should implement for the Weather class. MaThe Driver Program: Forecaster.java After these three classes have been developed, you also need to write a program with a maIf the season for the Weather object is summer : Output Requirements The forecast is steamy The temperature is 32° C or above

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Season.java

public class Season {

   private String season;
  
   // default constructor
   public Season()
   {
       season = "winter";
   }
  
   // return the season
   public String getSeason()
   {
       return season;
   }
  
   // set the season
   public void setSeason(String newSeason)
   {
       // validate newSeason
       if(newSeason.equalsIgnoreCase("summer") || newSeason.equalsIgnoreCase("winter") || newSeason.equalsIgnoreCase("autumn")
               || newSeason.equalsIgnoreCase("fall") || newSeason.equalsIgnoreCase("spring"))
           season = newSeason.toLowerCase();
   }
  
   // return String representation of Season
   public String toString()
   {
       return season;
   }
  
   // returns true if this season is equal to other season
   public boolean equals(Season other)
   {
       if(season.equals(other.getSeason()))
           return true;
       else if((season.equals("fall") && other.getSeason().equals("autumn")) || (season.equals("autumn") && other.getSeason().equals("fall"))) // fall and autumn are same
           return true;
       return false;
   }
}
//end of Season.java

// Temperature.java

public class Temperature {
  
   private double degrees;
   private char scale;
  
   // default constructor
   public Temperature()
   {
       degrees = 0;
       scale = 'C';
   }
  
   // parameterized constructor
   public Temperature(double temp, char sc)
   {
       // validate scale
       if(sc == 'C')
       {
           scale = sc;
           double tempF = ((temp*9)/5)+32;
          
           // validate temperature
           if(tempF >= -50 && tempF <=150)
               degrees = temp;
           else
               degrees = 0;
       }else if(sc == 'F')
       {
           scale = sc;
           if(temp >= -50 && temp <=150)
               degrees = temp;
           else
               degrees = 0;
       }else
       {
           degrees = 0;
           scale = 'C';
       }
   }
  
   // return temperature degrees
   public double getTemp()
   {
       return degrees;
   }
  
   // return temperature scale
   public char getScale()
   {
       return scale;
   }
  
   // set the temperature degrees and scale
   public void set(double temp, char sc)
   {
       if(sc == 'C')
       {
           double tempF = ((temp*9)/5)+32;
           if(tempF >= -50 && tempF <=150)
           {
               degrees = temp;
               scale = sc;
           }
       }else if(sc == 'F')
       {
           if(temp >= -50 && temp <=150)
           {
               degrees = temp;
               scale = sc;
           }
       }
   }
  
   // set temperature degrees
   public void setTemp(double temp)
   {
       if(scale == 'C')
       {
           double tempF = ((temp*9)/5)+32;
           if(tempF >= -50 && tempF <=150)
               degrees = temp;
          
       }else if(scale == 'F')
       {
           if(temp >= -50 && temp <= 150)
               degrees = temp;
       }
   }
  
   // set temperature scale
   public void setScale(char sc)
   {
       if(scale == 'F')
       {  
           if(sc == 'C')
           {
               degrees = ((degrees-32)*5)/9;
               scale = sc;
              
           }
       }else
       {
           if(sc == 'F')
           {
               degrees = ((degrees*9)/5)+32;
               scale = sc;
           }
       }
   }
  
   // return String representation
   public String toString()
   {
       return(degrees+" degrees "+scale);
   }
  
   // returns true if this temperature equals other else false
   public boolean equals(Temperature other)
   {
       double tempF1,tempF2;
       if(scale == 'F')
           tempF1 = degrees;
       else
           tempF1 = ((degrees*9)/5)+32;
      
       if(other.getScale() == 'F')
           tempF2 = other.getTemp();
       else
           tempF2 = ((degrees*9)/5)+32;
      
       if(Math.abs(tempF1-tempF2) <= 0.001)
           return true;
       return false;
   }
}

//end of Temperature.java

// Weather.java

public class Weather {
  
   private Temperature temp;
   private int humidity;
   private int windSpeed;
   private Season s;
  
   // default constructor
   public Weather()
   {
       temp = new Temperature();
       humidity = 50;
       windSpeed = 0;
       s = new Season();
   }
  
   // parameterized constructor
   public Weather(double t, char sc, String whichSeason )
   {
       temp = new Temperature(t,sc);
       humidity = 50;
       windSpeed = 0;
       s = new Season();
       s.setSeason(whichSeason);
   }
  
  
   // return season
   public Season getSeason()
   {
       return s;
   }
  
   // set season
   public void setSeason(Season newSeason)
   {
       s.setSeason(newSeason.getSeason());
   }
  
   // return temperature
   public Temperature getTemp()
   {
       return temp;
   }
  
   // return humidity
   public int getHumidity()
   {
       return humidity;
   }
  
   // return wind speed
   public int getWindSp()
   {
       return windSpeed;
   }
  
  
   // set temperature
   public void setTemperature(Temperature t)
   {
       temp.set(t.getTemp(), t.getScale());
   }
  
   // set humidity
   public void setHumidity(int h)
   {
       if(h>=0 && h<=100)
           humidity = h;
   }
  
   // set wind speed
   public void setWindSp(int sp)
   {
       if(sp >= 0)
           windSpeed = sp;
   }
  
  
   // return String representation of Weather
   public String toString()
   {
       return("The weather is currently "+temp+" with "+humidity+"% humidity and a "+windSpeed+" mph wind");
   }
  
   // returns true if this weather is equal to other
   public boolean equals(Weather other)
   {
       if((temp.equals(other.getTemp())) && (humidity == other.getHumidity()))
           return true;
       return false;
   }
  
}

//end of Weather.java

// Forecaster.java

import java.util.Scanner;

public class Forecaster {
  
   // method to forecast the weather
   public static void forecast(Weather weather)
   {
       double tempF , tempC;
      
       if(weather.getTemp().getScale() == 'F')
       {
           tempF = weather.getTemp().getTemp();
           tempC = ((weather.getTemp().getTemp()-32)*5)/9;
       }
       else
       {
           tempF = ((weather.getTemp().getTemp()*9)/5)+32;
           tempC = weather.getTemp().getTemp();
       }
      
      
       if(weather.getSeason().getSeason().equals("winter"))
       {
           if(tempF < 10 && weather.getWindSp() > 15)
               System.out.println("The forecast is frigid");
           else if(tempF >=10 && tempF <=30 && weather.getHumidity() >= 80)
               System.out.println("The forecast is snow");
           else if(tempF >=28 && tempF <= 33 && weather.getHumidity() >= 60 && weather.getHumidity() <= 80)
               System.out.println("The forecast is icy");
           else if(tempF > 40)
               System.out.println("The forecast is warm");
           else
               System.out.println("The forecast is cold");
       }else if(weather.getSeason().getSeason().equals("summer"))
       {
           if(tempC >= 32 && weather.getWindSp() < 10 && weather.getHumidity() >=70)
               System.out.println("The forecast is steamy");
           else if(tempC >= 32 && weather.getWindSp() >= 20 && weather.getHumidity() >= 70)
               System.out.println("The forecast is stormy");
           else if(tempC >= 30 && weather.getHumidity() < 50)
               System.out.println("The forecast is dry heat");
           else if(tempC >= 30 && tempC < 32 && weather.getWindSp() >= 20)
               System.out.println("The forecast is hot and windy");
           else if(tempC >= 30)
               System.out.println("The forecast is hot");
           else
               System.out.println("The forecast is warm");
       }else if(weather.getSeason().getSeason().equals("spring"))
       {
           if(tempF > 65 && tempF <= 80 && weather.getWindSp() >= 20 && weather.getHumidity() >= 80)
               System.out.println("The forecast is stormy");
           else if(tempF < 50)
               System.out.println("The forecast is chilly");
           else if(weather.getHumidity() < 80 && weather.getWindSp() >= 20)
               System.out.println("The forecast is windy");
           else
               System.out.println("The forecast is pleasant");
       }else
       {
           if(tempF >= 65 && tempF < 80 && weather.getWindSp() < 15 && weather.getHumidity() <= 60)
               System.out.println("The forecast is nice");
           else if(tempF >= 80)
               System.out.println("The forecast is too warm");
           else if(tempF >= 40 && tempF < 65 && weather.getWindSp() > 15)
               System.out.println("The forecast is chilly");
           else if(weather.getHumidity() >= 80)
               System.out.println("The forecast is rainy");
           else
               System.out.println("The forecast is typical");
       }
   }

   public static void main(String[] args) {


       String season;
       char scale;
       double degrees;
       int humidity, windSpeed;
      
       int choice;
       Scanner scan = new Scanner(System.in);
      
       Weather weather = new Weather();
       Season s = new Season();
       // loop continues till the user wants
       do
       {
           // display the menu
           System.out.println("1. Set temperature preference");
           System.out.println("2. Set season");
           System.out.println("3. Set weather");
           System.out.println("4. Get forecast");
           System.out.println("5. Print weather");
           System.out.println("6. Quit");
          
           // input of menu choice
           System.out.print("Enter your choice : ");
           choice = scan.nextInt();
          
           scan.nextLine();
           System.out.println();
           // perform the operation based on menu choice
           switch(choice)
           {
           case 1:
               System.out.print("Enter the temperature preference (C/F) : ");
               scale = scan.nextLine().charAt(0);
               while((scale != 'C') && (scale != 'F')) // loop that continues until user enters a valid scale
               {
                   System.out.println("Invalid choice.");
                   System.out.print("Enter the temperature preference (C/F) : ");
                   scale = scan.nextLine().charAt(0);
                  
               }
              
               weather.getTemp().setScale(scale);
               break;
           case 2:
               System.out.print("Enter season : ");
               season = scan.nextLine();
               while(!season.equalsIgnoreCase("summer") && !season.equalsIgnoreCase("winter") && !season.equalsIgnoreCase("autumn")
                       && !season.equalsIgnoreCase("fall") && !season.equalsIgnoreCase("spring")) // validation loop
               {
                   System.out.println("Invalid season. Season can be spring, summer, winter, autumn or fall");
                   System.out.print("Enter season : ");
                   season = scan.nextLine();
               }
              
              
              
               s.setSeason(season);
               weather.setSeason(s);
               break;
           case 3:
               System.out.print("Enter temperature : ");
               degrees = scan.nextDouble();
               System.out.print("Enter humidity : ");
               humidity = scan.nextInt();
               System.out.print("Enter Wind Speed : ");
               windSpeed = scan.nextInt();
               scan.nextLine();
              
               while(true) // validation loop
               {
                   if(weather.getTemp().getScale() == 'C')
                   {
                       double tempF = ((degrees*9)/5)+32;
                       if(tempF >= -50 && tempF <= 150 && humidity >=0 && humidity <=100 && windSpeed >= 0)
                           break;
                              
                   }else
                   {
                       if(degrees >= -50 && degrees <=150 && humidity >=0 && humidity <=100 && windSpeed >= 0)
                           break;
                   }
                   System.out.println("One or more invalid values");
                   System.out.print("Enter temperature : ");
                   degrees = scan.nextDouble();
                   System.out.print("Enter humidity : ");
                   humidity = scan.nextInt();
                   System.out.print("Enter Wind Speed : ");
                   windSpeed = scan.nextInt();
                   scan.nextLine();
               }
              
               weather.getTemp().setTemp(degrees);
               weather.setHumidity(humidity);
               weather.setWindSp(windSpeed);

               break;
              
           case 4:
               forecast(weather);
               break;
           case 5:
               System.out.println(weather);
               break;
           case 6:
               break;
           default:
               System.out.println("Invalid choice");
           }
          
           System.out.println();
       }while(choice != 6);
      
       scan.close();
   }

}

//end of Forecaster.java

Output:

1. Set temperature preference 2. Set season 3. Set weather 4. Get forecast 5. Print weather 6. Quit Enter your choice : 5 The

1. Set temperature preference 2. Set season 3. Set weather 4. Get forecast 5. Print weather 6. Quit Enter your choice : 5 The

1. Set temperature preference 2. Set season 3. Set weather 4. Get forecast 5. Print weather 6. Quit Enter your choice : 4 The

Enter season: Spring 1. Set temperature preference 2. Set season 3. Set weather 4. Get forecast 5. Print weather 6. Quit Ente

Add a comment
Know the answer?
Add Answer to:
In this assignment you will be implementing a weather forecaster. It involves writing 3 different classes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi im having trouble with this homework question. Can someone show me how to do this in C++ with all the steps? Weather Analysis WeatherAnalyzer - New Day Data 2- Good Days 3- Summary 0- Exit Step 0:...

    Hi im having trouble with this homework question. Can someone show me how to do this in C++ with all the steps? Weather Analysis WeatherAnalyzer - New Day Data 2- Good Days 3- Summary 0- Exit Step 0: You can get the sample file from this link: Each row in the text file represent weather measurement of a day. For each day, the file contains the temperature (first column), humidity (second column) and the wind (third column) values emp 70...

  • A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:     &

    A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:           temp (double) and tempScale (char; either ‘C’ or ‘F’) Constructor methods:    Temperature() – sets temp to 0 Celsius                                                 Temperature(double initialTemp) – sets temp                                                 Temperature(char tempType) – sets tempScale to ‘C’ or ‘F’)                                                 Temperature(double initialTemp, char tempType) --                                                                 sets temp and tempScale to ‘C’ or ‘F’) Accessor methods:          getTemp – returns value of temp for the object                                                 getTempScale– returns temperature scale (‘C’ or...

  • CIT-111 Homework #4, Part A                                      

    CIT-111 Homework #4, Part A                                                                    Chapter 4, problem #7 (pages 254-255) A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:           temp (double) and tempScale (char; either ‘C’ or ‘F’) Constructor methods:    Temperature() – sets temp to 0 Celsius                                                 Temperature(double initialTemp) – sets temp                                                 Temperature(char tempType) – sets tempScale to ‘C’ or ‘F’)                                                 Temperature(double initialTemp, char tempType) --                                                                 sets temp and tempScale to ‘C’ or ‘F’) Accessor methods:          getTemp – returns value of...

  • This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and...

    This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and TweetManager. You will load a set of tweets from a local file into a List collection. You will perform some simple queries on this collection. The Tweet and the TweetManager classes must be in separate files and must not be in the Program.cs file. The Tweet Class The Tweet class consist of nine members that include two static ones (the members decorated with the...

  • For this homework assignment, you will create two classes that will work with each other. The...

    For this homework assignment, you will create two classes that will work with each other. The first is a class you will write is called CQuadratic, which represents a second-order quadratic equation (e.g., 3x2 + 2x + 9).  CQuadratic will only have one constructor (type constructor), which will take the coefficients of the quadratic equation (e.g., 3, 2, 9) and assign them to it’s three private data members (the three coefficient variables). The class also has an Evaluate function that passes...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress,...

    DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress, Cart and Main. ITEM CLASS Your Item class should contain: Attributes (protected) String name - name of the Item double price - price of the item Methods (public) void setName(String n) - sets the name of Item void setPrice(double p) - sets the price of Item String getName() - retrieves name double getPrice() - retrieves price String formattedOutput() returns a string containing detail of...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT