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

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

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

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

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

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

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

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

Midterms Save the Dates!

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

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

CS 11 java track: lecture 1

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CHAPTER 1 Introduction to Computers and Java

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

Midterms Save the Dates!

BASICS.

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

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

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

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

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

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

COMP-202: Foundations of Programming

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

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

CS 231 Data Structures and Algorithms, Fall 2016

COMP Computer Basics. Yi Hong May 13, 2015

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

CompSci 125 Lecture 02

What did we talk about last time? Examples switch statements

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

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

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

C02: Overview of Software Development and Java

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

Computer Science is...

Week 2: Data and Output

Software and Programming 1

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Java Bytecode (binary file)

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

Midterms Save the Dates!

COMP-202 Unit 0: Course Details

Lesson 01 Introduction

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

Introduction Basic elements of Java

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

CMSC131. Introduction to your Introduction to Java. Why Java?

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

Lecture Set 2: Starting Java

Lecture 1 - Introduction (Class Notes)

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

4. Java Project Design, Input Methods

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

Lecture Set 2: Starting Java

CS 106 Introduction to Computer Science I

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

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

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

Programming Language Concepts: Lecture 2

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

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

CS1 Lecture 3 Jan. 18, 2019

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Anatomy of a Java Program: Comments

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

Mr. Monroe s Guide to Mastering Java Syntax

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Introduction to Computers and Java

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

COMP 202 Java in one week

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

Introduction to Computers and Java

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

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

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

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

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

CS 251 Intermediate Programming Java Basics

Lecture 1: Introduction to Java

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

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

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

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

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

DM550 Introduction to Programming part 2. Jan Baumbach.

AP Computer Science Unit 1. Programs

Elementary Programming

First Java Program - Output to the Screen

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

COMP 102: Computers and Computing

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

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

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

Transcription:

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

Assignment Due Date Assignment 1 is now due on Tuesday, Jan 20 th, 11:59pm. Quiz 1 is still due on Tuesday, Jan 13 th, 11:59pm. In general, quizzes and assignments will be due on Tuesday at 11:59pm (unless we note otherwise). 2

Midterm Conflicts You have one if you are taking: BIOL 205 BIOL 303 PHAR 301 This class has priority according to McGill midterm policy. Please ask your other instructor for an alternate midterm time! 3

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

This Lecture Programming languages and Java Hello World! our first Java program Variables String operations 5

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? 6

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

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 8

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) 9

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 10

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. 11

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! 12

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; Or in Scratch: 13

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) 14

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. 15

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 16

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 17

Dun Dun Dun!!!! OUR FIRST JAVA PROGRAM 18

Hello World! public class HelloWorld { } public static void main(string[] args) { } System.out.println("Hello, world!"); How to run this? Go to command line: > javac HelloWorld.java > java HelloWorld Your IDE (e.g., DrJava, Eclipse) will make this easier, but do this at least once to understand. 19

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 } 20

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! 21

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 } 22

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. 23

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. 24

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 25

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. 26

Your Turn Write a Java program that prints the following, in order. Yesterday was Wednesday, Wednesday Today is Thursday, Thursday We-we-we so excited We gonna have COMP-202 today Adapted from Jey and Wilson, (2011) 27

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 28

How to store things VARIABLES: DECLARING AND ASSIGNING TO 29

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. 30

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!) 31

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. 32

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? 33

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; 34

Your Turn 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 35

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! */ 36

Back to Our Problem How to read input from the user? Use the following magical (for now) incantation: At the top of the file: import java.util.scanner; In the main method: Scanner scanner = new Scanner(System.in); Then, call this method to read a line: scanner.nextline(); Problem: need to store it in a variable! 37

Putting It All Together import java.util.scanner; public class Repeater { } public static void main(string[] args) { } System.out.println("Say something!"); // create a Scanner to read some input Scanner scanner = new Scanner(System.in); String s = scanner.nextline(); 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. 38

Comparing and concatenating Strings STRING OPERATIONS 39

What Can We Do With Strings? String comparison Are two strings the same? String concatenation Put together multiple Strings These are more examples of methods provided by Java so that we don t need to write our own methods to do these common operations. 40

String Comparison Check whether two Strings are equal: somestring.equals(otherstring) This is another method one that returns a value! (true or false) More on these true or false values next week! 41

Examples of String Comparison "Abc".equals("Abc") "Abc".equals("A") "ABC".equals("Abc") Can also compare String variables String myname = "Jackie"; String yourname = "Bob"; System.out.println(myName.equals(myName)); System.out.println(myName.equals(yourName)); 42

String Concatenation Put together two Strings with the + operator. firststring + secondstring System.out.println("This is " + "one sentence."); String secondstring; secondstring = "COMP-202"; System.out.println("This is " + secondstring); secondstring = "Sparta!"; System.out.println("This is " + secondstring); 43

Summary Natural vs. programming languages compiler JVM Java code -> byte code -> machine code Classes and methods main method Displaying output Getting input Variables String comparison and concatenation 44