Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Similar documents
Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

Midterms Save the Dates!

COMP 202 Java in one week

ECE 122 Engineering Problem Solving with Java

Midterms Save the Dates!

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CompSci 125 Lecture 02

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Program Fundamentals

Midterms Save the Dates!

An overview of Java, Data types and variables

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Midterms Save the Dates!

CS 177 Recitation. Week 1 Intro to Java

Midterms Save the Dates!

Midterms Save the Dates!

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

2 rd class Department of Programming. OOP with Java Programming

Midterms Save the Dates!

DM550 Introduction to Programming part 2. Jan Baumbach.

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

CS11 Java. Fall Lecture 1

Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

Computer Components. Software{ User Programs. Operating System. Hardware

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

CSC 1214: Object-Oriented Programming

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Perl Basics. Structure, Style, and Documentation

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Building Java Programs. Introduction to Programming and Simple Java Programs

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

CS 11 java track: lecture 1

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

Lab # 2. For today s lab:

Midterms Save the Dates!

Full file at

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

3. Java - Language Constructs I

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Java Bytecode (binary file)

Chapter 2: Programming Concepts

PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

Computer Components. Software{ User Programs. Operating System. Hardware

Building Java Programs. Chapter 1: Introduction to Java Programming

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Basic Programming Language Syntax

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

Midterms Save the Dates!

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

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

The Java Language Rules And Tools 3

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

CS 231 Data Structures and Algorithms, Fall 2016

Lecture Set 2: Starting Java

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

Pace University. Fundamental Concepts of CS121 1

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

Programming Lecture 3

Lecture Set 2: Starting Java

CS1 Lecture 3 Jan. 18, 2019

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

Lecture 1: Basic Java Syntax

The Java Language The Java Language Reference (2 nd ed.) is the defining document for the Java language. Most beginning programming students expect

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

printf( Please enter another number: ); scanf( %d, &num2);

Midterms Save the Dates!

These are notes for the third lecture; if statements and loops.

Java Programming. Atul Prakash

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Week - 01 Lecture - 04 Downloading and installing Python

Survey #2. Assignment #3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Static Interface Types.

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

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

COMP 110 Project 1 Programming Project Warm-Up Exercise

assembler Machine Code Object Files linker Executable File

MP 3 A Lexer for MiniJava

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello {

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CS221: Algorithms and Data Structures. Asymptotic Analysis. Alan J. Hu (Borrowing slides from Steve Wolfman)

Language Fundamentals Summary

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Machine vs. High-Level Languages Interpreters and Compilers Writing a Simple Java Program Readings Your textbook is Big Java (3rd Ed). This week s reading: Chapter 1. Next Week: Ch 2.1-2.5, Ch 4.1-4.2. Lecture 1 Some slides borrowed from Kurt Eiselt and Steve Wolfman. Some learning goals from Beth Simon. Administrative Stuff You will need: a Campus-Wide Login (CWL), and an account with the CS department. You can find instructions for getting all of these on WebCT. This course is on the new Vista server: http://www.vista.ubc.ca A good starting point is the course website, which will also get you to WebCT: http://www.ugrad.cs.ubc.ca/~cs111/ Labs and Tutorials Labs and tutorials start next Monday. However, do Lab #0 (on WebCT) on your own, this week. Lab #1 will be available soon, too, so you should read that to be ready for next week. You must be enrolled in a lab section. If the lab sections you need are full, go to ICICS Room 201 and see a Computer Science undergrad advisor! Learning Goals By the end of class today you will be able to Explain why it s useful to have both machine/assembly language and high-level computer languages (and how they are different) Describe what compilers and interpreters do Type in, compile, and run a simple Java computer program. HINT: You should actually do this on your own before the next class! Navigate around a simple Java program and recognize some basic building blocks, such as the class header, methods, identifiers, strings, numerical constants, and comments. Use white space and comments to make programs easier to read. Last Time Computer science gets physical artifacts to aid or exhibit thinking! Basics of how a computer is organized, how it works. Binary representation: everything is just 0s and 1s Memories: fixed size chunks of data that are accessed via numerical addresses Processors/CPUs: execute instructions one-at-a-time a time You can make the computer do anything you want, if you program it correctly But you must write in a language the computer understands! 1

George and Stephen go to France George is American. He knows only English. Stephen is Canadian. He is bilingual in English and French. How can George communicate in France? George and Stephen go to France How can George communicate in France? 1. If he wants to communicate quickly, then Stephen can interpret translating French to English and English to French on-the-fly. 2. If there s a lot of stuff to translate (e.g., a speech, or a long document), then Stephen can translate the whole thing at once. Now, George can read it whenever he wants. George and Stephen go to France Translations can be combined: In the Louvre, they see inscriptions in Egyptian hieroglyphics. A museum sign gives a French translation. Stephen interprets the sign for George. Health Education in Remote Areas In remote areas of the world, there are languages spoken by small groups of people, and also a national language spoken by the mainstream, e.g.,: Many native languages vs. Spanish in Latin America Minority languages vs. Mandarin in China Regional languages vs. Hindi or English in India How do you provide health info to the isolated? George can understand the hieroglyphics. Health Education in Remote Areas Local Interpreter Local Interpreter Original Info (English) Localized Field Manual (Spanish) English- Spanish Translation Local Interpreter Nahuatl Speaker Maya Speaker Zapoteco Speaker Machine Language This is the native language of a computer. Remember: Everything is in binary! Each instruction does very little. The computer does them very fast. Each kind of processor has its own machine language, e.g.: x86 (Intel, AMD), Windows and new Apples PowerPC (Freescale, IBM), older Apples, Sony PS3 SPARC (Sun), used in Sun servers Many more 2

Machine Language Examples: An instruction to read the content of a memory location: x86: movl %eax,(%ebx) 10001011 11000000 00011011 SPARC: ld [%r8+0],%r9 % 11 01001 00000 01000 1 0000000000000 (Examples not guaranteed to be perfect. It s very error-prone to do these by hand!) (Assembly Language) Assembly language is just an easier-to-read version of machine language: x86: movl %eax,(%ebx) 10001011 11000000 00011011 SPARC: ld [%r8+0],%r9 % 11 01001 00000 01000 1 0000000000000 (Almost never do humans write machine language. It s just a nit-picky translation.) High-Level Language A High-Level Language is a computer language designed to be easier for humans: a=b+c; Must be translated into machine language so the computer can understand d it. Who does the translation? The computer! High-Level Language A High-Level Language is a computer language designed to be easier for humans: a=b+c; Must be translated into machine language so the computer can understand d it. You could translate it as you go (interpreter). You could translate it in advance (compiler). Java is the high-level language we ll use. Modern, widely used, portable, safe. Compilation Example On an Intel-based Linux PC, the a=b+c example turns into: movl -8(%ebp), %eax addl -12(%ebp), %eax movl %eax, -16(%ebp) Interpreters and Compilers An interpreter translates the high-level language into machine language on-the-fly, executing the instructions as it goes. A compiler translates the high-level language program all at once in advance. Both compilers and interpreters are themselves computer programs. Which is better? (Remember George and Stephen in France?) 3

java JVM on MacOS java JVM on Windows Java Does Both! Your Program.java (Java) Your Program.class (Java Bytecodes) javac Compiler java JVM on Unix Windows PC Macintosh SPARC Server A Simple Java Program public static void main(string[ ] args) Controlling the Computational Behavior A procedure or algorithm is a collection of instructions in some meaningful order that results in useful behaviour on behalf of the device that executes the instructions. When the instructions are written in a symbolic language that can be executed by a computer, the procedure is called a computer program. A process is what happens when a computer follows a program - it s a procedure in execution. Procedures and algorithms Computer people often use the words procedure and algorithm interchangeably...we will too. An algorithm is a finite procedure written in a fixed symbolic vocabulary governed by precise instructions moving in discrete steps, 1, 2, 3,... whose execution requires no insight, cleverness, intuition, intelligence, or perspicuity and that sooner or later comes to an end David Berlinski in The Advent of the Algorithm Procedures and algorithms Here s why we get frustrated when we start to learn to write program to make computers do stuff: An algorithm is a finite procedure written in a fixed symbolic vocabulary governed by precise instructions moving in discrete steps, 1, 2, 3,... whose execution requires no insight, cleverness, intuition, intelligence, or perspicuity and that sooner or later comes to an end We don t have a lot of practice at being precise! Procedures and algorithms Here s why we get frustrated when we start to learn to write program to make computers do stuff: An algorithm is a finite procedure written in a fixed symbolic vocabulary governed by precise instructions moving in discrete steps, 1, 2, 3,... whose execution requires no insight, cleverness, intuition, intelligence, or perspicuity and that sooner or later comes to an end We don t have a lot of practice at being stupid! 4

How to avoid frustration Practice, Practice, Practice It takes a lot of practice to learn to be precise enough to make a computer do what you want It takes a lot of practice to keep from assuming that the computer is smarter than it really is It takes a lot of practice to get good at this stuff What do you see? Comments Comments Anything from // to end of line, or Anything between /* and Comments are ignored by Java compiler. Ignored by computer, important for humans! Comments White Space (e.g., Indentation) 5

White Space White space means the spaces, line breaks, and tabs in your program. Java ignores white space (with very few exceptions), so you can format any way you like. Good Formatting Use white space to format programs so they re easier for people to understand. (Appendix A is a good start.) Example Write Readable Programs! Use comments, formatting, and other ways to make your program easy to understand. TAs will be grateful. Other programmers will be grateful. You will be grateful. A program is both an expression of an idea, and a way to make a computer do a task. (Interesting legal/philosophical issues ) White Space (e.g., Indentation) Strings Strings Anything in quotation marks is a string. Beware: Java doesn t use smart quotes. This tells Java that you really want exactly those characters (letters, digits, whatever), just like that. t As if you re quoting someone. 6

Strings (and Reserved Words) and Reserved Words Word-like things in the program. Special rules, which we ll learn very soon. are usually names of things, like HelloTester Reserved words are special words that have special meanings to Java, which we ll learn later, like class and public (and Reserved Words) Curly Braces public static void main(string[ ] args) Curly Braces, Square Brackets, Parentheses Java uses three different pairs of parenthesis-like symbols to group things: Curly Braces: Square Brackets: [ ] Parentheses: () We ll learn the rules for what to use where later. Curly braces usually group big things over many lines; parentheses usually group on one line. Must be balanced, just like parentheses in math: v(w+x(y-z)) 7

Curly Braces Numbers There were no numbers in our simple program, but you ll often see numbers in Java programs, and they usually mean what they look like: 3 3.14159 Operators There weren t many operators in our simple Java program, but whenever you see symbols like + or =, they are probably Java operators. Operators tell Java to perform some action. You ll learn most of the Java operators in this course. A Simple Java Program public static void main(string[ ] args) Multiple Statements System.out.print( I am feeling chatty ); System.out.println( today. ); Another Example public class ManyOreos public static void main (String[] args) System.out.println("Feed me more Oreos!"); // I'm being sloppy. You'll learn better ways to do this later. javax.swing.joptionpane.showmessagedialog (null,"give me a cookie!"); javax.swing.joptionpane.showmessagedialog (null,"i want cookie!"); javax.swing.joptionpane.showmessagedialog (null,"cookie!!!!"); 8

Remember the Program Counter Program Counter was the special part of a processor that points to the instruction to execute. Fetch-Decode-Execute: one instruction at a time. Java works the same way. You can use your finger to point to where you are in your program. Even expert programmers do this sometimes! More on public class Oreo public static void main (String[] args) System.out.println ("Feed me more Oreos!"); Words we use when writing programs are called identifiers except those inside the quotes Sample Java Application Program public class Oreo public static void main (String[] args) System.out.println ("Feed me more Oreos!"); Kurt made up identifier Oreo Sample Java Application Program public class Oreo public static void main (String[] args) System.out.println ("Feed me more Oreos!"); Other programmers chose identifiers System, out, and println They wrote printing program. Part of huge library of useful programs that comes with Java By using the right identifier, we find what we want. Sample Java Application Program public class Oreo public static void main (String[] args) System.out.println ("Feed me more Oreos!"); Special identifiers in Java called reserved words don t use them in other ways 9

Reserved Words Get familiar with these (Appendix G) But you don t need to memorize all 52 for exam abstract do if private throw boolean double implements protected throws break else import public transient byte enum instanceof return true case extends int short try catch false interface static void char final long strictfp volatile class finally native super while const float new switch continue for null synchronized default goto package this Identifier must Start with a letter and be followed by Zero or more letters and/or digits Digits are 0 through 9. Letters are the 26 lowercase letters in English alphabet the 26 uppercase letters in English alphabet plus the $ and _ also alphabetic characters from other languages Identifier must Start with a letter and be followed by Zero or more letters and/or digits Digits are 0 through 9. Letters are the 26 characters in English alphabet both uppercase and lowercase plus the $ and _ also alphabetic characters from other languages Which of the following are not valid identifiers? username user_name $cash 2ndName first name user.age _note_ note2 Identifier must Start with a letter and be followed by Zero or more letters and/or digits Digits are 0 through 9. Letters are the 26 characters in English alphabet both uppercase and lowercase plus the $ and _ also alphabetic characters from other languages Which of the following are not valid identifiers? username user_name $cash 2ndName first name user.age _note_ note2 Java is case sensitive Oreo oreo OREO 0reo are all different identifiers, so be careful common source of errors in programming Java is case sensitive Oreo oreo OREO 0reo are all different identifiers, so be careful common source of errors in programming are these all valid identifiers? 10

Creating identifiers in your Java programs Remember other people read what you create Make identifiers meaningful and descriptive for both you and them No limit to how many characters you can put in your identifiers but don t get carried away public class ReallyLongNamesWillDriveYouCrazyIfYouGoOverboard public static void main (String[] args) System.out.println ("Enough already!"); Laws versus Customs/Conventions In real life, some things are illegal: murder, shoplifting, running stop signs, etc. Other things are legal, but social norms dictate that we don t do them, because life is better for everyone if we agree to follow the rules: cutting in line, talking on your cell phone in class/movie/concert, being rude, etc. Java has conventions, too. Questions? Rules for identifiers are like laws: If you break them, the compiler won t let your program compile: E.g.,: 3am is not a legal identifier. Java also has conventions. These are the normal way Java programmers do things. It s good to follow them. E.g.,: CamelCase: Makes it easier to read. Don t start identifiers with $ or _ We ll learn more as the course goes on 11