Introduction to Computers, Programs, and Java PDF

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Summary

This document provides an introduction to computers, programs, and the Java programming language. It covers basic concepts like CPU, memory, storage, and input/output devices. The document also discusses the role of software in controlling computer actions, and the use of different programming languages.

Full Transcript

Chapter 1 Introduction to Computers, Programs, and Java 1 What is a Computer? A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices. Video links https://www.youtube.com/watch?v=...

Chapter 1 Introduction to Computers, Programs, and Java 1 What is a Computer? A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices. Video links https://www.youtube.com/watch?v=HB4I2CgkcCo Very detailed : https://www.youtube.com/watch?v=d86ws7mQYIg Bus Storage Communication Input Output Memory CPU Devices Devices Devices Devices e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, and Tape and NIC Mouse Printer 2 CPU ▪ 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. – The speed of the CPU has been improved continuously. – An Intel Pentium 4 Processor runs at 3 gigahertz (1 gigahertz is 1000 megahertz). Bus Storage Communication Input Output Memory CPU Devices Devices Devices Devices e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, and Tape and NIC Mouse Printer 3 Moore’s Law ▪ Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately every two years. ▪ The observation is named after Gordon Moore, the co-founder of Fairchild Semiconductor and Intel, whose 1965 paper described a doubling every year in the number of components per integrated circuit, and projected this rate of growth would continue for at least another decade. ▪ In 1975, he revised the forecast to doubling every two years. – The period is often quoted as 18 months because of Intel executive David House, who predicted that chip performance would double every 18 months 4 CPU 5 Memory ▪ Memory is to store data and program instructions for CPU to execute. ▪ A memory unit is an ordered sequence of bytes, each holds eight bits. ▪ A program and its data must be brought to memory before they can be executed. ▪ 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. Bus Storage Communication Input Output Memory CPU Devices Devices Devices Devices e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, and Tape and NIC Mouse Printer 6 How Data is Stored? Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. 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. 7 How Data is Stored? Memory address Memory content...... 2000 01001010 Encoding for character ‘J’ 2001 01100001 Encoding for character ‘a’ 2002 01110110 Encoding for character ‘v’ 2003 01100001 Encoding for character ‘a’ 2004 00000011 Encoding for number 3 ▪ A one page word document might take 20KB (20,000 bytes) ▪ 50 pages would take 1 MB (1 million bytes) ▪ 50,000 pages would need 1 GB (1 billion bytes) 8 Storage Devices ▪ 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 (hard disks and floppy disks), ▪ Optical disc drives CD and DVD and ▪ Universal Serial bus (USB) flash drives. Bus Storage Communication Input Output Memory CPU Devices Devices Devices Devices e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, and Tape and NIC Mouse Printer 9 Input and Output Devices ▪ I/O devices let the user communicate with the computer ▪ The most common input devices are the keyboard and mouse ▪ The most common output devices are monitors and printers Bus Storage Communication Input Output Memory CPU Devices Devices Devices Devices e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, and Tape and NIC Mouse Printer 10 Communication Devices ▪ A regular modem uses a phone line and can transfer data in a speed up to 56,000 bps (bits per second). ▪ A DSL (digital subscriber line) also uses a phone line and can transfer data in a speed 20 times faster than a regular modem. ▪ A cable modem uses the TV cable line maintained by the cable company. A cable modem is as fast as a DSL. ▪ Network interface card (NIC) is a device to connect a computer to a local area network (LAN). The LAN is commonly used in business, universities, and government organizations. A typical type of NIC, called 10BaseT, can transfer data at 10 mbps (million bits per second). ▪ Wireless networking is now extremely popular in homes, business and schools. Laptops are equipped with a wireless adapter that enables the computer to connect to the LAN and Internet Bus Storage Communication Input Output Memory CPU Devices Devices Devices Devices e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, and Tape and NIC Mouse Printer 11 Programs Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages. 12 Programming Languages Machine Language Assembly Language High-Level Language 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. Programming 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 13 Programming Languages Machine Language Assembly Language High-Level Language 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. For example, to add two numbers, you might write an instruction in assembly code like this: ADD 2,3 result 14 Programming Languages Machine Language Assembly Language High-Level Language The high-level languages are English-like and easy to learn and program. 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; 15 Popular High-Level Languages Language Description Ada Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada language was developed for the Department of Defense and is used mainly in defense projects. BASIC Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily by beginners. C Developed at Bell Laboratories. C combines the power of an assembly language with the ease of use and portability of a high-level language. C++ C++ is an object-oriented language, based on C. C# Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft. COBOL COmmon Business Oriented Language. Used for business applications. FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications. Java Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform- independent Internet applications. Pascal Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a simple, structured, general-purpose language primarily for teaching programming. Python A simple general-purpose scripting language good for writing short programs. Visual Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop Basic graphical user interfaces. 16 Interpreting/Compiling Source Code A program written in a high-level language is called a source program or source code. Because 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 an interpreter or a compiler. 17 Interpreting Source Code An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, and then executes it right away, as shown in the following figure. Note that a statement from the source code may be translated into several machine instructions. 18 Compiling Source Code A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed, as shown in the following figure. 19 Operating Systems The operating system (OS) is a program that manages and controls a computer’s activities. The popular operating systems for general-purpose computers are Microsoft Windows, Mac OS, and Linux. Application programs, such as a Web browser or a word processor, cannot run unless an operating system is installed and running on the computer. 20 Operating Systems The major tasks of an OS are: – Controlling and monitoring system activities Handling i/o, keeping track of files on storage devices, controlling peripheral devices, responsible for security, Allocating and assigning system resources Determines what resources a program needs and allocates/assigns them to run the program – Scheduling operations Multiprogramming, multithreading and multiprocessing 21 Why Java? The answer is that Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. The future of computing is being profoundly influenced by the Internet, and Java promises to remain a big part of that future. Java is the Internet programming language. – Java is a general purpose programming language. – Java is the Internet programming language. 22 Java, Web, and Beyond Java can be used to develop standalone applications. Java can be used to develop applications running from a browser. Java can also be used to develop applications for hand-held devices. Java can be used to develop applications for Web servers. 23 History of Java Java goes back to 1991 when a group of engineers at Sun, led by Patrick Naughton and James Gosling, wanted to design a small computer language to be used in consumer devices like cable TV switchboxes. The language had to be small and generate tight code as the devices did not have a lot of power or memory. Also, because manufacturer’s used different CPUs, it had to be platform independent. 24 History of Java To meet these requirements a portable language that generated intermediate code for a hypothetical (virtual) machine was needed. The intermediate code could then be used on any machine that had the correct interpreter. Gosling called the language Oak (tree outside his window) but another language already had that name so it was changed to Java. Intro to Java / Liang / Chapter 1 Slide 25 History of Java Their first product was an extremely intelligent remote control (power of a SPARCStation in a box 6”x4”x4”) but no one at Sun was interested in producing it nor were any of the standard consumer electronics companies. Bids for other projects also failed and no one was interested in buying its technology. Intro to Java / Liang / Chapter 1 Slide 26 History of Java In the meantime, the WWW was growing by leaps and bounds. In 1994, most people were using Mosaic as their browser. – It was developed in part by an undergrad student, Marc Andreessen, at the Univ of Illinois for $6.85 an hour. Later he moved on to become one of the cofounders and CTO at Netscape. Intro to Java / Liang / Chapter 1 Slide 27 History of Java Gosling saw an opportunity to develop a “real cool browser” that needed some of the features that were included in Java. – The HotJava browser was written in Java to show off the power of Java and how code could be executed within web pages (applets). It was previewed at SunWorld ’95 and the Java craze began. In the fall of 1995, Netscape decided to make their browser Java enabled and other companies followed suit. Intro to Java / Liang / Chapter 1 Slide 28 James Gosling James Gosling and Sun Microsystems – Awarded the ACM Software System Award in 2003 for original contributions and implementation of the Java programming language, widely used in computer software programs because of its security, portability and networking capabilities. 29 Companion Website Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic www.cs.armstrong.edu/liang/JavaCharacteristics.pdf 30 JDK Editions Java Standard Edition (SE) – can be used to develop client-side standalone applications or applets. Java Enterprise Edition (EE) – can be used to develop server-side applications such as Java servlets, Java ServerPages, and Java ServerFaces. Java Micro Edition (ME) – can be used to develop applications for mobile devices such as cell phones. This book uses SE to introduce Java programming All Java software available on Oracle http://www.oracle.com/technetwork/java/index.html. 31 Popular Java IDEs NetBeans https://netbeans.org/ Eclipse https://www.eclipse.org/ DrJava https://sourceforge.net/projects/drjava/ Department webpage for IDEs http://www.sci.brooklyn.cuny.edu/~goetz/java/ 32 A Simple Java Program // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Note: Clicking the green button displays the source code Welcome with interactive animation. You can also run the code in a browser. Internet connection is needed for this button. 33 Creating, Compiling, and Running Programs 34 Compiling & Interpreting On command line – Compiling: javac myProgram.java – Interpreting: java myProgram Java source file Java bytecode file The compiler translates a source code program into a platform- independent, intermediate language called Java bytecodes. The interpreter parses and runs each Java bytecode (machine code) instruction on the computer. Compilation happens just once; interpretation occurs each time the program is executed 35 Executing Applications Java bytecode help make "write once, run anywhere" possible. You can compile your program into bytecode on any platform that has a Java compiler. The bytecodes can then be run on any implementation of the Java VM. As long as a computer has a Java VM, the same program written in the Java programming language can run on Windows, Solaris or on an iMac workstation 36 animation Trace a Program Execution Enter main method // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 37 animation Trace a Program Execution Execute statement // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 38 animation Trace a Program Execution // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } print a message to the console 39 Two More Simple Examples WelcomeWithThreeMessages Run ComputeExpression Run 40 Anatomy of a Java Program Class name Main method Statements Statement terminator Reserved words Comments Blocks 41 Class Name Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 42 Main Method Line 2 defines the main method. In order to run a class, the class must contain a method named main. The program is executed from the main method. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 43 Statement 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!“. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 44 Statement Terminator Every statement in Java ends with a semicolon (;). // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 45 Reserved words 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. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 46 Blocks A pair of braces in a program forms a block that groups components of a program. public class Test { public static void main(String[] args) { Class block System.out.println("Welcome to Java!"); Method block } } 47 Special Symbols Character Name Description {} Opening and closing Denotes a block to enclose statements. braces () Opening and closing Used with methods. parentheses [] Opening and closing Denotes an array. brackets // Double slashes Precedes a comment line. " " Opening and closing Enclosing a string (i.e., sequence of characters). quotation marks ; Semicolon Marks the end of a statement. 48 { …} // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 49 ( … ) // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 50 ; // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 51 // … // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 52 "…" // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 53 Programming Style and Documentation Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles 54 Appropriate Comments Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program. 55 Naming Conventions Choose meaningful and descriptive names. Class names: – Capitalize the first letter of each word in the name. For example, the class name ComputeExpression. 56 Proper Indentation and Spacing Indentation – Indent two spaces. Spacing – Use blank line to separate segments of the code. 57 Block Styles Use end-of-line style for braces. Next-line public class Test style { public static void main(String[] args) { System.out.println("Block Styles"); } } End-of-line style public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } } 58 Programming Errors Syntax Errors – Detected by the compiler Runtime Errors – Causes the program to abort Logic Errors – Produces incorrect result 59 Syntax Errors public class ShowSyntaxErrors { public static main(String[] args) { System.out.println("Welcome to Java); } } ShowSyntaxErrors Run 60 Runtime Errors public class ShowRuntimeErrors { public static void main(String[] args) { System.out.println(1 / 0); } } ShowRuntimeErrors Run 61 Logic Errors public class ShowLogicErrors { public static void main(String[] args) { System.out.println("Celsius 35 is Fahrenheit degree "); System.out.println((9 / 5) * 35 + 32); } } ShowLogicErrors Run 62

Use Quizgecko on...
Browser
Browser