1 / 48

Introductions to Java Programming

Introductions to Java Programming. Chapter 1. Introduction to Computers, Programs, and Java. To create (or develop) software, which is also called a program. Software contains the instructions that tell a computer what to do . Solving a problem using a programming language. Programming.

bayle
Télécharger la présentation

Introductions to Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introductions to Java Programming Chapter 1

  2. Introduction to Computers, Programs, and Java

  3. To create (or develop) software, which is also called a program. • Software contains the instructions that tell a computer what to do. • Solving a problem using a programming language Programming

  4. Each language was invented for a specific purpose – to build on the strengths of a previous language • To give the programmer a new and unique set of tools • There is no “best” language • Each has its own strengths and weaknesses Languages

  5. A computer consists of a CPU, memory, storage devices (hard disk, CDs, …), input devices (keyboard, scanner, …), output devices (monitor, printer, …) and communication devices (modems, network interface cards, …). A computer’s components are interconnected by a subsystem called a bus. What is a Computer?

  6. The central processing unit (CPU) is the brain of a computer. • It retrieves instructions from memory and executes them. • The CPU speed is measured in megahertz (MHz), with 1 megahertz equaling 1 million pulses per second. • Or in gigahertz (GHz), with 1 gigahertz equaling 1,000 megahertz (MHz) or 1 billion pulses per second. CPU

  7. The CPU has two components: a control unit and an arithmetic/logical unit. • Control Unit: controls and coordinates the actions of the other components • The arithmetic/logic unit: performs the numeric operations (adding, subtracting, multiplying, and dividing) and logical operations (comparisons). CPU

  8. Consists of an ordered sequence of bytes for storing programs as well as data that the program is working with. • It is the working area for executing a program. • Every byte in the memory has a unique address. • A memory byte is never empty, but its initial content may be meaningless to your program. • The current content of a memory byte is lost whenever new information is placed in it. • RAM – Random Access Memory: the memory can be access in any order • Generally speaking, the more RAM a computer has the faster it can operate, but there are limits to this rule of thumb. Memory

  9. Computers are really nothing more than a series of switches. • Computers use zeros and ones because digital devices have two stable states, which are referred to as zero – off, and one –on, by convention. • This switch is called a bit. • 8 bits make a byte. • Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits. • The programmers need not to be concerned about the encoding and decoding of data, which is performed automatically by the system based on the encoding scheme. The encoding scheme varies. For example, character ‘J’ is represented by 01001010 in one byte. A small number such as three can be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes. No two data can share or split a same byte. A byte is the minimum storage unit. How Data is Stored

  10. Memory is volatile, because information is lost when the power is off. Programs and data are permanently stored on storage devices and are moved to memory when the computer actually uses them. • There are three main types of storage devices: • Magnetic disk drives (tapes) • Optical disc drives (CD, DVD, and Blu Ray) • USB flash drives (acts like a portable hard drive) Drives are devices for operating a medium. The drive reads data from the medium and writes data onto the medium. Storage Devices

  11. Keyboard • Mouse • Monitor Input & Output Devices

  12. Resolution: The resolution specifies the number of pixels per square inch. Pixels (short for “picture elements”) are tiny dots that form an image on the screen. The resolution can be set manually. The higher the resolution, the sharper and clearer the image is. • dot pitch: The dot pitch is the amount of space between pixels. The smaller the dot pitch, the better the display. Monitor Resolution and Dot Pitch

  13. Dial-up modem: uses a phone line and can transfer data at a speed up to 56,000 bps (bits per second). • DSL – Digital Subscriber Line: also uses a standard phone line, transfers data 20 times faster than dial-up. Actual speed depends on the technology. • Cable modem: uses cable TV line, generally faster than DSL. • Wireless networking: enables a computer to connect to a local area network. Communication Devices

  14. Operating systems perform basic tasks, such as recognizing input from the keyboard, sending output to the monitor, keeping track of files and folders on storage devices, and controlling peripheral devices, such as disk drivers and printers. • It must ensure that different programs and users working at the same time do not interfere with each other. • It is also responsible for the security, ensuring that unauthorized users and programs do not access the system. Operating Systems

  15. Users and applications access the computer’s hardware via operating system. Operating Systems

  16. There are hundreds of programming languages. • Programming languages make is easier for humans to use. • Computers do not understand human languages, so programs must be written in a language that it can use. • We categorize Programming Languages into 3 groups: • Machine Language • Assembly Language • High-Level Language Programming Languages

  17. Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Program with native machine language is a tedious process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010 • It is the computer’s native language, it differs among different types of computers. Machine Language

  18. Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. Assembly Language

  19. Uses mnemonics to represent each of the machine-language instructions, such as add or sub. • For example, to add two numbers, you might write an instruction in assembly code like this: ADD F3 R1, R2, R3 • Assembly language is referred to as a low-level language, because it is close in nature to machine language and is machine dependent. Assembly Language

  20. High-level languages are human-like and easier to use. • The instructions are called statements. • For example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1415; High-Level Language

  21. A program written in a high-level language is called a source program. • Since a computer cannot understand a source program a source program must be translated into machine code for execution. • The translation can be done using another programming tool called a interpreter or a compiler. High-Level Language

  22. An interpreter reads one statement from the source code, translates it into the machine code or virtual machine code, and then executes it right away. • “One line at a time.” Interpreter

  23. A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed. • It translates the source program into a machine language program called an object program. • The object program is often then linked with other supporting library code before the object can be executed on the machine. Compiler

  24. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. • The bytecode can then run on any computer with a Java Virtual Machine, as shown below. • Java Virtual Machine is a software that interprets Java bytecode. Compiling Java Source Code

  25. Java can be written, compiled, and then run anywhere that has a JVM, Java Virtual Machine. • Has strict rules of usage. If you do not follow the rules when writing a program, the computer will not be able to understand it. • Has language specifications, syntax & semantics, that have to be followed. • Has reserved words, also called keywords, that have a specific meaning to the compiler and cannot be used for any other purpose. • Is case sensitive. • Has special characters. (See table 1.2 page 18) Java

  26. Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. • For example, when the compiler sees the word class, it understands that the word after class is the name for the class. • Other reserved words are public, static, and void. Reserved Words

  27. Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. • Examples of modifiers are public and static. • Other modifiers are private, final, abstract, and protected. • A public datum, method, or class can be accessed by other programs. • A private datum or method cannot be accessed by other programs. Modifiers are discussed in Chapter 6, “Objects and Classes.” Modifiers

  28. A statement represents an action or a sequence of actions. • The statement System.out.println("Welcome to Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!“ • Every statement in Java ends with a semicolon (;). Statements

  29. The class is the essential Java construct. • A class is a template or blueprint for objects. • To program in Java, you must understand classes and be able to write and use them. • The mystery of the class will continue to be unveiled throughout this book. For now, though, understand that a program is defined by using one or more classes. Classes

  30. What is System.out.println? • It is a method: a collection of statements that performs a sequence of operations to display a message on the console. • It can be used even without fully understanding the details of how it works. • It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!“ • You can call the same println method with a different argument to print a different message. Methods

  31. The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements; } main Method

  32. Application Program Interface • The library • Contains predefined classes and interfaces for developing Java programs. • Always expanding. http://www.oracle.com/technetwork/java/index.html API

  33. Java Development Toolkit • Consists of separate programs, each invoked from a command line, for developing and testing Java programs. • You can use any IDE, integrated development environment, for developing Java programs quickly. • Editing, compiling, building, debugging, and online help are integrated in one graphical user interface. • Enter source code in one window or open an existing file in a window, and then click a button to compile and run the program. JDK

  34. Listing 1.1 A Simple Java Program

  35. Creating, Compiling, and Running Programs

  36. animation Enter main method //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Trace a Program Execution

  37. animation Execute statement //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Trace a Program Execution

  38. animation //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Trace a Program Execution print a message to the console

  39. Programming style deals with what programs look like and how readable it is for us. • Documentation is the explanatory remarks, notes, and comments pertaining to a program. • Are important as coding. • Good programming style and documentation reduce the chance of errors and make programs easy to read. Programming Style and Documentation

  40. Include a summary at the beginning of the program that explains what the program does, its key features, and any unique techniques it uses. • In a long program, you should also include comments that introduce each major step and explain anything that is difficult to read. • It is important to make comments concise so that they do not crowd the program or make it difficult to read. Comments

  41. Line comments: begin with // • Use when making comments within a method • Block comments: begin with /* and end with */ • Javadoc comments: /** and end with */ • These can be extracted into an HTML file using the JDK’s javadoc command. • Use this type for commenting on an entire class or an entire method. • These comments must precede the class or the method header in order to be extracted into a javadoc HTML file. Comments

  42. A consistent indentation style makes programs clear and easy to read, debug, and maintain. • Indentation: used to illustrate the structural relationships between a program’s components or statements. • Java can read a program all on one line (assuming it is correct), humans have a hard time doing this. • Indent each subcomponent or statement at least 2 spaces more that the construct within which it is nested. • A single space should be added on both sides of a binary operator. System.out.println(3+4*4); System.out.println(3 + 4 * 4); Proper Indentation and Spacing Bad Style Good Style

  43. A block is a group of statements surrounded by braces. • There are two popular styles: • Next-line style • End-of-line style • Please see page 25, 1.10.3 Block Styles

  44. Are categorized into three types: • Syntax Errors • Runtime Errors • Logic Errors Programming Errors

  45. Errors that are detected by the compiler. • Result from errors in code construction, such as • mistyping a keyword, • Omitting some necessary punctuation • Or using an opening brace without a corresponding closing brace • Usually easy to detect, because the compiler tells you where they are and what caused them. Syntax Errors

  46. Runtime errors are errors that cause a program to terminate abnormally. • They occur while a program is running if the environment detects an operation that is impossible to carry out. • Input mistakes typically cause runtime errors. • Example: the program needs an integer (like 3), but the user inputs a letter (like w) • Dividing by zero is a runtime error. • Relatively easy to find since the program aborts at the error. Runtime Errors

  47. Logic errors occur when a program does not perform the way it was intended to. • Occurs for many different reasons. • Can be challenging. Logic Errors

More Related