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

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

CP122 CS I. Chapter 5: Decisions

CP122 Computer Science I. Chapter 3: Methods/Functions

CS 106 Introduction to Computer Science I

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

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

What did we talk about last time? Examples switch statements

4. Java Project Design, Input Methods

PROGRAMMING FUNDAMENTALS

Fundamentals of Programming Data Types & Methods

Simple Java Reference

Getting started with Java

Chapter 4: Control Structures I

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

Variables, Constants, and Data Types

Controls Structure for Repetition

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

Basics of Java Programming

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

CP122 CS I. Chapter 13: Recursion

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

Computational Expression

CP122 Computer Science I. Chapter 2: Using Objects

Java Module Lesson 2A Practice Exercise

Introduction to Programming Using Java (98-388)

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

4 Programming Fundamentals. Introduction to Programming 1 1

Java Basic Datatypees

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

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

APCS Semester #1 Final Exam Practice Problems

MODULE 02: BASIC COMPUTATION IN JAVA

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Chapter 02: Using Data

Full file at

Software Practice 1 Basic Grammar

Chapter 3: Operators, Expressions and Type Conversion

Example: Monte Carlo Simulation 1

Object-Oriented Programming

Text User Interfaces. Keyboard IO plus

Topic 11 Scanner object, conditional execution

First Java Program - Output to the Screen

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

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

Lesson 3: Accepting User Input and Using Different Methods for Output

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

Assignment 2.4: Loops

Welcome to the Using Objects lab!

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

Introduction to Java

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

Chapter 2: Basic Elements of Java

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

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

Chapter 2: Using Data

JAVA OPERATORS GENERAL

Exercise: Inventing Language

CS 106 Introduction to Computer Science I

6.092: Java for 6.170

Pace University. Fundamental Concepts of CS121 1

AP Computer Science A

AP Computer Science Unit 1. Programs

Topics. Chapter 5. Equality Operators

Following is the general form of a typical decision making structure found in most of the programming languages:

Condensed Java. 12-Oct-15

Chapter 6 Primitive types

Using Java Classes Fall 2018 Margaret Reid-Miller

COMP-202: Foundations of Programming

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

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Mr. Monroe s Guide to Mastering Java Syntax

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Topic 11 Scanner object, conditional execution

CS 106 Introduction to Computer Science I

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

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

Review for Test 1 (Chapter 1-5)

CT 229 Fundamentals of Java Syntax

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.

Key Differences Between Python and Java

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Data Structure and Programming Languages

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

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

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

Program Fundamentals

6.096 Introduction to C++ January (IAP) 2009

Java Basic Programming Constructs

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

CS11 Java. Fall Lecture 1

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Course Outline. Introduction to java

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

Transcription:

CP122 Computer Science I Binary, Strings, Conditionals, User Input

Everysight Raptor AR cycling glasses ($499) Tech News!

Tech News! Everysight Raptor AR cycling glasses ($499) 42% of US children ages 0-8 have their own tablet

Update Zombie.java

Topics What are binary numbers? How do we get input from the user? What operations can we perform on/with Strings? How can our programs make decisions?

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

Storing Numbers How many distinct values can you create given one binary bit? 8 bits together are called a byte. How many values can be created 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 : 1 byte short : 2 bytes int : 4 bytes long : 8 bytes Floating-Point Types 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

String Object String welcome = Hi! Thanks for visiting! ;

String Object String welcome = Hi! Thanks for visiting! ; int stringsize = welcome.length();

String Object String welcome = Hi! Thanks for visiting! ; int stringsize = welcome.length(); int firsta = welcome.indexof('a');

More 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);

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 // 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 // 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();

Getting User Input // 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(); // Read in the next token as a String String token = myscanner.next();

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

User Input : Dialog Box // 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 // 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! );

FancyCalculator.java

Constants public class BasketballScoreboard { public static void main(string[] args) { // Team 1 makes a 3-pointer scoreboard.updateteam1score(3);

Constants public class BasketballScoreboard { public static final int THREE_POINTER = 3; public static final int FIELD_GOAL = 2; public static final int FOUL_SHOT = 1; public static void main(string[] args) { // Team 1 makes a 3-pointer scoreboard.updateteam1score(basketballscoreboard.three_pointer); // Team 2 makes a foul shot scoreboard.updateteam2score(basketballscoreboard.foul_shot);

Instance Variables vs. Local Variables Instance variables are declared inside a class, but outside of any specific method They can be used anywhere within the class That is, their scope is class-wide

Instance Variables vs. Local Variables Local variables are declared within a specific method They can be used only within that method That is, their scope is only method-wide

Instance Variables vs. Local Variables Why would we ever want to use local variables? Aren't instance variables more powerful?

Program Control Flow public class MyProgram { public static void main(string[] args) { // Statement 1 // Statement 2 // Statement 3 // Statement 4

Program Control Flow public class MyProgram { public static void main(string[] args) { // Statement 1 // Statement 2 // Statement 3 // Statement 4

How can we make our programs do different things?

Make Decisions public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( What is your IQ? ); int iq = Integer.parseInt(iqString); if (iq > 150) { System.out.println( You're a genius! );

Make Decisions A boolean condition: public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( evaluates to true or What is your IQ? ); false int iq = Integer.parseInt(iqString); if (iq > 150) { System.out.println( You're a genius! );

Make Decisions The body of the if- public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( statement is only executed What is if your the IQ? ); int iq = Integer.parseInt(iqString); condition is true. if (iq > 150) { System.out.println( You're a genius! );

Make Decisions public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( What is your IQ? ); int iq = Integer.parseInt(iqString); if (iq > 150) { System.out.println( You're a genius! ); else { System.out.println( Sorry! You're not a genius );

Make Decisions public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( What is your IQ? ); int iq = Integer.parseInt(iqString); The body of the elseclause is only executed if (iq > 150) { if the condition is false. System.out.println( You're a genius! ); else { System.out.println( Sorry! You're not a genius );

Make Decisions public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( Notice: else is not followed by a What condition. is your IQ? ); int iq = Integer.parseInt(iqString); It always executes if the if condition is false. if (iq > 150) { System.out.println( You're a genius! ); else { System.out.println( Sorry! You're not a genius );

Make Multiple Decisions if (iq > 150) { System.out.println( You're a genius! ); else if (iq > 125) { System.out.println( You're pretty smart. ); else if (iq > 100) { System.out.println( You're kinda smart. ); else { System.out.println( I've got some bad news... );

Make Multiple Decisions if (iq > 150) { System.out.println( You're a genius! ); else if (iq > 125) { System.out.println( You're pretty smart. ); else if (iq > 100) { System.out.println( You're kinda smart. ); else { System.out.println( I've got some bad news... ); Each else-if condition is only checked if the above conditions are all false.

Make Multiple Decisions if (iq > 150) { System.out.println( You're a genius! ); else if (iq > 125) { System.out.println( You're The else pretty block smart. ); is only executed if all the above else if (iq > 100) { conditions are false. System.out.println( You're kinda smart. ); else { System.out.println( I've got some bad news... );

Comparison Operators == : equal to < : less than > : greater than <= : less then or equal to >= : greater than or equal to!= : not equal to

Sequential ifs x = 5; y = 17; if (x < 10) { System.out.println( Small x ); if (y > 25) { System.out.println( Big y );

Sequential ifs x = 5; y = 17; if (x < 10) { System.out.println( Small x ); if (y > 25) { System.out.println( Big y ); else { System.out.println( Small y );

String/Object Comparison Operator String name1 = Terry ; String name2 = Tracy ; // DOESN'T WORK LIKE YOU THINK! if (name1 == name2) { System.out.println( A MATCH! );

String/Object Comparison Operator String name1 = Terry ; String name2 = Tracy ; // DOESN'T WORK LIKE YOU THINK! if (name1 == name2) { System.out.println( A MATCH! ); // WORKS! if (name1.equals(name2)) { System.out.println( A MATCH! );

Nested if-statements if (numberhomeruns > 30) { if (numberstrikeouts < 100) { System.out.println( You're a great hitter! ); else { System.out.println( You have power, ); System.out.println( but you miss too often );

boolean Variables boolean mybool = (x < y); if (mybool) { System.out.println( x is smaller than y );

Compound Boolean Conditions if (iq < 150 && iq > 125) { System.out.println( You're pretty smart. );

Compound Boolean Conditions if (iq < 150 && iq > 125) { System.out.println( You're pretty smart. ); Boolean AND

Compound Boolean Conditions if (iq > 150 iq < 50) { System.out.println( You're at one of the extremes ); Boolean OR

Truth Tables for AND and OR

Compound Boolean Conditions if (125 < iq < 150) { System.out.println( You're pretty smart. ); This doesn't work!

Comparing Zombies