o
UpperBoundedCounter
) (Approval)UpperBouncedCounter
that models an up/down counter with an upper limit. The counter can always be decremented, but can only be incremented as long as the
current value is less then the limit.
The class should contain the following state and behavior:
value
and limit
up
and down
methods. The methods return true if the operation could be performed and false otherwise.
toString
method that prints the value and limit in the format value/limit
read
method that accepts a Scanner
, reads in an initial value and a limit (in that order), and returns a new
UpperBoundedCounter
object constructed from those values
main
method (that's the next exercise)
The next lab (1.1.2) illustrates the object in use; you might want to take a look at it before you start implementing your class.
toString
UpperBoundedCounterApp
) (Approval)main
method (and any other supporting methods you want) that illustrates the use of your class from Lab 1.1.1. The app should:
counter_actions.text
, and print its value
+
for an up
and
-
for a down
.
counter_actions.text
0 5 + + + + + + - - - + + +Sample Test Run
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
Here is the actual output from System.out
(stdout or the standard output stream)
as you will see in in CodeLab's test case tables.
stdout
After reading in ubc1: 0/5 Action is + ... up: true ... ubc1: 1/5 Action is + ... up: true ... ubc1: 2/5 Action is + ... up: true ... ubc1: 3/5 Action is + ... up: true ... ubc1: 4/5 Action is + ... up: true ... ubc1: 5/5 Action is + ... up: false ... ubc1: 5/5 Action is - ... down: true ... ubc1: 4/5 Action is - ... down: true ... ubc1: 3/5 Action is - ... down: true ... ubc1: 2/5 Action is + ... up: true ... ubc1: 3/5 Action is + ... up: true ... ubc1: 4/5 Action is + ... up: true ... ubc1: 5/5 Creating ubc2 with a value one more than ubc1's value and a limit twice that of ubc1's limit Initially: ubc2: 6/10 Bumping ubc2 up to its limit After an up: ubc2: 7/10 After an up: ubc2: 8/10 After an up: ubc2: 9/10 After an up: ubc2: 10/10 ... and now down to ubc1's value After a down: ubc2: 9/10 After a down: ubc2: 8/10 After a down: ubc2: 7/10 After a down: ubc2: 6/10 After a down: ubc2: 5/10
read
method of the class
System.out
output for an interactive program.
System.out
or file output, click the 'More Information' link. The Lab page (on the main course page) has a description on how to
read the string comparison; once you get the basic idea it can be very helpful.
Color
)Color
, that supports RGB (red-green-blue) color values.
RGB color values consist of three integers values (for the red, green, and blue primary values) each in the range of 0 … 255
,
with 0
being an absence of the corresponding primary, and 255
being full saturation.
Being that 255
is FF
in hexadecimal, colors are often saintly representing as a six digit hex value, e.g. #000000 (for black — no color),
or #ff0000 (for red — full red, no green or black).
The class supports the following state and behavior:
toString
tht at prints the color in the format (r, g, b)
isValid
: accepts an RGB trio of values and returns whether it is a valid color, i.e., all values are in proper range
getHex
: accepts an RGB trio of values and returns its hex representation as a String
printf
you learned in 1115, with two differences:
%x
instead of the %d
used to represent them as decimal values. To get
00
rather than 0
(i.e., always use two digits) and to have the leading character be a 0
rather than
a blank, you write %02x
.
printf
send the output to a PrintStream
typically System.out
. We want the result to be in a
String
; to do this use the method String.format
instead; it returns a String
as its value
isGrey
: accepts the rgb trio and returns whether the color is a shade of grey (shades of grey are colors with all three components having
the same value.
isPrimary
: accepts the rgb trio and returns whether it is RED, GREEN, or BLUE (i.e., one of the primaries is 255, and the others are 0).
getName
: accepts the trio and returns the color if it's 'well-known', according to the following table:
Name | Red | Green | Blue |
---|---|---|---|
Black | 0 | 0 | 0 |
Blue | 0 | 0 | 255 |
Green | 0 | 255 | 0 |
Aqua | 0 | 255 | 255 |
Red | 255 | 0 | 0 |
Magenta | 255 | 0 | 255 |
Yellow | 255 | 255 | 0 |
White | 255 | 255 | 255 |
The method returns a diagnostic message if the color is not one of the above
Color
objects for Black, White, Red, Green, and Blue.
static
(and final
— use the proper naming convention of
all caps.
ColorApp
))r: 0 g: 0 b: 0 is valid: true hex: #000000 name: Black is grey: true is primary: false r: 0 g: 0 b: 255 is valid: true hex: #0000ff name: Blue is grey: false is primary: true r: 0 g: 255 b: 0 is valid: true hex: #00ff00 name: Green is grey: false is primary: true r: 0 g: 255 b: 255 is valid: true hex: #00ffff name: Aqua is grey: false is primary: false r: 255 g: 0 b: 0 is valid: true hex: #ff0000 name: Red is grey: false is primary: true r: 255 g: 0 b: 255 is valid: true hex: #ff00ff name: Magenta is grey: false is primary: false r: 255 g: 255 b: 0 is valid: true hex: #ffff00 name: Yellow is grey: false is primary: false r: 255 g: 255 b: 255 is valid: true hex: #ffffff name: White is grey: true is primary: false r: 10 g: 20 b: 30 is valid: true hex: #0a141e name: Unknown color (#0a141e) is grey: false is primary: false === Printing out the pre-defined Color objects BLACK: (0, 0, 0) WHITE: (255, 255, 255) RED: (255, 0, 0) GREEN: (0, 255, 0) BLUE: (0, 0, 255)
static
objects of the class within the class
color.data
0 0 0 0 0 255 0 255 0 0 255 255 255 0 0 255 0 255 255 255 0 255 255 255 10 20 30Sample Test Run
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
stdout
r: 0 g: 0 b: 0 is valid: true hex: #000000 name: Black is grey: true is primary: false r: 0 g: 0 b: 255 is valid: true hex: #0000ff name: Blue is grey: false is primary: true r: 0 g: 255 b: 0 is valid: true hex: #00ff00 name: Green is grey: false is primary: true r: 0 g: 255 b: 255 is valid: true hex: #00ffff name: Aqua is grey: false is primary: false r: 255 g: 0 b: 0 is valid: true hex: #ff0000 name: Red is grey: false is primary: true r: 255 g: 0 b: 255 is valid: true hex: #ff00ff name: Magenta is grey: false is primary: false r: 255 g: 255 b: 0 is valid: true hex: #ffff00 name: Yellow is grey: false is primary: false r: 255 g: 255 b: 255 is valid: true hex: #ffffff name: White is grey: true is primary: false r: 10 g: 20 b: 30 is valid: true hex: #0a141e name: Unknown color (#0a141e) is grey: false is primary: false
Point
class presented in class; i.e., several values representing a higher-level
concept (integers x and y representing a point; integers r, g, and b representing a color)
printf
and String.format
, you supposedly learned it in
1115, and even if you didn't, and even if we're not going to use them, you should have some familiarity with format strings
isPrimary
(does it seem familiar?)
Color
class in Java's GUI packages, and it has some interesting properties; once we move to real class code, we'll be familiar with the
basic semantics of color and that will make the discussion easier.