ICOM 4015: Advanced Programming

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

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

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

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

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"

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

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

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

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

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

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

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

Week 2: Data and Output

Software and Programming 1

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

Introduction. Chapter1

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

Chapter 2: Programming Concepts

Full file at

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.

Basic Programming Language Syntax

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

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

Introduction to Computers and Java

Software and Programming 1

Course Outline. Introduction to java

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

Java Coding 3. Over & over again!

COMP 202 Java in one week

Introduction to Computers and Java

Oct Decision Structures cont d

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

Building Java Programs

Introduction to Computers and Java

BASICS.

Introduction to Computers and Java

Mobile App:IT. Methods & Classes

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

Building Java Programs

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

CHAPTER 1 Introduction to Computers and Java

Chapter 1 Introduction to Computers, Programs, and Java

Module 1: Introduction to Computers, Programs, and Java

Pace University. Fundamental Concepts of CS121 1

Lesson 01 Introduction

Building Java Programs

Java Basics Lecture: January 26, 2012 (On-line Lecture 1)

Chapter 1 Introduction to java

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

COMP163. Introduction to Computer Programming. Introduction and Overview of the Hardware

COMP 110 Project 1 Programming Project Warm-Up Exercise

Chapter 1 Introduction to Computers, Programs, and Java

Java Language. Programs. Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs.

Java Bytecode (binary file)

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

4 Programming Fundamentals. Introduction to Programming 1 1

CS 11 java track: lecture 1

Chapter 1 Lab Algorithms, Errors, and Testing

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

CS 177 Recitation. Week 1 Intro to Java

Lecture Set 2: Starting Java

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

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

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

Lecture Set 2: Starting Java

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

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

Object-Oriented Programming

BITG 1113: Introduction To Computers And Programming Language LECTURE 1 LECTURE 1 1

Introduction Basic elements of Java

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

Java Programming Constructs Java Programming 2 Lesson 1

CS 231 Data Structures and Algorithms, Fall 2016

COMP 202 Java in one week

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

Introduction. Introduction to OOP with Java. Lecture 01: Introduction to OOP with Java - AKF Sep AbuKhleiF -

CP122 CS I. Chapter 1: Introduction

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

SDKs - Eclipse. SENG 403, Tutorial 2

CS11 Java. Fall Lecture 1

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

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Introduction to Java Programming

1. Java is the fastest growing programming language in the world.

Transcription:

ICOM 4015: Advanced Programming Lecture 1 Reading: Chapter One: Introduction

Chapter 1 Introduction

Chapter Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code and high level programming languages To become familiar with your computing environment and your compiler To compile and run your first Java program To recognize syntax and logic errors To write pseudocode for simple algorithms

What Is Programming? Computers are programmed to perform tasks Different tasks = different programs Program Sequence of basic operations executed in succession Contains instruction sequences for all tasks it can execute Sophisticated programs require teams of highly skilled programmers and other professionals

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.

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.

Self Check 1.3 Can a computer program develop the initiative to execute tasks in a better way than its programmers envisioned? Answer: No the program simply executes the instruction sequences that the programmers have prepared in advance.

The Anatomy of a Computer Central processing unit Chip Transistors Storage Primary storage: Random-access memory (RAM) Secondary storage: e.g. hard disk Removable storage devices: e.g.: floppy disks, tapes, CDs Peripherals Executes very simple instructions Executes instructions very rapidly General purpose device

Central Processing Unit

A Memory Module with Memory Chips

A Hard Disk

A Motherboard

Schematic Diagram of a Computer

The ENIAC

Self Check 1.4 Where is a program stored when it is not currently running? Answer: In secondary storage, typically a hard disk.

Self Check 1.5 Which part of the computer carries out arithmetic operations, such as addition and multiplication? Answer: The central processing unit.

Machine Code Generally, machine code depends on the CPU type However, the instruction set of the Java virtual machine (JVM) can be executed on many types of CPU Java Virtual Machine (JVM) a typical sequence of machine instructions is: 1. Load the contents of memory location 40. 2. Load the value 100. 3. If the first value is greater than the second value, continue with the instruction that is stored in memory location 240.

Machine Code Machine instructions are encoded as numbers: 21 40 16 100 163 240 Compiler translates high-level language to machine code

Self Check 1.6 What is the code for the Java virtual machine instruction Load the contents of memory location 100? Answer: 21 100

Self Check 1.7 Does a person who uses a computer for office work ever run a compiler? Answer: No a compiler is intended for programmers, to translate high-level programming instructions into machine code.

The Java Programming Language Simple Safe Platform-independent ( write once, run anywhere ) Rich library (packages) Designed for the internet

Applet on a Web Page

Java Versions Version Year Important New Features 1.0 1996 1.1 1997 Inner classes 1.2 1998 Swing, Collections 1.3 2000 Performance enhancements 1.4 2002 Assertions, XML 5 2004 Generic classes, enhanced for loop, auto-boxing, enumerations 6 2006 Library improvements 7 2010 Small language changes and library improvements

Self Check 1.8 What are the two most important benefits of the Java language? Answer: Safety and portability.

Self Check 1.9 How long does it take to learn the entire Java library? Answer: No one person can learn the entire library it is too large.

ch01/hello/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 } Program Run: Hello, World!

The Structure of a Simple Program: Class Declaration Classes are the fundamental building blocks of Java programs: public class HelloPrinter starts a new class 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

The Structure of a Simple 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 public static void main(string[] args) { }... declares a main method

The Structure of a Simple Program: Comments The first line inside the main method is a comment: // Display a greeting in the console window Compiler ignores any text enclosed between // and end of the line Use comments to help human readers understand your program

The Structure of a Simple Program: Statements The body of the main method contains statements inside the curly brackets ({}) Each statement ends in a semicolon (;) Statements are executed one by one String: a sequence of characters enclosed in double quotation marks: "Hello, World!" Our method has a single statement: System.out.println("Hello, World!"); which prints a line of text: Hello, World

The Structure of a Simple Program: Method Call System.out.println("Hello, World!"); is a method call A method call requires: 1. The object that you want to use (in this case, System.out) 2. The name of the method you want to use (in this case, println) 3. Parameters enclosed in parentheses (()) containing any other information the method needs (in this case, "Hello, World!")

Syntax 1.1 Method Call

The Structure of a Simple Program: Strings String: a sequence of characters enclosed in double quotation marks: "Hello, World!"

Self Check 1.10 How would you modify the HelloPrinter program to print the words "Hello," and "World!" on two lines? Answer: System.out.println("Hello,"); System.out.println("World!");

Self Check 1.11 Would the program continue to work if you omitted the line starting with //? Answer: Yes the line starting with // is a comment, intended for human readers. The compiler ignores comments.

Self Check 1.12 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.

Editing a Java Program Use an editor to enter and modify the program text Java is case-sensitive Be careful to distinguish between upper- and lowercase letters Lay out your programs so that they are easy to read

Compiling and Running a Java Program The Java compiler translates source code into class files that contain instructions for the Java virtual machine A class file has extension.class The compiler does not produce a class file if it has found errors in your program The Java virtual machine loads instructions from the program's class file, starts the program, and loads the necessary library files as they are required

HelloPrinter in a Console Window

HelloPrinter in an IDE

From Source Code to Running Program

Self Check 1.13 Can you use a word processor for writing Java programs? Answer: Yes, but you must remember to save your file as plain text.

Self Check 1.14 What do you expect to see when you load a class file into your text editor? Answer: A sequence of random characters, some funny looking. Class files contain virtual machine instructions that are encoded as binary numbers.

Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!); Syntax error Run-time error: Causes the program to take an action that the programmer did not intend Examples: System.out.println("Hello, Word!"); System.out.println(1/0); Logic error

Error Management Strategy Learn about common errors and how to avoid them Use defensive programming strategies to minimize the likelihood and impact of errors Apply testing and debugging strategies to flush out those errors that remain

Self Check 1.15 Suppose you omit the // characters from the HelloPrinter.java program but not the remainder of the comment. Will you get a compile-time error or a run-time error? Answer: A compile-time error. The compiler will not know what to do with the word Display.

Self Check 1.16 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.

Self Check 1.17 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.

Algorithms Algorithm: A sequence of steps that is: unambiguous executable terminating Algorithm for deciding which car to buy, based on total costs: For each car, compute the total cost as follows: annual fuel consumed = annual miles driven / fuel efficiency annual fuel cost = price per gallon x annual fuel consumed operating cost = 10 x annual fuel cost total cost = purchase price + operating cost If total cost1 < total cost2 Choose car1 Else Choose car2

Pseudocode Pseudocode: An informal description of an algorithm: Describe how a value is set or changed: total cost = purchase price + operating cost Describe decisions and repetitions: For each car operating cost = 10 x annual fuel cost total cost = purchase price + operating cost Use indentation to indicate which statements should be selected or repeated Indicate results: Choose car1

Program Development Process

Self Check 1.18 Investment 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? Algorithm: Start with a year value of 0 and a balance of $10,000. Repeat the following steps while the balance is less than $20,000. Add 1 to the year value. Multiply the balance value by 1.05 (a 5 percent increase). 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

Self Check 1.19 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.