Computer Programming I - Class

COP 2210

Summer "B" Semester 2007

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.


Table of Contents


Got Java?

   Java 2 Software (Essential) - 51.33 MB

Also known as the Java 2 Development Kit (JDK), from Sun Microsystems (freeware)

  1. After you follow the link above, make sure you scroll down and click the Download button to the right of the heading "JDK 5.0 Update 12"
    • Make sure you get the JDK and not the JRE!
    • Do not click the "Get the JDK Download" link at the top of the page!

  2. Click the Windows Offline Installation, Multi-language link to begin the download (unless you are running Linux)

   Java Documentation (Strongly Recommended) - 44.05 MB

Also from Sun, the official Java 2 Language Documentation, in HTML format

Follow the link above, scroll down to the heading "J2SE 5.0 Documentation "and click the Download button

   Java Integrated Development Environments (Choose One!)

JCreator 3.50 - 2.19 MB - The Limited Edition (LE) is free

NetBeans 5.5.1 - 53.7 MB (freeware from Sun Microsystems)

Eclipse 3.2.2 - 121 MB (freeware)

Note: You must install the Java 2 software (above) before installing any of the IDE's

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
Top               Home


Textbook Resources - Big Java, Second Edition

    Source Code for Programs in the Textbook (Zip files)
[Ch. 1] [Ch. 2] [Ch. 3] [Ch. 4] [Ch. 5] [Ch. 6] [Ch. 7] [Ch. 8] [Ch. 16]

    Answers to Exercises (Word docs)
[Ch. 1] [Ch. 2] [Ch. 3] [Ch. 4] [Ch. 5] [Ch. 6] [Ch. 7] [Ch. 8] [Ch. 9] [Ch. 16]

    Bug List - Author's list of all known errors in the book

    Help with IDE's
How to use all major Java IDE's, including NetBeans, Eclipse, and JCreator

    Big Java - Link to the home page for the book
Top               Home


Unit 1 - Introduction

   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
   Data Representation
The ASCII and Unicode character sets

   Assignment #1
Creating, Compiling, Debugging, and Executing a Java Program
   ChangeMakerTester.java
"Test" class to be used with this assignment

   Intro to Strings and Java Output
String literals and concatenation, escape sequences, and PrintStream class methods print and println for output
Top               Home


Unit 2 - Using an Existing Class

   OOP Terminology and Concepts
Classes, objects, methods, instance variables, class interface vs. class implementation, and more
   Intro to Variables
Introduction to variables and data types. Includes rules for naming variables and how to declare variables with or without initial values
   The Assignment Statement
Syntax and semantics (meaning) of Java's assignment statement
   Objects, Classes, and Methods
Basic concepts and terms including classes, objects, methods, method parameters, methods that return a value vs. methods that do not, and how to call them
   Hello2.java
Declaring String object-variables, creating String objects, and assigning the objects to the object-variables (aka: making a variable "point to" an object)
   MethodCalls.java
Shows how to call methods that return a value - using String class methods length and replace - and those that do not - e.g., println. Also shows variable declarations and assignment statements
   More on the String Class
Declaring String object-variables, creating String objects, and String class methods indexOf and substring
   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
   Constructing Objects
Declaring object variables, creating objects using the new operator, objects vs. object references, the null reference, and throwing a NullPointerException
   FunWithRectangles.java
Everything you need to know to use an existing class! Shows variable declarations, assignment statements, creating objects, and calling accessor and mutator methods for those objects

   Assignment #2
Using an Existing Class: Creating and Manipulating Objects

   The Assignment Operator and Object Variables
Assigning one object variable to another
   AssignmentDemo.java
Shows what happens when you assign one object variable to another

   Assignment #3 - Using an Existing Class: Accessor and Mutator Methods
Balloon.java - Class to be used with this assignment
Balloon.html - HTML "help" pages for Balloon class

Top               Home


Unit 3 - Implementing ("Creating") Classes

   Implementing ("Defining") Methods
What we need to know to begin writing our own methods
   Class Constructors
A constructor is a special method that is called automatically whenever an object is created via the new operator
   Parameters and Arguments
When a method is called, the parameters in the method heading receive the values of the corresponding arguments in the method call. This is how information is passed into a method
   Instance Variables, Parameter Variables, and Local Variables
The scope, lifetime, and initial value of a variable depends on how the variable is used (i.e, where it is declared)
   BankAccountTest.java
The BankAccount class provides examples of instance variables, a class constructor, and accessor and mutator methods
   Method Overloading
Overloading class constructors allows creation of objects with different numbers of initial values specified
Balloon2.java (Class with overloaded constructors)
OverloadedConstructors.java (Test class for Balloon2 class)

   Assignment #4 - Creating a Class

Top               Home


Unit 4 - Fundamental Data Types, Reading User Input, and More

   Java's "Primitive" Types
The so-called "primitive" types are the exception to the "Everything in Java is an Object" rule, as they are implemented differently. 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
   Mixed-Type Expressions
When an arithmetic operation involves operands of two different types, the result is "promoted" to the higher type...
   Type Conversions (aka: Type "Casting")
"Narrowing" vs. "Widening" type conversions and implicit and explicit type conversions explained. Also, how to use an explicit type cast to avoid integer division
   Math Class Methods
A table summarizing some of the more useful methods of Java's Math class
   PythagorasTest.java
Shows how to call the static Math class methods, using sqrt and pow
   Using the Scanner Class for Interactive Input
Basics of interactive input and introduction to the new Java 1.5 Scanner class
   InputDemo.java
Demonstrates Scanner methods next(), nextInt(), and nextDouble()
   Defined Constants
Using defined (aka: "symbolic") constants helps make our programs easier to understand and modify
   Align.java
A class to format output. Right-align, left-align, or center-align data in a field of a specified width, with floating-point numbers optionally rounded to a specified number of decimal places or formatted as currency
   Align.html
HTML "help" pages for the Align class. These are generated by the javadoc utility program, using the Java "documentation comments" in the Align class
   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
   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
Top               Home


Unit 5 - Style and Documentation Standards for Java Programs

   Programming Style
Conventions for creating programs that are easy to read
   Java "Documentation Comments"
When a class contains special Java "documentation comments" the javadoc utility program can create a neat set of HTML "help" pages that explain the purpose of the class, what methods are available, and how to call each method. This is very helpful to programmers who need to use classes created by others
   Using the javadoc Utility Program
How to use the javadoc utility to automatically generate HTML "help" pages for your classes. These pages will have the same format as the official Java Language Documentation from Sun Microsystems
   Internal Documentation
Internal documentation consists of comments - included in your Java code - that explain what you are doing and how you are doing it. This is a necessity in the real world where programmers may be called upon to debug or modify code written by others
Top               Home


Unit 6 - Decision-Making (aka: "Selection" or "Conditional Execution")

   Relational Operators and Expressions
A relational expression compares two quantities for one of 6 possible relationships
   The if Statement
Syntax and execution of the single-alternative and two-alternative forms of the if statement
   PayMaster1.java
Demonstrates the single-alternative if statement
   PayMaster2.java
The two-alternative if statement
   "Nested" if Statements
An if statement may be "nested" inside the "if branch" or "else branch" of another if statement. In the "if" branch, this leads to more complex conditions. In the "else" branch, multiple alternatives
   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
   Testing Strings for Equality
To test Strings for equality we cannot use the "==" operator. Instead, we use the equals() method

   Assignment #5
Decision-Making
Top               Home


Unit 7 - Iteration (aka: "Repetition" or "Looping")

   The while Statement
Syntax and execution of Java's while loop. Loop control variables and loop "necessities"
   InvestmentTester.java
CH's Investment class uses a while loop to compute the number of years it takes for an investment to double in value. Also shows an accumulator
   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 is unchanged except for the prompts to the user. Shows that multiple data values may be placed in the input buffer at the same time, and that data remain in the buffer until extracted by a Scanner method
   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
   GaussTester.java
Gauss class uses a for statement to compute sum of integers from 1 to n, where n is entered by the user

   Assignment #6
Repetition and Decision-Making

   TemperatureConverter.java
Uses a for statement 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
   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
   Random Numbers
Java's Random class makes it easy to generate random numbers - floating-point numbers, integers, and integers within a specified range

   EOFTest.java
Nested while loops. The outer loop uses Scanner methods hasNext() and nextLine() to read lines of text until end-of-file. The inner loop uses hasNext() and next() to extract each "token" from each input line

Top               Home


Unit 8 - Designing Classes

   Object-Oriented Analysis and Design
Where object-oriented programs come from
   Some Class and Program Design Guidelines
Cohesion good, coupling bad! (Side effects bad, too!)
   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
   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

Top               Home


Unit 9 - ArrayLists

   Intro to ArrayLists
Array and ArrayList concepts and terminology, advantages of list processing, when to use an ArrayList, list items and list indices, basics of list processing, ArrayList methods, "overflowing the bounds" of a list, and Java 1.5's "generic" ArrayLists
   ArrayLists and Primitive Types
Explains how Java 1.5 makes it easy to use ArrayLists of primitives types via autoboxing and autounboxing
   ArrayListIntro.java
Shows the very basics of list processing: using a for statement to traverse a list, and ArrayList methods add(), size(), and get()

   ArrayLists of Objects - a Complete Example

[BankAccount.java] [Bank.java] [BankTester.java]

BankAccount objects have an account number and a balance that can be modified by deposits and withdrawals

The Bank class has an instance variable that is an ArrayList-of-BankAccount, which maintains a list of accounts (i.e., BankAccount objects), and methods that return the total on deposit in all accounts, the account number of the account with the largest balance, and the number of accounts that contain a minimum balance or more. There is also a search method, where the user enters an account number and the balance of the corresponding account is returned

The test class creates a Bank object and several BankAccount objects which are added to the list, and then calls the methods of the Bank class

   DataListTester.java - ArrayLists of Primitive Types
The DataList class creates a list of integers, and has methods to add an int to a list, return the average and maximum value, and print a table of the values and their deviations from the average

   Assignment #7
ArrayList Processing

Top               Home


Unit 10 - Data Files

   Working with Data Files in Java
Explains file types and access methods, throws clauses, using the Scanner class to read from input files, using the FileWriter and PrintWriter classes to write to output files, and closing a file
   FileIO.java
Demonstrates everything you need to know about working with data files
Top               Home