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

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

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

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

CS 177 Recitation. Week 1 Intro to Java

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

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

COMP-202: Foundations of Programming

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

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

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

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

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

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

CHAPTER 1 Introduction to Computers and Java

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

Week 2: Data and Output

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

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

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

BASICS.

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

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

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

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

Lecture 1: Introduction to Java

CS 106 Introduction to Computer Science I

Lesson 01 Introduction

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

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

CS 231 Data Structures and Algorithms, Fall 2016

COMP 215: INTRO TO PROGRAM DESIGN. Prof. Chris Jermaine Chris Prof. Chris Dr. Chris

Software and Programming 1

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

Algorithms and Programming I. Lecture#12 Spring 2015

CS 11 java track: lecture 1

How to approach a computational problem

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

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

CS 251 Intermediate Programming Java Basics

CompSci 125 Lecture 02

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

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

Lesson 3: Accepting User Input and Using Different Methods for Output

What did we talk about last time? Examples switch statements

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

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

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

ICOM 4015: Advanced Programming

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Midterms Save the Dates!

Lab # 2. For today s lab:

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Primitive Data, Variables, and Expressions; Simple Conditional Execution

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

CSCI 1103: Introduction

Programming with Java

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

Computer Basics 1/6/16. Computer Organization. Computer systems consist of hardware and software.

Lecture 1 - Introduction (Class Notes)

Java Bytecode (binary file)

Midterms Save the Dates!

PROGRAMMING STYLE. Fundamentals of Computer Science I

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

At its simplest, a computer is a collection of wires, transistors and electrical components that are connected in a specific way When you send

Import Statements, Instance Members, and the Default Constructor

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

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

Conditionals, Loops, and Style

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

AP Computer Science A: Java Programming

Mr. Monroe s Guide to Mastering Java Syntax

CP122 CS I. Chapter 1: Introduction

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

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

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Software and Programming 1

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Introduction to Computers and Java

Getting started with 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 Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

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

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

First Java Program - Output to the Screen

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

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

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

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

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

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

Chapter 1: Introduction to Computers and Programming

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

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

Transcription:

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

Learn about cutting-edge research over lunch with cool profs January 18-22, 2015 11:30 AM Redpath Museum More information: www.mcgill.ca/science

What is an algorithm? Last Class What algorithms did we discuss last class involving binary numbers? 3

This Lecture Programming languages and Java Hello World! our first Java program Variables 4

Programming and Computers Programming is essentially providing a list of instructions to the computer to solve a specific problem or problems. Problem: computers require very specific instructions! What s wrong with just cook dinner? 5

1. Turn on the stove. 2. Add butter. 3. Crack an egg. 4. Wait until ready. More Details Still too vague! 6

Natural vs. Programming Languages He made her duck. He cooked duck for her. He cooked the duck that belongs to her. He created the duck that belongs to her (e.g., a toy). He caused her to lower her body suddenly. He magically transformed her into a mass of fowl. Her duck is doing something interesting, and he arrived in time to observe. Who are he, her, and the duck anyway? The problem of ambiguity 7

An Aside This (natural language processing or computational linguistics) is my research area Look for a course in the SOCS on this topic after you ve done more CS! (Likely to be an upper-year cross-listed course) 8

Why Are Computers So Dumb? Computers have no idea of context or our real world just a bunch of wires and electrical switches that encode data! Remember last class? Binary representations of objects 9

Components of A Computer Short-term memory (RAM): stores temporary data Processor (CPU): performs computations Output devices (e.g., monitors, speakers): displays information to users Input devices (e.g., keyboard, mouse): gets information from users Permanent storage (e.g., hard drive, DVD) Also components that connect other components. 10

Computer Instructions A processor can only perform a small set of tasks, which need to be combined to solve a complex problem. Load a number from a specific location in RAM into a register (workspace) Add two numbers in registers Multiply two numbers in registers Save a number into a specific location in RAM Remember all of this is done in binary! 11

Abstractions Over Data We create abstractions of the data and of the common operations to stay sane. Rather than: add eax,ebx Add the contents of register ebx to register eax We might instead write: numpeople = numpeople + numseen; 12

Compilers This means we need another computer program to translate a higher-level programming language into a form that the processor can work with. This program is called a compiler. Higher-level programming language compiler Processor instructions (machine code) 13

Bytecode Machine codes are dependent on the computer architecture Must recompile to different machine code for each architecture Java aims to be portable, so that you don t need do this for each architecture. It does this by introducing an intermediate level called bytecode. 14

Interpreting Bytecode There is yet another program, called the Java Virtual Machine (JVM), which converts bytecode into processor instructions as they are needed. Now, only the JVM is dependent on the architecture. Java compiler bytecode JVM processor instructions 15

Java vs. Other Languages We are going to study Java. However, the basic principles will be the same in most other common languages (e.g., C, C++, C#, Python, Perl, Matlab) Differences: What abstractions come with the language The exact grammar and syntax for writing in the language 16

Dun Dun Dun!!!! OUR FIRST JAVA PROGRAM 17

Hello World! public class HelloWorld { public static void main(string[] args) { System.out.println("Hello, world!"); } } How to run this in DrJava? Hit Run OR Type in the Interactions pane > run HelloWorld Your IDE (e.g., DrJava, Eclipse) compiles the program for you and interprets it. 18

Curly Braces public class HelloWorld { } public static void main(string[] args) { System.out.println("Hello, world!"); } Curly braces denote blocks of code. These blocks help us keep track of what part of code are related. You can tell which opening { matches which closing }, as the first { corresponds to the last } 19

Mismatched { Error public class HelloWorld { public static void main(string[] args) { System.out.println("Hello, world!"); } What happens if we try to compile the program with mismatched curly braces? What if we add an extra? The compiler gives a syntax error the program is not valid according to the grammar of Java! 20

class in Java public class HelloWorld { } public static void main(string[] args) { System.out.println("Hello, world!"); } One of the key units of organization in Java is called a class. Here, we are saying I want to create a class called HelloWorld, which is: public (more on this later), defined by what is in between the outer { and } 21

Methods public class HelloWorld { } public static void main(string[] args) { System.out.println("Hello, world!"); } On the second line, we have another { In this case, the code before defines a method, another key unit of organization in Java. Don t worry about public, static, void, String[], args for now. 22

Methods and Classes 1. Almost every line of code you write in Java will be inside a class. This helps keep things nicely organized. 2. Almost every line of code you write in Java will also be inside of a method. This also helps keep things nicely organized. 3. Every method you ever write, will be part of a class. 23

The main Method public class HelloWorld { } public static void main(string[] args) { System.out.println("Hello, world!"); } The main method is a very particular method. When you run a Java program, you actually run a Java class. The execution will start at the beginning of the method called main inside the class that you run 24

Statements public class HelloWorld { public static void main(string[] args) { System.out.println("Hello, world!"); } } Inside a method, you can put many statements or commands. All statements in Java end in a semi-colon. System.out.println( contents ); prints the contents to screen. " " means the stuff inside is a String of text. 25

Exercise Write a Java program that prints the following, in order. Hello, World! I m excited to be learning how to program. You ll see what I ll be able to do! 26

What About Input? We ve seen how to display output. What about taking input from the user? We need: A way to get the input from the user Somewhere to put the input that we get 27

How to store things VARIABLES: DECLARING AND ASSIGNING TO 28

Variable A variable is a place in memory reserved for storing a value. In Java, we give each variable a name, to make it easier to identify and a type to keep track of the kind of data we are storing. String myname; Essentially, you could say a variable is a typed label for a space of memory. 29

Variable Declaration These statements declare variables of various types. What are the types and the names? String myname; int myage; double myheight; boolean isstudent; You can name a variable anything that you want, as long as it only contains letters, numbers, and _, and doesn t start with a number. But you need to choose a clear, simple and appropriate name for readability (and full marks!) 30

Setting the Value of a Variable The assignment operator = sets the variable on the left-hand side to the value on the right-hand side. String myname; myname = "Jackie"; Or combined: String myname = "Jackie"; The expression can be complicated, so it will be evaluated for its value first. 31

Assignment Examples int x; int y; x = 10; y = 100 + 10; x = y + 3; What are the values of x and y at each line above? What happens if we try to use a variable we haven t declared, like z? 32

Not the = in Math Confusingly, this = is actually an operation the action of setting the variable on the LHS to the value on the RHS. It is not a statement of equivalency! Why don t the following make sense? 3 = x; 2 * x = 3 * y + 5; Why does the following make sense? x = x + 2; 33

int x; Variable Declaration x 34

int x; x = 1; Variable Assignment 1 x 35

Variable Reassignment int x; x = 1; x = x + 2; 3 x 36

Exercise Write a piece of code that swaps the value of two int numbers x, and y. So, if x is 5 and y is 4 before the piece of code is run, x is 4 and y is 5 after. Fill in the : int x = 5; int y = 4; // by here, x is 4 and y is 5 37

Important Remark: Comments By the way, you can (and should!) make annotations in your code about what it does so that it is easy to understand. These are known as comments. // This will be ignored by the compiler /* So will this, if you need to write a comment that spans multiple lines. */ /* It is important to comment, especially in tricky parts of the code. But don't go overboard and comment every single line! */ 38

Reading Input We can pass some input to the program! > run SomeProgram word1 word2 word3 You access the words (as many as you want) in the main method using args[0], args[1], args[2], Then, you can store it in your own variable, print it, or whatever you want! 39

Putting It All Together public class PrintFirstWord { } public static void main(string[] args) { String s = args[0]; } System.out.println(s); Note that this line prints the contents of what is stored in variable s, not the String s, because there are no quotations. 40

Exercise Write a program that: Takes one String as the input, Stores it in a String variable that you declare: Then prints Your word is X, where X is the String that was passed as input. Tip: use + to concatenate two Strings. e.g., ab + cd gives you abcd 41

Summary Natural vs. programming languages compiler JVM Java code -> byte code -> machine code Classes and methods main method Displaying output Getting input from user Variables 42