CIS 160 Demonstration Programs

Comments, declaring and initializing variables

  1. BlockComment.java: program that does nothing, but has block comments
  2. LineComment.java: program that does nothing, but has single line comments
  3. Variables1.java: declaring and initializing variables
  4. Variables2.java: using variables
  5. Variables3.java: using reference variables

Output

  1. Hello.java: write text to the screen
  2. Hello2.java: write text to the screen, print vs. println
  3. SimpleOutput.java: basic console output using System.out (print, println, printf)
  4. TestFormatting.java: three ways of formatting numeric output
  5. TestPrintf.java: formatted output using printf
  6. Exer01.java: demonstration of printf (Exercise 1)

Input

  1. GetKeyboard1.java: get keyboard input using inputStreamReader and convert it to a number (a double)
  2. GetKeyboard2.java: get keyboard input using inputStreamReader and convert it to a number (all numeric data types)
  3. TestScanner.java: keyboard input using the Scanner class
  4. TestScanner2.java: closes Scanner resource when done using a "finally" block
  5. TestScanner3.java: closes Scanner resource using a "try-with-resources" statement
  6. GUI_IO1.java: simple GUI I/O (JOptionPane)
  7. Exer02.java: demonstration of JOptionPane for input and output (Exercise 2)
  8. Keyboard.java: demonstration object used to help with user input
  9. TestKeyboard.java: simple demo using Keyboard class
  10. FormatOutput.java: uses complex input and DecimalFormat output formatting

Miscellaneous

  1. StaticVariables.java: static variables and constants
  2. TestTokenizer1.java: tokenizing strings, priming read
  3. StepByStep.java: pre and post increment and decrement
  4. TestEscape.java: escape sequences
  5. TestFinal.java: constants
  6. TestSpin.java: use of '\r' to create spinner
  7. CalcPay.java: in-class simple pay calculator
  8. TestToken.java: StringTokenizer demo
  9. PayCalc.java: calculate gross pay given hours and rate (with overtime)
  10. SquareRoot.java: using Math.sqrt() to find square roots
  11. SquareRoot2.java: same as SquareRoot.java, but uses printf() for formatting output
  12. FindRemainder.java: casting, modulus operator, concatenation, escape sequences
  13. CalculateSphereVolume.java: calculates the volume of a sphere using a calculus-style technique
  14. DisplayTruthTable.java: displays a truth table
  15. BaseConverter.java: converts numbers between bases

Basic file input/output

  1. DemoFileObject.java: uses File object to get file information
  2. DemoFileReader.java: uses File, FileReader, and FileWriter to copy a file
  3. FileOutput.java: simple file output
  4. FileInput.java: simple file input
  5. FileOutput2.java: FileOutput.java modified to output more data
  6. FileInput2.java: uses Scanner object to read file
  7. BasicFileTest.java: basic file I/O
  8. BasicFileTest2.java: basic file I/O
  9. Tokenize.java: String and StringTokenizer methods (javahead.txt data file)

Selection

  1. CalcDiscount1.java: single-branch selection, some String methods
  2. CalcDiscount2.java: double-branch selection
  3. CalcDiscount3.java: more concise double-branch selection
  4. Conditional1.java: conditional operator (finding maximum)
  5. Conditional2.java: conditional operator (choosing singular/plural)
  6. Menu1.java: multi-branch selection (switch statement)
  7. Menu2.java: multi-branch selection (switch statement), char, String methods
  8. MultiwayBranch1.java: multi-branch selection (switch statement)
  9. MultiwayBranch2.java: multi-branch selection (nested if/else)
  10. MultiwayBranch3.java: multi-branch selection (shortcut nested if/else)
  11. MultiwayBranch4.java: multi-branch selection (switch statement with fall-through)
  12. MultiwayBranch5.java: multi-branch selection (switch statement with fall-through)
  13. MultiwayBranch6.java: multi-branch selection (switch statement)

Repetition

  1. Loops1.java: repetition statements (basic, counter-controlled)
  2. Loops2.java: repetition statements (pre-test vs. post-test)
  3. Loops3.java: repetition statements (accumulators)
  4. Loops4.java: repetition statements (changing the increment value)
  5. Loops5.java: sentinel controlled repetition
  6. Loops17.java: iterating over a collection (for-each)
  7. Loops6.java: repetition with error checking (try/catch)
  8. Loops7.java: repetition error checking with break and continue
  9. Loops8.java: priming read, reading from a file (loopdata.txt data file)
  10. Loops9.java: alternative to priming read (loopdata.txt data file)
  11. Loops10.java: using a for loop with sentinel values (loopdata.txt data file)
  12. Loops11.java: break and continue
  13. Loops12.java: nested loops
  14. Loops13.java: break in inner loop
  15. Loops14.java: labeled break
  16. Loops15.java: continue in inner loop
  17. Loops16.java: labeled continue
  18. Menu3.java: the menu program using a post-test loop
  19. Ch5Ex8.java: demonstration of while loops
  20. Ch5Ex9.java: demonstration of for loops
  21. Ch5Ex9x.java: demonstration of for loops

Basic GUI programming

  1. GUI01.java: creating a window
  2. GUI02.java: creating multiple windows
  3. GUI03.java: keeping track of the number of windows
  4. GUI04.java: inheriting from JFrame
  5. GUI05.java: creating and adding components
  6. GUI06.java: using a layout manager
  7. GUI07.java: formatting labels, adding textboxes
  8. GUI08.java: adding buttons, making textboxes read-only
  9. GUI09.java: adding a button listener (event handler)
  10. GUI10.java: adding a listener to the calc button
  11. Panel1.java: nested GUI containers for complex layouts
  12. NestedJPanels.java: nested JPanels for more complex layouts · screenshot of NestedJPanels.java.
  13. TestGrid.java: demonstration of GridLayout layout manager
  14. TestFlow.java: demonstration of FlowLayout layout manager
  15. TestBorder.java: demonstration of BorderLayout layout manager
  16. Ch06Ex01.java: demonstration working GUI

Methods and recursion

  1. Method1.java: basic user-defined methods
  2. Method2.java: methods returning void
  3. Method3.java: methods returning values
  4. Method4.java: passing by value
  5. Method5.java: passing reference variables
  6. Method6.java: passing Strings
  7. Method7.java: overloading methods
  8. Method8.java: payroll calculation using multiple methods (paydata.txt data file)
  9. AreaCalculator.java: basic user-defined method
  10. Factorial.java: recursive method
  11. Recurse.java: various recursion algorithms
  12. Problem151.java: solution to Project Euler problem 151

Arrays

  1. Array0.java: using "String[] args" to get command line arguments
  2. Array1.java: basic array usage; passing arrays to methods
  3. Array2.java: reading array data from a file; dealing with partially filled arrays
    people.txt: data file for Array2.java
  4. Array3.java: processing file data before saving it in an array; using methods to process data
    people.txt: data file for Array3.java
  5. Array4.java: reading parallel arrays from file
    people2.txt: data file for Array4.java
  6. Array5.java: linear searching of arrays; using parallel arrays
    people2.txt: data file for Array5.java
  7. Array6.java: sorting parallel arrays
    people2.txt: data file for Array6.java
  8. Array7.java: using arrays of objects
    people2.txt: data file for Array7.java
  9. 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)
  10. Array9.java: reading an indeterminate number of items into an array with error checking
  11. CardProgram1.java: creates and shuffles a deck of cards (as an array) and then deals two hands
  12. CardProgram2.java: creates and shuffles a deck of cards (as an array) and then lets user search for cards
  13. CardProgram3.java: creates and shuffles a deck of cards (as an array) using Java libraries

Searching and sorting

  1. Search.java: linear and binary searching
  2. Sort.java: various sorting algorithms

Object-oriented programming

  1. Employee.java: Employee class implementation
    TestEmployee.java: driver to test Employee class  · sample output
  2. TestCylinder.java: inheritance and composition demonstration
  3. Keyboard.java: demonstration object used to help with user input

Exceptions

  1. TestException1.java: a non-compiling exception demonstration
  2. TestException2.java: a non-compiling exception demonstration
  3. TestException3.java: specifying but not catching an exception
  4. TestException4.java: catching an exception demonstration
  5. TestException5.java: displaying a special exception message

Applets and JApplets

  1. JApplet1.java: a demonstration applet
  2. ADemo1.html: a simple JApplet
  3. ADemo2.html: a simple Applet
  4. ADemo3.html: a simple JApplet (archived)

Collections

  1. TestEmployee2.java: Employee class with Vector for storage
  2. TestMap.java: demonstration of HashMap class using Employee class
  3. TestSet.java: demonstration of HashSet class
  4. TestSet2.java: demonstration of TreeSet class
  5. TestSet3.java: demonstration of LinkedHashSet class
  6. TestList1.java: demonstration of ArrayList class
  7. TestList2.java: demonstration of LinkedList class
  8. TestQueue1.java: demonstration of Queue interface
  9. Reverse.java: demonstration of Stack class