CS 101 (Spring 1999)
Lab 1:Expressions, Functions, and Procedural Abstraction
(Mondays 2 PM)
(Fridays 2 PM)
| || 15 ||Jan
|| 22 ||Jan
By the end of this lab, you should...
be able to write Java expressions for mathematical formulas.
understand the use of predicates, logical operators, and conditional execution.
understand procedural abstraction, formal parameters, actual parameters,
and return values.
be able to define procedures that describe simple computations.
understand naming abstraction, and the difference between a name and a
Read over the entire lab before you start. Before beginning the assignment, download
the provided portions of the lab into your cs101 folder by clicking on
the "download project" button in the on-line version of this assignment.
project folder for PC]]]
project files for UNIX]]]
In this lab, you will implement the "brains" of
a calculator. You have been supplied with a Calculator class that,
when instantiated, displays a calculator window on the screen, complete
with buttons and display. However, if you run the program, you will
notice that the calculator is "stupid." It doesn't know how to perform
any of the operations. Your job is to create a SmartCalculator that
extends the Calculator by defining methods that perform the appropriate
operations. After you define the various methods, you should test
them by running the program. Enter values into the calculator and
check the results.
Begin by opening the Lab1.prj project file in Symantec Cafe.
Try executing it. Notice that the buttons can be clicked, but
calculator is "stupid," as described above.
Now open the file SmartCalculator.java within the Symantec Cafe
Supply the necessary header information (name, lab section, etc.).
This is the only file you need to modify for this lab.
In this file, you'll
extend the calculator by providing the functionality to make it "smart"
as described below.
Each of the following specifications describes the input parameters
and return values of a desired procedure that is needed as a method of
the calculator. For each specification,
Hint: To avoid making the same mistakes over and over, test as you
go. That is, type in a procedure, compile, execute, and verify the results,
and then go on to the next problem. After you get the hang of it,
you can write more code before you test. Java's built-in class Math
may be helpful for some of these exercises.
write a Java procedure that correctly implements that specification,
type your procedure into the file SmartCalculator.java,
test that the procedure makes the corresponding button on the calculator
work correctly. In addition to "normal" cases, test "unusual" cases,
such as zero or negative argument values, where appropriate.
Unary operators on numbers. Each of these are operators that
take a single number (in this case, a double) as a parameter,
and return a double as a result. The first one has already
been done for you in the provided file.
Write a method called negate that returns the negated value of
its input parameter.
Write a method called diagonal that takes as its input parameter
the length of a side of a square, and returns the length of the diagonal
of the square.
Write a method called circumference that takes as its input parameter
the length of the radius of a circle, and returns the length of the circumference
of the circle.
Write a method called area that takes as its input parameter the
length of the radius of a circle, and returns the area of the circle.
Binary operators on numbers. Each of these are operators that
take in two numbers (both of type double) as parameters, and return
a double as a result.
Binary operators on booleans. Each of these are operators
that take in two numbers (both of type boolean) as parameters,
and return a boolean as a result.
Write a method called plus that returns the sum of its two input
Write a method called times that returns the product of its two
Write a method called minus that returns the difference of its
two input parameters (the first minus the second).
Write a method called divide that returns the quotient of its
two input parameters (the first divided by the second).
Write a method called and that returns true iff both input parameters
Write a method called or that returns true iff at least one of
the input parameters is true.
Write a method called nand as defined by column 3 in the following
Write a method called xor as defined by column 4 in the following
||x NAND y
||x XOR y
Memory operations. Now it's time to implement the methods
corresponding to the top three buttons in the orange column of the calculator.
These buttons add a value into the calculator's memory (M+), recall the
value from the memory (MR), and exchange the currently displayed value
with the one in the calculator's memory (MXCH). In order to keep
track of the current value of the memory, you should first declare an
instance variable of type double at the top of your SmartCalculator
class definition. The declaration should appear as the first
line following the open brace, before any method definitions. You
can choose any appropriate name for the variable. Once that is done,
you can define the various methods that access and modify the calculator's
memory as follows. In each of these procedures, when we say "the
calculator's memory," we are referring to the instance variable you have
defined as part of the SmartCalculator object.
Be sure to test all of your methods thoroughly by entering values into
the calculator and checking the answers. For each of the boolean
operations, be sure to test all four possible input combinations.
Also, remember to try zero values and negative values when testing numerical
Define a method called memoryPlus that takes a double as
its parameter. The method will return no value at all (return type
void), but instead will add the given parameter's value to the
current value of the calculator's memory, and then save the sum in the
Define a method called memoryRecall that takes no parameters and
returns the value in the calculator's memory.
Define a method called memoryExchange that takes a double
as its parameter. The method will save the given parameter's
value into the value of the calculator's memory, and then return as its
result the old value in the calculator's memory. In order
to perform this swap, you will need to declare a local variable inside
the method that can temporarily save the old value so that the value will
be available when you're ready to return from the method. Ask if
you have questions. It can be a little tricky until you see it.
What To Turn In:
For every CS101 lab you turn in, you should fill in
sheet and staple it on the front of your lab. You can either print
out the cover sheet (on a single page, please) or just pick one up from
the stack on top of the CS101 mailbox. Attach a paper printout of the SmartCalculator.java
file containing the methods you wrote, and place it in the CS101 mailbox
by 2:00pm on the due date. Check that you have filled in the
header information (name, email, date, and lab section) at the top of the
file. If you need help printing, ask a TA or refer to the FAQ
Last modified 14:31:03 CST 21 January 1999
by Ron K. Cytron