CIS 160 (Java I) Demonstration Programs
Comments, declaring and initializing variables
- BlockComment.java: program that does nothing, but has block comments
- LineComment.java: program that does nothing, but has single line comments
- Variables1.java: declaring and initializing variables
- Variables2.java: using variables
- Variables3.java: using reference variables
Output
- Hello.java: write text to the screen
- Hello2.java: write text to the screen, print vs. println
- SimpleOutput.java: basic console output using System.out (print, println, printf)
- TestFormatting.java: three ways of formatting numeric output
- TestPrintf.java: formatted output using printf
- Exer01.java: demonstration of printf (Exercise 1)
Input
- GetKeyboard1.java: get keyboard input using inputStreamReader and convert it to a number (a double)
- GetKeyboard2.java: get keyboard input using inputStreamReader and convert it to a number (all numeric data types)
- TestScanner.java: keyboard input using the Scanner class
- TestScanner2.java: closes Scanner resource when done using a "finally" block
- TestScanner3.java: closes Scanner resource using a "try-with-resources" statement
- GUI_IO1.java: simple GUI I/O (JOptionPane)
- Exer02.java: demonstration of JOptionPane for input and output (Exercise 2)
- Keyboard.java: demonstration object used to help with user input
- TestKeyboard.java: simple demo using Keyboard class
- FormatOutput.java: uses complex input and DecimalFormat output formatting
Miscellaneous
- StaticVariables.java: static variables and constants
- TestTokenizer1.java: tokenizing strings, priming read
- StepByStep.java: pre and post increment and decrement
- TestEscape.java: escape sequences
- TestFinal.java: constants
- TestSpin.java: use of '\r' to create spinner
- CalcPay.java: in-class simple pay calculator
- TestToken.java: StringTokenizer demo
- PayCalc.java: calculate gross pay given hours and rate (with overtime)
- SquareRoot.java: using Math.sqrt() to find square roots
- SquareRoot2.java: same as SquareRoot.java, but uses printf() for formatting output
- FindRemainder.java: casting, modulus operator, concatenation, escape sequences
- CalculateSphereVolume.java: calculates the volume
of a sphere using a calculus-style technique
- DisplayTruthTable.java: displays a truth table
- BaseConverter.java: converts numbers between bases
Basic file input/output
- DemoFileObject.java: uses File object to get file information
- DemoFileReader.java: uses File, FileReader, and FileWriter to copy a file
- FileOutput.java: simple file output
- FileInput.java: simple file input
- FileOutput2.java: FileOutput.java modified to output more data
- FileInput2.java: uses Scanner object to read file
- BasicFileTest.java: basic file I/O
- BasicFileTest2.java: basic file I/O
- Tokenize.java: String and StringTokenizer methods
(javahead.txt data file)
Selection
- CalcDiscount1.java: single-branch selection, some String methods
- CalcDiscount2.java: double-branch selection
- CalcDiscount3.java: more concise double-branch selection
- Conditional1.java: conditional operator (finding maximum)
- Conditional2.java: conditional operator (choosing singular/plural)
- Menu1.java: multi-branch selection (switch statement)
- Menu2.java: multi-branch selection (switch statement), char, String methods
- MultiwayBranch1.java: multi-branch selection (switch statement)
- MultiwayBranch2.java: multi-branch selection (nested if/else)
- MultiwayBranch3.java: multi-branch selection (shortcut nested if/else)
- MultiwayBranch4.java: multi-branch selection (switch statement with fall-through)
- MultiwayBranch5.java: multi-branch selection (switch statement with fall-through)
- MultiwayBranch6.java: multi-branch selection (switch statement)
Repetition
- Loops1.java: repetition statements (basic, counter-controlled)
- Loops2.java: repetition statements (pre-test vs. post-test)
- Loops3.java: repetition statements (accumulators)
- Loops4.java: repetition statements (changing the increment value)
- Loops5.java: sentinel controlled repetition
- Loops17.java: iterating over a collection (for-each)
- Loops6.java: repetition with error checking (try/catch)
- Loops7.java: repetition error checking with break and continue
- Loops8.java: priming read, reading from a file
(loopdata.txt data file)
- Loops9.java: alternative to priming read
(loopdata.txt data file)
- Loops10.java: using a for loop with sentinel values
(loopdata.txt data file)
- Loops11.java: break and continue
- Loops12.java: nested loops
- Loops13.java: break in inner loop
- Loops14.java: labeled break
- Loops15.java: continue in inner loop
- Loops16.java: labeled continue
- Menu3.java: the menu program using a post-test loop
- Ch5Ex8.java: demonstration of while loops
- Ch5Ex9.java: demonstration of for loops
- Ch5Ex9x.java: demonstration of for loops
Basic GUI programming
- GUI01.java: creating a window
- GUI02.java: creating multiple windows
- GUI03.java: keeping track of the number of windows
- GUI04.java: inheriting from JFrame
- GUI05.java: creating and adding components
- GUI06.java: using a layout manager
- GUI07.java: formatting labels, adding textboxes
- GUI08.java: adding buttons, making textboxes read-only
- GUI09.java: adding a button listener (event handler)
- GUI10.java: adding a listener to the calc button
- Panel1.java: nested GUI containers for complex layouts
- NestedJPanels.java: nested JPanels for more complex layouts
· screenshot of NestedJPanels.java.
- TestGrid.java: demonstration of GridLayout layout manager
- TestFlow.java: demonstration of FlowLayout layout manager
- TestBorder.java: demonstration of BorderLayout layout manager
- Ch06Ex01.java: demonstration working GUI
Methods and recursion
- Method1.java: basic user-defined methods
- Method2.java: methods returning void
- Method3.java: methods returning values
- Method4.java: passing by value
- Method5.java: passing reference variables
- Method6.java: passing Strings
- Method7.java: overloading methods
- Method8.java: payroll calculation using multiple methods
(paydata.txt data file)
- AreaCalculator.java: basic user-defined method
- Factorial.java: recursive method
- Recurse.java: various recursion algorithms
- Problem151.java: solution to Project Euler problem 151
Arrays
- Array0.java: using "String[] args" to get command line arguments
- Array1.java: basic array usage; passing arrays to methods
- Array2.java: reading array data from a file; dealing with partially filled arrays
people.txt: data file for Array2.java
- Array3.java: processing file data before saving it in an array;
using methods to process data
people.txt: data file for Array3.java
- Array4.java: reading parallel arrays from file
people2.txt: data file for Array4.java
- Array5.java: linear searching of arrays; using parallel arrays
people2.txt: data file for Array5.java
- Array6.java: sorting parallel arrays
people2.txt: data file for Array6.java
- Array7.java: using arrays of objects
people2.txt: data file for Array7.java
- Array8.java: using the Arrays class to sort an array of objects
array8data.txt: data file for Array8.java (make sure after transferring
data file that there is no blank line after last record and theat each record is on its own line)
- Array9.java: reading an indeterminate number of items into an array with error checking
- CardProgram1.java: creates and shuffles a deck of cards (as an array) and then deals two hands
- CardProgram2.java: creates and shuffles a deck of cards (as an array) and then lets user search for cards
- CardProgram3.java: creates and shuffles a deck of cards (as an array) using Java libraries
Searching and sorting
- Search.java: linear and binary searching
- Sort.java: various sorting algorithms
Object-oriented programming
- Employee.java: Employee class implementation
TestEmployee.java: driver to test Employee class
· sample output
- TestCylinder.java: inheritance and composition demonstration
- Keyboard.java: demonstration object used to help with user input
Exceptions
- TestException1.java: a non-compiling exception demonstration
- TestException2.java: a non-compiling exception demonstration
- TestException3.java: specifying but not catching an exception
- TestException4.java: catching an exception demonstration
- TestException5.java: displaying a special exception message
Applets and JApplets
- JApplet1.java: a demonstration applet
- ADemo1.html: a simple JApplet
- ADemo2.html: a simple Applet
- ADemo3.html: a simple JApplet (archived)
Collections
- TestEmployee2.java: Employee class with Vector for storage
- TestMap.java: demonstration of HashMap class using Employee class
- TestSet.java: demonstration of HashSet class
- TestSet2.java: demonstration of TreeSet class
- TestSet3.java: demonstration of LinkedHashSet class
- TestList1.java: demonstration of ArrayList class
- TestList2.java: demonstration of LinkedList class
- TestQueue1.java: demonstration of Queue interface
- Reverse.java: demonstration of Stack class
Return Links
Mr. Klick's Home Page