Fund. CS II (CS152 2004F)

Class 14: Laboratory: Java Basics, Revisited

Back to Loops. On to Laboratory: Java Basics, Continued.

Held: Monday, 20 September 2004

Summary: Today we check our understanding of Java basics by writing some simple Java programs.

Due

Assignments

Notes:

Preparation

Exercise 1: Commenting on Colors

a. Create a main class called Critic in package youruserid.lab01a. The Critic class should contain only a main method. That method should prompt the user for his or her favorite color and make a comment on that color.

b. Update Critic so that it makes a different comment for the color blue than for other colors.

Exercise 2: A Talent Show

a. Create three model classes, Roger, Pauline, and Chris. Each should provide a method, comment(PrintWriter pen, String phrase), that provides a comment on the particular string. (E.g., "Hmmm ... phrase fascinates me.")

b. Create a main class, Panel, with a main method that builds an object in each of the three classes you've just created, prompts the user for a favorite phrase, and asks each of the objects to comment on that phrase.

Additional Preparation

Prepare a directory structure for a package named lab01.userid.

Exercise 3: Counting

a. Write a main class, UpCounter, in package lab01.userid. The main method should prompt for an integer and print out the integers between 1 and that integer, inclusive, in increasing order. For example,

Please enter an integer: 5
1 2 3 4 5

b. Write a main class, DownCounter, in the same package. The main method should prompt for an integer and print the integers between that integer and 1, inclusive, in decreasing order. For example,

Please enter an integer: 3
3 2 1

c. Write a main class, UpDownCounter, in the same package. The main method should prompt for an integer, and count from 1 up to that integer and then back down to 1. For example,

Please enter an integer: 4
1 2 3 4 3 2 1

d. Write a main class, EvenCounter, in the same package. The main method should prompt for an integer, and print the positive odd numbers less than that integer. For example,

Please enter an integer: 12
1 3 5 7 9 11

e. Write a main class, DoubleCounter, in the same package. The main method should prompt for an integer, and count up to every number between 1 and the integer entered. For example,

Please enter an integer: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Back to Loops. On to Laboratory: Java Basics, Continued.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Dec 8 10:37:11 2004.
The source to the document was last modified on Thu Aug 26 20:22:22 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2004F/Outlines/outline.14.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu