CS4A Programming Examples

Your system will have to have a program set to load .java extension files. Except for review, examples will generally follow book, but will have enhancements.

Most of These Examples are Available from Book Companion Website

Basic Input Class - Will Not be Reviewed in Class
0

J2SE5 added formatted input and output, Savitchin.java is no longer needed.

Java Has Just Been Updated Java SE6 - Web 2.0, Ajax Web Servies.

Chapter 1 Examples
1.1 HelloWorld.java First Java Program Prints Out Hello World
1.2 FirstProgram.java page 23 Second Java Program Prints Out Hello World and Also accept keyboard input
   
   
Chapter 2 Examples
2.1 EggBasket.java page 43
2.2 Optional will not be covered EggBasket1.java structure - object oriented version of EggBasket
2.3 DinnerBill.java Compute whole dollar amounts for sharing a Restaurant bill. Demonstrates typecasting, mixed types, assignments, variables.
2.4 Optional will not be covered DinnerBill1.java
2.5 Discount.java computes a 15% discount before and after tax
2.6 ChangeMaker.java page 65 example
2.7 Increment.java java example showing pre and post increment and decrement unary operator

2.8

Discount2.java String example of Discount.java
2.9 Optional will not be covered Discount1.java OOP example of Discount1.java with methods
2.10 MyString.java illustrates some of the methods on page 82-85 Display 2.6 - Has ASCII chart
2.11 StringDemo.java same as example on page 87 but with escape character added
2.12 MyPrintStuff.java section 2.3 example
2.13 ScannerDemo.java Class Scanner Demo
2.14 CircleCalculation.java page 91 demonstrates comment style, indentation, and meanifull variable names
2.15 CircleCalculation2.java page 94 demonstrates global constant PI
Chapter 3 Examples
3.1 BankBalance.java book example page 106
3.2 BankBalance2.java illustrates block if
3.3 MyIf.java book examples on pages 108-112 example or compound booleans
3.4 StringEqualityDemo.java display 3.3 example page 112; checking for equality with classes implement equal
3.5 StringCompareDemo.java another lexicographical ordering pages 112-114

3.6

MultipleBirths.java illustrates switch statement page 123 display 3.5
3.7 MySwitch.java illustrates switch, dual cases for same block page 124
3.8 Grader.java illustrates set of nested ifs for determining grades page 120-121
3.9 MyGrader.java illustrates use of switch and integer division
3.10 WhileDemo.java illustrates while loop counts from 1 to input number
3.11 DoWhileDemo.java illustrates post-test do while loop for counting up
3.12 BugTrouble.java illustrates while loop with be expression
3.13 ForDemo.java illustrates the for loop page 142
3.14 MyFactorial.java illustrates for and while loop for calculating factorial; and infinite loops
3.15 BreakDemo.java demonstrates the break statement - break takes you out of inner most loop
3.16 ExamAverager.java demonstrates while statement with termination
3.17 LoopBugs.java demonstrates common loop bugs
3.18 Boolean2.java demonstrates left to right evaluation of boolean expressions, and left to right to method calls

 

Chapter 4 Examples

 

  OOP Presentation
  Display 4-13 Encapsulation Page 226
4.1 SpeciesFirstTry.java page 185 Class Example with three Instance Variables and three actions( Methods) Display 4.2
4.2 SpeciesFirstTryDemo.java page 187 creates a SpeciesFirstTry object
4.3 SpeciesFirstTryThis.java illlustrates this pointer
4.4 SpeciesFirstTryThisDemo.java creates objects for SpeciesFirstTryThis class
   
4.5 BankAccount.java Display 4.5 (Used Illustrating Masking of a Local Variable) page 201
4.6 LocalVariablesDemoProgram.java (Creates BankAccount Object, and masks a local variable of BankAccount)
4.7 SpeciesSecondTry.java Display 4.6 Methods Passing Parameters
4.8 SpeciesSecondTryDemo.java Display 4.7
4.9 SpeciesSecondTryV2.java illustrates passing multiple parameters
4.10 SpeciesSecondTryV2Demo.java illustrates passing multiple parameters
4.11 SpeciesThirdTry.java Demonstrates pre and post condition comments
4.12 SpeciesThirdTryDemo.java illustrates use of private and public data
4.12 SpeciesFourthTry.java illustrates accessor (access get data) and mutator (mutate or set data)
4.13 SpeciesFourthTryDemo.java creates the object
4.14 Purchase.java (demonstrates program with lots of sets, get, read, etc ) - Programming interested in only method names get, set readInput, writeOutput
4.15 PurchaseDemo.java (uses Purchase class)
4.16 SpeciesFourthTry.java species class with private data, get and set methods
4.17 SpeciesThisDemo.java illustrates tables on page 233,
4.18 Species.java creates a sample equals methods
4.19 SpeciesEqualDemo.java compares address comparison with == and class equals method

4.20

DemoSpecies.java used to illustrate call by value vs call by reference
4.21 ParametersDemo.java main program for DemoSpecies.java

 

Chapter 5

 

5.1 Oracle.java illustrates a private helper methods, program outputs initial canned advice and then uses users previous input
5.2 OracleDemo.java main program for Oracle.java
5.3 CircleFirstTry.java illustrates static variables and static methods
5.4 CircleDemo.java call circleFirstTry class
5.5 PlayCircle.java illustrates a class with both static and non static members
5.6 PlayCircleDemo.java calls playcircle
5.7 PlayCircle2.java main within the definition class
5.8 StaticDemo.java illustrates how only all class objects share the same static variable
5.9 Circle.java illustrates a set of static methods that utilize Math.PI static variable
5.10 MyDecimalFormat.java illustrates correct currency formatting using java.text.DecimalFormat class, includes testing procedures
5.11 DollarFirstTry.java outputs dollars and cents using math.round and integer division/mod
5.12 DollarFirstTryDriver.java main method for creating DollarFirstTry object
5.13 Dollars.java fixed bug in DollarFirstTry.java for handling negative values - code not tight example showing tight code
5.14 Statistician.java display 5.16 page 296 illustrates method overload, same name different arguments
5.15 Pet.java page 299 (set is overloaded) cannot overload gets
5.16 Money.java page 306, set and object return examples
5.17 MoneyDemo.java page 311 tests Money class
5.20 PetRecord.java 5 overloaded constructors
5.21 PetRecordDemo.java page 317 tests PetRecord class

 

Chapter 6

6.1 ArrayOfTemperatures.java page 345 display 6.1 sample array of 7 double temperatures
6.2 ArrayOfTemperatures2.java page 350, length instance variable for array and constant used in declaring array size
6.3 ArrayOfTemperatures3.java object orient example of 6.1 and 6.2 with initialization, array passing by reference
6.4 Zoo.java illustrates example of an array of objects and an internal private class
6.5 ArgumentDemo.java illustrates use of array element as an actual element
6.6 TestProgram.java illustrates passing string arrays to main
6.7 ArrayAreObjects.java illustrates that arrays are an object with an address pointing to an array of primitive types
6.8 TestEquals.java tests equality of arrays
6.9 ReturnArrayDemo.java returns the starting address of an array
6.10 Shoe.java has example of selection sort - Wait to casino in CS4B
6.11 SelectionSort.java display 6.13 good example of code that is not tight (Spaghetti Code)
6.12 InterestTable.java display 6.5 page 391
6.13 InterestTable3.java display 6.19, page 396, illustrates table points to an array of row address, each row address points to a column of numbers
Chapter 7
7.1 Person.java base class with instance variable name and standard APIs:set,get,writeName
7.2 Student.java derived (subclass of super class person) ad studentNumber instance variable
7.3 InheritanceDemo.java creates student object and calls underlying methods
7.4 Undergraduate.java extends Student.java, page 430
7.5 UndergraduateDemo.java creates Undergraduate object
7.6 MyObject.java illustrates some of the basic methods of eve class java.lang.Object
Quick Chapter 1-6 Review Programs
1 StringDemo.java page 87
2 MySavitchIn.java section 2.3 example
3 SpeciesFirstTry.java 4.1 page 185
4 SpeciesFirstTryDemo.java 4.2 page 187
5 StaticDemo.java 5.8
  Statistician.java page 296
 

PetRecord.java 5.20 page 313

  PetRecordDemo.java 5.21 page 317
  ArrayOfTemperatures3.java 6.3
  Array.java 2-D array
Chapter 8 Examples - Exceptions
5 HelloWorld3.java throws an exception but doesn't handle it
6 GotMilk.java copy of example on page 515
7 GotMilk1.java structured version of above book example
8 GotMilk2.java exception example of GotMilk1.java structured version of book example on page 516
9 GotMilk3.java additional exception example
10 DivideByZero.java example of creating a new exception pages 529-531
11 DivideByZero1.java example adding two exceptions
10

DivideByZero2.java demonstrates multiple exceptions, multiple catches, and throws

11 DivideByZero3.java demonstrates simplfied exception creation and string tokenizer

 

Chapter 9 File I/O

13 TextFileOutputDemo.java copy of example on page 583, demonstrates PrintWrite, File Creation
14 TextFileOutputDemo1.java writes 100 sequential numbers 5 per line, demonstrates flush, file append
15 TextFileInputDemo.java page 591 sample reads from text file using BufferedReader
16 WordFinder.java page 597 stringTokenizer example
17 WordCount.java start of a word count program, stringTokenizer and Input file I/O
18 TextEOFDemo.java page 600 demonstrates both file read and writing with EOF check
Skipping Binary File I/O
19 FileClassDemo2.java page 650
20 FileClassDemo3.java upgraded version of FileClassDemo2.java further demonstrates File class methods

 

Chapter 10 Dynamic Data Structures

 

22 MyVector.java illustrates basic methods of java.util.Vector class
23 MyVector1.java illustrates need to typecast Object parent class
24 VectorDemo.java example from page 675
25 VectorDemo1.java clone example class
End of First Handout
26 ListNode.java book page 682 with main for testing
27 StringLinkedList.java page 683-684
28 LinkedListDemo.java page 688 Display 10.8

29

StringLinkedListSelfContained.java Display 10.9
30 StringLinkedListWithIterator.java Display 10.11
31 StringLinkedListDemo2.java Implements 10.13

 

Chapter 11 Recursion

 

32 MyFactorialrec.java examples of recursion and corresponding iterative methods
33 RecursionDemo.java Display 11.1 - translates integer to corresponding english digits
34 IterativeDemo.java Display 11.3 iterative version of recursiondemo.java
35 RecursionDemo2.java Display 11.4 counts number of 0 in an integer
36 CountDown.java Display 11.5 with enhancements, counts down

37

ArraySearcher.java Display 11.6 Recursive code for Binary Search
38 ArraySearcherDemo.java Display 11.8 main method for calling the Binary Search

 

Chapter 12 Swing

39 FirstSwingDemo.java Display 12.1 creates JFrame object
40 WindowDestroyer.java Display 12.2 implements the event closeWindow
41 FirstWindow.java Display 12.4 illustrates JFrame inheritance to create a basic JFrame window
42 FirstWindowDemo.java Display 12.5 has main program for creating FirstWindow Object
43 SecondWindow.java display 12.6 extends JFrame and adds constructor for setting content Pane background color
44 SecondWindowDemo.java Display 12.8 creates two SecondWindow objects with differenct background colors
45 BorderLayoutDemo.java Display 12.10 extends JFrame and illustrate layout manager class BorderLayout

46

ButtonDemo.java Display 12.12 extends JFrame implement ActionListener, illustrates two buttons
47 PanelDemo.java Display 12.13 illustrates putting the two previous buttons inside a panel at South of Content Pane
48 MemoSaver.java Display 12.15 illustrates JTextArea with two strings of text that can be displayed one at a time
49 LabelDemo.java Display 12.16 illustrates JTextField one line TextArea
50