Computer Programming I - Class

COP 2210

Spring Semester 2003


Read this first!

NOTE:

Students must bring printed copies of all class handouts and sample programs to class.

Class handouts are in MS Word '97 format.
Sample programs are plain text .java files and can be downloaded and run.
(Programs have all been tested and contain no syntax or logic errors.)

Additional files will be posted throughout the semester.

This web page is intended to supplement the textbook Big Java by Cay Horstmann


Table of Contents


Got Java?

   Java 2 Software
From Sun Microsystems, the Java Software Developers Toolkit (SDK)
(Follow this link. Then, under the heading Download J2SE v 1.4.xxx click on the Download link in the SDK column (not the JRE column) of the Windows row
   Java Documentation
Also from Sun, official Java documentation in hypertext format
(Follow this link, then scroll down to the bottom of the page for the documentation)
   JCreator 2.50 - A nice Java IDE (Integrated Development Environment)
- The Limited Edition (LE) is free
- Note: Must previously have installed Java 2
Top               Home


Before Beginning (Class policies, etc)

   How to be Successful in This Class
A word to the wise...
   Class Policies
Official class rules regarding late assignments, makeup tests, partial credit, etc., and School of Computer Science policy on Incompletes and Academic Honesty
   Source Code
Download all the sample programs in the textbook
   Errata
Links to author's list of typographical errors in the textbook
Top               Home


Unit 1 - Introduction (Chapter 1)

   Brief History of Programming Languages
Machine Languages, Assembly Languages, High-Level Languages, and the role of the compiler
   Errors
Syntax Errors, Exceptions, and Logic Errors
   Hello.java
Traditional first program shows basic Java program layout with a public class definition and main method. Also shows an output statement with a string literal (i.e., "constant")
   Ground Rules for Java Programs
As shown in Hello.java, some basic rules for all Java programs
   Using the JCreator IDE
How to create, save, compile, and execute Java programs using JCreator, and how to print the program listing (i.e., the "source" code) and the program output. Also covers important lab security procedures
   Answers to Exercises in Chapter 1

   Assignment #1
Using the JCreator IDE
Top               Home


Unit 2 - Introduction to Objects and Classes (Chapter 2)

   OOP Terminology and Concepts
Classes, Objects, Methods, Instance Variables, Class Interface vs. Class Implementation, and more, explained
   MoveTest.java
Shows object variables, object references, object construction ("instantiation") and "sending messages" to objects (i.e., calling methods for objects). Also, string concatenation, the length() method of the String class, and some escape sequences
   GreeterTest.java
Shows a method that returns a String object. The Greeter class has a default constructor
   Class Constructors
A constructor is a special method that is called automatically whenever an object is instantiated (i.e., created)
   Parameters and Arguments
When a method is called, the parameters in the method heading automatically take on the values of the corresponding arguments in the method call. This is how information is passed into a method
   GreeterTest2.java
Second version of the Greeter class shows how a constructor uses its parameters to initialize the instance variables of the object being created
   All About Variables
What is a variable and what are the differences between instance variables, parameter variables ("parameters"), and local variables?
   More About Methods
There are two kinds of methods in the world - those that return a value and those that do not. Here's what you need to know about them
   Accessor and Mutator Methods
Also known as "get" and "set" methods, accessor methods return the value of an instance variable and mutator methods change the value of an instance variable
   Method Overloading
Overloading class constructors allows creation of objects with different numbers of initial values specified
   BankAccountTest.java
Shows overloaded constructors, and accessor and mutator methods (aka: "get" and "set" methods)

   Assignment #2
A class with accessor and mutator methods

   Answers to Exercises in Chapter 2
Top               Home


Unit 3 - Fundamental Data Types, Reading User Input, and More - Chapter 3

   Java's "Primitive" Types
The primitive types are implemented differently than objects. Although 6 different numeric types are available, there is generally no reason to use any other than int and double
   Java Arithmetic
The arithmetic operators and operator precedence. Also, integer division and modulus ("mod")
   PizzaCalculatorTest.java
Shows arithmetic expressions and assignment statements

   Assignment #3
Java Arithmetic

   PythagorasTest.java
Shows how to call the static methods of Java's Math class, using methods sqrt and pow
   Reading User Input
How to use method showInputDialog of the JOptionPane class to display a dialog box and return a string entered by the user. Also, using methods parseInt and parseDouble to convert a string to an int or double value
   InputTest.java
Demonstrates methods showInputDialog and parseInt
   Formatted Output
Java's NumberFormat class makes it easy to print floating-point numbers rounded to any number of decimal places, or formatted as currency
   Programming Style
Conventions for creating readable code
   Implicit and Explicit Method Parameters and this
Every non-static method is automatically passed one implicit parameter, which is the object for which the method was called. The reference to the implicit parameter is this
   BankAccountTest2.java
Demonstrates objects passed implicitly and explicitly, this, and calling one method of a class from another
   The Assignment Statement in Java
What Java's "=" operator means
   AssignmentDemo.java
Using "=" with objects

   Assignment #4
Input and Using the Math Class Methods

   Shadowing
If a method has a parameter variable or local variable with the same name as an instance variable, then the instance variable is "shadowed" or "hidden" by the parameter or local variable and cannot be accessed in that method. We can use this to guard against accidental shadowing
   Answers to Exercises in Chapter 3
Top               Home


Unit 4 - Decision-Making - Chapter 5

   The if Statement
Syntax and execution of the single-alternative, double-alternative, and multiple-alternative forms of the if statement
   PayMaster1.java
Demonstrates the single-alternative if statement
   PayMaster2.java
The two-alternative if statement
   Boolean Operators and Expressions
Boolean operators !, &&, and ||, operator precedence, truth tables, and DeMorgan's Laws
   LeapYearTester.java
Demonstrates all things boolean: operators, variables, assignment statements, and a boolean method
   LeapYearTester2.java
Shows a method with multiple return statements

   Study Guide for the Midterm Exam

   Assignment #5
Decision-Making and boolean Type

   Answers to Exercises in Chapter 5 (Decision-Making)
Top               Home


Unit 5 - Iteration (i.e., Repetition) - Chapter 6

   The while Statement
Syntax and execution of Java's while loop. Loop control variables and loop "necessities"
   GaussTester.java
Gauss class uses a while loop to compute sum of integers from 1 to n, where n is entered by the user. Demonstrates counters and accumulators
   DataSetTester.java
DataSet class computes average and maximum of a set of data values. Driver class DataSetTester uses a while loop to read and process data until end-of-file
   DataSetTester2.java
Version 2 of the DataSetTester class shows how an input statement can be part of the loop condition! This is commonly done in loops that read and process a set of data
   Some Comments on Comments
Documentation standards for our programs, and how to use Java's documentation comments
   Using the javadoc Utility Program
How to use the javadoc utility to automatically generate HTML "help" pages your classes. These pages will have the same format as the official Java Language Documentation from Sun Microsystems
   The for Statement
Java's for loop is a count-controlled while loop, used when you can determine in advance the exact number of iterations to be done
   TemperatureConverter.java
Uses a for loop to print a table of Fahrenheit temperatures and their Celsius equivalents
   The do-while Statement
The "do-while" loop is a post-test loop. Also, when to use each type of loop

   Assignment #6
Repetition (and static "class" variables and methods)

   PrimeMinister.java
Finds and prints the first N prime numbers, where N is entered by the user. Demonstrates the conditional loops (while and do-while). Also, private "utility" methods and String class method length
   TableTester.java
Uses nested for statements to print multiplication tables
   Answers to Exercises in Chapter 6 (Repetition)

Top               Home


Unit 6 - Intro to Java's Arrays Class (13.4 - 13.6)

   Intro to Arrays
Basic concepts: data structures, arrays, array elements and array subscripts. Using a loop to "traverse" an array, declaring array object variables and creating array objects, the length instance variable, and overflowing the bounds of an array
   ElementaryListTester.java
The ElementaryList class has an instance variable that is an array object variable, and a constructor that creates an array object of the size (i.e., number of elements) specified by the user. Shows how to use a for statement to "traverse" an array (i.e., to "visit" each element)
   Partially Filled Arrays
We don't always know in advance how many values will be stored in an array. In that case, the array may be only partially filled, and we must use a counter to keep track of the number of elements used
   DataListTester.java
The DataList class implements a simple list of integers, using an array. It has methods to add an int to a list, to return the average, to return the maximum, and to print a table of the values and their deviations from the average. Shows how to use a counter to store data in an array when the number of data values is not known in advance
   Align.java
A class to right-align, left-align, or center-align data in a field of a specified width, making it easy to display output in table form. The DataList class "depends on" the Align class, so to use the DataList class you must store a copy of the Align class in the same folder
   The StringTokenizer Class
How to break up a long string (such as a line of input) into individual, whitespace-delimited "tokens" that may be processed separately
   TokenizerDemo.java
Reads any number of sentences entered by the user, tokenizes each one, and prints each token

   Assignment #7 - Arrays

   PurchaseListTester.java
A Purchase object has the number of items bought, item description, and cost of the item. The PurchaseList class uses an array to maintain a list of Purchase objects.
   SearchAndSortTester.java
Demonstrates the linear search and selection sort algorithms. Also, random numbers
   The Binary Search
The binary search algorithm is much more efficient than the linear search, but can only be done on a sorted array

   Assignment #8 - Arrays of Objects
universe.txt - Data file to be used with this assignment
Stuff.java - Class to be used with this assignment

   Answers to Array Exercises in Chapter 13

Top               Home


Unit 7 - Data Files (15.1 - 15.3)

   Working with Data Files in Java
Everything you need to know about working with text files, including file types and access methods, throws clauses, FileReader, BufferedReader, FileWriter, and PrintWriter classes, methods read and readLine, methods print and println, and method close
   FileIO.java
Demonstrates everything you need to know about working with data files

Top               Home


Unit 8 - Designing Classes - Chapter 7

   ParamPasser.java
In Java, all method parameters are passed "by value". This program shows this to be true for primitive-type parameters
   ParamPasser2.java
This program shows that object variables (which store object references ) are also passed by value. This means that although you can't make an object parameter point to a different object, you can modify the object to which it points
   Static "Class" Variables and Static Methods
What they are, why we need them, how they are used
   StaticDemo.java
Demonstrates everything you need to know about static variables and methods
   Answers to Exercises in Chapter 7 (Designing Classes)

Top               Home


Unit 9 - The String Class, Revisited

   Major String Class Methods
String class methods length, indexOf, substring, toUpperCase, and toLowerCase, and a review of String concatenation
   StringMethodDemo.java
Shows how to use all the String methods listed above

Top               Home