CMPU 102 - Lab
10 - April 24th, 2017
In this lab, you will
- practice using command-line arguments in a program,
- practice writing while and for loops, including the new enhanced
for loop, and
- modify a program to include a JButton from javax.swing to
exit.
Part 1:
Download RevPolish.java file
- Log in to your Vassar CS account by typing your username and
password
at the login window.
- In the cs102 directory on your Desktop (or in a place of your own
choosing), make a new directory for lab 2 by entering:
mkdir lab10
- Change to the directory you made in the last step by entering:
cd lab10
-
Download the file RevPolish.java into
your cs102/lab10 directory.
This program is intended to be the basis for a very
simple "reverse Polish"
calculator (so-called because the operands are entered before the
operator). Reverse Polish notation (or just RPN) by analogy with the
related Polish notation, a prefix notation introduced in 1920 by the
Polish mathematician Jan Lukasiewicz, is a mathematical notation
wherein every operator follows all of its operands. It is also known as
Postfix notation and is parenthesis-free.
- Compile the RevPolish.java file in the Console window by typing
javac
RevPolish.java at the command line (or by pressing the
Compile button inside DrJava).
The file should compile with no errors.
Run the file by typing java
RevPolish 3 4 at the command
line or in the DrJava interactions pane. The file should output the
line 3 + 4 = 7 to
the standard output (the screen).
Create a reverse Polish calculator
Augment the RevPolish.java file
so that it takes a third argument from the
command line. This argument (args[2]) will be one of the following
arithmetic operators:
+ - * / %
When finished, your program should read three arguments from the
command
line, determine the correct operation to perform on the two
integers, perform the operation, and produce the result. An example
session from running the program would appear as follows (the ">" is
the prompt from either the command line or the DrJava interactions pane
and the bolded text is what the user types to run the program):
NOTE: If you are entering the command line arguments at the
Linux operating system prompt, you will need to type the * inside
quotation marks, "*". If you don't include the quotation marks,
the operating system tries to interpret the * as a wildcard.
> java RevPolish 3 67 +
3 + 67 = 70
> java RevPolish 8 2 *
8 * 2 = 4
> java RevPolish 23 3 %
23 % 3 = 2
Part 2:
Download the file LoopDeeLoop.java,
that contains starter code for the following:
- A constructor with no arguments:
public
LoopDeeLoop()
- A private method for each exercise listed below,
exercise1...exercise4.
exercise1 through exercise4 should call private helper methods.
- A private method with no arguments:
private
String dashLine()
// Constructs and returns a String composed of 50 to 80 dashes
Fill in the method bodies to perform each of the following functions
using both while and for
loops:
- Print all integers from 0...100 that are both evenly divisible
by 3 and 5.
- Print the first 10 powers of 2 (be sure these are printed as
integers.)
- Print the sequence of integers, beginning at 100, counting
downward by 5 ([100, 95, 90,85, ...]), and ending at or above 0.
- Create an array of integers from 1 to 50 and use the enhanced for loop-to
print its elements.
- Call the dashLine()
method before and after each of exercises 1 through 4. The results from running this file are shown below.
Exercise 1: Print all the integers from 0 to 100 that are evenly divisible by 3 and 5:
Using a while loop:
0 3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51
54 55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100
Using a for loop:
0 3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51
54 55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100
--------------------------------------------------------------------------------
Exercise 2: Print the powers of 2 from 2^0 to 2^10:
Using a while loop:
1 2 4 8 16 32 64 128 256 512 1024
Using a for loop:
1 2 4 8 16 32 64 128 256 512 1024
--------------------------------------------------------------------------------
Exercise 3: Print the sequence of integers, beginning at 100, counting downward by 5, and ending at or above 0:
Using a while loop:
100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0
Using a for loop:
100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0
--------------------------------------------------------------------------------
Exercise 4: Create an array containing the sequence of integers
from 1 to 50 and print the array using an enhanced for-loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Part 3:
Add a JButton to
the LoopDeLoop program to exit the window. The steps to do this
are given below:
- Import the packages javax.swing, java.awt.event, and acm.graphics.
- Declare a private JButton instance variable.
- Add a public void method called run that takes no parameters and
contains the code to
- instantiate the JButton object with label "Exit"
- add the JButton to the SOUTH border of the window
- add action listeners
- Add an actionPerformed method with definition public void
actionPerformed(ActionEvent e) that contains the single statement
System.exit(0);
This line causes the system to exit the window.
Following the steps above should result in a bar at the bottom of the
window that has a JButton to quit the program.
After the Lab
Don't forget to submit your lab and log out.