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.
Here is a sample set of data:
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))
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:
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)
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.
Quadrilateral) (Approval)
(x1, y1) (point 1) (x2, y2) (point 2)
______________________
\ /
\ /
\________________/
(x4, y4) (point 4) (x3, y3) (point 3)
(Note the order of the points; that is the order in which they are input)
The class should contain the following:
Point instance variables corresponding to the four points displayed above
liesInOneQuadrant, that returns true if the quadrilateral is completely contained within
a single quadrant (i.e., all four points are inthe sme quadrant).
isParallelogram: returns true of the quadrilateral is a parallelogram, i.e., opposite sides are equal
isRhombus: returns true if the quadrilateral is a rhombus, i.e., all four sides are equal
toString it should be the concatenation of the toString of the four Point instance variables
in the proper order.
I've supplied the Line and Point classes (both as source and class files; you might want to experiment with your IDE in how to add .class files
to your project.
Points
Lines
Point objects (we discussed this in class).
Points.
Point class … you have that from your instance variables
isRhombus amd isParallelogram methods require the length of the various sides, and length is a method of
the Line class. You have a couple of options to get that from your four Point instance variables:
side1 of type line, side2, etc (you can initialize them in the constructor
together with the four Point instance variables (not recommended, don't need the Line objects as instance variables; it's redundant.
Line objects) in the above two method (i.e., when and where you need them).
length method on the sides under being looked at.
isParallelogram could also be defined by leveraging Line's slope method but would either fail, or require
special logic for the case of vetical sides (which have undefined slope). (After you get your class working, you might want to try using slope instead; it may give you
a chance to become a bit familiar with Java's NaN (not-a-number) value.)
QuadrilateralApp) (Approval)quadrilateral.data
1 1 2 1 2 2 1 2 1 -1 1 1 -2 1 -2 -1 0 0 1 0 1 1 0 2Sample Test Run
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
stdout
(1, 1) - (2, 1) / (2, 2) - (1, 2) (1, 1) Quadrant: Quadrant 1 (1) (2, 1) Quadrant: Quadrant 1 (1) (2, 2) Quadrant: Quadrant 1 (1) (1, 2) Quadrant: Quadrant 1 (1) Quadrilateral lies in one quadrant: true Rhombus (1, -1) - (1, 1) / (-2, 1) - (-2, -1) (1, -1) Quadrant: Quadrant 4 (4) (1, 1) Quadrant: Quadrant 1 (1) (-2, 1) Quadrant: Quadrant 2 (2) (-2, -1) Quadrant: Quadrant 3 (3) Quadrilateral lies in one quadrant: false Parallelogram (0, 0) - (1, 0) / (1, 1) - (0, 2) (0, 0) Quadrant: Origin (0) (1, 0) Quadrant: Positive x axis (5) (1, 1) Quadrant: Quadrant 1 (1) (0, 2) Quadrant: Positive y axis (6) Quadrilateral lies in one quadrant: false Simple quadrilateral
Lines or Points
Phonebook) (Approval)Phonebook)last-name first-name phone-number
PhonebookApp) (Approval)PhonebookEntry class source file as well the Phonebook file).
last-name first-name phone-number
sorted.text
stdin
l Arnow David r 456-789-0123 l Weiss Jerrold l Weiss Gerald r 111-123-4567 q
phonebook.text
Arnow David 123-456-7890 Harrow Keith 234-567-8901 Jones Jackie 345-678-9012 Augenstein Moshe 456-789-0123 Sokol Dina 567-890-1234 Tenenbaum Aaron 678-901-2345 Weiss Gerald 789-012-3456 Cox Jim 890-123-4567 Langsam Yedidyah 901-234-5678 Thurm Joseph 012-345-6789Sample Test Run
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
stdout
lookup, reverse-lookup, quit (l/r/q)? last name? first name? David Arnow's phone number is 123-456-7890 lookup, reverse-lookup, quit (l/r/q)? phone number (nnn-nnn-nnnn)? 456-789-0123 belongs to Augenstein, Moshe lookup, reverse-lookup, quit (l/r/q)? last name? first name? -- Name not found lookup, reverse-lookup, quit (l/r/q)? last name? first name? Gerald Weiss's phone number is 789-012-3456 lookup, reverse-lookup, quit (l/r/q)? phone number (nnn-nnn-nnnn)? -- Phone number not found lookup, reverse-lookup, quit (l/r/q)? 3 lookups performed 2 reverse lookups performedsorted.text
Arnow David 123-456-7890 Augenstein Moshe 456-789-0123 Cox Jim 890-123-4567 Harrow Keith 234-567-8901 Jones Jackie 345-678-9012 Langsam Yedidyah 901-234-5678 Sokol Dina 567-890-1234 Tenenbaum Aaron 678-901-2345 Thurm Joseph 012-345-6789 Weiss Gerald 789-012-3456
Container) (Approval)Container class from Lecture 4:
String
contains: similar to find except this method returns true/false
instead of the position. You should assume the availability of find and NOT repeat the search logic.
(See the note in explanation below).
set: this method accepts an index and value, and assigns the value to the location specified by the index. For example:
set(values, size, 10, 14);assigns
14 to location 10 in the array.
set is the counterpart to get: the former returns the value at a location
in the container; the latter assigns (overwrites) an element in the container.
input file
A dog A cat F cat F cow F dog C cat C cow G 1 G 0 S 1 cow F cow F catSample Test Run
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
stdout
After adding dog: {dog}
After adding cat: {dog, cat}
cat is at position 1 of the container
cow is not in the container
dog is at position 0 of the container
cat is in the container
cow is not in the container
The value at location 1 is cat
The value at location 0 is dog
After setting position 1 to cow: {dog, cow}
cow is at position 1 of the container
cat is not in the container
contains using find (as opposed to
recoding the search logic, or don't even understand what you're being asked to do, go look at my Lecture 12
(Techniques III) notes -- look for (Linear) Searching an Array; it shows you what I want. (But please
give it some thought first.
ContainerApp))
CAPACITY)
toString
find to get
this() construct in constructors.
The integer classes implemented in this lab are for all practical purposes useless, the primitive int type provides the
same operations, and in a much more intuitive fashion. We introduce them for several reasons:
ImmutableInt) (Approval)Immutable, modelling integers, with the following state/behavior:
ImmutableInt add(ImmutableInt immutableInt)
ImmutableInt sub(ImmutableInt immutableInt)
ImmutableInt mul(ImmutableInt immutableInt)
ImmutableInt div(ImmutableInt immutableInt)
toString()
int instance variable maintaining the integer value of the object
this(…) construct)
div
…
ImmutableInt i1 = new ImmutableInt(10);
System.out.println("i1: " + i1);
…
System.out.println("i1.add(i2): " + i1.add(i2);
…
stdout
i1: 10 i2: 20 i3: 0 i1.add(i2): 30 i1.sub(i2): -10 i1.mul(i2): 200 i1.div(i2): 0 i1: 10 i2: 20 i3: 0
ImmutableIntApp) (Approval)
MutableInt) (Approval)MutableInt, modelling integers,with the following state/behavior:
MutableInt increaseBy(MutableInt immutableInt)
MutableInt decreaseBy(MutableInt immutableInt)
MutableInt multiplyBy(MutableInt immutableInt)
MutableInt divideBy(MutableInt immutableInt)
MutableInt reset()
toString()
int instance variable maintaining the integer value of the object
this(…) construct)
reset sets the internal state variable to 0
divideBy
…
MutableInt i0 = new MutableInt();
System.out.println("i0: " + i0);
…
System.out.println("i0.increaseBy(i1): " + i0.increaseBy(i1);
…
stdout
===== Initial Values ===== i0: 0 i1: 10 i2: 5 i3: 20 i4: 25 i0.increaseBy(i1): 10 i0: 10 i1: 10 i2: 5 i3: 20 i4: 25 i0.decreaseBy(i2): 5 i0: 5 i1: 10 i2: 5 i3: 20 i4: 25 i0.multiplyBy(i3): 100 i0: 100 i1: 10 i2: 5 i3: 20 i4: 25 i0.divideBy(i4): 4 i0: 4 i1: 10 i2: 5 i3: 20 i4: 25 i0.reset(): 0 i0: 0 ===== Chaining ===== i0.increaseBy(i1).decreaseBy(i2).multiplyBy(i3).divideBy(i4): 4 i0: 4 i1: 10 i2: 5 i3: 20 i4: 25
ImmutableIntApp) (Approval)