250 likes | 441 Views
Week 1. Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham. Week 1 Topics. 1.1.1 What is Programming? 1.1.2 The Anatomy of a Computer 1.1.3 Translating Human-Readable Programs to Machine Code 1.1.4 The Java Programming Language
E N D
Week 1 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham
Week 1 Topics 1.1.1 What is Programming? 1.1.2 The Anatomy of a Computer 1.1.3 Translating Human-Readable Programs to Machine Code 1.1.4 The Java Programming Language 1.1.5 The Structure of a Simple Program 1.1.6 Compiling and Running a Java Program 1.1.7 Errors 1.1.8 Algorithms
1.1.1 What is Programming? • A computer must be programmed to perform tasks. Different tasks require different programs. • A computer executes a sequence of very basic operations in rapid succession • A program can balance a checkbook; process words; play a game • A sophisticated program is composed of extremely primitive operations
1.1.1 What is Programming Cont. • A typical primitive operation may be: • Put a red dot onto this screen position • Send the letter A to the printer • Get a number from this location in memory • Add up two numbers • If this value is negative, continue the program at that instruction • The creation of a sophisticated program may require a team of many highly skilled programmers, graphic artists, and other professionals
1.1.2 The Anatomy of a Computer • At the heart of a computer lies the central processing unit (CPU). It consists of a single chip (integrated circuit) or a small number of chips. • The computer keeps data and programs in storage. There are two kinds of storage. • Primary storage, also called random-access memory (RAM) or simply memory, is fast and expensive and is made from memory chips. It loses all its data when the power is turned off.
1.1.2 The Anatomy of a ComputerCont. • Secondary storage, usually a hard disk, provides less expensive storage and persists without electricity. • Some computers are self-contained units, whereas others are interconnected through networks. Home computers are usually connected to the internet via a dialup or broadband connection. • Most computers have removable storage devices that can access data or programs such as zip drives, flash drives, tapes or compact discs (CDs)
1.1.2 The Anatomy of a ComputerCont. • The CPU, the RAM and the electronics controlling the hard disk and other devices are interconnected through a set of electrical lines called a bus • A motherboard contains the CPU, the RAM and connectors to peripheral devices (monitor, speakers, printer, mouse, keyboard, etc.) • The CPU reads machine instructions from memory which direct it to communicate with memory, secondary storage and peripheral devices
1.1.3 Translating Human-Readable Programs to Machine Code • A CPU executes machine instructions • Machine instructions are very simple, are encoded as numbers and stored in memory, and can be executed very quickly • Because machine instructions are encoded as numbers, it is difficult to write programs in machine code • High-level programming languages such as Java allow you to describe tasks at a higher conceptual level than machine code
1.1.3 Translating Human-Readable Programs to Machine Code Cont. • These machine instructions: • Load the contents of memory location 40 • Load the value 100 • If the first value is greater than the second value, continue with the instructions that is stored in memory location 240 might be encoded something like this: 21 40 16 100 163 240
1.1.3 Translating Human-Readable Programs to Machine Code Cont. • This Java code (high-level instructions): if (intRate > 100) System.out.println(“Interest rate error”); might be compiled and translated into 21 40 16 100 163 240 . . . • A compiler is a sophisticated program that translates programs written in a high-level language into machine code
1.1.4 The Java Programming Language • Java was originally designed for programming consumer devices, but it was first successfully used to write internet applets • Java was designed to be safe and portable, benefiting both internet users and students • Java has a very large library • There are packages in the Java library for graphics, user interface design, cryptography, networking, sound, database storage, and many other purposes.
1.1.4 The Java Programming Language Cont. • Even expert Java programmers cannot hope to know the contents of all of the packages • Focus on learning those parts of the library that you need for your programming projects • Although you will learn a good deal about the Java language and the most important library packages, keep in mind that the central goal of this course is not to make you memorize Java minutiae, but to teach you how to think about programming.
1.1.5 The Structure of a Simple Program File HelloTester.java: public class HelloTester { public static void main(String[] args) { // Display a greeting to the console window System.out.println(“Hello World!”); } } Output: Hello, World!
1.1.5 The Structure of a Simple Program Cont. • Java is case sensitive. You must be careful about distinguishing between upper and lowercase letters. • Lay out your programs so that they are easy to read • Classes are the fundamental building blocks of Java programs • Almost every Java application contains a class with a main method. When the application starts, the instructions in the main method are executed.
1.1.5 The Structure of a Simple Program Cont. • Each class contains definitions of methods • Each method contains a sequence of instructions • Use comments to help human readers understand your program • A method is called by specifying an object, the method name, and the method parameters • A String is a sequence of characters enclosed in quotation marks
1.1.5 The Structure of a Simple Program Cont. Calling a Method: System.out.println(“Hello World!”); Method Parameters Object
1.1.6 Compiling and Running a Java Program • You use an editor to write your Java program statements • When you save your program statements, it becomes sourcecode and will have a .java extension, for example HelloTester.java • A successful compile of your source code will create virtual machine instructions called class files and will have a .class extension, for example HelloTester.class
1.1.6 Compiling and Running a Java Program Cont. From Source Code to Running Program Editor Source File Compiler Class files and Library files Virtual Machine Running Program
1.1.7 Errors • A syntax error is a violation of the rules of the programming language. The compiler detects syntax errors. • System.ouch.println(“Hello World!”); • A logic error causes a program to take an action that the programmer did not intend. You must test your programs to find logic errors. • System.out.println(“Hello Word!”);
1.1.8 Algorithms • An algorithm is a sequence of steps that is unambiguous, executable and terminating • Common algorithms we will cover later on in the course includes finding a minimum or maximum number in a list, sorting a list, searching a list • Pseudocode is an informal description of a sequence of steps for solving a problem.
1.1.8 Algorithms Cont. • The Program Development Process: • Understand the problem • Develop and describe an algorithm • Test the algorithm with different inputs • Translate the algorithm into Java • Compile and test your program
1.1.8 Algorithms Cont. • Example: • A salesperson is paid $5.00 for a transaction (sale) less than $100, 15% of sales for a sale between $100 and $1000 inclusive, and $200 for a sale greater than $1000 • Read a list that contains a salesperson name, transaction amount, and write to a commission amount the calculated commission
1.1.8 Algorithms Cont. for each entry in the list: if the sale amount is less than 100 then write 5.00 to the commission field if the sale amount is between 100 and 1000 then write .15 x sale amount to the commission field if the sale amount is greater than 1000 then write 200.00 to the commission field end conditional logic end repetition logic
Reference: Big Java 4th Edition by Cay Horstmann 1.1.1 What is Programming? (section 1.1 in Big Java) 1.1.2 The Anatomy of a Computer (section 1.2 in Big Java) 1.1.3 Translating Human-Readable Programs to Machine Code (section 1.3 in Big Java) 1.1.4 The Java Programming Language (section 1.4 in Big Java) 1.1.5 The Structure of a Simple Program (section 1.5 in Big Java) 1.1.6 Compiling and Running a Java Program (section 1.6 in Big Java) 1.1.7 Errors (section 1.7 in Big Java) 1.1.8 Algorithms (section 1.8 in Big Java) PC Block Diagram from: http://www.vaughns-1-pagers.com/computer/pc-block-diagram.htm