SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Similar documents
Building Java Programs

Building Java Programs

Building Java Programs

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Topic 4 Expressions and variables

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

Variables Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Introduction to Computer Programming

CS 106A, Lecture 5 Booleans and Control Flow

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Lecture 2: Operations and Data Types

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Building Java Programs Chapter 2

CS 101 Computer Programming

First Java Program - Output to the Screen

Getting started with Java

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Table of Contents Date(s) Title/Topic Page #s. Abstraction

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

CSE 142, Summer 2014

CSE 142, Summer 2015

Building Java Programs

CS 106 Introduction to Computer Science I

IT 374 C# and Applications/ IT695 C# Data Structures

CS112 Lecture: Primitive Types, Operators, Strings

Variables, Constants, and Data Types

CS313D: ADVANCED PROGRAMMING LANGUAGE

Assumptions. History

CS111: PROGRAMMING LANGUAGE II

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Java Basics. SP17 ICS 111 Ed Meyer

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Variables, Types, and Expressions

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

Full file at

CS 106A, Lecture 25 Life After CS 106A, Part 1

CSEN 102 Introduction to Computer Science

Introduction to Java

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

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

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

Lec 3. Compilers, Debugging, Hello World, and Variables

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

More on Strings. String methods and equality. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Compu<ng and Mathema<cs h=p://

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Introduction to Java

Fall 2017 CISC124 9/27/2017

Chapter 2: Data and Expressions

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

CC112 Structured Programming

COMP String and Console I/O. Yi Hong May 18, 2015

Section 2: Introduction to Java. Historical note

Fall 2017 CISC124 9/16/2017

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

2 rd class Department of Programming. OOP with Java Programming

Computational Expression

Java Basic Datatypees

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Week 2: Data and Output

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

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

Fundamentals of Programming CS-110. Lecture 2

What did we talk about last time? Examples switch statements

CS111: PROGRAMMING LANGUAGE II

Getting Started Values, Expressions, and Statements CS GMU

Chapter 2: Data and Expressions

CS1 Lecture 3 Jan. 22, 2018

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Nested Loops Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

CS 106A, Lecture 9 Problem-Solving with Strings

Visual C# Instructor s Manual Table of Contents

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Full file at

2 nd Week Lecture Notes

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

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

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

6.096 Introduction to C++ January (IAP) 2009

Expressions, Statements, and Control Structures

CIS 110: Introduction to Computer Programming

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CMPT 125: Lecture 3 Data and Expressions

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

Chapter 2: Using Data

CS 106A, Lecture 7 Parameters and Return

Building Java Programs. Introduction to Programming and Simple Java Programs

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

Chapter 2: Data and Expressions

CSc Introduction to Computing

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

Transcription:

SSEA Computer Science: CS106A Dr. Cynthia Lee Lecturer in Computer Science Stanford

Topics for today Learn how to harness computing power to solve problems. To that end: Introduce Java programming language Shared structure with Karel Console programs and printing Variables Data types Assignment

Welcome to Java!

The Java programming language Depending on metric, between #1 and top 5 language Android programming Web servers Desktop apps Syntactically, a close relative to C++ (CS106B) and C (CS107)

Console Programs

Console programs import acm.program.*; public class Name extends ConsoleProgram { public void run() { statements; } } Unlike Karel, many programs produce their behavior as text. console: Text box into which the behavior is displayed. output: Messages displayed by the program. input: Data read by the program that the user types.

The console public class Hello extends ConsoleProgram { public void run() { println("hello, world!"); println(); println("this program produces"); println("four lines of output"); } } Its output: Hello, world! This program produces four lines of output console: Text box into which the output is printed.

ConsoleProgram methods The ConsoleProgram contains these useful methods: Method pause(ms); setfont("font"); setsize(w, h); settitle("text"); Description halts for the given # of milliseconds changes the text size/shape sets the console window's onscreen size sets the title bar text public class Hello extends ConsoleProgram { public void run() { setfont("comic Sans MS-Bold-16"); setsize(500, 300); println("hello, world!");...

The console The name console comes from historical computer systems that would take up an entire room, and have one console station where the operator would interact with and control the system IBM 3070 Grace Hopper and the UNIVAC

The println statement A statement that prints a line of output on the console. pronounced "print-linn" Two ways to use println : println("text"); Prints the given message as output. A message is called a string; it starts/ends with a " quote character. The quotes do not appear in the output. A string may not contain a " character.* println(); Prints a blank line of output. * unless you use a special escape sequence

Escape sequences escape sequence: A special sequence of characters used to represent certain special characters in a string. \t tab character \n new line character \" quotation mark character \\ backslash character Example: println("\\hello\nhow\tare \"you\"?\\\\"); Output: \hello how are "you"?\\

Variables

Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. THREE COMPONENTS OF A VARIABLE: Name Type Value Three steps for using a variable: Declare it Initialize it Use it - introduce its name and type - store a value into it - print it or use it as part of an expression

Declaration variable declaration: Sets aside memory for storing a value. Variables must be declared before they can be used. Gives the NAME and the TYPE Syntax: type name; int zipcode; zipcode double mygpa; mygpa

Data types type: A category or set of data values. Constrains the operations that can be performed on data Many languages ask the programmer to specify types Examples: integer, real number, string Note: Internally, computers store everything as 1s and 0s 104 01101000 "hi" 0110100001101001 Java gives data types to help humans interpret the values

Java's primitive types primitive types: some simple types for numbers, text, etc. Java also has object types, which we'll talk about later Name Description Examples int integers (up to 2 31-1) 42, -3, 0, 926394 double real numbers (up to 10 308 ) 3.1, -0.25, 9.4e3 char single text characters 'a', 'X', '?', '\n' boolean logical values true, false Why does Java distinguish integers vs. real numbers?

Assignment assignment: Stores a value into a variable. Gives the variable its VALUE The value can be an expression; the variable stores its result. Syntax: name = expression; int zipcode; zipcode = 90210; zipcode 90210 double mygpa; mygpa = 1.0 + 2.25; mygpa 3.25

Declare + initialize in one A variable can be declared/initialized in one statement. This is probably the most commonly used declaration syntax. Syntax: type name = expression; double tempf = 98.6; tempf 98.6 int x = (11 / 2) + 3; x 8

Using variables Once given a value, a variable can be used in expressions: int x = 3; println("x is " + x); // x is 3 println(5 * x - 1); // 5 * 3-1 You can assign a value more than once: int x = 3; println(x + " here"); // 3 here x x = 4 + 7; println("now x is " + x); // now x is 11

More on Assignment

Assignment Assignment uses =, but it is not an algebraic equation = means, "store the value at right in the variable at left" The right side expression is evaluated first, and then its result is stored in the variable at left. What happens here? (A) x x+2 int x = 3; x = x + 2; //??? (B) x 2 (C) x 3 (D) Other or causes an error

Assignment Assignment uses = symbol that you ve seen in math class, but it is not an algebraic equation in the sense that you ve seen in math class = means, "store the value at right in the variable at left" The right side expression is evaluated first, and then its result is stored in the variable at left. What happens here? int x = 3; x = x + 2; //??? x