Fall 2017 CISC124 9/27/2017

Similar documents
5/24/2006. Last Time. Announcements. Today. Method Overloading. Method Overloading - Cont. Method Overloading - Cont. (Midterm Exam!

Fall 2017 CISC124 10/1/2017

Week 6: Review. Java is Case Sensitive

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class:

Fall 2017 CISC124 9/16/2017

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

Fundamental Java Syntax Summary Sheet for CISC124, Fall Java is Case - Sensitive!

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

We now start exploring some key elements of the Java programming language and ways of performing I/O

Fundamental Java Syntax Summary Sheet for CISC101, Spring Java is Case - Sensitive!

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

Getting started with Java

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

Using Java Classes Fall 2018 Margaret Reid-Miller

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

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

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

Full file at

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

COE318 Lecture Notes Week 4 (Sept 26, 2011)

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

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

CS11 Java. Fall Lecture 1

Introduction to Programming Using Java (98-388)

CSC Java Programming, Fall Java Data Types and Control Constructs

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

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

Chapter 2: Data and Expressions

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

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

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

CIS 110: Introduction to Computer Programming

Lecture 1. Course Overview Types & Expressions

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Week 7 - More Java! this stands for the calling object:

Today CISC124. Building Modular Code. Designing Methods. Designing Methods, Cont. Designing Methods, Cont. Assignment 1 due this Friday, 7pm.

H212 Introduction to Software Systems Honors

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

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

SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

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

Chapter 2: Using Data

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

MATHEMATICAL FUNCTIONS CHARACTERS, AND STRINGS. INTRODUCTION IB DP Computer science Standard Level ICS3U

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java Basics. SP17 ICS 111 Ed Meyer

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

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

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

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

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

Chapter 2: Data and Expressions

Operators and Expressions

Fall 2017 CISC/CMPE320 9/27/2017

VARIABLES AND TYPES CITS1001

ECE 122. Engineering Problem Solving with Java

First Java Program - Output to the Screen

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CS 1301 Ch 8, Part A

Program Elements -- Introduction

COMP String and Console I/O. Yi Hong May 18, 2015

Java Basic Datatypees

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

Variables, Constants, and Data Types

Visual C# Instructor s Manual Table of Contents

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CS 106 Introduction to Computer Science I

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

Programming with Java

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

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

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Chapter 02: Using Data

JavaScript: The Basics

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

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

Java Overview An introduction to the Java Programming Language

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS 61B Data Structures and Programming Methodology. June David Sun

Strings, Strings and characters, String class methods. JAVA Standard Edition

HTML5 and CSS3 More JavaScript Page 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Review of Java. or maybe not

PIC 20A Number, Autoboxing, and Unboxing

CISC 110 Week 3. Expressions, Statements, Programming Style, and Test Review

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

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

Programming Language Basics

Chapter 2: Data and Expressions

Lecture 2: Intro to Java

Lecture Set 4: More About Methods and More About Operators

M e t h o d s a n d P a r a m e t e r s

Formatting Output & Enumerated Types & Wrapper Classes

An overview of Java, Data types and variables

Transcription:

CISC124 Assignment 1 due this Friday at 7pm by submission to an onq dropbox. First onq quiz next week write in lab. More details in yesterday s lecture. Today Intro. to 2D Arrays (and iteration examples). Useful Java classes. Fall 2017 CISC124 - Prof. McLeod 1 Fall 2017 CISC124 - Prof. McLeod 2 Introduction to Multi-Dimensional Arrays We have discussed the declaration of 1D arrays. For example: double[] oned = new double[100]; To assign values, for example: for (int i = 0; i < oned.length; i++) oned[i] = i; For each dimension, just add another set of [ ]: int[][] twod = new int[4][20]; Has room for 80 values. To assign, for example: int row, col; for (row = 0; row < twod.length; row++) for (col = 0; col < twod[row].length; col++) twod[row][col] = row * col; Fall 2017 CISC124 - Prof. McLeod 3 Fall 2017 CISC124 - Prof. McLeod 4 It is helpful to think of a 2D array as storing tabular data, like a spreadsheet. For convenience (and to help you code consistently) you can think of the first dimension as the rows and the second dimension as the columns. To get the number of rows, use twod.length, and to get the number of columns, grab one of the rows and use twod[0].length. You can use three sets of [ ] to get a 3 dimensional array. Using the spreadsheet analogy, the third dimension could be the sheet number, where each sheet contains a table. For a good analogy for a 4D array you might need to look up a Star Trek script Fall 2017 CISC124 - Prof. McLeod 5 Fall 2017 CISC124 - Prof. McLeod 6 Prof. Alan McLeod 1

See TestLoops.java Iterating 2D Arrays Can a for/each loop be used to assign or alter values in an array? Fall 2017 CISC124 - Prof. McLeod 7 Some Useful Java Classes The classes defined in the java.lang package are automatically imported for you, since they are used quite often. They include: The Wrapper classes Math Object String System Thread (How do I know all this?...) Fall 2017 CISC124 - Prof. McLeod 8 Aside - static Methods static attributes and methods are loaded once into memory and not garbage collected until main is finished. These methods will run faster the second time (and later) they are invoked. Generally, they are utility methods that do not depend on the values of a class attributes. static methods can be invoked without instantiation of the Object that owns them. Math.random(), for example. static Methods, Cont. static methods and attributes are shared by all instances of a class there is only one copy of these methods in memory. A static method can only invoke other static methods in its own class you can t have pieces of code disappearing from a static method in memory... This is all done for reasons of ease of use and efficiency. Fall 2017 CISC124 - Prof. McLeod 9 Fall 2017 CISC124 - Prof. McLeod 10 Math Class As you would expect: A collection of static constants and static mathematical methods. You cannot instantiate the Math class, but why would you want to? Let s just look over the API. Wrapper Classes Sometimes it is necessary for a primitive type value to be an Object, rather than just a primitive type. Some data structures only store Objects. Some Java methods only work on Objects. Wrapper classes also contain some useful constants and a few handy methods. Fall 2017 CISC124 - Prof. McLeod 11 Fall 2017 CISC124 - Prof. McLeod 12 Prof. Alan McLeod 2

Wrapper Classes - Cont. Each primitive type has an associated wrapper class: char int long float double Character Integer Long Float Double Each wrapper class Object can hold the value that would normally be contained in the primitive type variable, but now has a number of useful static methods. Wrapper Classes - Cont. Integer number = new Integer(46);// Wrapping Integer num = new Integer( 908 ); Integer.MAX_VALUE // gives maximum integer Integer.MIN_VALUE // gives minimum integer Integer.parseInt( 453 ) // returns 453 Integer.toString(653) // returns 653 number.equals(num) // returns false int anumber = number.intvalue(); // anumber is 46 Fall 2017 CISC124 - Prof. McLeod 13 Fall 2017 CISC124 - Prof. McLeod 14 Wrapper Classes Cont. The Double wrapper class has equivalent methods: Double.MAX_VALUE // gives maximum double value Double.MIN_VALUE // gives minimum double value Double.parseDouble( 0.45E-3 ) // returns 0.45E-3 See the API for other methods and constants dealing with NaN and -Infinity and Infinity Fall 2017 CISC124 - Prof. McLeod 15 Wrapper Classes Cont. The Character wrapper class: has methods to convert between ASCII and Unicode numeric values and characters. isdigit(character) returns true if character is a digit. isletter(character) isletterordigit(character) isuppercase(character) islowercase(character) iswhitespace(character) tolowercase() touppercase() Fall 2017 CISC124 - Prof. McLeod 16 System Class Other Useful System Class Methods We have used: System.out.println() System.out.print() System.out.printf() Also: System.err.println() System.currentTimeMillis() Returns, as a long, the number of milliseconds elapsed since midnight Jan. 1, 1970. System.exit(0) Immediate termination of your program. System.getProperties() All kinds of system specific info - see the API. System.getProperty(string) Displays single system property. System.nanoTime() Time in nanoseconds Fall 2017 CISC124 - Prof. McLeod 17 Fall 2017 CISC124 - Prof. McLeod 18 Prof. Alan McLeod 3

Strings, so Far String literals: Press <enter> to continue. String variable declaration: String teststuff; or: String teststuff = A testing string. ; String concatenation ( addition ): String teststuff = Hello ; System.out.println(testStuff + to me! ); Would print the following to the console window: Hello to me! Strings - Cont. Escape sequences in Strings: These sequences can be used to put special characters into a String: \ a double quote \ a single quote \\ a backslash \n a linefeed \r a carriage return \t a tab character Fall 2017 CISC124 - Prof. McLeod 19 Fall 2017 CISC124 - Prof. McLeod 20 Strings, so Far - Cont. For example, the code: System.out.println( Hello\nclass! ); prints the following to the screen: Hello class! Since String s are Objects they can have methods. String methods (67 of them!) include: length() equals(otherstring) equalsignorecase(otherstring) tolowercase() touppercase() trim() charat(position) substring(start) substring(start, End) Fall 2017 CISC124 - Prof. McLeod 21 Fall 2017 CISC124 - Prof. McLeod 22 indexof(searchstring) replace(oldchar, newchar) startswith(prefixstring) endswith(suffixstring) valueof(integer) String s do not have any attributes. See the API Docs for details on all the String class methods. A few examples: int i; boolean abool; String teststuff = A testing string. ; i = teststuff.length(); // i is 17 abool = teststuff.equals( a testing string. ); // abool is false Fall 2017 CISC124 - Prof. McLeod 23 abool = teststuff.equalsignorecase( A TESTING STRING. ); // abool is true Fall 2017 CISC124 - Prof. McLeod 24 Prof. Alan McLeod 4

char achar; achar = teststuff.charat(2); // achar is t i = teststuff.indexof( test ); // i is 2 Fall 2017 CISC124 - Prof. McLeod 25 Aside - More about String s Is Hello class (a String literal) an Object? Yup, Hello class!.length() would return 12. Also, String s are immutable meaning that they cannot be altered, only re-assigned. There are no methods that can alter characters inside a string while leaving the rest alone. Arrays are mutable, in contrast any element can be changed Fall 2017 CISC124 - Prof. McLeod 26 Aside - Exercises You are now ready for exercises 4, 5 and 6: Exercise 4 on File I/O the content will not be discussed in class. Exercise 5 will give you practice with the String and Character classes. Exercise 6 will give you more practice with modular program design and get you thinking about Object Oriented design. Other java.lang Classes Object is the base class for all objects in Java. We ll need to learn about object hierarchies (Inheritance) for this to make more sense. Thread is a base class used to create threads in multi-threaded program. More about this topic near the end of the course. Fall 2017 CISC124 - Prof. McLeod 27 Fall 2017 CISC124 - Prof. McLeod 28 Prof. Alan McLeod 5