Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

1400 - Prog - 7 - Weather Forecaster CIST 1400, Introduction to Computer Programming Programming Assignment In this assignment you will be implementing a weather forecaster. It involves writing 3...

1 answer below »
1400 - Prog - 7 - Weather Forecaste
CIST 1400, Introduction to Computer Programming
Programming Assignment
In this assignment you will be implementing a weather forecaster. It involves writing 3 different classes plus a driver program. It is recommended that
you write one class at a time and then write a driver (tester) program to ensure that it works before putting everything together. Alternately, you can
use the Interactions tab of jGRASP to test some of the early classes. This is discussed in the next few pages.
Write a multi-class program with three classes and a driver.
Based on the following UML diagrams, implement the three required classes, Temperature , Season , and Weather . You will also implement
a program that uses all of these classes in some fashion, Forecaster.java .
Name your classes as follows:
Temperature in source file Temperature.java
Season in source file Season.java
Weather in source file Weather.java
Forecaster in source file Forecaster.java
Program 7
Overview
Goals
What to Implement
Class and File Naming
Work on each class individually, and only in little bits at a time. The suggested order of development for the classes is:
1. Season
2. Temperature
3. Weathe
As you write each of these, test each method as you write it. Methods of testing are discussed in the next section, "How to Test Individual Classes".
After these three classes are developed, you can then work on the Forecaster program that will be discussed later in this document.
Plan to spend the full time available on this assignment. This is not an assignment that can be written in a night or two. If you parcel out you
development over the course of the entire time available, it should be manageable. This is especially important because you can't submit to WebCAT
until you have all four classes written and can submit them all at the same time.
As you develop individual classes ( Season.java , Temperature.java , and Weather.java ), you can test them separately in Web-CAT fo
no grade.
Web-CAT "Assignment"
Labeled FOR TESTING ONLY - NO GRADE
File(s) to Submit
Summer XXXXXXXXXXHW07 - Season Season.java
Summer XXXXXXXXXXHW07 - Temperature Temperature.java
Summer XXXXXXXXXXHW07 - Weather Season.java , Temperature.java , Weather.java
When you are ready to test the Forecaster.java file, you will submit all four files ( Season.java , Temperature.java ,
Weather.java , and Forecaster.java ) to the actual assignment that will be graded: HW 07 - Summer 2019 : Weather Forecaster -
SUBMIT ALL FOUR FILES FOR GRADING.
Since the Submission Energy feature of WebCAT is turned on, you only have 3 submissions per hour once you are at the point of testing all four files
working together. This means that a lot of testing needs to be done locally before you're ready to submit to WebCAT.
How To Not Be Overwhelmed By This Assignment
Testing Individual Classes with Web-CAT
As you write each of the classes below, you should test them as you implement various fields and methods. One way to do this is to write an
accompanying driver program. For instance, as you write the Temperature class, you might also write TemperatureDriver.java which
includes a main() method and does basic calls on each of the methods. This would give you good practice working with each of the methods in
the class to build on for future use in other classes.
Another, quicker way to test classes as you write them is to use Interactions pane/tab of jGRASP. When you have written a little bit of code (i.e.,
implemented some fields or a single method of the class), you can compile that code in jGRASP as normal and then go to the "Interactions" tag in the
ottom window of jGRASP (alongside "Compile Messages", "jGRASP Messages", and "Run I/O").
After you have compiled the class you're working on, selected the "Interactions" tab, and then clicked in that window at the bottom of jGRASP, you
can interactively create and work with objects, inspecting the object contents (fields) in the "Workbench" window on the left side of jGRASP.
Using this method of code writing can speed up the development process dramatically because you can immediately catch issues with your code and
test things immediately.
It is suggested that you write a method and then compile and test that method immediately instead of trying to write all of the methods at once and
then test them. Once you are fairly certain one method works co
ectly, move on to the next method. You may discover in later testing that something
may be
oken and have to come back and fix it, but test each method as thoroughly as possible after you write it.
As an example, when writing the Temperature class, implement the instance variables and write the default constructor and then test it to make
sure it works co
ectly.
Notice in the example on the next page that you're not required to use semicolons to end statements in the Interactions tab because you're not
necessarily writing complete Java statements, you're just testing individual methods. Additionally, a number of different methods are being tested in
the example image displayed, but only after they have each been individually tested incrementally.
How to Test Individual Classes
One possible use of the Interactions tab to test your Temperature class as you develop it:
What the Workbench panel might look like after working with a Temperature object in the Interactions pane a bit:
The first, shortest, and easiest class you should write is the Season class. This class represents a season and should be saved in the file
Season.java . Use the UML diagram below for requirements.
Season
- season : String
«constructor» Season()
+ getSeason(): String
+ setSeason(newSeason: String) : void
+ toString() : String
+ equals(other: Season) : boolean
Here are
ief descriptions of the fields:
Field Description Notes
season Stores name of cu
ent season The value can be one of spring , summer , fall , winter , autumn in lowercase
Here are
ief descriptions of all of the methods you should implement for the Season class. Make sure to name them and implement them exactly
as you see them here as this is how Web-CAT will test them.
The Season Class
PLEASE NOTE: The "Sample Interactions Tab Test Code" would be entered into the Interactions tab in jGRASP after you have added a method and
successfully compiled the code. These samples are not comprehensive and may not test every single aspect of the methods. You should use these
as jumping-off points for your own testing.
Method Description
Sample Interactions Tab Test
Code
Season() Default constructor, defaults to winte
Season x = new Season();
Season y = new Season()
getSeason() returns the cu
ent value of season
Season a = new Season()
a.getSeason()
setSeason(newSeason)
Sets season to lowercase version of parameter newSeason if valid,
egardless of capitalization (i.e., SuMMer and summer and SUMMER
should all set summer for season); nothing done if newSeason is invalid.
Season b = new Season();
.setSeason("summer")
.getSeason()
.setSeason("aUtUmN")
.getSeason()
.setSeason("potato")
.getSeason()
toString() Simply returns the season, the same as getSeason()
Season c = new Season()
c
c.toString()
equals(other)
Returns equality of two Season objects if their seasons match; autumn
and fall are considered the same.
Season d = new Season()
Season e = new Season()
d.equals(e)
e.equals(d)
e.setSeason("sumMER")
d.equals(e)
The Season Class, continued
The next class you should write is the Temperature class. This class represents a temperature and should be saved in the file
Temperature.java .
Use the UML diagram below for requirements. Exact implementation is up to you. Pay careful attention to changing the scale or setting the
temperature. For instance, if the temperature is set at 32° F and then the scale is changed to C with the setScale() method, how will you
handle the temperature?
Temperature
- degrees : double
- scale : cha
«constructor» Temperature()
«constructor» Temperature(temp : double, sc: char)
+ getTemp(): double
+ getScale() : cha
+ set(temp: double, sc: char) : void
+ setTemp(temp: double) : void
+ setScale(sc: char) : void
+ toString() : String
+ equals(other: Temperature) : boolean
Here are
ief descriptions of the fields:
Field Description Notes
degrees Stores temperature should be between -50° and +150° Fahrenheit, inclusive
scale Indicates Celsius or Fahrenheit as must be capital C or F
The Temperature Class
Here are
ief descriptions of all of the methods you should implement for the Temperature class. Make sure to name them and implement them
exactly as you see them here as this is how Web-CAT will test them:
Method Description
Sample Interactions Tab Test
Code
Temperature() Default constructor, sets temperature to 0° Celsius. Temperature a = new Temperature()
Temperature(temp,sc)
Specific constructor, sets temperature and scale; make sure
temperature falls within valid range and scale is co
ect.
Temperature b = new Temperature(10,'C')
Temperature c = new Temperature(-40,'F')
Temperature d = new Temperature(-60,'F')
Temperature e = new Temperature(151,'F')
Temperature f = new Temperature(66,'C')
Temperature f = new Temperature(-46,'C')
Temperature g = new Temperature(10,'z')
getTemp() Returns the cu
ent temperature based on the scale.
.getTemp()
c.getTemp()
c.getTemp()
d.getTemp()
e.getTemp()
f.getTemp()
g.getTemp()
getScale() Returns the value of the scale field.
.getScale()
c.getScale()
c.getScale()
d.getScale()
e.getScale()
f.getScale()
g.getScale()
set(temp,sc) Sets temperature and scale; if either is invalid nothing happens
.set(10,'C')
.set(-40,'F')
.set(-60,'F')
.set(151,'F')
.set(66,'C')
.set(-46,'C')
.set(10,'z')
setTemp(temp)
Validates the temperature for object's scale; no change to
temperature if invalid; make sure to validate for given scale.
Temperature j = new Temperature()
j.getTemp()
j.setTemp(12)
j.getTemp()
Temperature k = new Temperature(112,'F')
k.setTemp(-14.2)
The Temperature Class, continued
Method Description
Sample Interactions Tab Test
Code
setScale(sc)
Sets the scale; parameter can be either upper or lowercase,
Answered Same Day Jul 28, 2021

Solution

Abhishek answered on Jul 28 2021
141 Votes
42756 - Weather Season/Solution/Season.java
42756 - Weather Season/Solution/Season.java
public class Season {
    private String season;
    
    public Season() {
        this.season="winter";
    }
    public String getSeason() {
        return season;
    }
    public void setSeason(String newSeason) {
        newSeason=newSeason.toLowerCase();
        String[] valid=new String[] {"spring" , "summer" , "fall" , "winter" , "autumn"};
        for(String s:valid)
        {
            if(newSeason.equals(s))
            {
                this.season = newSeason;
                return;
            }
        }
    }
    
    @Ove
ide
    public String toString() {
        return getSeason();
    }
    
    @Ove
ide
    public boolean equals(Object obj) {
        Season s=(Season)obj;
        if(s.season.equals("autumn") && season.equals("fall"))
            return true;
        if(s.season.equals("fall") && season.equals("autumn"))
            return true;
        return s.season.equals(season);
    }
}
42756 - Weather Season/Solution/Forecaster.java
42756 - Weather Season/Solution/Forecaster.java
import java.util.Scanner;
public class Forecaster {
    private static Scanner sc;
    public static void main(String[] args) {
        sc=new Scanner(System.in);
        System.out.println("Welcome to the Weather Forecaster.");
        Weather weather=new Weather();
        String choice="";
        do {
            choice=menu();
            System.out.println();
            if(choice.equals("1"))
            {
                char pref=getString("Enter temperature preference (C/F): ",new String[] {"C","F"}).charAt(0);
                weather.getTemp().setScale(pref);
                System.out.println();
            }
            else if(choice.equals("2"))
            {
                String season=getString("Enter season: ",
                        new String[] {"spring" , "summer" , "fall" , "winter" , "autumn"});
                weather.setSeason(season);
                System.out.println();
            }
            else if(choice.equals("3"))
            {
                setWeather(weather);
                System.out.println();
            }
            else if(choice.equals("4"))
            {
                printForecast(weather);
                System.out.println();
            }
            else if(choice.equals("5"))
            {
                System.out.println(weather);
                System.out.println();
            }
            else if(choice.equals("6"))
            {}
            else 
            {
                System.out.println("Invalid selection.");
                System.out.println();
            }
        }while(!choice.equals("6"));
                
                
    }
    
    private static void setWeather(Weather w) {
        System.out.print("Enter temperature: ");
        double temp=Double.parseDouble(sc.nextLine());
        System.out.print("Enter humidity: ");
        int h=Integer.parseInt(sc.nextLine());
        System.out.print("Enter windspeed: ");
        int ws=Integer.parseInt(sc.nextLine());
        System.out.print("Enter wind direction: ");
        String wd=sc.nextLine();
        char sc=w.getTemp().getScale();
        double tempF=temp;
        if(sc=='C')
            tempF=(tempF * (9.0/5.0)) + 32;
        if(tempF<-50 || tempF>150 || h<0 || h>100 || ws<0)
        {
            System.out.println("Invalid entry, try again.");
            setWeather(w);
            return;
        }
        String[] valid=new String[] {"N" ,"E" , "S" , "W" , "NE" , "SE" , "SW" , "NW"};
        wd=wd.toUpperCase();
        for(String s:valid)
        {
            if(wd.equals(s))
            {
                w.setHumidity(h);
                w.getTemp().setTemp(temp);
                w.setWindDir(wd);
                w.setWindSp(ws);
                return;
            }
        }
        System.out.println("Invalid entry, try again.");
        setWeather(w);
        return;
        
    }
    
    private static void printForecast(Weather w) {
        
        
        if(w.getSeason().equals("winter"))
        {
            printWinterWeather(w);
        }
        if(w.getSeason().equals("summer"))
        {
            printSummerWeather(w);
        }
        if(w.getSeason().equals("fall")|| w.getSeason().equals("autumn"))
        {
            printFallWeather(w);
        }
        if(w.getSeason().equals("spring"))
        {
            printSpringWeather(w);
        }
        
        
    }
    private static void printFallWeather(Weather w) {
        Temperature temp=w.getTemp();
        char initScale=temp.getScale();
        temp.setScale('F');
        if(temp.getTemp()>=65  &&  temp.getTemp()<80 && w.getWindSp()<15 && w.getHumidity()<=60 )
        {
            System.out.println("The forecast is nice");
        }
        else if(temp.getTemp()>=80)
        {
            System.out.println("The forecast is too warm");
        }
        else if(temp.getTemp()>=40 && temp.getTemp()<65 && w.getWindSp()>15 )
        {
            System.out.println("The forecast is chilly");
        }
        else if(w.getHumidity()>=80 )
        {
            System.out.println("The forecast is rainy");
        }
        else
            System.out.println("The forecast is typical");
        temp.setScale(initScale);           
    }
    private static void printSummerWeather(Weather w) {
        Temperature temp=w.getTemp();
        char initScale=temp.getScale();
        temp.setScale('C');
        if(temp.getTemp()>=32 &&  w.getWindSp()<10 && w.getHumidity()>=70)
        {
            System.out.println("The forecast is steamy");
        }
        else if(temp.getTemp()>=32 && w.getWindSp()>=20 && w.getHumidity()>=70)
        {
            System.out.println("The forecast is stormy");
        }
        else if(temp.getTemp()>=30 && temp.getTemp()<50 )
        {
            System.out.println("The forecast is dry heat");
        }
        else if(temp.getTemp()>=30 && temp.getTemp()<32 &&  w.getWindSp()>=20)
        {
            System.out.println("The forecast is hot and windy");
        }
        else if(temp.getTemp()>=30)
        {
            System.out.println("The forecast is hot");
        }
        else
            System.out.println("The forecast is warm");
        temp.setScale(initScale);           
    }
    private static void printWinterWeather(Weather w) {
        Temperature temp=w.getTemp();
        char initScale=temp.getScale();
        temp.setScale('F');
        if(temp.getTemp()<10 &&  w.getWindSp()>15)
        {
            System.out.println("The forecast is frigid");
        }
        else if(temp.getTemp()>=10 && temp.getTemp()<=30 && w.getHumidity()>=80)
        {
            System.out.println("The forecast is snow");
        }
        else if(temp.getTemp()>=28 && temp.getTemp()<=33 && w.getHumidity()>=60 && w.getHumidity()<=80)
        {
            System.out.println("The forecast is icy");
        }
        else if(temp.getTemp()>40)
        {
            System.out.println("The forecast is warm");
        }
        else
            System.out.println("The forecast is cold");
        temp.setScale(initScale);       
    }
    private static void printSpringWeather(Weather w) {
        Temperature temp=w.getTemp();
        char initScale=temp.getScale();
        temp.setScale('F');
        if(temp.getTemp()>65 && temp.getTemp()<=80 && w.getWindSp()>=20 && w.getHumidity()>=80)
        {
            System.out.println("The forecast is stormy");
        }
        else if(temp.getTemp()<50)
        {
            System.out.println("The forecast is chilly");
        }
        else if(w.getHumidity()<80 && w.getWindSp()>=20)
        {
            System.out.println("The forecast is windy");
        }
        else
            System.out.println("The forecast is pleasant");
        temp.setScale(initScale);
        
    }
    private static String getString(String string, String[] strings) {
        System.out.print(string);
        String input=sc.nextLine();
        for(String s:strings)
        {
            if(s.equalsIgnoreCase(input))
                return input;
        }
        return getString(string, strings);
    }
    
    private static String menu() {
        System.out.println("1. Set temperature preference\n" + 
                "2. Set season\n" + 
                "3. Set weather\n" + 
                "4. Get forecast\n" + 
                "5. Print the weather\n" + 
                "6. Quit");
        System.out.print("Enter choice: ");
        return sc.nextLine();
    }
}
42756 - Weather Season/Solution/Temperature.java
42756 - Weather Season/Solution/Temperature.java
public class Temperature {
    private double degrees;
    private char scale;
    public Temperature() {
        this.scale='C';
        this.degrees=(0 * (9.0/5.0)) + 32;;
    }
    public Temperature(double temp, char sc) {
        
        double tempF=temp;
        if(sc!='C' && sc!='F')
        {
            this.scale='C';
            this.degrees=0;
            System.out.println(scale+"="+degrees);
            return;
        }
        if(sc=='C')
            tempF=(tempF * (9.0/5.0)) + 32;
        if(tempF<-50 || tempF>150)
            this.degrees = 0;
        else
            this.degrees=tempF;
        this.scale = sc;
        System.out.println(scale+"="+degrees);
    }
    
    public double getTemp()
    {
        if(scale=='C')
            return (degrees - 32.0) * (5.0/9.0);
        else 
            return degrees;
    }
    public char getScale() {
        return scale;
    }
    
    public void set(double temp, char sc) {
        
        double tempF=temp;
        if(sc!='C' && sc!='F')
        {
            System.out.println(scale+"="+degrees);
            return;
        }
        if(sc=='C')
            tempF=(tempF * (9.0/5.0)) + 32;
        if(tempF<-50 || tempF>150)
            return;
        
        this.degrees=tempF;
        this.scale = sc;
        System.out.println(scale+"="+degrees);
    }
    
    public void setTemp(double temp)
    {
        double tempF=temp;
        if(scale=='C')
            tempF=(tempF * (9.0/5.0)) + 32;
        if(tempF<-50 || tempF>150)
            return;
        this.degrees=tempF;
    }
    
    public void setScale(char sc)
    {
        if(sc!='C' && sc!='F')
        {
            return;
        }
        this.scale=sc;
    }
    
    @Ove
ide
    public String toString() {
        return getTemp()+" degrees "+getScale();
    }
    
    @Ove
ide
    public boolean equals(Object obj) {
        Temperature temp=(Temperature)obj;
        return Math.abs(temp.degrees-degrees)<0.001;
    }
}
42756 - Weather Season/Solution/Weather.java
42756 - Weather Season/Solution/Weather.java
public class Weather {
    private Temperature temp;
    private int humidity;
    private int windspeed;
    private String windDirection;
    private Season s;
    public Weather() {
        temp=new Temperature();
        humidity=50;
        windspeed=0;
        windDirection="W";
        s=new Season();
    }
    public Weather(double t, char sc, String whichSeason)
    {
        temp=new Temperature(t, sc);
        humidity=50;
        windspeed=0;
        windDirection="W";
        this.s=new Season();
        s.setSeason(whichSeason);
    }
    public void setSeason(String  newSeason)
    {
        s.setSeason(newSeason);
    }
    public String getSeason()
    {
        return s.getSeason();
    }
    public Temperature getTemp()
    {
        return this.temp;
    }
    public int getHumidity()
    {
        return this.humidity;
    }
    public int getWindSp()
    {
        return this.windspeed;
    }
    public String getWindDir()
    {
        return this.windDirection;
    }
    public void setTemperature(Temperature t)
    {
        this.temp=t;
    }
    public void setHumidity(int h)
    {
        if(h<0 || h>100)
            return;
        this.humidity=h;
    }
    public void setWindSp(int sp)
    {
        if(sp<0)
            return;
        this.windspeed=sp;
    }
    public void setWindDir(String dir)
    {
        String[] valid=new String[] {"N" ,"E" , "S" , "W" , "NE" , "SE" , "SW" , "NW"};
        dir=dir.toUpperCase();
        for(String s:valid)
        {
            if(dir.equals(s))
            {
                this.windDirection = dir;
                return;
            }
        }
    }
    @Ove
ide
    public String toString() {
        return "The weather is cu
ently "+temp+" with "+humidity+"% humidity and a "+
                windspeed+" mph wind from the "+windDirection;
    }
    @Ove
ide
    public boolean equals(Object obj) {
        Weather w=(Weather)obj;
        return w.temp.equals(temp)&& w.humidity==humidity;
    }
}
42756 - Weather Season/Sample Runs/Lengthy Sample Run With All Good Input.txt
Welcome to the Weather Forecaster.
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 5
The weather is cu
ently 0.0 degrees C with 50% humidity and a 0 mph wind from the W
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 1
Enter temperature preference (C/F): F
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 2
Enter season: summe
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 3
Enter temperature: 8
Enter humidity: 30
Enter windspeed: 20
Enter wind direction: NW
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 5
The weather is cu
ently 8.0 degrees F with 30% humidity and a 20 mph wind from the NW
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 4
The forecast is warm
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 1
Enter temperature preference (C/F): C
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 5
The weather is cu
ently -13.333333333333334 degrees C with 30% humidity and a 20 mph wind from the NW
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 1
Enter temperature preference (C/F): F
1. Set temperature preference
2. Set season
3. Set weathe
4. Get forecast
5. Print the weathe
6. Quit
Enter choice: 5
The weather is cu
ently 8.0 degrees F with 30% humidity...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here