CS Programming I: Primitives and Expressions

Similar documents
CS Programming I: Branches

CS Programming I: Branches

CS Programming I: Arrays

Full file at

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CIS 110: Introduction to Computer Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Full file at

Pace University. Fundamental Concepts of CS121 1

CS Programming I: Using Objects

Basics of Java Programming

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

CS Programming I: Using Objects

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

COMP 110 Introduction to Programming. What did we discuss?

LECTURE 3 C++ Basics Part 2

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Visual C# Instructor s Manual Table of Contents

UNIT- 3 Introduction to C++

CS112 Lecture: Primitive Types, Operators, Strings

Chapter 2: Using Data

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

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

Computational Expression

Chapter 2: Data and Expressions

COMP Primitive and Class Types. Yi Hong May 14, 2015

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Data types Expressions Variables Assignment. COMP1400 Week 2

Computer Programming CS F111

CS Programming I: ArrayList

Program Fundamentals

Midterms Save the Dates!

Chapter 6 Primitive types

Chapter 2: Data and Expressions

CS Programming I: Programming Process

CS Programming I: Programming Process

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Lecture 3 Tao Wang 1

Variables and Operators 2/20/01 Lecture #

Welcome to the Primitives and Expressions Lab!

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Chapter 2: Basic Elements of C++

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Variables. location where in memory is the information stored type what sort of information is stored in that memory

Computers Programming Course 5. Iulian Năstac

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Declaration and Memory

CS Programming I: Exceptions

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

CS 251 Intermediate Programming Java Basics

Introduction to Computer Science and Object-Oriented Programming

egrapher Language Reference Manual

Chapter 3: Operators, Expressions and Type Conversion

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

3. Java - Language Constructs I

Zheng-Liang Lu Java Programming 45 / 79

CS110: PROGRAMMING LANGUAGE I

Chapter 2: Data and Expressions

Getting started with Java

Introduction to Programming Using Java (98-388)

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Chapter 2: Using Data

Programming with Java

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

JAVA OPERATORS GENERAL

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

EXPRESSIONS AND ASSIGNMENT CITS1001

Objectives. In this chapter, you will:

CS 302: Introduction to Programming

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Chapter 2: Using Data

AP Computer Science A

C++ Programming: From Problem Analysis to Program Design, Third Edition

ARG! Language Reference Manual

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Exercise: Inventing Language

1.00 Lecture 4. Promotion

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Operators. Java operators are classified into three categories:

CS61B Lecture #14: Integers

I. Variables and Data Type week 3

CIS133J. Working with Numbers in Java

CS111: PROGRAMMING LANGUAGE II

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Basics of Programming

Chapter 02: Using Data

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Transcription:

CS 200 - Programming I: Primitives and Expressions Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455

Primitives

1/26 Two-state machine Binary Data Units Bit The smallest unit of information. 0 or 1, false or true, off or on, low or high,...

1/26 Two-state machine Binary Data Units Bit The smallest unit of information. 0 or 1, false or true, off or on, low or high,... Byte 8-bits.

1/26 Two-state machine Binary Data Units Bit The smallest unit of information. 0 or 1, false or true, off or on, low or high,... Nibble 4-bits. Byte 8-bits.

1/26 Two-state machine Binary Data Units Bit The smallest unit of information. 0 or 1, false or true, off or on, low or high,... Nibble 4-bits. Byte 8-bits. Word A group of bytes, depends on the systems: 16-bit systems is 2 byte words, 32-bit is is 4 byte words,...

1/26 Two-state machine Binary Data Units Bit The smallest unit of information. 0 or 1, false or true, off or on, low or high,... Nibble 4-bits. Byte 8-bits. Word A group of bytes, depends on the systems: 16-bit systems is 2 byte words, 32-bit is is 4 byte words,... KB vs kb 1024 bytes vs 1000 bytes.

1/26 Two-state machine Binary Data Units Bit The smallest unit of information. 0 or 1, false or true, off or on, low or high,... Nibble 4-bits. Byte 8-bits. Word A group of bytes, depends on the systems: 16-bit systems is 2 byte words, 32-bit is is 4 byte words,... KB vs kb 1024 bytes vs 1000 bytes. MiB vs MB 1024 2 bytes vs 1000 2 bytes.

1/26 Two-state machine Binary Data Units Bit The smallest unit of information. 0 or 1, false or true, off or on, low or high,... Nibble 4-bits. Byte 8-bits. Word A group of bytes, depends on the systems: 16-bit systems is 2 byte words, 32-bit is is 4 byte words,... KB vs kb 1024 bytes vs 1000 bytes. MiB vs MB 1024 2 bytes vs 1000 2 bytes. GiB vs GB 1024 3 bytes vs 1000 3 bytes.

2/26 Variable Declaration type variablename = expression; Type Java primitives: int, byte, char, short, long, float and double.

2/26 Variable Declaration type variablename = expression; Type Java primitives: int, byte, char, short, long, float and double. Restricts the variable to:

2/26 Variable Declaration type variablename = expression; Type Java primitives: int, byte, char, short, long, float and double. Restricts the variable to: A set of values.

2/26 Variable Declaration type variablename = expression; Type Java primitives: int, byte, char, short, long, float and double. Restricts the variable to: A set of values. A set of operations.

2/26 Variable Declaration type variablename = expression; Type Java primitives: int, byte, char, short, long, float and double. Restricts the variable to: A set of values. A set of operations. Java is a strongly typed language.

2/26 Variable Declaration type variablename = expression; Type Java primitives: int, byte, char, short, long, float and double. Restricts the variable to: A set of values. A set of operations. Java is a strongly typed language. All variables are declared with a specific type and remain that type.

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_).

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits.

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits. Cannot be a keyword (Ex: int, case, while)

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits. Cannot be a keyword (Ex: int, case, while) Can be any length.

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits. Cannot be a keyword (Ex: int, case, while) Can be any length. Case-senstive: foobar foobar

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits. Cannot be a keyword (Ex: int, case, while) Can be any length. Case-senstive: foobar foobar CS 200 Style and Good Practice

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits. Cannot be a keyword (Ex: int, case, while) Can be any length. Case-senstive: foobar foobar CS 200 Style and Good Practice Style guide: https://cs200-www.cs.wisc.edu/wp/style-guide/

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits. Cannot be a keyword (Ex: int, case, while) Can be any length. Case-senstive: foobar foobar CS 200 Style and Good Practice Style guide: https://cs200-www.cs.wisc.edu/wp/style-guide/ Use lowercamelcase: thisislowercamelcase

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits. Cannot be a keyword (Ex: int, case, while) Can be any length. Case-senstive: foobar foobar CS 200 Style and Good Practice Style guide: https://cs200-www.cs.wisc.edu/wp/style-guide/ Use lowercamelcase: thisislowercamelcase Don t use $ or _

3/26 A rose by any other name... Naming Rules Starts with a letter, $, or underscore (_). Subsequent characters can be letters, $, (_) or digits. Cannot be a keyword (Ex: int, case, while) Can be any length. Case-senstive: foobar foobar CS 200 Style and Good Practice Style guide: https://cs200-www.cs.wisc.edu/wp/style-guide/ Use lowercamelcase: thisislowercamelcase Don t use $ or _ Length: not too long, but clearly identify the role.

4/26 Type: int Declaration: int myint = 2; Values Integer in 4 bytes (32 bits)

4/26 Type: int Declaration: int myint = 2; Values Integer in 4 bytes (32 bits) 2, 147, 483, 648 to 2, 147, 483, 647 inclusive

4/26 Type: int Declaration: int myint = 2; Values Integer in 4 bytes (32 bits) 2, 147, 483, 648 to 2, 147, 483, 647 inclusive Other integer types byte short long 1 byte 2 bytes 8 bytes 128 to 127 32, 768 to 32, 767 9, 223, 372, 036, 854, 775, 808 to 9,..., 807

5/26 Type: double Declaration: double mydbl = 1.618; Values Real (floating point) number in 8 bytes (64 bits)

5/26 Type: double Declaration: double mydbl = 1.618; Values Real (floating point) number in 8 bytes (64 bits) About 15 digits of precision

Type: double Declaration: double mydbl = 1.618; Values Real (floating point) number in 8 bytes (64 bits) About 15 digits of precision Range is approx. ±1.7 10 308 5/26

Type: double Declaration: double mydbl = 1.618; Values Real (floating point) number in 8 bytes (64 bits) About 15 digits of precision Range is approx. ±1.7 10 308 Another real type: float 5/26

Type: double Declaration: double mydbl = 1.618; Values Real (floating point) number in 8 bytes (64 bits) About 15 digits of precision Range is approx. ±1.7 10 308 Another real type: float Real (floating point) number in 4 bytes (32 bits) 5/26

Type: double Declaration: double mydbl = 1.618; Values Real (floating point) number in 8 bytes (64 bits) About 15 digits of precision Range is approx. ±1.7 10 308 Another real type: float Real (floating point) number in 4 bytes (32 bits) About 6 digits of precision 5/26

Type: double Declaration: double mydbl = 1.618; Values Real (floating point) number in 8 bytes (64 bits) About 15 digits of precision Range is approx. ±1.7 10 308 Another real type: float Real (floating point) number in 4 bytes (32 bits) About 6 digits of precision Range is approx. ±3.4 10 38 5/26

6/26 TopHat Question 1 What is not a double literal? a. 0.5 b..5 c. 5 d. 5.5e5

7/26 Constant Variable final type VARIABLE_NAME = expression; final Modifier Cannot change the value after initialization.

7/26 Constant Variable final type VARIABLE_NAME = expression; final Modifier Cannot change the value after initialization. CS 200 Style and Good Practice Use constants to keep any hard-coded data in one place. Names are in ALL CAPS with _ between words. E.g.: SOME_CONST_NAME

Expressions

8/26 Assignments somevar = expression Assigment Operator: = Right-to-left associative:

8/26 Assignments somevar = expression Assigment Operator: = Right-to-left associative: 1 Evaluate expression.

8/26 Assignments somevar = expression Assigment Operator: = Right-to-left associative: 1 Evaluate expression. 2 Assign the value of expression to somevar

8/26 Assignments somevar = expression Assigment Operator: = Right-to-left associative: 1 Evaluate expression. 2 Assign the value of expression to somevar E.g: a = b = c = d = 5 + 5;

8/26 Assignments somevar = expression Assigment Operator: = Right-to-left associative: 1 Evaluate expression. 2 Assign the value of expression to somevar E.g: a = b = c = d = 5 + 5; TopHat Question 2: What is the value of a?

9/26 Swapping Variables Consider the following code snippet: int a = 2; int b = 3;

9/26 Swapping Variables Consider the following code snippet: int a = 2; int b = 3; TopHat Question 3: How can we swap the values?

10/26 Operators Arithmetic Operators Unary:

10/26 Operators Arithmetic Operators Unary: -: Negation

10/26 Operators Arithmetic Operators Unary: -: Negation Binary:

10/26 Operators Arithmetic Operators Unary: -: Negation Binary: +: Addition

10/26 Operators Arithmetic Operators Unary: -: Negation Binary: +: Addition -: Subtraction

10/26 Operators Arithmetic Operators Unary: -: Negation Binary: +: Addition -: Subtraction *: Multiplication

10/26 Operators Arithmetic Operators Unary: -: Negation Binary: +: Addition -: Subtraction *: Multiplication /: Division

10/26 Operators Arithmetic Operators Unary: -: Negation Binary: +: Addition -: Subtraction *: Multiplication /: Division %: Remainder (modulo)

10/26 Operators Arithmetic Operators Unary: -: Negation Binary: +: Addition -: Subtraction *: Multiplication /: Division %: Remainder (modulo) Compound:

10/26 Operators Arithmetic Operators Unary: -: Negation Binary: +: Addition -: Subtraction *: Multiplication /: Division %: Remainder (modulo) Compound: +=: Add and assign E.g. a += b is shorthand for a = a + b

10/26 Operators Arithmetic Operators Unary: -: Negation Binary: +: Addition -: Subtraction *: Multiplication /: Division %: Remainder (modulo) Compound: +=: Add and assign E.g. a += b is shorthand for a = a + b Other variants: -=, *=, /=, %=

11/26 Operator Precedence

11/26 Operator Precedence CS 200 Style and Good Practice Use parentheses to explicitly indicate the precedence.

12/26 Type Conversion char byte short int long float double Narrowest Widest

12/26 Type Conversion char byte short int long float double Narrowest Widest Implicit Conversion Arithmetic operators: Implicit cast to the widest type. Assignment: Implicit cast RHS to LHS type if wider. E.g. double dbl = 2;

Type Conversion char byte short int long float double Narrowest Widest Implicit Conversion Arithmetic operators: Implicit cast to the widest type. Assignment: Implicit cast RHS to LHS type if wider. E.g. double dbl = 2; Explicit Conversion: Casting (type) E.g. int i = (int)2.5; 12/26

13/26 Integer Division Truncation with / When both operands are integers (int, short, etc), the result will also be an integer.

13/26 Integer Division Truncation with / When both operands are integers (int, short, etc), the result will also be an integer. Java will truncate the result: Chop off the decimal.

13/26 Integer Division Truncation with / When both operands are integers (int, short, etc), the result will also be an integer. Java will truncate the result: Chop off the decimal. E.g.: int a = 3 / 2; //Sets a to 1 double b = (double)(3 / 2); // Still 1 double c = (double)3 / 2; // Now 1.5

Simple Memory Model Memory 0xffffffff 0xff000000 14/26

Simple Memory Model Memory 0xffffffff int i = 6; 6 i 0xff000000 14/26

Simple Memory Model Memory 0xffffffff int i = 6; int j = 6; 6 j 6 i 0xff000000 14/26

Simple Memory Model Memory 0xffffffff int i = 6; int j = 6; double d = 2.5; 2.5 d 6 j 6 i 0xff000000 14/26

Simple Memory Model Memory 0xffffffff int i = 6; int j = 6; double d = 2.5; short s = 25; 25 s 2.5 d 6 j 6 i 0xff000000 14/26

Simple Memory Model Memory 0xffffffff int i = 6; int j = 6; double d = 2.5; short s = 25; 25 s 2.5 d 6 j 6 i 0xff000000 Key Points Variables are allocated slots in memory with a memory address. The value of the variables are stored in the assigned memory slots. 14/26

Methods

Methods and Functions What is a Method? Evolved from mathematical functions, e.g. f (x) = 2x + 1 15/26

Methods and Functions What is a Method? Evolved from mathematical functions, e.g. f (x) = 2x + 1 A parameterized block of code the performs a sequence of instructions and returns a single value. 15/26

Methods and Functions What is a Method? Evolved from mathematical functions, e.g. f (x) = 2x + 1 A parameterized block of code the performs a sequence of instructions and returns a single value. Can have many parameters, but only a single return value. 15/26

Methods and Functions What is a Method? Evolved from mathematical functions, e.g. f (x) = 2x + 1 A parameterized block of code the performs a sequence of instructions and returns a single value. Can have many parameters, but only a single return value. Why use Methods? Abstraction: modular design. End of redundant code: tempted to copy-and-paste code; make a method. Good coding practice: easier to read. 15/26

16/26 Structure modifiers returntype methodname(ptype1 pname1,...) { body } Method Terminology Modifiers E.g. public, static, final. Header Modifier, return type, method name, and the types and names of the parameters. Signature Method name and the types of the parameters. Body Block of code containing the method s instructions. Definition The header and the body.

16/26 Structure E.g. public static double avgtwoints(int p1, int p2) { return (p1 + p2)/2.0; } Method Terminology Modifiers E.g. public, static, final. Header Modifier, return type, method name, and the types and names of the parameters. Signature Method name and the types of the parameters. Body Block of code containing the method s instructions. Definition The header and the body.

17/26 Using Methods public static double avgtwoints(int p1, int p2) { return (p1 + p2)/2.0; } Calling a Method avgtwoints(5, 7) methodname(arg1, arg2,...) The call of the method is evaluated to the return value of the method, using the arguments provided.

18/26 Parameters public static double avgtwoints(int p1, int p2) { return (p1 + p2)/2.0; } Method Parameters Scope: considered as variables within the body of the function. Pass by value: changing the value in the method doesn t change the passed value. Initialized at the moment of the method call.

19/26 TopHat Question 4 What is the output? public class TopHat4 { public static int somemethod ( int a) { a = 5; return a; } } public static void main ( String [] args ) { int a = 4; somemethod (a); System. out. print (a); }

20/26 Return Value Method Value (return) The method is evaluated with the passed parameters. void methods do not return a value. All other methods return a single value.

20/26 Return Value Method Value (return) The method is evaluated with the passed parameters. void methods do not return a value. All other methods return a single value. Method Side Effects Anything in a method that changes the state of the running program is considered a side effect, such as:

20/26 Return Value Method Value (return) The method is evaluated with the passed parameters. void methods do not return a value. All other methods return a single value. Method Side Effects Anything in a method that changes the state of the running program is considered a side effect, such as: Printing to the screen. Reading input from the user. Changing global variables.

21/26 TopHat Question 6 What is the value of the following method when called by mymethod(10): public static int mymethod ( int i) { System. out. println (" Value is " + i); return 6; } a. 6 b. 10 c. Value is 10 d. void

22/26 Method Comments CS 200 and Good Practice /** * Calculates the average of two integer values. * * @ param p1 First integer value. * @ param p2 Second integer value. * @ return Average of p1 and p2. */ public static double avgtwoints ( int p1, int p2) { return (p1 + p2 )/2.0; }

Method Comments CS 200 and Good Practice /** * Calculates the average of two integer values. * * @ param p1 First integer value. * @ param p2 Second integer value. * @ return Average of p1 and p2. */ public static double avgtwoints ( int p1, int p2) { return (p1 + p2 )/2.0; } Javadoc Comments 1 An explanation of the method. 2 @param An explanation for each parameter. 3 @return An explanation of the value returned. 22/26

23/26 Method Exercise Take the program circle.java that we wrote and refactor it to use methods. Consider the code, what might be some reasonable methods to add?

Using Libraries

24/26 Libraries Code Libraries A collection of classes and methods. Java comes with a set of standard libraries such as java.util.scanner and java.lang.math 3rd Party Libraries also exist.

25/26 Math Class Standard Java class (optional for Math): import java.lang.math; Basic Numeric Operations Collection of static methods and constants. Methods: pow, abs, ceil, etc... Members: E, PI API: https://docs.oracle.com/javase/8/docs/api/ java/lang/math.html

26/26 Further Reading COMP SCI 200: Programming I zybooks.com, 2015. zybook code: WISCCOMPSCI200Spring2018 Chapter 2. Primitives and Expressions

Appendix References Appendix

Appendix References References

Appendix References 27/26 Image Sources I https://brand.wisc.edu/web/logos/ http://www.zybooks.com/