Using System.out.println()

Similar documents
} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

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

Notes from the Boards Set BN19 Page

CS 231 Data Structures and Algorithms, Fall 2016

1.00/1.001 Tutorial 1

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

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

CS 106 Introduction to Computer Science I

AP Computer Science A

Introduction to Programming Using Java (98-388)

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS11 Java. Fall Lecture 1

More on variables and methods

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

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

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

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

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Getting started with Java

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

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

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Midterms Save the Dates!

Java Fall 2018 Margaret Reid-Miller

Chapter 1 Introduction to java

CS 177 Week 15 Recitation Slides. Review

} Each object in a Java program has an identifier (name) } This includes:

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

Programming with Java

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Jython. secondary. memory

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

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

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

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

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Elementary Programming

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

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

last time in cs recitations. computer commands. today s topics.

Full file at

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

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

(Refer Slide Time: 00:23)

Mr. Monroe s Guide to Mastering Java Syntax

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

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

106A assignment review #4. 13 Feb :30p-6:30p.! Miles Seiver

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Software and Programming 1

CIS 110: Introduction to Computer Programming

Program Fundamentals

Lecture Set 4: More About Methods and More About Operators

"Hello" " This " + "is String " + "concatenation"

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Lecture 1. Types, Expressions, & Variables

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

Object-Oriented Programming

CS 106 Introduction to Computer Science I

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

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

Lab 3: Call to Order CSCI 2101 Fall 2017

Comp Assignment 2: Object-Oriented Scanning for Numbers, Words, and Quoted Strings

Peer Instruction 1. Elementary Programming

Lecture Set 4: More About Methods and More About Operators

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

Software and Programming 1

Introduction to Java Applications

Lecture 2: Variables & Assignments

CMPSCI 187 / Spring 2015 Hangman

Midterms Save the Dates!

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Lab 3: Call to Order CSCI 2101 Fall 2018

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

What did we talk about last time? Examples switch statements

Lecture Notes for CS 150 Fall 2009; Version 0.5

EE 422C HW 6 Multithreaded Programming

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

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

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

CEN 414 Java Programming

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSE20 : Lab #4 - Data Types

CS1 Lecture 3 Jan. 18, 2019

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

CHAPTER INTRODUCTION

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

Zheng-Liang Lu Java Programming 45 / 79

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

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Datatypes, Variables, and Operations

Transcription:

Programming Assignments Read instructions carefully Many deduction on Program 3 for items in instructions Comment your code Coding conventions 20% of program grade going forward Class #23: Characters, Strings, and Elementary Debugging Software Design I (CS 120): D. Mathias, 06 March Other conventions listed in instructions Do not zip just the Java file. Put all src files in a folder: LastFirst (e.g. MathiasDavid) zip that folder and submit 19 September 2018 Software Design I (CS 120) 2 Displaying Text with System.out.println } Debugging programs often requires knowing what the values of some variables are during run-time } Explains performance issues (e.g., Why is the window in the wrong place when I draw it in my program? ) } Can be complex and error-prone to calculate by hand } One possibility: have the program print out the values as it runs, so you can see what it is doing } One simple way to do this is with a print or print-line command (these each do the same thing, but the second one adds a line-break at the end of what it prints out): System.out.print( Hello ); System.out.println( Hello ); Using System.out.println() } This print-line method writes characters to the default output location (usually a terminal screen) } In Eclipse, text will usually appear at the bottom of the editing area, but in other contexts, when the program runs, the text may appear somewhere else entirely } It will automatically convert most primitive types to a printable representation of their value: System.out.println( 100 ); System.out.println( 38.7 ); } The command also has many uses in writing simple programs that give output back to the user: System.out.println( 3 * 4 + 5 / 6 3 * 2 ); 19 September 2018 Software Design I (CS 120) 3 19 September 2018 Software Design I (CS 120) 4 1

Elementary Debugging with println() } When you run into difficulties in a program, it is sometimes useful to know the values of some of your variables, if that is not already obvious to you } If you are having trouble determining why an object is not where you want it, for example, you might print out its location variables after some calculation: Code Examples: Debugging Let s debug some actual Java code examples int windowwidth = 500; int windowheight = 400; win.setsize( windowwidth, windowheight ); int ovalsize = 100; int ovalloc = windowwidth / 2 - ovalsize; System.out.println( ovalloc ); 19 September 2018 Software Design I (CS 120) 5 19 September 2018 Software Design I (CS 120) 6 Characters } The Java char primitive type } Represents a single character, and is given values using single quotes: char ch1 = 'a'; char ch2 = '8'; char ch3 = '#'; } Has increment/decrement operators, just like an integer: char ch = 'a'; ch++; System.out.println( ch ); // gives output: b ch = '8'; ch -; System.out.println( ch ); // gives output: 7 } The char type is stored using 16 bits (2 bytes) of memory } Will automatically widen to any larger numerical types (int, long, float, double) } Can be cast down to generate a char from an int, etc. } Note: casting up & down with char can be surprising until you learn the basic character/number equivalencies! char ch = '2'; int i = ch; System.out.println( i ); // output: 50 ch = (char) i; System.out.println( ch ); // output: '2' i = 99; ch = (char) 99; System.out.println( ch ); // output: c 19 September 2018 Software Design I (CS 120) 7 19 September 2018 Software Design I (CS 120) 8 2

} One actually handy use of char arithmetic and int conversion is when we want to compute the n-th letter in the alphabet sequence (which comes in handy surprisingly often!) [C] Finally, do assignment. When the code is complete, the value of let13 is now the char 'm'. char leta = 'a'; char let13 = (char) ( leta + 12 ); [B] Now do cast step, back to character that is 12 positions higher than a. Note: this is only done after the math due to parentheses on the arithmetic. [A] We have mixedmode arithmetic, so we will widen all values to largest type (int). This takes position of a (97) and adds 12 to it (109). 19 September 2018 Software Design I (CS 120) 9 char letter13 = (char) ( leta + 12 ); } The reason something like this works is that certain parts of the character set Java uses are encoded in sequential order: char bunch of stuff a b... y z bunch more stuff position 0 1... 96 97 98... 121 122 123... } We can use this fact, plus the auto-coercion of the smaller type (char) to the larger type (int), to slide up and down in the order } This is a much better idea than trying to remember these numbers ourselves (or looking them up every time we need them)! } Same trick works for upper-case letters A Z, and digits 0 9 char capital26 = (char) ( 'A' + 25 ); // 'Z' char digit4 = (char) ( '0' + 3 ); // '3' 19 September 2018 Software Design I (CS 120) 10 Printing & Storing Longer Sequences } The Java String class } A special basic class that is fundamental to the language } Has some behavior that is similar to primitive types } Can be specified as a sequence of characters in double quotes } Use of new and constructor is optional and not required String s = "Hello"; String s2 = new String( "Hello There!" ); String s3 = "Hello There!\n"; } Basic concatenation (+) operation: takes any values, converts to String objects if needed, and chains them together: String s = "Hello" + "There!"; // "HelloThere!" String s2 = "Hello" + " " + "There!"; // "Hello There!" System.out.println( "x = " + 100 ); // "x = 100" 19 September 2018 Software Design I (CS 120) 11 Strings are Not Primitive Types } String is a reference type (class) in Java } There is no direct conversion between String and any primitive value String s = "3"; String s2 = 3; String s3 = '3'; int number = "3"; } Basic concatenation (+) operation can force the conversion of a primitive to a String: This is OK. These are all compile errors, even with char type! String s1 = "Number is " + number; // Number is 3" String s2 = "" + number; // "3" This is the empty String. It is a String, but has no characters in it. 19 September 2018 Software Design I (CS 120) 12 3

Converting Other Types to String } Basic concatenation (+) operation can force the conversion of other values to a String: String s1 = "Number is " + number; // Number is 3" String s2 = "" + number; // "3" } Be careful: where you place quotation marks matters! } Anything in quotes is reproduced exactly as typed String s1 = "" + number; // "3" String s2 = "" + "number"; // "number" Strings are Immutable } In Java, once created, a String does not change } Thus code like this will run fine, but will not change the content of the String in any way String name = David"; name.touppercase(); System.out.println( name ); // David } If we want to change the String, we must replace it with a new one, using an assignment: String name = David"; name = name.touppercase(); System.out.println( name ); // DAVID 19 September 2018 Software Design I (CS 120) 13 19 September 2018 Software Design I (CS 120) 14 String Methods in the String Class String <<constructor>> String( String ) <<query>> char charat( int ) int length() String substring( int ) String substring( int, int ) <<translate>> String tolowercase() String touppercase() } Strings are objects with many methods Outputs character at input position first char at: 0 last char at: length() 1 Outputs length of String Outputs sub-part of String, starting from input position, going all the way to end Outputs subpart of String, starting from first input position, going to second - 1 Methods in the String Class String first, last; first = "Ronald"; last = "McDonald ; System.out.print( first.charat( 1 ) ); // o System.out.print( last.length() ); // 8 System.out.print( last.substring( 2 ) ); // Donald System.out.print( first.substring( 0, 3 ) ); // Ron first = first.tolowercase(); last = last.touppercase(); <<constructor>> String( String ) <<query>> char charat( int ) int length() String substring( int ) String substring( int, int ) <<translate>> String tolowercase() String touppercase() Output lower/upper case version (Note: do not change original) Full list online at: http://docs.oracle.com/javase/8/docs/api/java/lang/string.html System.out.println( first + " " + last ); // ronald MCDONALD 19 September 2018 Software Design I (CS 120) 15 19 September 2018 Software Design I (CS 120) 16 4

Coming Attractions } Meetings this week: } Monday/Wednesday: regular classroom } Tuesday/Friday: in the CS Lab (16 Wing) } Program 04: due Wed. 13 March, 11:59 PM on Canvas } Program 05: due Wed. 27 March } Reading assignment: Ch. 5 } Next Monday, 11 March, by start of class } Office Hours: Wing 212 } Monday/Wednesday/Friday: 11:00 AM 12:00 PM } Tuesday: 3:00 PM 4:00 PM 19 September 2018 Software Design I (CS 120) 17 5