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

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

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

Classes and Objects Part 1

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

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

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

Using Classes and Objects. Chapter

Chapter. Let's explore some other fundamental programming concepts

Chapter 2: Data and Expressions

Formatting Output & Enumerated Types & Wrapper Classes

Chapter 2: Data and Expressions

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

Packages & Random and Math Classes

COMP-202 Unit 8: Basics of Using Objects

Using Classes and Objects

Full file at

Using Java Classes Fall 2018 Margaret Reid-Miller

Chapter 3: Using Classes and Objects

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

ing execution. That way, new results can be computed each time the Class The Scanner

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

More on variables and methods

ECE 122 Engineering Problem Solving with Java

COMP-202 Unit 5: Basics of Using Objects

Computational Expression

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

Chapter 2: Data and Expressions

Using Classes and Objects

2: Basics of Java Programming

Chapter 2 ELEMENTARY PROGRAMMING

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

Lecture Set 2: Starting Java

Objects and Classes -- Introduction

Lecture Set 2: Starting Java

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 2 Elementary Programming

Data Conversion & Scanner Class

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

ECE 122. Engineering Problem Solving with Java

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

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

Java Basic Introduction, Classes and Objects SEEM

Lecture 3: Variables and assignment

Primitive Data Types: Intro

AP Computer Science A

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Getting started with Java

First Java Program - Output to the Screen

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Chapter 2: Using Data

Reading Input from Text File

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Using Classes and Objects

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

More non-primitive types Lesson 06

2 rd class Department of Programming. OOP with Java Programming

Computational Expression

CMPT 125: Lecture 3 Data and Expressions

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Primitive Data Types and Operations

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

Full file at

Java Basic Introduction, Classes and Objects SEEM

Data Representation Classes, and the Java API

JAVA WRAPPER CLASSES

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

VARIABLES, DATA TYPES,

Midterms Save the Dates!

CS 302: Introduction to Programming

What did we talk about last time? Examples switch statements

Program Elements -- Introduction

PIC 20A Number, Autoboxing, and Unboxing

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

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

CS 106 Introduction to Computer Science I

Class Library java.lang Package. Bok, Jong Soon

An overview of Java, Data types and variables

Important Java terminology

B.V. Patel Institute of BMC & IT, UTU 2014

STUDENT LESSON A10 The String Class

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

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

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

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

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

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

H212 Introduction to Software Systems Honors

Basic Computation. Chapter 2

Basic Computation. Chapter 2

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

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

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

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

Transcription:

We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects Class libraries Wrapper classes Standard I/O Formatting output Initially, we can think of an object as a collection of services that we can tell it to perform for us The services are defined by methods in a class that defines the object In the Bush program, we invoked the println method of the System.out object: System.out.println ("We need an energy bill that encourages consumption."); object method Information provided to the method (parameters) 1

The System.out object provides another service as well The print method is similar to the println method, except that it does not advance to the next line Therefore anything printed after a print statement will appear on the same line See Countdown.java An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't really have to think about its internal details in order to use it We don't have to know how the println method works in order to invoke it Therefore, we can write complex software by organizing it carefully into classes and objects 2

Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks, represents a String object The string concatenation operator (+) is used to append one string to the end of another It can also be used to append a number to a string A string literal cannot be broken across two lines in a program See Facts.java The plus operator (+) is also used for arithmetic addition The function that the + operator performs depends on the type of the information on which it operates If both operands are strings, or if one is a string and one is a number, it performs string concatenation If both operands are numeric, it adds them The + operator is evaluated left to right Parentheses can be used to force the operation order See Addition.java 3

!" What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret the second quote as the end of the string System.out.println("I said "Hello" to you."); An escape sequence is a series of characters that represents a special character An escape sequence begins with a backslash character (\), which indicates that the character(s) that follow should be treated in a special way System.out.println ("I said \"Hello\" to you.");!" Some Java escape sequences: Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash See Poem.java 4

A variable either holds a primitive type, or it holds a reference to an object A class name can be used as a type to declare an object reference variable String name; No object has been created with this declaration An object reference variable holds the address of an object The object itself must be created separately We use the new operator to create an object name = new String ("Sparky the clown"); This calls the String constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class 5

Because strings are so common, we don't have to use the new operator to create a String object name = "Sparky the clown"; This is special syntax that only works for strings Once an object has been instantiated, we can use the dot operator to invoke its methods name.length() The String class has several methods that are useful for manipulating strings, ex: String(String str), char charat(int index), int compareto(string str), String concat(string str), boolean equals(string str), boolean equalsignorecase(string str), int length(), String replace(char oldchar, char newchar), String substring(int offset, int endindex), String tolowercase(), String touppercase(), Many of the methods return a value, such as an integer or a new String object See the list of String methods in Appendix M See StringMutation.java 6

# A class library is a collection of classes that we can use when developing programs There is a Java standard class library that is part of any Java development environment These classes are not part of the Java language per se, but we rely on them heavily The System class and the String class are part of the Java standard class library Other class libraries can be obtained through third party vendors, or you can create them yourself The classes of the Java standard class library are organized into packages Some of the packages in the standard class library are: Package java.lang java.applet (java.awt javax.swing java.net java.util Purpose General support Creating applets for the web Graphics and graphical user interfaces) Additional graphics capabilities and components Network communication Utilities 7

$% When you want to use a class from a package, you could use its fully qualified name java.util.random Or you can import the class, then just use the class name import java.util.random; To import all classes in a particular package, you can use the * wildcard character import java.util.*; $% All classes of the java.lang package are automatically imported into all programs That's why we didn't have to explicitly import the System or String classes in earlier programs The Random class is part of the java.util package It provides methods that generate pseudo-random numbers We often have to scale and shift a number into an appropriate range for a particular purpose See RandomNumbers.java 8

Some methods can be invoked through the class name, instead of through an object of the class These methods are called class methods or static methods The Math class contains many static methods, providing various mathematical functions, such as absolute value, trigonometry functions, square root, etc. temp = Math.cos(90) + Math.sqrt(delta); Two categories of data in Java : primitive data and objects. We use wrapper classes to manage primitive data as objects Each wrapper class represents a particular primitive type Integer everest = new Integer(8850); We have just «wrapped» the primitive integer value8850 into an object referenced by theeverest variable. All wrapper classes are part of thejava.lang package: Byte, Short, Integer, Long, Float, Double, Character, Boolean, Void. 9

$$& Integer (int value) byte bytevalue() double doublevalue() float floatvalue() int intvalue() long longvalue() static int parseint (String str) static String tobinarystring(int num) The Scanner class is part of the Java standard class library Part of the java.util package Contains several methods for reading input values for various types from various sources (keyboard, file) A Scanner object assumes that white space (delimiters) separates elements of the input (tokens) See Echo.java 10

$$& Scanner (InputStream source) Scanner (File source) Scanner (String source) String next () String nextline () double nextdouble () float nextfloat () int nextint () long nextlong () short nextshort () String nextboolean () Boolean hasnext () '$ The NumberFormat class has static methods that return a formatter object getcurrencyinstance() getpercentinstance() Each formatter object has a method called format that returns a string with the specified information in the appropriate format See PriceOfHappiness.java 11

'$ TheDecimalFormat class can be used to format a floating point value in generic ways For example, you can specify that the number be printed to three decimal places The constructor of thedecimalformat class takes a string that represents a pattern for the formatted number See CircleStats.java 12