COMP 110/L Lecture 4. Kyle Dewey

Similar documents
Peer Instruction 1. Elementary Programming

COMP 110/L Lecture 5. Kyle Dewey

Ex: If you use a program to record sales, you will want to remember data:

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

AP Computer Science Unit 1. Programs

4. Java Project Design, Input Methods

COMP 110/L Lecture 10. Kyle Dewey

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

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

5) (4 points) What is the value of the boolean variable equals after the following statement?

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting

COMP 110/L Lecture 7. Kyle Dewey

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

Important Java terminology

COMP 110 Programming Exercise: Simulation of the Game of Craps

Chapter 4 Classes in the Java Class Libraries

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

What did we talk about last time? Examples switch statements

I. Variables and Data Type week 3

ECE 122 Engineering Problem Solving with Java

Lecture Set 2: Starting Java

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

Lecture Set 2: Starting Java

Introduction to Computer Programming

COMP Introduction to Programming Primitive Types, Strings and Console I/O

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

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

COMP 202 Java in one week

Text Input and Conditionals

COMP 202. Java in one week

Binary Search. Roland Backhouse February 5th, 2001

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

COMP 110/L Lecture 13. Kyle Dewey

3. Simple Types, Variables, and Constants

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Programming with Java

COMP 122/L Lecture 2. Kyle Dewey

There are some standard operations on arrays that are used a lot. But Java lacks much support for doing these easily.

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

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

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Elementary Programming

Data Conversion & Scanner Class

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Darrell Bethea May 25, 2011

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Java Console Input/Output The Basics. CGS 3416 Spring 2018

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Introduction to Java Unit 1. Using BlueJ to Write Programs

Chapter 2 Elementary Programming

A+ Computer Science -

Midterms Save the Dates!

Java Console Input/Output The Basics

COMP 110/L Lecture 19. Kyle Dewey

Chapter 2: Review Exercise Solutions R2.1

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

c) And last but not least, there are javadoc comments. See Weiss.

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

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Object Oriented Programming. Java-Lecture 1

Java Coding 3. Over & over again!

Exercise: Inventing Language

COMP 110/L Lecture 6. Kyle Dewey

printf( Please enter another number: ); scanf( %d, &num2);

Computer Science 145 Midterm 1 Fall 2016

COMP-202: Foundations of Programming

Array. Array Declaration:

COMP 110/L Lecture 20. Kyle Dewey

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

VARIABLES AND TYPES CITS1001

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

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Variables and numeric types

CS 302: Introduction to Programming

A variable is a name for a location in memory A variable must be declared

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Lesson 2.4 Arraylist

Getting Started with Python

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

CS61C L10 MIPS Instruction Representation II, Floating Point I (6)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Computational Expression

Introduction to Programming Using Java (98-388)

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Problem Solving for Intro to Computer Science

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

AP Computer Science A

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

Variables and Data Representation

A+ Computer Science -

General Instructions. You can use QtSpim simulator to work on these assignments.

Search Lesson Outline

Transcription:

COMP 110/L Lecture 4 Kyle Dewey

Outline New types: long and double Reading in with Scanner Performing operations on them How they interact with each other and other types Exponentiation with Math.pow()

New Type: long

-If we try this with a really big number (e.g., 9876543210), it will outright crash -If we try it with two still pretty big numbers (e.g., 1234567890 and 1234567890), it will produce incorrect results, even getting a negative number out of two positive numbers Revisit: AddTwo.java

-This range is around +/- 2 billion. -2 billion sounds like a lot, and it s big enough for most things, but there are 7 billion people on the planet Fundamental Problem int stores integers in the following range: -2 31 to (2 31-1) Numbers out of this range won t work right

long for Bigger Integers long works like int, but its range is exponentially larger -263 to (2 63-1)

Working with long Declaring a long variable long mylong;

Working with long Declaring a long variable long mylong; Reading in a long with Scanner Scanner in = new Scanner(System.in); long mylong = in.nextlong(); -Instead of declaring an int variable, we can declare a long variable -We can read in a long using nextlong(), as opposed to nextint()

Example: LongAddTwo.java

Specifying long By default, if you write a number, Java assumes it s an int If you follow it with an l (the letter ell), Java will treat it as a long

Specifying long By default, if you write a number, Java assumes it s an int If you follow it with an l (the letter ell), Java will treat it as a long 14 // int

Specifying long By default, if you write a number, Java assumes it s an int If you follow it with an l (the letter ell), Java will treat it as a long 14 // int 14l // long (that s an ell)

Interactions with long String concatenation works like it does with int

Interactions with long String concatenation works like it does with int my string + 14l

Interactions with long String concatenation works like it does with int my string + 14l my string14

Interactions with long String concatenation works like it does with int my string + 14l my string14 13l + other string

Interactions with long String concatenation works like it does with int my string + 14l my string14 13l + other string 13other string

Interactions with long Addition works like it does with int

Interactions with long Addition works like it does with int 5l + 4l

Interactions with long Addition works like it does with int 5l + 4l 9l

Interactions Between long and int Values coerce into long -Intuition: long is bigger, so it wins

Interactions Between long and int Values coerce into long 4l + 2 -Intuition: long is bigger, so it wins

Interactions Between long and int Values coerce into long 4l + 2 6l -Intuition: long is bigger, so it wins

Interactions Between long and int Values coerce into long 4l + 2 6l 3 + 6l -Intuition: long is bigger, so it wins

Interactions Between long and int Values coerce into long 4l + 2 6l 3 + 6l 9l -Intuition: long is bigger, so it wins

New Type: double

Revisit: AddTwo.java -If we try to put in a floating-point value, it outright crashes -We want support for floating-point values (these are really useful!)

double for Floating-Point double stores floating-point values float also stores floating-point values, but it s half the size of double Narrower range, less precise

Working with double Declaring a double variable double mydouble;

Working with double Declaring a double variable double mydouble; Reading in a double with Scanner Scanner in = new Scanner(System.in); double mydouble = in.nextdouble();

Example: DoubleAddTwo.java

Specifying double If the number contains a decimal point, Java treats it as a double

Specifying double If the number contains a decimal point, Java treats it as a double 4.5 // double

Specifying double If the number contains a decimal point, Java treats it as a double 4.5 // double 1.0 // double

Specifying double If the number contains a decimal point, Java treats it as a double 4.5 // double 1.0 // double 0.2 // double

Interactions with double String concatenation works like it does with int

Interactions with double String concatenation works like it does with int my string + 0.5

Interactions with double String concatenation works like it does with int my string + 0.5 my string0.5

Interactions with double String concatenation works like it does with int my string + 0.5 my string0.5 0.2 + other string

Interactions with double String concatenation works like it does with int my string + 0.5 my string0.5 0.2 + other string 0.2other string

Interactions with double Addition works like it does with int

Interactions with double Addition works like it does with int 5.0 + 4.2

Interactions with double Addition works like it does with int 5.0 + 4.2 9.2

Interactions Between double and int Values coerce into double

Interactions Between double and int Values coerce into double 0.5 + 2

Interactions Between double and int Values coerce into double 0.5 + 2 2.5

Interactions Between double and int Values coerce into double 0.5 + 2 2.5 3 + 0.75

Interactions Between double and int Values coerce into double 0.5 + 2 2.5 3 + 0.75 3.75

Interactions Between double and long Values coerce into double

Interactions Between double and long Values coerce into double 0.5 + 4l

Interactions Between double and long Values coerce into double 0.5 + 4l 4.5

Interactions Between double and long Values coerce into double 0.5 + 4l 4.5 3l + 0.75

Interactions Between double and long Values coerce into double 0.5 + 4l 4.5 3l + 0.75 3.75

Exponentiation with Math.pow()

Exponentiation Use Math.pow() for exponentiation (something to the power of something else)

Exponentiation Use Math.pow() for exponentiation (something to the power of something else) Wanted: 2 7

Exponentiation Use Math.pow() for exponentiation (something to the power of something else) Wanted: 2 7 Math.pow(2, 7)

Exponentiation Use Math.pow() for exponentiation (something to the power of something else) Wanted: 2 7 Math.pow(2, 7) Wanted: 3.4 5.6

Exponentiation Use Math.pow() for exponentiation (something to the power of something else) Wanted: 2 7 Math.pow(2, 7) Wanted: 3.4 5.6 Math.pow(3.4, 5.6)

Example: Exponentiation.java