Computer Programming I - Class

COP 2210

Spring Semester 2020

Read this first!

Online notes are in MS Word '97 format.
Sample programs are .java files.
All programs have been tested and contain no syntax or logic errors.

Additional files - including all assignments - will be posted throughout the semester.



Table of Contents


Got Java?

   Java Software (Essential) - Also known as The Java SE Development Kit or JDK

  1. Click the link above, then scroll down to "Java SE 8u231" and click the [DOWNLOAD] button under JDK
  2. Accept the licensing agreement
  3. Choose the version for your operating system. Note that there are two links for Windows - one for older 32-bit Windows (Windows x86) and another for 64-bit (Windows x64). The 32-bit version will run on all Windows. To see what version of Windows you have, click the Start button, then Control Panel > System and Security > System
  4. After downloading, double-click on the file icon to install

Note: You must install Java before installing NetBeans

   NetBeans IDE 8.2 (Essential) - Do NOT download any version other than 8.2!

Click the link above, choose your preferred language and platform, and then click the Download button under the "Java SE" column

   Java Language Documentation, in HTML Format (Strongly Recommended)

Click the link above, scroll down to "Java SE 8 Documentation," and then click the [DOWNLOAD] button to the right

Click here to view the documentation online


Textbook Resources - Big Java, 6th Edition - Early Objects

Recommended: The "e-book," (download + online version of the book) which provides valuable "hands-on" resources with instant feedback,
including short programming exercises and practice quizzes

Also available in loose-leaf and bound paperback formats. All formats available in FIU Bookstore or from Wiley

    Big Java 6 Home Page
  • Student Companion Site and access instructions
  • Source code for all sample programs (in downloadable .zip format)
  • Bug List - all known errors in the book
  • Tech support and help

    Answers to Review Exercises - 6th Edition
All Chapters

    Answers to Review Exercises - 5th Edition
[Ch. 1] [Ch. 2] [Ch. 3] [Ch. 4] [Ch. 5] [Ch. 6] [Ch. 7] [Ch. 8] [Ch. 9]
[Ch. 10] [Ch. 11] [Ch. 12] [Ch. 13] [Ch. 14] [Ch. 15] [Ch. 16] [Ch. 17]

Top               Home


Unit 0 - Before Beginning

   Class Rules
Class policy regarding late assignments, makeup tests, partial credit, incompletes, and academic honesty
   How to be Successful in This Class
A word to the wise...
   Submitting Your Assignments
Follow these directions to make sure you receive credit for your assignments!
   Appealing Assignment Grades
FIU policy on what to do if you feel an assignment was graded erroneously
   How to Create a "Zip" File
Top               Home


Unit 1 - Introduction

   Powerpoint - Chapter 1
   Brief History of Programming Languages
Machine Languages, Assembly Languages, High-Level Languages, compilers and interpreters
   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 NetBeans IDE
An Integrated Development Environment is a program we use to create, edit, compile, execute, and save our Java programs

   Assignment #1 - Creating, Compiling, and Executing a Java Program
ChangeMakerTester.java - A "test" class for the ChangeMaker class. Creates and manipulates a ChangeMaker object

   Data Representation
The ASCII and Unicode character sets
   Intro to Strings and Java Output
String literals and concatenation, methods print and println, and escape sequences
Top               Home


Unit 2 - Using an Existing Class

   Powerpoint - Chapter 2
   OOP Terminology and Concepts
Classes, objects, methods, instance variables, class interface vs. class implementation, and more
   Introduction to Variables and Data Types
Intro to primitive numeric types int and double, and Java class String, rules for naming variables, and declaring 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 arguments, methods that return a value vs. methods that do not, and how to call them
   MethodCalls.java
Shows how to call methods that return a value - e.g., String class method length() - and those that do not - e.g., println(). Also shows variable declarations, assignment statements, and concatenation
   Major String Class Methods
Substring concepts and the indexOf, substring, length, toUppercase, toLowerCase, and charAt methods. Also, Java exceptions and the StringIndexOutOfBounds exception, and "overloaded" methods
   StringMethodsDemo.java
The indexOf and substring methods in fast and furious action!
   Accessor and Mutator Methods
AKA: "get" and "set" methods. An accessor method returns the value of one of the instance variables of the object for which it is called. A mutator methods changes the value of one or more instance variables
   Creating Objects
Declaring object variables, creating objects using the new operator, objects vs. object references, the null reference, and throwing a NullPointerException
   FunWithRectangles.java
Shows everything you need to know to use an existing class - creating objects, calling accessor and mutator methods for those objects, variable declarations, and assignment statements

   Assignment #2 - Using an Existing Class: Creating and Manipulating Objects
Balloon.java - Class to be used with this assignment
Balloon.html - Html "help" pages for the Balloon class

   The Assignment Operator and Object Variables
When we assign one object variable to another we do NOT wind up with two identical objects. Instead, we get two object variables both pointing to the same object
   AssignmentDemo.java
Proves that the above statement is true
Top               Home


Unit 3 - Implementing ("Creating") Classes

   Powerpoint - Chapter 3
   Implementing ("Defining") Methods
What we need to know to begin writing our own methods
   Class Constructors
A constructor is a special method that we call to create an object and initialize its instance variables. Unlike all other methods, a constructor is called when the new operator is executed
   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)
   BankAccount.java
The BankAccount class provides examples of everything we need to know to create a class - instance variable declarations, a class constructor, accessor and mutator methods, and parameter variables and local variables
BankAccountTest.java - test class for the BankAccount class

   Assignment #3 - Creating a Class

   Algorithms, Pseudocode, and Stepwise Refinement
Where methods come from
   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 can not be accessed in that method. We must use this to guard against shadowing
   Method Overloading
Overloading class constructors enables us to create objects with varying numbers of "construction parameters" passed
BankAccount2.java (Same BankAccount class, but with overloaded constructors)
OverloadedConstructors.java (Test class for BankAccount2 class)

Top               Home


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

   Powerpoint - Chapter 4
   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, operator precedence, and 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 showInputDialog method of the JOptionPane Class
How to create a dialog boxes for interactive input
   InputDemo.java
Demonstrates interactive input using JOptionPane, and the parseInt(), and parseDouble() methods

   Assignment #4 - Integer Arithmetic, Arithmetic Expressions, and Reading User Input

   Defined Constants
Using defined (aka: "symbolic") constants helps make our programs easier to understand and modify
   Formatted Output - the printf Method
How to use printf to control the width of the output field, number of decimal places shown, right or left justification, etc
   Implicit and Explicit Method Parameters and this
"Explicit" parameters are those that appear in the parameter list in the method declaration. In addition, every non-static method is automatically passed one "implicit" parameter, which is a pointer to the object for which the method was called. It's name is this
   BankAccountTest2.java
Demonstrates objects passed implicitly and explicitly, this, and calling a method of a class from another method of the same class
   The "Shortcut (or, "Arithmetic") Assignment Operators
By combining an arithmetic operator ( * , / , % , + , - ) with an assignment, these operators save you a few keystrokes
   The Autoincrement and Autodecrement Operators
Because incrementing and decrementing counters are such common operations, Java has a set of operators that do just that (and save you a few keystrokes)
Top               Home


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

   Powerpoint - Chapter 5 (Decisions)
   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 and "Cascaded" ifs
A "nested if" is an if statement within an if statement. In the "if" branch, this creates more complex conditions; in the "else" branch, multiple alternatives. A better way to code multiple alternatives is via "cascaded ifs"
   Testing Strings for Equality
To test Strings for equality we cannot use the "==" operator. Instead, we call the equals() and equalsIgnoreCase() methods

   Assignment #5 - Intro to Decision-Making

   Boolean Operators and Expressions
Boolean operators !, &&, and ||, operator precedence, truth tables, and DeMorgan's Laws
   Primitive Type boolean
Boolean literals, variables, assignments, and methods
   LeapYearTester2.java
Demonstrates all things boolean: operators, expressions, variables, assignment statements, and a boolean method

   Assignment #6
Advanced Decision-Making: Boolean Operators, Expressions, and Methods

   Testing Floating-Point Numbers for Equality
Because floating-point arithmetic is only approximate, testing floating-point numbers for equality is not dependable. Instead, we test whether the difference between two numbers is "close enough" to zero that we can consider them to be "equal"
   More on Decision-Making
"Shortcut" (or, "lazy") evaluation of boolean expressions, tesing programs that make decisions, impossible conditions and unavoidable conditions, and the "dangling else" problem (and how to avoid it)
   The switch Statement (Optional)
When testing an int, String, or char expression for equality to any of a list of constants, the switch statement requires a bit less coding than cascaded "if" statements
   SwitchDemo.java
Shows two switch statements - one with break statements and one with the default "fall through" behavior
Top               Home


Unit 6 - 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 HTML "help" pages that contain everything a programmer needs to know in order to use it: the purpose of the class, how to create objects, what methods are available, and how to call them
   Using the javadoc Utility in NetBeans
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 API Language Documentation
   Packages
A package is a folder for storing related classes. In NetBeans, the "default" package is the src folder. Here is how to create and use your own packages and how to get NetBeans to do it all for you!
   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 7 - Iteration (aka: "Repetition" or "Looping")

   Powerpoint - Chapter 6 (Looping)
   The while Statement
Syntax and execution of Java's while loop. Loop control variables and loop "necessities"
   DataSet.java | DataSetTester.java
The DataSet class computes the average and maximum of a set of data values, using a counter and an accumulator. The DataSetTester class uses a while loop to read and process any number of data values until end-of-file
   InvestmentTester.java
CH's Investment class demonstrates a while loop and a for loop
   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 the user enters n
   TemperatureConverter.java
Uses a for statement to print a table of Fahrenheit temperatures and their Celsius equivalents

   Assignment #7 - Repetition and Decision-Making
Date.java | SpeedDating.java | Date.html

   The do-while Statement
The "do-while" loop is a post-test loop, meaning the loop body is always done at least one time (unlike the for and while loops). Also, when to use each type of loop
   PasswordTester.java
The Password class creates a password and gives the user 3 attempts to match it. Shows a do-while loop with a compound condition and loop postconditions
   Nested Loops
The inner loop is fully executed - from the initialization of the loop control variable until the boolean expression becomes false - on each iteration of the outer
   TableTester.java
Uses nested for statements to print multiplication tables
   Intro to the Scanner class
The Scanner class can be used to extract the individual "tokens" from a string
   InputDemo2.java
Uses Scanner methods next(), nextInt(), and nextDouble() to extract tokens from a string. This allows us to enter multiple input values - separated by spaces - in showInputDialog()
   ScannerDemo.java
Nested while loops. The outer loop uses Scanner method hasNext() to read any number of lines of text, until end-of-file. The inner loop then uses Scanner methods hasNext() and next() to extract each "token" from the line read
   The "Loop and a Half" Problem and the break Statement
The break statement causes an immediate exit from the loop that contains it
   More on Iteration
Loop pitfalls and implementing the repeat-until construct in Java
   Random Numbers
Java's Random class makes it easy to generate random numbers - floating-point numbers, ints, and ints within a specified range

Top              Home


Unit 8 - Java's ArrayList Class

   Powerpoint - Chapter 7 (The ArrayList Class Only)
   Intro to ArrayLists
Array and ArrayList concepts and terminology, advantages of lists, when to use an ArrayList, list items and list indices, basics of list processing, ArrayList methods, "overflowing the bounds" of a list, and "generic" ArrayLists
   ArrayLists and Primitive Types
To store values of a primitive type (e.g. int), we create an ArrayList of the associated "wrapper" class (e.g. Integer)
   ArrayListIntro.java
Shows the basics of list processing: using a for statement to traverse a list, and ArrayList methods add(), size(), and get()

   DataSet2.java
New, improved version of our old friend the DataSet class, has an instance variable that is an ArrayList-of-integer
DataSetTest.java - test class

   ArrayLists of Objects - a Complete Example of List Processing

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). The class has methods that

  1. add a new account to the Bank (i.e. populate the list)
  2. return the total on deposit in all accounts
  3. return the number of accounts that have a specified minimum balance
  4. return the BankAccount object (i.e., a pointer to it) with the largest balance
  5. search the list of accounts and return the BankAccount object (i.e., a pointer to it) that has a specified account number, which is exactly what happens when you insert your debit card into an ATM

The test class creates a Bank object and reads data for any number of accounts until eof. For each set of data read, it creates a new BankAccount object and adds it to the list by calling method addAccount(). At eof - after the list has been populated -, it calls the other Bank class methods to demonstrate list processing

   Assignment #8 - ArrayLists of Objects
CD-data.txt - input file

   The "Enhanced" for Statement - Optional
The enhanced for statement lets us access every object in an ArrayList with one less line of code than the traditional for. It can't do anything that the traditional for can't do and you can do things with the traditional for that cannot be done with the "enhanced"

Top              Home


Unit 9 - 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

   Magic8Ball.java | Magic8BallTester.java | 8BallAnswers.txt
A digital version of the classic prognosticating device. The test class uses Scanner methods hasNext() and nextLine() to read the answers from input file 8BallAnswers.txt until end-of-file. The Magic8Ball class reviews ArrayLists and random numbers

   The Bank Program Revisited - Input and Output Files

BankTester2.java | BankData.txt | Bank2.java | BankAccount.java

The BankTester class has been modified to read the account data from the input file BankData.txt. In the Bank2 class, method printList() has been modified to print the list of accounts to a file, as well as to the screen

Top              Home


Unit 10 - Arrays (Introduction)

Intro to Arrays in Java
Array concepts and syntax, arrays vs ArrayLists, and array processing in general
ArrayIntro.java
Declaring an array, using for statements to populate it and to process the values stored, and the length instance variable
ArrayDataSet.java
Array-oriented version of the DataSet2 class. The implementation has been changed from an ArrayList to an array of ints
ArrayDataSetTest.java - test class

Would you believe another version of the file-oriented Bank program? The implementation of the Bank has been changed from an ArrayList to an array of BankAccount objects. Another great example of information hiding as the test class did not have to be modified in any way! Also shows how to use a counter when the exact number of objects to be stored is not known in advance

Bank.java | BankAccount.java | BankTester.java
BankData.txt - data file

Top              Home


Unit 11 - Miscellaneous

   Static "Class" Variables and Static Methods
What they are, why we need them, how to declare and use them
   Parameter-Passing Mechanisms
In Java, all method parameters are passed "by value." This means that the method parameter is a copy of the corresponding argument...
   ParamPasser.java
Proves that primitive-type parameters are passed by value
   ParamPasser2.java
proves that object variables parameters (which store object references) are also passed by value:
1. In a method, you can make an object variable parameter point to a different object, but this does not affect the original argument since you only changed the value of the copy
2. In a method, if you modify the object "pointed to" by the parameter, you are also modifying the object pointed to by the argument, since the parameter and the argument both point to the same object
   String Comparisons
We have seen that Strings are compared for equality via methods equals and equalsIgnoreCase. To compare Strings for the <, <=, >, and >= relationships we use methods compareTo and compareToIgnoreCase

Top              Home