CP122 Computer Science I. Chapter 2: Data Types, Variables, and I/O

Similar documents
CP122 Computer Science I. Binary, Strings, Conditionals, User Input

CP122 Computer Science I. Chapter 3: Methods/Functions

CP122 Computer Science I. Chapter 2: Using Objects

Basics of Java Programming

Computational Expression

PROGRAMMING FUNDAMENTALS

Variables, Constants, and Data Types

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

Chapter 02: Using Data

Full file at

Getting started with Java

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

Basic Computation. Chapter 2

CEN 414 Java Programming

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Chapter 2: Using Data

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

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

Chapter 2 Primitive Data Types and Operations

4 Programming Fundamentals. Introduction to Programming 1 1

Course Outline. Introduction to java

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

CP122 CS I. Chapter 11: File I/O and Exceptions

Arithmetic and IO. 25 August 2017

CP122 CS I. Chapter 11: File I/O and Exceptions

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

Full file at

CP122 CS I. Chapter 7: File I/O and Exceptions

CS 302: Introduction to Programming

Chapter 2 Elementary Programming

What methods does the String class provide for ignoring case sensitive situations?

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

Datatypes, Variables, and Operations

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

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Chapter 2 ELEMENTARY PROGRAMMING

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

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

4. If the following Java statements are executed, what will be displayed?

Fundamentals of Programming Data Types & Methods

Software Practice 1 Basic Grammar

CS 106 Introduction to Computer Science I

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Key Differences Between Python and Java

Mr. Monroe s Guide to Mastering Java Syntax

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

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

Lecture 2 Tao Wang 1

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

Chapter 2 Working with Data Types and Operators

Chapter 2: Programming Concepts

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

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

Basic Computation. Chapter 2

Programming with Java

Chapter 2 Primitive Data Types and Operations. Objectives

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Fall 2017 CISC124 9/16/2017

What did we talk about last time? Examples switch statements

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Programming in Java

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Pace University. Fundamental Concepts of CS121 1

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

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

Data types Expressions Variables Assignment. COMP1400 Week 2

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

Input. Scanner keyboard = new Scanner(System.in); String name;

CP122 CS I. Chapter 13: Recursion

Program Fundamentals

JAVA Programming Fundamentals

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

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

Object-Oriented Programming

Full file at

13 th Windsor Regional Secondary School Computer Programming Competition

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

4 WORKING WITH DATA TYPES AND OPERATIONS

Java Module Lesson 2A Practice Exercise

St. Edmund Preparatory High School Brooklyn, NY

MODULE 02: BASIC COMPUTATION IN JAVA

Introduction to Java Applications

Chapter 2 Primitive Data Types and Operations

Introduction to Java Applications; Input/Output and Operators

JAVA OPERATORS GENERAL

Chapter 2: Basic Elements of Java

Introduction to Java & Fundamental Data Types

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Lecture Set 2: Starting Java

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

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

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

JAVA Programming Concepts

Peer Instruction 1. Elementary Programming

Basic Operations jgrasp debugger Writing Programs & Checkstyle

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

Transcription:

CP122 Computer Science I Chapter 2: Data Types, Variables, and I/O

Course blogger?

QRC Tutoring

Tech News! President Obama proposes overhaul of NSA data collection

Tech News! President Obama proposes overhaul of NSA data collection Google Glass partners with Ray-Ban

Hacker's Tip of the Day: Use Linux Open Source Software

Hacker's Tip of the Day: Use Linux Open Source Software Free

Hacker's Tip of the Day: Use Linux Open Source Software Free *cough* Better *cough*

Using IDEs Eclipse JGrasp BlueJ Netbeans Dr. Java

Chapter 2: Fundamental Data Types Variables and data types Arithmetic Binary data Input/Output Strings

Data Types Every value in a Java program has a type

Data Types Every value in a Java program has a type That is, what kind of data does the value store?

Data Types Every value in a Java program has a type That is, what kind of data does the value store? Binary data: all 1's and 0's

Data Types Every value in a Java program has a type That is, what kind of data does the value store? Binary data: all 1's and 0's How are the 1's and 0's interpreted?

A Familiar Type: A Literal String public class MyFirstProgram { public static void main(string[] args) { System.out.println( This is my first program! ); } }

Other Data Types int (integer) - Any whole number Examples: 15, -50, 0

Other Data Types int (integer) - Any whole number Examples: 15, -50, 0 double (floating-point number) A number with the decimal part Examples: 3.1415, 0.25, 1E6

Expressions Expression : a combination of variables, literals, and operators Examples: 10 + 25 100 * 6-2 100 * (6-2)

Microquiz What are the data types of the following values? 87 12.5 I heart CS

Variables Variable: a named and typed place to store a value int userage = 19;

Variables Variable: a named and typed place to store a value int userage = 19; int is the type of the new variable.

Variables Variable: a named and typed place to store a value int userage = 19; userage is the name of the variable.

Variables Variable: a named and typed place to store a value int userage = 19; 19 is the initial value for the variable.

Variables Variable: a named and typed place to store a value int userage = 19; = is the assignment operator.

Variables Variable: a named and typed place to store a value int userage = 19; double hourlypay = 31.50;

Variables Variable: a named and typed place to store a value int userage = 19; double hourlypay = 31.50; String avatarname = n00bdestroyer ;

Identifier Name Rules - Consist of letters, digits, underscores, and dollar signs (do not use dollar signs) - Cannot use spaces or other symbols - Java keywords are illegal (public, static, void)

Identifier Name Style Rules - Start variable names with a lowercase letter - Capitalize subsequent words in the variable (camelcase) userage hourpay - Start class names with an uppercase letter - Use UpperCamelCase

Identifier Name Style Rules - Choose descriptive variable names! double x3 = mm2 / i6;

Identifier Name Style Rules - Choose descriptive variable names! double x3 = mm2 / i6; double hourlypay = salary / hoursworked;

Assignment Operator int userage = 19;... userage = 20; userage = 19 + 1; userage = userdays / 365;

Other Operators int userage = 19; // Increment userage (add 1 to it) userage = userage + 1;

Other Operators int userage = 19; // Increment userage (add 1 to it) userage = userage + 1; // Increment userage (add 1 to it) userage += 1;

Other Operators int userage = 19; // Increment userage (add 1 to it) userage = userage + 1; // Increment userage (add 1 to it) userage += 1; // Increment userage (add 1 to it) userage++;

Calculator.java

Storing Numbers How many distinct values can you represent given one binary bit?

Storing Numbers How many distinct values can you represent given one binary bit? 8 bits together are called a byte. How many values can be represented with a byte?

Challenge Question What is the largest value that we can store in an int variable? What does this tell us about the number of bits used to store each int variable?

Table of Sizes Bit Value 0 or 1 Byte 8 bits Kilobyte 1024 bytes Megabyte 1024 kilobytes Gigabyte 1024 megabytes Terabyte 1024 gigabytes Petabyte 1024 terabytes Exabyte 1024 petabytes

Java Primitive Data Types Integer Types byte short int long : : : : 1 2 4 8 Floating-Point Types byte bytes bytes bytes float : 4 bytes double : 8 bytes Other Types char : 2 bytes boolean : 1 bit

Binary Numbers How do we represent the decimal number 13 in binary?

Binary Numbers What decimal number does the binary number 10110 represent?

Binary Data Microquiz Convert to binary: 15 21 5 Convert to decimal: 1001 110110 101010

Crazy Microquiz Convert from decimal to base 7. 44 12 Convert from decimal to hexadecimal (base 16) 11 33

There are only 10 types of people in the world: those who understand binary and those who don't.

There are only 10 types of people in the world: those who understand ternary, those who don't, and those who thought this was going to be a binary joke.

Did you know that Halloween is the same day as Christmas? =

Did you know that Halloween is the same day as Christmas? = 25 DEC = 31 OCT

String Variables // Declare a String variable and initialize it String username = mwhitehead ; System.out.println(userName);

String Methods // Convert a String into an integer String useragestring = 23 ; int userage = Integer.parseInt(userAgeString);

More String Methods // Convert a String into an integer String useragestring = 23 ; int userage = Integer.parseInt(userAgeString); // Convert a String into a double String gpastring = 3.42 ; double gpa = Double.parseDouble(gpaString);

More String Methods // Extract a single character from a String String fullname = Stephanie Brown ; char firstletter = fullname.charat(0);

More String Methods // Extract a single character from a String String fullname = Stephanie Brown ; char firstletter = fullname.charat(0); // Extract the first nine characters from a String String fullname = Stephanie Brown ; String firstname = fullname.substring(0, 9);

More String Methods // Extract a single character from a String String fullname = Stephanie Brown ; char firstletter = fullname.charat(0); Inclusive start index // Extract the first nine characters from a String String fullname = Stephanie Brown ; String firstname = fullname.substring(0, 9);

More String Methods // Extract a single character from a String String fullname = Stephanie Brown ; char firstletter = fullname.charat(0); Exclusive end index // Extract the first nine characters from a String String fullname = Stephanie Brown ; String firstname = fullname.substring(0, 9);

More String Methods // Concatenate two strings into one String firstname = Stephanie ; String lastname = Brown ; String fullname = firstname + lastname;

Escape Sequences // Insert a newline character System.out.println( Hello \n World );

Escape Sequences // Insert a newline character System.out.println( Hello \n World ); // Insert a literal backslash (\) System.out.println( Saved in C:\\Documents );

Getting User Input import java.util.scanner;... // Create a Scanner object Scanner myscanner = new Scanner(System.in); // Read in an integer that the user types in int userint = myscanner.nextint();

Getting User Input import java.util.scanner;... // Create a Scanner object Scanner myscanner = new Scanner(System.in); // Read in an integer that the user types in int userint = myscanner.nextint(); // Read in a whole line as a String String line = myscanner.nextline();

User Input : Dialog Box import javax.swing.joptionpane;... // Create a pop-up window String prompt = Please enter your username ; String username = JOptionPane.showInputDialog(prompt);

User Input : Dialog Box import javax.swing.joptionpane;... // Create a pop-up window String prompt = Please enter your username ; String username = JOptionPane.showInputDialog(prompt); String prompt2 = Please enter your password ; String password = JOptionPane.showInputDialog(prompt2);

User Input : Dialog Box import javax.swing.joptionpane;... // Create a pop-up window String prompt = Please enter your username ; String username = JOptionPane.showInputDialog(prompt); String prompt2 = Please enter your password ; String password = JOptionPane.showInputDialog(prompt2); JOptionPane.showMessageDialog(null, Logged in! );

BetterCalculator.java

Break!