Download presentation
Presentation is loading. Please wait.
Published byAileen Fitzgerald Modified over 9 years ago
1
Java Programming, 2E Introductory Concepts and Techniques Chapter 1 An Introduction to Java and Program Design
2
Chapter 1: An Introduction to Java and Program Design 2 Introduction A program is a step-by-step series of instructions for a computer Programming is the process of writing these instructions Programmers, or developers, design and write programs using a programming language or development tool Java is a programming language that provides the structure for efficient and economical programs
3
Chapter 1: An Introduction to Java and Program Design 3 What Is Java? High-level language Object-oriented –Data and operations are packaged into a single unit called an object Basic syntax derived from C, C++, and Smalltalk –Designed by a team from Sun Microsystems led by James Gosling in the early 1990’s
4
Chapter 1: An Introduction to Java and Program Design 4 What Is Java? Parsimonious –Compatible with older versions Robust –Strongly typed and incorruptible data Secure –Protection against misuse of code Portable –Platform-independent
5
Chapter 1: An Introduction to Java and Program Design 5 Java Program Types Console and Windowed applications Applets Servlets Web Services
6
Chapter 1: An Introduction to Java and Program Design 6 Console Applications Stand-alone programs using a command- line interface
7
Chapter 1: An Introduction to Java and Program Design 7 Windowed Applications Stand-alone programs using a graphical user interface (GUI)
8
Chapter 1: An Introduction to Java and Program Design 8 Applets Client-side programs executed within a Web browser
9
Chapter 1: An Introduction to Java and Program Design 9 Programming a Computer Companies need developers to build general application software packages Custom applications are built for specific needs Existing programs need maintenance and upgrades New applications will be needed due to emerging technologies
10
Chapter 1: An Introduction to Java and Program Design 10 Programmers follow a general methodology called the program development cycle to structure the development process. The phases of the cycle are Analyze the requirements Design the solution Validate the design Implement the design Test the solution Document the solution
11
Chapter 1: An Introduction to Java and Program Design 11 Phase 1 – Analyze Requirements Verify that the requirements are clear and complete Evaluate the problem to determine that it is solvable using a program List the required input and output data Determine whether the input data is available for testing
12
Chapter 1: An Introduction to Java and Program Design 12 Phase 1 – Analyze Requirements Ensure that a solution, or algorithm, can be developed with the information provided in the requirements Verify the user interface specifications
13
Chapter 1: An Introduction to Java and Program Design 13
14
Chapter 1: An Introduction to Java and Program Design 14 Phase 2 – Design Solution Develop a logical model that illustrates the sequence of steps you will take to solve the problem Use design tools such as storyboards, object structure diagrams, flowcharts, and pseudocode to outline the logic of the program An algorithm is a sequence of precise instructions that leads to a solution –Example: Determine how many times a name occurs in a list of names
15
Chapter 1: An Introduction to Java and Program Design BREAK 15
16
Chapter 1: An Introduction to Java and Program Design Example: Determine how many times a name occurs in a list of names
17
Chapter 1: An Introduction to Java and Program Design 17 Phase 2 – Design Solution Storyboards are sketches of the user interface
18
Chapter 1: An Introduction to Java and Program Design 18 Phase 2 – Design Solution Flowcharts graphically represent the logic used to develop an algorithm Control structures allow the programmer to specify the code that will execute only if a condition is met Flowcharts use pseudocode, English, or mathematical notation inside symbols to represent the steps of a solution
19
Chapter 1: An Introduction to Java and Program Design 19
20
Chapter 1: An Introduction to Java and Program Design 20
21
Chapter 1: An Introduction to Java and Program Design 21 Phase 2 – Design Solution Pseudocode is an English representation of how the program code should be written
22
Chapter 1: An Introduction to Java and Program Design 22 Phase 3 – Validate Design The programmer steps through the solution with test data The user agrees that the program design solves the problem put forth in the requirements The user verifies that the initial requirements document contains all necessary requirements
23
Chapter 1: An Introduction to Java and Program Design 23 Phase 4 – Implement Design Write the code that translates the design into a program Create the user interface Create comments within the code that explains the purpose of the code Test the code as it is written Test related code
24
Chapter 1: An Introduction to Java and Program Design 24 Phase 4 – Implement Design
25
Chapter 1: An Introduction to Java and Program Design 25 Phase 5 – Test Solution Create a test plan with test cases of sample input data and expected output Perform integration testing to ensure that components interact correctly Test boundary values Document any problems –If results are unsatisfactory, a new iteration of the development cycle begins
26
Chapter 1: An Introduction to Java and Program Design 26 Phase 6 – Document Solution Requirements documents, program design documents, user interface documents, and documentation of the code Test cases and proof of successful completion of testing Program code should be archived electronically
27
Chapter 1: An Introduction to Java and Program Design BREAK 27
28
Chapter 1: An Introduction to Java and Program Design 28 Object-Oriented Programming and Design Object-oriented programming –Data and the code that operates on the data are packaged into a single unit called an object Object-oriented design –Identifies how objects interact with each other to solve a problem
29
Chapter 1: An Introduction to Java and Program Design 29 Objects Class –Implementation of an object or set of objects with a common structure and behavior Class diagram –A tool displaying a hierarchy of classes, including superclasses and subclasses Instance –A specific occurrence of an object Attributes –The properties of an individual object Method –The code of an operation on the data of an object
30
Chapter 1: An Introduction to Java and Program Design 30
31
Chapter 1: An Introduction to Java and Program Design 31 Operations Message –The activation of an operation through naming the object and the operation to be performed on the object Trigger –Impetus for a sent message Event –The process of a trigger sending a message that results in an operation
32
Chapter 1: An Introduction to Java and Program Design 32 Event Diagrams Graphically represents relationships among event and operations Useful for designing event-driven programs
33
Chapter 1: An Introduction to Java and Program Design 33 Operation, message, trigger, and event
34
Chapter 1: An Introduction to Java and Program Design 34 Encapsulation The process of hiding the implementation details of an object from its user The user is shielded from the system’s complexity Information hiding provides access to an object only through its messages Objects can be modified without requiring application modification
35
Chapter 1: An Introduction to Java and Program Design 35 Inheritance An efficient way to reuse code by defining a subclass as an extension of another class The subclass inherits all the data and functions of the superclass The subclass has at least one attribute or method that differs from its superclass
36
Chapter 1: An Introduction to Java and Program Design 36 Polymorphism Allows an instruction to be given to an object using a generalized command The same command will obtain different results depending on the object receiving the command The specific actions internal to the object are encapsulated from the user
37
Chapter 1: An Introduction to Java and Program Design 37 Abstraction Simplifies complex reality by modeling classes appropriate to the problem Works at most appropriate level of inheritance for a given aspect of the problem Abstraction is also achieved through composition
38
Chapter 1: An Introduction to Java and Program Design BREAK 38
39
Chapter 1: An Introduction to Java and Program Design Object Oriented Design ID classes to be written ID behaviors (methods) for each class Determine relationship between classes Write the interface (public method headers) for each class Implement the methods Many applications have similar object types –Low level basic component(s) –Collection of low level components –Controlling object that puts everything together –Display object (could be a GUI) 39
40
Chapter 1: An Introduction to Java and Program Design Class Relationships IS-A – inheritance –Subclass IS-A Superclass –Subclass extends Superclass –Example: Pencil IS-A Writing Utencil HAS-A – composition –ClassA HAS-A instance variable of type ClassB –Example: MixedNumber HAS-A FractionNumber Independent Class – class that needs no other classes for its implementation 40
41
Chapter 1: An Introduction to Java and Program Design Types of Development Bottom up – implement simplest classes/methods first –Methods/classes added one at a time, tested as you go –A driver class (a class that is meant to run other methods/classes) is often used for testing Top down – implement the highest level controlling object first –Stub – a dummy (empty) method used for testing – stands in until the actual method is written 41
42
Chapter 1: An Introduction to Java and Program Design Designing Methods Procedural (Functional) Abstraction – using “helper methods” to cut down on repeated code –Example – reduce() Information Hiding – declaring instance variables (data/attributes) and helper methods as private 42
43
Chapter 1: An Introduction to Java and Program Design 43
44
Chapter 1: An Introduction to Java and Program Design Program Analysis Correctness – a program works for every possible set of test data Assertions – a precise statement about a program at any given point –If an assertion is proved to be true, then program is working correctly Precondition: for any piece of code (method, loop, block), a statement of what is true immediately before execution of that code Postcondition: for any piece of code (method, loop, block), a statement of what is true immediately after execution of that code 44
45
Chapter 1: An Introduction to Java and Program Design Efficiency An efficient algorithm is economical in the use of: –CPU time – the number of machine operations required to carry out the algorithm (arithmetic operations, comparisons, swaps, etc.) –Memory – the number and complexity of the variables used Algorithms are also graded on readability 45
46
Chapter 1: An Introduction to Java and Program Design Example 1 – Write a program that simulates a game of Bingo. There should be at least 2 players, each of whom has a bingo card, and a caller who calls the numbers Nouns to consider: Basic Objects: Collection: Controller: Display: 46
47
Chapter 1: An Introduction to Java and Program Design Example 2 – Write a program that maintains an inventory of stock items for a small store. Nouns to consider: Basic Objects: Collection: Controller: Display: 47
48
Chapter 1: An Introduction to Java and Program Design Testing and Debugging bug debugging Kinds of Errors: –Syntax/Semantic error – compilation errors –Run-time error –Logic error Error message vs. warning message
49
Chapter 1: An Introduction to Java and Program Design BREAK 49
50
Chapter 1: An Introduction to Java and Program Design 50 What Is the Java SDK? The Java Software Development Kit (SDK) is a programming package to develop Java applications The Java Runtime Environment (JRE) provides the tools to deploy Java applications
51
Chapter 1: An Introduction to Java and Program Design 51 Features of the Standard Edition The Java Compiler –Converts code into bytecode The Java Virtual Machine –Contains an interpreter to execute the bytecode The Java API –The standard set of packages available in Java –java.sun.com The Java Applet Viewer –Mini browser to display Java applets
52
Chapter 1: An Introduction to Java and Program Design 52 Other Java Development Tools VATE (value-added text editor) –Color codes elements and numbers lines –Examples: TextPad, Eclipse, Visual Studio
53
Chapter 1: An Introduction to Java and Program Design What is computer hardware? Computer hardware are the physical components of the computer.
54
Chapter 1: An Introduction to Java and Program Design Input/Output Devices Input/Output devices provide communication between user and hardware. –Input Devices Keyboard Mouse Scanner –Output Devices Monitor Speakers Printer
55
Chapter 1: An Introduction to Java and Program Design Processors and Memory Central Processing Unit (CPU) –Performs basic functions, millions and billions of times per second (brains of the computer) Random-Access Memory –Stores data used by the CPU (before and after processing)
56
Chapter 1: An Introduction to Java and Program Design Data Storage Data storage uses a variety of media. Capacity is measured in bits and bytes: –A bit represents the on or off state of a transistor (symbolized by a 1 or a 0). –A byte is eight bits. –A kilobyte is 2 10 or 1,024 bytes. –A megabyte is 1,048,576 bytes.
57
Chapter 1: An Introduction to Java and Program Design Hard Drives The hard drive is the primary storage device in a computer. Hard drives are: –Long term, rewritable storage –Large capacity –Inexpensive –Fixed media (relatively difficult to move from one computer to another)
58
Chapter 1: An Introduction to Java and Program Design Removable Media Some storage devices are more portable: –CD/DVD Medium capacity Inexpensive Easy to transport from one computer to another –Flash, Zip, USB drives Differing capacities Differing price per MB
59
Chapter 1: An Introduction to Java and Program Design Computer Software Software can be divided into two categories: –Systems software includes operating systems, compilers, and utilities. –Application software runs on top of an operating system.
60
Chapter 1: An Introduction to Java and Program Design What is an operating system? An operating system (OS) manages the hardware and software on a computer system. An OS: –Manages memory and hardware resources –Allocates resources to applications –Provides a consistent interface for applications
61
Chapter 1: An Introduction to Java and Program Design Operating Systems UNIX/Linux –Multiuser OS –Multitasking –Runs on many types of hardware –Modular tools Mac OS –First mainstream graphical user interface –Icons (pictures) and mouse replaced command line interface DOS/Windows –DOS gained popularity with first PCs –Windows provided graphical interface to DOS –Windows later separated itself from DOS underpinnings
62
Chapter 1: An Introduction to Java and Program Design Low-Level Languages Low-level programming languages use simple commands to communicate with the CPU: –Machine language (most basic language of the CPU) –Assembly language (human readable, but close to machine language)
63
Chapter 1: An Introduction to Java and Program Design High-Level Languages High-level languages can be procedural or object-oriented: –Procedural languages use a step-by-step process to solve a problem. Basic, Pascal, C –Object-oriented languages model problems using objects that correspond to real-world counterparts. Smalltalk, C++, Java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.