Com 121 Intro to Java Prgrming Theory Book
Total Page:16
File Type:pdf, Size:1020Kb
UNESCO-NIGERIA TECHNICAL & VOCATIONAL EDUCATION REVITALISATION PROJECT-PHASE II NATIONAL DIPLOMA IN COMPUTER TECHNOLOGY Introduction to Scientific Programming Using Java COURSE CODE: COM121 THEORY BOOK Version 1: December 2008 Introduction to Scientific Programming Using Java Page 1 Table of Contents Week 1: Java Programming Basics I ........................................................ A Brief History of Java ............................................................................................................................. 6 Why Java? ................................................................................................................................................. 6 Types of Java Programs ............................................................................................................................ 7 Introduction to Java Applications ................................................................................................................. 7 Components of a Java Application Program ............................................................................................. 8 Compilation and Execution of Java Programs ........................................................................................ 12 Week 2: Java Programming Basics II .................................................... Using Simple Graphical Interface ............................................................................................................... 15 Week 3: .................................................................................................. Data Types in Java .................................................................................................................................. 18 Integers and Floating Points .................................................................................................................... 19 Arithmetic Operators .............................................................................................................................. 20 Precedence of Arithmetic Operators ....................................................................................................... 21 Reference (Non-primitive Data Types) ................................................................................................... 22 Variable Declaration ............................................................................................................................... 24 Using Graphical User Interfaces ................................................................................................................. 28 Week 4: Program Development Techniques .......................................... Program Development Stages ..................................................................................................................... 33 Problem solving ...................................................................................................................................... 33 Week 5: Understand Insatiable Classes ................................................... Classes, Objects, Methods and Instance Variables ................................................................................. 40 Insatiable Classes ........................................................................................................................................ 41 Declaring a Class with a Method and Instantiating an Object of a Class ............................................... 42 Class Circle ......................................................................................................................................... 43 Class CircleTest ................................................................................................................................ 44 Week 6: Introduction to Applets .................................................................................................. Week 7: Know the Use of Conditional Statements ................................ 49 Algorithms .............................................................................................................................................. 50 Introduction to Scientific Programming Using Java Page 2 Pseudocode ............................................................................................................................................. 50 Sequence Structure in Java ..................................................................................................................... 52 Selection Statements in Java .................................................................................................................. 52 if Single-Selection Statement ............................................................................................................... 53 if...else Double-Selection Statement ................................................................................................. 53 Conditional Operator (?:) ...................................................................................................................... 54 Nested if...else Statements ............................................................................................................... 55 Dangling-else Problem ......................................................................................................................... 56 Blocks ...................................................................................................................................................... 57 Week 8: Know the Use of Selection Statements .................................... The while Repetition Statement ........................................................................................................... 60 Formulating Algorithms: Counter-Controlled Repetition ....................................................................... 61 Week 9: Recursion ................................................................................. Recursive Concepts ................................................................................................................................ 65 Example Using Recursion: Factorials ..................................................................................................... 66 Week10: Characters and Strings ........................................................... Fundamentals of Characters and Strings ................................................................................................. 70 What are Strings? .................................................................................................................................... 70 String Constructors ............................................................................................................................ 71 String Methods length, charAt and getChars ........................................................................ 72 Comparing Strings ................................................................................................................................... 73 General Learning Objectives for Week11: Arrays................................................................................. 79 Week 11: Arrays ..................................................................................................................................... Declaring and Creating Arrays ................................................................................................................ 81 Examples Using Arrays ........................................................................................................................... 83 Using an Array Initializer ......................................................................................................................... 84 Calculating a Value to Store in Each Array Element ............................................................................... 86 Summing the Elements of an Array ........................................................................................................ 87 Week12: Event Driven Programs .......................................................... Overview of Swing Components ............................................................................................................. 89 Displaying Text and Images in a Window ............................................................................................... 90 Introduction to Scientific Programming Using Java Page 3 Labeling GUI Components ...................................................................................................................... 90 Text Fields and an Introduction to Event Handling with Nested Classes ............................................... 92 Creating the GUI...................................................................................................................................... 96 Steps Required to Set Up Event Handling for a GUI Component ........................................................... 96 Using a Nested Class to Implement an Event Handler ........................................................................... 97 Registering the Event Handler for Each Text Field.................................................................................. 98 Details of Class TextFieldHandler's actionPerformed Method ............................................ 98 Week14: Inheritance ........................................................................... Introduction