CIM7B 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.

Basic Input Class
 

Will be covering Object Oriented Programming later on...

Original White Paper James Gosling

Java as a OOP Teaching Tool

 

  MyFormatter.java demonstrates J2SE5 formatted output | cheat sheet
 

MyScanner.java demonstrates J2SE5 formatted input | cheat sheet

 

0 Savitchin.java input/output class is now obsolete in J2SE5 java.util.Scanner & java.util.Formatter
Chapter 1-6 Review Programs
 

Java Primitive Types

Java Comparison Operators

Java Logical Operators

Table for Not !, && And, || Or, ^ Exor

1 StringDemo.java page 87
2 no longer being covered 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
21 WordCount2.java illustrates simultaneously Input/Output using File Class, Scanner, and PrintWriter
   

 

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  
 

Threads

sieve illustration | Java Applet & Discussion

   

Scanner Summary

  • java.util.Scanner scan = new java.util.Scanner( System.in ); // or use String

  • scan.nextInt( ); //returns next token which is an integer

  • scan.next(); // returns next token, default delimiters are white spaces

  • scan.close(); close Scanner when down

java.util.Formatter Summary

  • System.out.format( "Format String", args,... );
  • String str = new String.format( "ten=%d%n", 10 ); //ten=10
  • Format Specifier is %[flag][width][.precision]conversion
  • Conversion = [ d (decimal integer), f (floating point), c (character), s (string), n (new line)
  • Precision = is precision right of decimal point
  • width # characters output should occupy - floating default is left; width cause right
  • flag , use comma every 3 digits;
  • ) enclose negative number in parentheses
  • default with no width is length of output
  • - is left justified - over rides default right justification
 

 

Java Primitive Types

Type

Range

Memory Storage

byte
short
int
long
float
double

-27 to 27 -1
-215 to 215 -1
-231 to 231 - 1
-263 to 263 - 1
-3.4E38 to 3.4E38
-1.7E308 to 1.7E308

1 byte
2 bytes
4 bytes
8 bytes
4 bytes
8 byte

 

 

 

 

 

 

Java Comparison Operators

Operator

Example

Meaning

==
!=
>
>=
<
<=

x == y
x != y
x > y
x >= y
x < y
x <= y

x is equal to y
x is not equal to y
x is greater than y
x is greater than or equal to y
x is less than y
x is less than or equal to y

 

 

 

 

Java Logic Operators

Operator

Meaning

!
&&
||
^

Logical NOT
Logical AND
Logical OR
Logical exclusive