CS 204: Software Design
Getting started in Java
Due: 9:50AM Wednesday, October 20.
Some Getting-Started Programs
Once you have learned a programming language or two, getting started
in a new language is mostly a matter of finding good reference materials,
getting a source of sample programs, and writing a bunch of small programs
to get the syntax and core libraries under control.
When I want to learn a new programming language, I start by
writing a few small programs to make sure I can handle the basics.
Here's a collection of programs that make a nice introduction to most
programming languages.
- (Output.) Write the "hello, world" program to print a
simple text message to standard output.
- (Input.) Write a program that asks the user for his/her name,
age in years, and hourly wage as a decimal number, and prints
the information back.
- (A function.) Write a recursive factorial function as
an example of a function that takes an integer parameter and returns
an integer. Write a short main program to test the function.
- (Arithmetic and conditionals.) Write a change-making program.
Here's a description
of such a program as assigned to a recent CS 111 class.
- (Input/output parameters.) Write a function that takes two
parameters and swaps them. Whether this is possible depends on
whether your programming language supports pass-by-reference in
some form. (This is not possible in Java. Learning why is
an important part of learning to think in Java.)
- (File input, loops, and command-line arguments.) Count the lines in
a text file whose name is specified as a command-line argument.
- (Lists/arrays.) Given a text file consisting of one name on each line,
read in the list of names, sort them into alphabetical order, and
print the sorted list. Let the user specify the text file as a command-line
argument.
- (String manipulation and searching.) Count the number of times
each word in a text file appears. Print the results sorted in decreasing
order by word count. Let the user specify the text file as a command-line
argument. (If your language has a built-in hash table, like Python's dictionaries
or Perl's hashes, use that. Otherwise, a linear search ought to do the job,
though more slowly.)
- (Classes.) Create a class called Circle with instance variables to
keep track of the center and radius of a circle. The class should have a
suitable constructor (or constructors), plus methods getArea, getCircumference,
and a collection of appropriate accessors. Your program should read a list of circles
from a text file whose lines consist of three numbers separated by spaces
(e.g. "3.2 4 2.7" represents the circle of radius 2.7 centered at (3.2, 4)),
instantiating Circle objects for each one. Once you have a list of Circle
objects, run through the list reporting the center, radius, area, and
circumference of each circle. Let the user specify the file of circle data
as a command-line argument.
- (Pointers, references, and memory allocation.) Read a list of integers from a file
into a linked list of your own construction. Sort the linked list
(insertion sort and merge sort both work well with linked lists) and
print the sorted list. Let the user specify the file as a command-line
argument.
After getting these basics under control, I start exploring the language's
standard libraries. I will always want to know more about string manipulation,
the file system (create/delete/move files, traverse a directory tree, etc.),
simple GUIs and line graphics, invoking other programs, networking, etc. But
this is a longer-term project. If I have a good pet project to work on,
most of these libraries come up naturally.
Your assignment
Try writing the programs above using Java. Hand in the
factorial function and the word-counter.
Don't forget that good style and organization are valuable in any
program. Many of the smaller programs on the list above
can be written entirely in the main
program, but even so, they would benefit from a well-chosen function
or two.
Tutorials and reference materials
Sample programs
We'll develop a few examples in class, but the Internet is full of samples
as well. Searching for "java examples", "java samples", "java example programs", etc.
leads you to lots of stuff. Often, these pages are intertwined with a particular
development system (e.g. Eclipse) or are filled with pretty
bad code. Still, you can learn a lot about the language by trying some of the
samples you find on-line.
If there are particular samples you would like me to write, let me know.