COMP-202: Foundations of Programming

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

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

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

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

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

3. Java - Language Constructs I

Getting started with Java

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

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

CEN 414 Java Programming

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

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

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Full file at

Software and Programming 1

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

AP Computer Science Unit 1. Programs

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

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

What did we talk about last time? Examples switch statements

Chapter 2: Data and Expressions

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

Basics of Java Programming

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

CS 106 Introduction to Computer Science I

Chapter 2: Data and Expressions

Software and Programming 1

Chapter 2: Data and Expressions

COMP 202 Java in one week

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

VARIABLES AND TYPES CITS1001

MODULE 02: BASIC COMPUTATION IN JAVA

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

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

CS 11 java track: lecture 1

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

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

3. Java - Language Constructs I

6.096 Introduction to C++ January (IAP) 2009

COMP 110 Introduction to Programming. What did we discuss?

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

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

Object-Oriented Programming

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Elementary Programming

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

Chapter 2 Elementary Programming

Java Notes. 10th ICSE. Saravanan Ganesh

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

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

Zheng-Liang Lu Java Programming 45 / 79

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

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

A Java program contains at least one class definition.

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

Unit 3. Constants and Expressions

Computer Science II Lecture 1 Introduction and Background

CMPT 125: Lecture 3 Data and Expressions

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

4 Programming Fundamentals. Introduction to Programming 1 1

Visual C# Instructor s Manual Table of Contents

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Java Basic Programming Constructs

Lecture Set 2: Starting Java

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

COMP2121: Microprocessors and Interfacing. Number Systems

COMP-202: Foundations of Programming. Lecture 9: Arrays and Practice Jackie Cheung, Winter 2016

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Programming Lecture 3

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

Lecture Set 2: Starting Java

Chapter 2 ELEMENTARY PROGRAMMING

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

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

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

An overview of Java, Data types and variables

Program Fundamentals

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

Display Input and Output (I/O)

Getting started with Java

COMP 202 Java in one week

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Basic data types. Building blocks of computation

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

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Introduction to Object Oriented Systems Development. Practical Session (Week 2)

DATA TYPES AND EXPRESSIONS

Programming Language Concepts: Lecture 2

Chapter 2. Elementary Programming

More Programming Constructs -- Introduction

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

A very simple program. Week 2: variables & expressions. Declaring variables. Assignments: examples. Initialising variables. Assignments: pattern

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

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

Fall 2017 CISC124 9/16/2017

Transcription:

COMP-202: Foundations of Programming Lecture 3: Basic data types Jackie Cheung, Winter 2016

Review: Hello World public class HelloWorld { } public static void main(string[] args) { } System.out.println("Hello, World!"); What is the class name? Why do we want to give the class a name? What is special about the main method? Can we call it something else? 2

Variables A variable is a place in memory reserved for storing a value. We need them in order to: Store partial results Allow us to generalize our code Make our code easier to understand 3

Declaring and Assigning Declaring a variable: <type> <var_name>; int numstudents; Assigning a value to a variable: <var_name> = <value>; numstudents = 210; Doing both at the same time: <type> <var_name> = <value>; 4

Getting Input You can pass input Strings to the program > run SomeProgram these are words The input words can be accessed in the main method by the args variable. In the above example: args[0] these args[1] are args[2] words args[3] not defined! Error if you try to access it. 5

This Lecture Basic data types We'll start working with other kinds of data, not just Strings and ints! 6

BASIC DATA TYPES 7

Some Basic Variable Types String int long float double boolean char Letters; a sequence of characters Integer values Long integer values Real numbers Real numbers, with more precision True or false One character 8

More Types byte Can only store integers between -128 and 127 short Can only store integers between -32768 and 32767 Difference between byte, short, int, long: number of bits (binary digits) used. Difference between float and double also because of number of bits. 9

How Many Values? How many different values can you represent using: A single bit? Two bits? Three bits? In general, N bits? 10

How Many Bits? How many bits do you need to represent: Two different values? Four different values? Six different values? In general, x different values? 11

Exercise Write a program that computes and prints out how many bits (rounded up) are needed to represent x different values. You can work in doubles. In other words, it should compute the function f x = log 2 x Steps: 1. Declare a variable to hold x 2. The natural logarithm of a number using Math.log(<num>) 3. Compute the ceiling function using Math.ceil(<num>) Hint: log 2 x = log x log 2 12

Mismatching Types If you try to store something of type X in something of type Y, the Java compiler will complain. int x; x = "Hello"; What are the types of "Hello" and x? 13

When you write int x; Why Does Java Care? Java needs to set aside enough memory to store one integer value. If you try to store a String, it doesn't know whether it will fit or not! Also, ensuring type consistency helps us debug code. x = "Hello"; in the previous slide is obviously wrong! 14

Data Representation Third reason: remember how all data is represented as binary values/electrical bits? On = 1 = true Off = 0 = false The computer doesn't known the meaning of any numbers at all! The same sequence of bits that encode an int would be meaningless if decoded as a String. 15

int The type int represents an integer in range of: [ 2 31, 2 31 1], or [ 2147483648, 2147483647]. An int takes up 32 bits = 4 bytes in memory. So, there are 2 32 possible values that can be represented. Half of these are for negative numbers, and one more spot is taken up by zero. 16

double The type double represents a real number with fractional values. They cannot store an infinite number of digits (e.g., pi). They can only store to a limited precision. double almostpi = 3.14159; // can only store // some of the digits 17

double If you write.0 after an integer constant, it will be stored as a double. int x = 3.0; int x = 3; double x = 3.0; 18

float A float normally takes up 32 bits in memory. It is similar to a double except, doubles use 64 bits. To use a float, write f after the number. float x = 3.0f; Aside: floats and double are stored using scientific notation. + 6.02214129 10 23 sign mantissa exponent (except it is stored in binary, not decimal of course) 19

Real Numbers vs Floating Point Since a computer has limited memory, we can't actually store a number such as pi. It is also difficult to store something like 5.000000000000000000000000000000001 Remember this is mantissa 2 exponent It would take a lot of bits to store the mantissa! 20

char A character set is an ordered list of characters, where each character corresponds to a unique number. A char stores a single character from the Unicode character set. char gender; gender = 'M'; 21

char in Unicode Unicode is an international character set: A 65 $ 36 電 38651 ف 1601 Character literals are delimited by apostrophes: 'a', '$', 'X', '\n' 22

Escape Use an escape sequence for punctuation or spacing that cannot otherwise be expressed inside an apostrophe pair. char apos = '\''; char tab = '\t'; char newline = '\n'; 23

boolean A boolean stores things that are either true or false. boolean areyouonfacebook; areyouonfacebook = false; 24

byte, short, long Like ints, but uses different numbers of bits Type Bits Range byte 8 [ 128, 127] short 16 [ 32768, 32767] int 32 [ 2 31, 2 31 1] long 64 [ 2 63, 2 63 1] Trade-off: memory usage vs. what can be stored. In this class, use int unless you have a very good reason not to. (Likewise, use double rather than float.) 25

String A String stores a sequence of char's. It is a more complex type than the others mentioned. Why? How many bits do you need to store a String? We've seen how to assign text to a String: String helloworld = "Hello, World!"; We can also put escape sequences in " ": System.out.println("Hi \n\n\n\n\n much lower"); 26

Operations With Strings Being a more complex data type, there are many methods associated with Strings. <String1>.equals(<String2>) Gives us whether two Strings are equal, as a boolean. e.g., boolean isequal = "Hello".equals("hello"); <String>.length() Gives us length of String as an int. e.g., int greetinglength = "Hi there!".length(); 27

Questions What type of variable would you use to store the following? The number of school days in a year. The cost of a meal at a restaurant The number of minutes you will spend on COMP- 202 during the term A PIN for an ATM card Whether it snowed on January 13th 28

Converting Between Types It is possible to convert between types. e.g., to convert the String 32 to the int 32, or to the double 32.0. e.g., String to int: int thirtytwo = Integer.parseInt( 32 ); e.g., String to double: double threepointtwo = Double.parseDouble( 3.2 ); 29

Exercise Write a program that takes in a temperature in Fahrenheit, then computes and displays the equivalent in Celsius. celsius = (fahrenheit 32.0) * 5.0 / 9.0 Hint: break it down into the following steps: 1. Set up main method in a class 2. Get the input from args[0] and store it into a double variable 3. Implement the conversion formula 4. Display the output 30

Starter Code public class TempConverter { public static void main(string[] args) { // args[0] contains the temp in F // TODO: convert it into a double // TODO: compute temperature in Celsius // TODO: display new temperature System.out.println("The temperature in C is "); } } celsius = (fahrenheit - 32.0) * 5.0 / 9.0 31