ICOM 4015: Advanced Programming Lecture 1 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter One: Introduction.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. GCOC – A.P. Computer Science A College Board Computer Science A Topics Covered Program Design - Read and understand.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Your First Java Program: HelloWorld.java
Chapter 1: Introduction
Chapter 1 These slides for CSE 110 Sections are based in part on the textbook-authors’ slides, which are copyright by the authors. The authors state that.
COSC 120 Computer Programming
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
1 Fall 2008ACS-1903 Chapter 1 Topics Java History Java Programs Why Program? Computer Systems: Hardware and Software Programming Languages What Is a Program.
CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Microsoft Visual Basic 2005 CHAPTER 1 Introduction to Visual Basic 2005 Programming.
Computer Science 1710: Object- Oriented Programming 1 Spring 2007 Instructor: Andrew Vardy.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
Topics Introduction Hardware and Software How Computers Store Data
Week 1 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Copyright © 2013 by John Wiley & Sons. All rights reserved. INTRODUCTION CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct. 15,
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction.
Week 1 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.
Chapter 1 Introduction. Goal to learn about computers and programming to compile and run your first Java program to recognize compile-time and run-time.
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 1 – Introduction.
Programming With C.
Introduction to Computer Systems and the Java Programming Language.
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 1 – Introduction ( ปรับปรุง )
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 1 - Introduction.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CHAPTER 1 INTRODUCTION. CHAPTER GOALS To understand the activity of programming To understand the activity of programming To learn about the architecture.
Fall 2006Slides adapted from Java Concepts companion slides1 Introduction Advanced Programming ICOM 4015 Lecture 1 Reading: Java Concepts Chapter 1.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Chapter 1 Introduction. Chapter Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
CHAPTER 1 INTRODUCTION. CHAPTER GOALS To understand the activity of programming To learn about the architecture of computers To learn about machine code.
Chapter 1 Introduction. Components of a Computer CPU (central processing unit) Executing instructions –Carrying out arithmetic and logical operations.
Computer and Programming. Computer Basics: Outline Hardware and Memory Programs Programming Languages and Compilers.
Chapter 1 - Introduction. Announcements Info Sheet Web Site: Lab Hours NotecardsPictures.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Chapter 1 – Introduction
Department of Computer Science,
Introduction To recognize compile-time and run-time errors
John Woodward A Simple Program – Hello world
CSC201: Computer Programming
GC101 Introduction to computer and program
Introduction to Visual Basic 2008 Programming
Chapter Goals To learn about computers and programming
Chapter 1 – Introduction
Java programming lecture one
Goals To learn about computers and programming
Slides by Donald W. Smith
CS190/295 Programming in Python for Life Sciences: Lecture 1
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
Computer Programming-1 CSC 111
Presentation transcript:

ICOM 4015: Advanced Programming Lecture 1 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter One: Introduction

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 1 – Introduction

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 Chapter Goals

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 What Is Programming? PROGRAM = INSTRUCTIONS ENCODED IN SOME PROGRAMMING LANGUAGE (PL) PROGRAM ALLOW ONE HARDWARE TO SERVE MULTIPLE PURPOSES: word processors, spreadsheets, video, music, games, phone, calendar, ….

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. They must be decoded Sometimes by the processor (low level PL) Some other times by another program (high level PL). Eventually all programs translated into a lowest-level PL (machine-code) and are interpreted by the HW microprocessor Two types of decoders/translators: Interpreters: Executes as it translates Compilers: Translates to lower PL which is then translated again Java takes a hybrid approach Programs written in Java Then compiled in to Bytecode Then interpreted by Java Virtual Machine (JVM) JVM is a program previously compiled into native machine code JVM Embedded in other programs such as web browers How do Programs Run?

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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.1

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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.2

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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. Self Check 1.3

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 The Anatomy of a Computer

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Central Processing Unit

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A Memory Module with Memory Chips

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A Hard Disk

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A Motherboard

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Schematic Diagram of a Computer

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. The ENIAC

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

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

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Generally, machine code depends on the CPU type However, the instruction set of the Java virtual machine (JVM), known as bytecode, can be executed on many types of CPU Java Virtual Machine (JVM) – An interpreter for Java Bytecode A typical sequence of machine instructions is: 1.Load the contents of memory location Load the value If the first value is greater than the second value, continue with the instruction that is stored in memory location 240. Machine Code vs. Java Bytecode

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Machine instructions are encoded as numbers: Compiler translates high-level language to machine code Machine Code

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. What is the code for the Java virtual machine instruction “Load the contents of memory location 100”? Answer: Self Check 1.6

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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. Self Check 1.7

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Simple Safe Platform-independent (“write once, run anywhere”) Rich library (packages) Designed for the internet The Java Programming Language

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Applet on a Web Page

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. VersionYearImportant New Features Inner classes Swing, Collections Performance enhancements Assertions, XML 52004Generic classes, enhanced for loop, auto-boxing, enumerations 62006Library improvements 72010Small language changes and library improvements Java Versions

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. What are the two most important benefits of the Java language? Answer: Safety and portability. Self Check 1.8

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. How long does it take to learn the entire Java library? Answer: No one person can learn the entire library – it is too large. Self Check 1.9

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. public class HelloPrinter { public static void main(String[] args) { // Display a greeting in the console window System.out.println("Hello, World!"); } Program Run: Hello, World! ch01/hello/HelloPrinter.java

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 Java Program

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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: main Method

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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: Comments

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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: Statements

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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!" ) The Structure of a Simple Program: Method Call

Syntax 1.1 Method Call Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. String: a sequence of characters enclosed in double quotation marks: "Hello, World!" The Structure of a Simple Program: Strings

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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.10

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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.11

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. What does the following set of statements print? System.out.print("My lucky number is"); System.out.println( ); Answer: The printout is My lucky number is12 It would be a good idea to add a space after the is. Self Check 1.12

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 Editing a Java Program

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 Compiling and Running a Java Program

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. HelloPrinter in a Console Window

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. HelloPrinter in an IDE

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Compile-time error: A violation of the programming language rules that is detected by the compiler Example : System.out.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 Errors

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 Develop an Error Management Strategy

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. From Source Code to Running Program

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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.13

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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. Self Check 1.14

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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.15

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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.16

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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. Self Check 1.17

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 Algorithms

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 Pseudocode

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Program Development Process

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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, , , , ,736 Self Check 1.18

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 × = $ b.If not, 1.Compute the difference: (number of minutes) – Multiply that difference by Add $ Multiply the total by That is the answer. Self Check 1.19