Computer Science 523
Advanced Programming
Summer 2014, The College of Saint Rose
Class Examples
Class examples will be linked below. Please report any missing examples.
- Java Overview/Review
-
- Hello - the obligitory first example
- Seuss - more options on System.out.print and
System.out.println
- HelloYou - keyboard input (with Scanner), variables, String type, String concatenation
- Rectangle - int variables, Scanner's nextInt method, mathematical expressions
- MilesPerGallon - double variables, Scanner's nextDouble method
- RunsScored - named constant, int vs.double math, reading multiple items from a Scanner after a single prompt
- OlderThanJava - Java's if statement
- OlderYoungerThanJava - Java's if-else statement
- NoInterestLoan - JOptionPane dialog boxes, Integer.parseInt
- ShouldWeSki - a nested conditional
- NameAndTown - the if-else-if construct, String's length method
- BooleanDemo - more boolean operators, including
&&
and ||
, boolean variables
- MassPikeTolls - System.exit(1), error dialogs, String equals method
- CSOfficesIfElse - motivational example for switch statements
- CSOfficesSwitch - use of switch statements
- LittlePrimes - a switch with multiple cases executing the same code
- Payroll - DecimalFormat objects for custom floating point output format, multiple variable declarations on one line
- PerfectSquares - the while statement, the
++
operator
- MassPikeTollsBetter - better error checking using while loops
- Checkout - the do-while statement, the
+=
shorthand notation
- Sum1To10 - the for statement
- Countdown - counting down with a for loop
- Sum2ToNBy2 - the for statement counting by 2's
- GeometricFractionalSum - a counting loop with a second variable changing on each iteration
- RandomDemo - examples of how to generate various types of random numbers in Java
- GuessingGame - a random number for a children's guess the number game
- MonteCarloPi - using random numbers to approximate Pi, Math.sqrt
- File I/O
-
- HelloWorldFile - opening a file for output with a
PrintWriter
- CircleAreas - read in numbers from a keyboard,
print circle radii to a file
- AddNumbersFromFile - read in a fixed number of numbers from a file, add them up, a file Scanner
- AddNumbersFromFileSentinel - read in numbers from a file, add them up, ending with a sentinel value
- AddNumbersFromFileAll - read in numbers from a file until no more numbers are available (end of file), Scanner's hasNextInt method
- WordCount - count the number of words in an input file, Scanner's hasNext method
- Shout - read in a file, print its contents to another file but with all words upcased, Scanner's hasNextLine and String's toUpperCase method.
- Methods
-
- BaaBaaBad - motivating example for methods
- BaaBaaBetter - defining a static method
- BaaBaa2Methods - 2 methods
- NumberInfo - a method that takes a parameter
- HoursWorked - passing a String to a method, and using a Scanner to break it into components
- HoursWorked2 - same example, but pass in the Scanner as a parameter
- SumOfSquares - a method that takes two parameters
- Sum1ToN - a method that returns a value
- Sum1ToNBetter - a better version of the previous method
- Distance - a method that computes the distance between two points in the plane
- GradingBreakdown - a longer program that reads lots of inputs, just screaming out for a method to help organize
- GradingBreakdownBetter - the same program with a method introduced to organize the code better
- Classes
-
- RatiosNoClass - a motivating example without classes
- Ratios - a first look at a custom class
- PurchaseTracker - a custom class and a class variable
- DoublePair - a general purpose class that holds a pair of double values
- ObjectPair - a general purpose class that holds a pair of Object values
- GenericPair - a generic pair class using Java generics (type parameters)
- Collections
-
- Java Applets with Swing
-
- HelloWorldApplet - a "hello, world" type of program to demonstrate a very simple JApplet, using a JLabel
- ManyJLabels - attempt to add multiple components
- ManyJLabelsContentPane - multiple labels in a content pane
- ButtonClickCounter - the JButton, defining, adding, and implementing an event handler, JLabel's setText method
- ButtonClickCounterPanels - the JPanel, setSize for a window, setFont
- Voting - multiple JButtons, getFont
- VotingMany - arrays of Swing components
- VotingManyLayout - GridLayout
- PrimeFactors - the JTextField, basic exception handling, setText of a JTextField
- TicTacToe - using a 2D array of JButtons, enabling and disabling a component
- FiveGuys - lots more Swing components, layout, and listeners
- Recursion
-
- Sum1ToNRec - a recursive method to compute a mathematical formula
- Powers - a more advanced recursive method example, BigInteger
- RatioListApplet - a recursive data structure
- Inheritance
-
- The examples for this topic are those in Gaddis Chapter 11
- Linked Lists
-
- Searching and Sorting
-
- BinSearch - binary search on ints and Comparables