Chapter 1 - Introduction. Copyright 2014 by John Wiley & Sons. All rights reserved. 1

Similar documents
ICOM 4015: Advanced Programming

From Algorithms to Architecture....a lightning introduction to computer architecture

Chapter Goals. Chapter One: Introduction. Prerequisites. What Is Programming?

2/9/2012. Chapter One: Introduction. Chapter Goals

CHAPTER INTRODUCTION. Final Draft Oct. 15, Slides by Donald W. Smith TechNeTrain.com. Copyright 2013 by John Wiley & Sons. All rights reserved.

Chapter Goals. Contents. 1.1 Computer Programs

Chapter Goals. Contents INTRODUCTION

CHAPTER INTRODUCTION

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

Which statement regarding computer programs is correct? Which statement best describes a computer program?

Introduction to Software Development (ISD) David Weston and Igor Razgon

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Course Outline. Introduction to java

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

Introduction to Computers and Java

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Introduction to Computers and Java

Introduction to Computers and Java

Computer Hardware and Software Introduction. Mr. Smith AP Computer Science A

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer? A Bit of History

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Introduction to Computers and Java

Java Coding 3. Over & over again!

Week 2: Data and Output

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Introduction. Chapter CHAPTER GOALS. To understand the activity of programming. To learn about the architecture of computers

SDKs - Eclipse. SENG 403, Tutorial 2

CHAPTER 1 Introduction to Computers and Java

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Imperative and Object Oriented Programming. Tutorial 1. Charlie Abela Department of Artificial Intelligence

9/1/2015. Chapter 2 Using Objects. Objects and Classes. Using Objects. Using Objects. Using Objects. Classes. Ch. 1/2 Lab Time

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CSCI 161: Introduction to Programming I Lab 1b: Hello, World (Eclipse, Java)

4 Programming Fundamentals. Introduction to Programming 1 1

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Building Java Programs

Welcome to CSSE 220. We are excited that you are here:

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

Java Programming Constructs Java Programming 2 Lesson 1

BASICS.

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Building Java Programs

Full file at

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

Chapter 2 First Java Programs

First Java Program - Output to the Screen

Software and Programming 1

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Full file at

Chapter 1. Introduction to Computers and Java Objects. Background information. » important regardless of programming language. Introduction to Java

Lesson 2: First Java Programs

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

COMP 110 Project 1 Programming Project Warm-Up Exercise

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Oct Decision Structures cont d

How to make a "hello world" program in Java with Eclipse *

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Big Java. Fifth Edition. Chapter 3 Fundamental Data Types. Cay Horstmann

Assignment 1 What is a Computer?

Introduction to Computer Programming

Introduction to Software Development (ISD) Week 3

double float char In a method: final typename variablename = expression ;

Section 2.2 Your First Program in Java: Printing a Line of Text

Pace University. Fundamental Concepts of CS121 1

Introduction. Chapter1

Contents at a Glance

Object-Oriented Programming

CS 177 Recitation. Week 1 Intro to Java

Building Java Programs. Introduction to Programming and Simple Java Programs

! Learn how to think like a computer scientist. ! Learn problem solving. ! Read and write code. ! Understand object oriented programming

Chapter 1 INTRODUCTION

Fundamentos de programação

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

4. Structure of a C++ program

Building Java Programs

COMP 202 Java in one week

Mobile App:IT. Methods & Classes

Software and Programming 1

Code Ninjas: Introduction to Computer Science. Macomb Science Olympiad Presented by Swati Dharia

SECTION 2 (Part B) Types And Components Of Computer Systems. Sophia Pratzioti

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Chapter One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

Foreword...xv Introduction...1 Part I: Buying and Getting Started with Your Computer...5

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

4. If the following Java statements are executed, what will be displayed?

Advanced Computer Programming

Problem Solving With C++ Ninth Edition

Transcription:

Chapter 1 - Introduction Copyright 2014 by John Wiley & Sons. All rights reserved. 1

Chapter Goals To learn about computers and programming To compile and run your first Java program To recognize compile-time and run-time errors To describe an algorithm with pseudocode Copyright 2014 by John Wiley & Sons. All rights reserved. 2

Computer Programs Computers are programmed to perform many different tasks. Computers execute very basic instructions in rapid succession. A computer program is a sequence of instructions and decisions. Programming is the act of designing and implementing computer programs. The physical computer and peripheral devices are collectively called the hardware. The programs the computer executes are called the software. Copyright 2014 by John Wiley & Sons. All rights reserved. 3

Self Check 1.1 What is required to play a music CD on a computer? Answer: A program that reads the data on the CD and sends output to the speakers and the screen. Copyright 2014 by John Wiley & Sons. All rights reserved. 4

Self Check 1.2 Why is a CD player less flexible than a computer? Answer: A CD player can do one thing play music CDs. It cannot execute programs. Copyright 2014 by John Wiley & Sons. All rights reserved. 5

Self Check 1.3 What does a computer user need to know about programming in order to play a video game? Answer: Nothing Copyright 2014 by John Wiley & Sons. All rights reserved. 6

The Anatomy of a Computer Central processing unit (CPU) performs Program control Data processing Storage Memory (Primary storage) Secondary storage Peripherals To interact with human users Networks Copyright 2014 by John Wiley & Sons. All rights reserved. 7

Central Processing Unit Figure 1 Central Processing Unit Copyright 2014 by John Wiley & Sons. All rights reserved. 8

A Hard Disk Figure 2 Hard Disk Copyright 2014 by John Wiley & Sons. All rights reserved. 9

Schematic Diagram of a Computer Figure 3 Schematic Design of a Personal Computer Copyright 2014 by John Wiley & Sons. All rights reserved. 10

Self Check 1.4 Where is a program stored when it is not currently running? Answer: In secondary storage, typically a hard disk. Copyright 2014 by John Wiley & Sons. All rights reserved. 11

Self Check 1.5 Which part of the computer carries out arithmetic operations, such as addition and multiplication? Answer: The central processing unit. Copyright 2014 by John Wiley & Sons. All rights reserved. 12

Self Check 1.6 A modern smartphone is a computer, comparable to a desktop computer. Which components of a smartphone correspond to those shown in Figure 5 (Schematic Diagram of a Computer)? Answer: A smartphone has a CPU and memory, like any computer. A few smartphones have keyboards. Generally, the touchpad is used instead of a mouse. Secondary storage is in the form of a solid state drive. Of course, smartphones have a display, speaker, and microphone. The network connection uses the wireless radio to connect to a cell tower. Copyright 2014 by John Wiley & Sons. All rights reserved. 13

Computers are Everywhere This transit card contains a computer. Copyright 2014 by John Wiley & Sons. All rights reserved. 14

The ENIAC The ENIAC Copyright 2014 by John Wiley & Sons. All rights reserved. 15

The Java Programming Language Safe Portable Platform-independent Distributed as instructions for a virtual machine Vast set of library packages Designed for the Internet Copyright 2014 by John Wiley & Sons. All rights reserved. 16

Applet on a Web Page Figure 4 An Applet for Visualizing Molecules Running in a Browser Window (http://jmol.sourceforge.net/) Copyright 2014 by John Wiley & Sons. All rights reserved. 17

Java Versions Copyright 2014 by John Wiley & Sons. All rights reserved. 18

Self Check 1.7 What are the two most important benefits of the Java language? Answer: Safety and portability. Copyright 2014 by John Wiley & Sons. All rights reserved. 19

Self Check 1.8 How long does it take to learn the entire Java library? Answer: No one person can learn the entire library it is too large. Copyright 2014 by John Wiley & Sons. All rights reserved. 20

Becoming Familiar with Your Programming Environment An editor is a program for entering and modifying text, such as a Java program. Java is case sensitive. Java compiler translates source code into class files. Class files contain instructions for the Java virtual machine. Copyright 2014 by John Wiley & Sons. All rights reserved. 21

Becoming Familiar with Your Programming Environment 1. Start the Java development environment. 2. Write a simple program. 3. Run the program. 4. Organize your work. Copyright 2014 by John Wiley & Sons. All rights reserved. 22

Becoming Familiar with Your Programming Environment Figure 5 Running the HelloPrinter Program in an Integrated Development Environment Copyright 2014 by John Wiley & Sons. All rights reserved. 23

Becoming Familiar with Your Programming Environment Figure 6 Running the HelloPrinter Program in a Console Window Copyright 2014 by John Wiley & Sons. All rights reserved. 24

Becoming Familiar with Your Programming Environment Figure 7 From Source Code to Running Program Copyright 2014 by John Wiley & Sons. All rights reserved. 25

Becoming Familiar with Your Programming Environment Figure 8 A Folder Hierarchy Copyright 2014 by John Wiley & Sons. All rights reserved. 26

Self Check 1.9 Where is the HelloPrinter.java file stored on your computer? Answer: The answer varies among systems. A typical answer: /home/dave/cs1/hello/helloprinter.java or c:\users\dave\workspace\hello\helloprinter.java Copyright 2014 by John Wiley & Sons. All rights reserved. 27

Self Check 1.10 What do you do to protect yourself from data loss when you work on programming projects? Answer: You back up your files and folders. Copyright 2014 by John Wiley & Sons. All rights reserved. 28

Section_4/HelloPrinter.java 1 public class HelloPrinter 2 { 3 public static void main(string[] args) 4 { 5 // Display a greeting in the console window 6 7 System.out.println("Hello, World!"); 8 } 9 } Copyright 2014 by John Wiley & Sons. All rights reserved. 29

Analyzing Your First Program: Class Declaration Classes are the fundamental building blocks of Java programs: Declaration of a class called HelloPrinter public class HelloPrinter In Java, every source file can contain, at most one public class. The name of the public class must match the name of the file containing the class: Class HelloPrinter must be contained in a file named HelloPrinter.java Copyright 2014 by John Wiley & Sons. All rights reserved. 30

Analyzing Your First Program: Methods Each class contains declarations of methods. Each method contains a sequence of instructions. A method contains a collection of programming instructions that describe how to carry out a particular task. A method is called by specifying the method and its arguments. Copyright 2014 by John Wiley & Sons. All rights reserved. 31

Analyzing Your First Program: main Method Every Java application contains a class with a main method When the application starts, the instructions in the main method are executed Declaring a main method public static void main(string[] args) { }... Copyright 2014 by John Wiley & Sons. All rights reserved. 32

Analyzing Your First Program: Statements The body of the main method contains statements. Our method has a single statement: System.out.println("Hello, World!"); It prints a line of text: Hello, World! Copyright 2014 by John Wiley & Sons. All rights reserved. 33

Analyzing Your First Program: Method Call A method call: System.out.println("Hello, World!"); A method call requires: 1. The method you want to use (in this case, System.out.println) 2. Any values the method needs to carry out its task enclosed in parentheses (in this case, "Hello, World!") The technical term for such values is arguments Copyright 2014 by John Wiley & Sons. All rights reserved. 34

Syntax 1.1 Java Program Copyright 2014 by John Wiley & Sons. All rights reserved. 35

Analyzing Your First Program: Strings String: a sequence of characters enclosed in double quotation marks: "Hello, World!" Copyright 2014 by John Wiley & Sons. All rights reserved. 36

Analyzing Your First Program: Printing You can print numerical values System.out.println(3 + 4); evaluates the expression 3 + 4 displays the number 7. System.out.println method prints a string or a number and then starts a new line. The sequence of statements System.out.println("Hello"); System.out.println("World!"); Prints two lines Hello World! There is a second method, System.out.print, that you can use to print an item without starting a new line Copyright 2014 by John Wiley & Sons. All rights reserved. 37

Self Check 1.11 How do you modify the HelloPrinter program to greet you instead? Answer: Change World to your name (here, Dave): System.out.println("Hello, Dave!"); Copyright 2014 by John Wiley & Sons. All rights reserved. 38

Self Check 1.12 How would you modify the HelloPrinter program to print the word "Hello" vertically? Answer: System.out.println("H"); System.out.println("e"); System.out.println("l"); System.out.println("l"); System.out.println("o"); Copyright 2014 by John Wiley & Sons. All rights reserved. 39

Self Check 1.13 Would the program continue to work if you replaced line 7 with this statement? System.out.println(Hello); Answer: No. The compiler would look for an item whose name is Hello. You need to enclose Hello in quotation marks: System.out.println("Hello"); Copyright 2014 by John Wiley & Sons. All rights reserved. 40

Self Check 1.14 What does the following set of statements print? System.out.print("My lucky number is"); System.out.println(3 + 4 + 5); Answer: The printout is My lucky number is12. It would be a good idea to add a space after the is. Copyright 2014 by John Wiley & Sons. All rights reserved. 41

Self Check 1.15 What do the following statements print? System.out.println("Hello"); System.out.println(""); System.out.println("World"); Answer: Hello a blank line World Copyright 2014 by John Wiley & Sons. All rights reserved. 42

Errors A compile-time error (syntax error) is a violation of the programming language rules detected by the compiler. System.ou.println("Hello, World!"); A run-time error (logic error) causes a program to perform an action that the programmer did not intend. System.out.println("Hello, Word!"); Copyright 2014 by John Wiley & Sons. All rights reserved. 43

Errors Exception - a type of run-time error Generates an error message from the Java virtual machine This statement System.out.println(1 / 0) Generates this run-time error message "Division by zero" Copyright 2014 by John Wiley & Sons. All rights reserved. 44

Self Check 1.16 Suppose you omit the "" characters around Hello, World! from the HelloPrinter.java program. Is this a compile-time error or a run-time error? Answer: This is a compile-time error. The compiler will complain that it does not know the meanings of the words Hello and World. Copyright 2014 by John Wiley & Sons. All rights reserved. 45

Self Check 1.17 Suppose you change println to printline in the HelloPrinter.java program. Is this a compile-time error or a run-time error? Answer: This is a compile-time error. The compiler will complain that System.out does not have a method called printline. Copyright 2014 by John Wiley & Sons. All rights reserved. 46

Self Check 1.18 Suppose you change main to hello in the HelloPrinter.java program. Is this a compile-time error or a run-time error? Answer: This is a run-time error. It is perfectly legal to give the name hello to a method, so the compiler won't complain. But when the program is run, the virtual machine will look for a main method and won't find one. Copyright 2014 by John Wiley & Sons. All rights reserved. 47

Self Check 1.19 When you used your computer, you may have experienced a program that "crashed" (quit spontaneously) or "hung" (failed to respond to your input). Is that behavior a compile-time error or a run-time error? Answer: It is a run-time error. After all, the program had been compiled in order for you to run it. Copyright 2014 by John Wiley & Sons. All rights reserved. 48

Self Check 1.20 Why can't you test a program for run-time errors when it has compiler errors? Answer: When a program has compiler errors, no class file is produced, and there is nothing to run. Copyright 2014 by John Wiley & Sons. All rights reserved. 49

Problem Solving: Algorithm Design Algorithm: A sequence of steps that is: unambiguous executable terminating Copyright 2014 by John Wiley & Sons. All rights reserved. 50

An Algorithm for Solving an Investment Problem The problem: You put $10,000 into a bank account that earns 5 percent interest per year. How many years does it take for the account balance to be double the original Calculating by hand Copyright 2014 by John Wiley & Sons. All rights reserved. 51

An Algorithm for Solving an Investment Problem - continued The steps in the algorithm Start with a year value of 0, a column for the interest, and a balance of $10,000. Repeat the following steps while the balance is less than $20,000 Add 1 to the year value. Compute the interest as balance x 0.05 (i.e., 5 percent interest). Add the interest to the balance. Report the final year value as the answer. Copyright 2014 by John Wiley & Sons. All rights reserved. 52

Pseudocode Pseudocode: An informal description of of a sequence of steps for solving a problem Describe how a value is set or changed: total cost = purchase price + operating cost Multiply the balance value by 1.05. Remove the first and last character from the word. Describe decisions and repetitions: If total cost 1 < total cost 2 While the balance is less than $20,000 For each picture in the sequence Copyright 2014 by John Wiley & Sons. All rights reserved. 53

Pseudocode - continued Use indentation to indicate which statements should be selected or repeated: For each car operating cost = 10 x annual fuel cost total cost = purchase price + operating cost Indicate results: Choose car1. Report the final year value as the answer. Copyright 2014 by John Wiley & Sons. All rights reserved. 54

From Algorithm to Programs Copyright 2014 by John Wiley & Sons. All rights reserved. 55

Self Check 1.21 Suppose the interest rate was 20 percent. How long would it take for the investment to double? Answer: 4 years: 0 10,000 1 12,000 2 14,400 3 17,280 4 20,736 Copyright 2014 by John Wiley & Sons. All rights reserved. 56

Self Check 1.22 Suppose your cell phone carrier charges you $29.95 for up to 300 minutes of calls, and $0.45 for each additional minute, plus 12.5 percent taxes and fees. Give an algorithm to compute the monthly charge for a given number of minutes. Answer: Is the number of minutes at most 300? a. If so, the answer is $29.95 1.125 = $33.70. b. If not, 1. Compute the difference: (number of minutes) 300. 2. Multiply that difference by 0.45. 3. Add $29.95. 4. Multiply the total by 1.125. That is the answer. Copyright 2014 by John Wiley & Sons. All rights reserved. 57

Self Check 1.23 Consider the following pseudocode for finding the most attractive photo from a sequence of photos: Pick the first photo and call it "the best so far. For each photo in the sequence If it is more attractive than the "best so far Discard "the best so far. Call this photo "the best so far. The photo called "the best so far" is the most attractive photo in the sequence. Is this an algorithm that will find the most attractive photo? Answer: No. The step If it is more attractive than the "best so far" is not executable because there is no objective way of deciding which of two photos is more attractive. Copyright 2014 by John Wiley & Sons. All rights reserved. 58

Self Check 1.24 Suppose each photo in Self Check 23 had a price tag. Give an algorithm for finding the most expensive photo. Answer: Pick the first photo and call it "the most expensive so far. For each photo in the sequence If it is more expensive than "the most expensive so far Discard "the most expensive so far. Call this photo "the most expensive so far. The photo called "the most expensive so far" is the most expensive photo in the sequence Copyright 2014 by John Wiley & Sons. All rights reserved. 59

Self Check 1.25 Suppose you have a random sequence of black and white marbles and want to rearrange it so that the black and white marbles are grouped together. Consider this algorithm Repeat until sorted Locate the first black marble that is preceded by a white marble, and switch them. What does the algorithm do with this sequence: Spell out the steps until the algorithm stops. Continued Copyright 2014 by John Wiley & Sons. All rights reserved. 60

Self Check 1.25 Answer: Copyright 2014 by John Wiley & Sons. All rights reserved. 61

Self Check 1.26 Suppose you have a random sequence of colored marbles. Consider this pseudocode: Repeat until sorted Locate the first marble that is preceded by a marble of a different color, and switch them. Why is this not an algorithm? Answer: The sequence doesn t terminate. Consider the input: The first two marbles keep getting switched. Copyright 2014 by John Wiley & Sons. All rights reserved. 62