Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.
Getting Ready: Before going any further, you should:
downloads
directory/folder). In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above and then selecting
.java
Files: In most IDEs, .java
files
(i.e., source files) can just be copied into the project.
See the course "Help" page on your IDE for more information.
.class
and .jar
Files:
In some IDEs it is easier to use .class
files and in others it is easier to use a .jar
file
that contains the .class
files. Hence, you have been
provided with both.
See the course "Help" page on your IDE for more information.
Resources: In some IDEs, resources (e.g., images, data files) need to be in a special directory whereas in others they need to be in the working directory. See the course "Help" page on your IDE for more information.
Announcement
class to display air quality information.
ShowAQI.java
.
ShowAQI.java
.
ShowAQI
, what is required to
execute it "properly?
ShowAQI
using what you know is required to
execute it "properly" (see the course "Help" page for your IDE
if you don't remember how). What
happened?
ShowAQI
. Why did the window
have the color and text that it did?
findColor()
and findDescription()
methods static?
ShowAQI.findColor()
and ShowAQI.findDescription()
?
findColor()
method so that it returns the
appropriate Color
object. To save time, only consider
the first three categories, and treat SENSITIVE as the
default. (Don't be fancy -- just use nested if
statements or a switch
statement and hard-code the
ranges.)
findDescription()
method so that it returns the
appropriate String
object. Again, to save time, only consider
the first three categories, treat SENSITIVE
as the
default, and don't be fancy.
ShowAQI
application is not very good.
For one thing, the ShowAQI
class is not cohesive
(i.e., it does too many different kinds of things). For another,
the implementations of findColor()
and
findDescription()
contain duplicate code.
How can these two problems be solved?
AirQuality
objects
are there?
AirQuality
be a class
or
an enum
? In other words, do we need an intensive
definition or an extensive definition of the set
of AirQuality
objects?
AirQuality
enum. How many different
AirQuality
objects are there?
AirQuality
object have?
boolean contains(int aqi)
method
to AirQuality
that returns true
if aqi
is in the interval
[min
,max
] and false
otherwise.
Do not use an if
statement, a switch
statement, ternary operator, or local variables!
Color findColor(int aqi)
and String findDescription(int aqi)
methods in
the ShowAQI
class with a
AirQuality findAirQuality(int aqi)
method.
Again, limit your implementation to the first three categories.
However, this time, don't use hard-coded ranges. Instead, use the
contains()
method in the AirQuality
enum.
main()
method of the ShowAQI
class
so that it uses the findAirQuality()
method.
ShowAQI
.
ShowAQI
cohesive?
findAirQuality()
method from the ShowAQI
class to the AirQuality
enum. (Note: You will have to change
the visibility to public
.)
findAirQuality()
method static?
ShowAQI
class so that it uses
the findAirQuality()
method in
the AirQuality
class.
ShowAQI
.
ShowAQI
class cohesive?
findAirQuality()
method in the AirQuality
class complete?
AirQuality
objects,
how long would it take to add the other three cases?
AirQuality
objects,
how long would it take to add the other 97 cases?
AirQuality
need to be added to
the enum
three years from now. How likely is it that
you will remember to also modify the findAirQuality()
method?
values()
method and a
traditional for
loop to fully-implement
the findAirQuality()
method. (Return null
if the if the aqi
is out of bounds.)
ShowAQI
.
Copyright 2022