Sample Programs for Class 1
There are three (and 1/2) sample programs related to the material discussed
in the first class. Click here to
download the zip file containing these programs. Read on for more
information about the programs. (You should try them in the order
given below.)
Sample1.java
The Sample1.java program simply prints out a listing of the
parameters supplied on the command line when it is invoked. To
use it:
Compile (javac Sample1.java)
Execute with no parameters - java Sample1
Execute with parameters - java Sample1 first-parameter second-parameter
View Source
Rectangle1.java
The Rectangle1.java program defines a class (Rectangle1) which
contains methods for returning area, perimeter, whether or not the
created object is a square, and a method to print out descriptive
information. There is also a main entry point which creates two
rectangles and exercises the methods. To use it:
Compile - javac Rectangle1.java
Execute - java Rectangle1
View Source
Polygon.java
The Polygon.java program defines an interface listing several
methods that must be implemented by subclasses using this
interface. To use it:
Compile - javac Polygon.java
(It is not executable, see next program)
View Source
Rectp1.java
The Rectp1.java program is an incomplete implementation of the
Polygon interface. When it is compiled, you should receive an
error message indicating that it does not define one of the
required methods. To use it:
Compile - javac Rectp1.java (you should get an error)
Edit -edit the source file and add the missing method. You
should also add a "main" method that contains creation of a
Rectp1 rectangle and prints out descriptive information.
Recompile and execute
If you're more ambitious you could create another class, say
for a triangle which also implements the Polygon interface.
View Source