Formatting Output & Enumerated Types & Wrapper Classes

Similar documents
Packages & Random and Math Classes

Using Classes and Objects. Chapter

Using Classes and Objects

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

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

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

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

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

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

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

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

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

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

Data Conversion & Scanner Class

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Chapter 3 Using Classes and Objects

Java Classes: Math, Integer A C S L E C T U R E 8

AP Computer Science Unit 1. Programs

Introduction to Computer Science Unit 2. Notes

Object Oriented Programming. Java-Lecture 1

Classes and Objects Part 1

Full file at

CS111: PROGRAMMING LANGUAGE II

Using Classes and Objects

The Math Class. Using various math class methods. Formatting the values.

Pace University. Fundamental Concepts of CS121 1

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

Getting started with Java

Data Representation Classes, and the Java API

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

Chapter 3 Using Classes and Objects

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

An overview of Java, Data types and variables

Chapter 3: Using Classes and Objects

Introduction to Computer Science Unit 2. Notes

Introduction to Programming Using Java (98-388)

Example: Tax year 2000

CS2141 Software Development using C/C++ C++ Basics

Interfaces. Quick Review of Last Lecture. November 6, Objects instances of classes. Static Class Members. Static Class Members

Computational Expression

AP CS Unit 3: Control Structures Notes

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Introduction to Java Applications

Lab5. Wooseok Kim

Programming with Java

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

Lesson 4 Utility classes: Math, String, I/O

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Full file at

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

Computational Expression

CIS133J. Working with Numbers in Java

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Array. Prepared By - Rifat Shahriyar

STUDENT LESSON A7 Simple I/O

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

Lab5. Wooseok Kim

JAVA WRAPPER CLASSES

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

Number Representation & Conversion

OBJECT ORIENTED PROGRAMMING IN JAVA

Introduction to Java Unit 1. Using BlueJ to Write Programs

Fall 2017 CISC124 9/16/2017

Important Java terminology

CS110: PROGRAMMING LANGUAGE I

Full file at

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

Methods CSC 121 Fall 2016 Howard Rosenthal

PROGRAMMING FUNDAMENTALS

PIC 20A Number, Autoboxing, and Unboxing

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Example packages from Java s standard class library:

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

PIC 20A The Basics of Java

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

Programming in Java

CEN 414 Java Programming

printf( Please enter another number: ); scanf( %d, &num2);

Simple Java Reference

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

Name: Checked: Access the Java API at the link above. Why is it abbreviated to Java SE (what does the SE stand for)?


CS112 Lecture: Primitive Types, Operators, Strings

Using Java Classes Fall 2018 Margaret Reid-Miller

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

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

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

CS 231 Data Structures and Algorithms, Fall 2016

Java Programming. Atul Prakash

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

Data Types Reference Types

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

Elementary Programming

Transcription:

Formatting Output & Enumerated Types & Wrapper Classes Quick review of last lecture September 8, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev String Class The String Class Because strings are so common, we don't have to use the new operator to create a String object title = "Java Software Solutions"; This is special syntax that works only for strings Each string literal (enclosed in double quotes) represents a String object String Methods String Indexes Once a String object has been created, neither its value nor its length can be changed Thus we say that an object of the String class is immutable However, several methods of the String class return new String objects that are modified versions of the original See the list of String methods on page 119 and in Appendix M It is occasionally helpful to refer to a particular character within a string This can be done by specifying the character's numeric index The indexes begin at zero in each string In the string "Hello", the character 'H' is at index 0 and the 'o' is at index 4 See StringMutation.java (page 120) 1

String Mutations Example String Mutations Example mutation1=phrase.concat(, except from vending machines. ); mutation2= mutation1.touppercase(); String Mutations Example mutation3= mutation2.replace( E, X ); mutation4= mutation3.substring(3, 30); Class Libraries A class library is a collection of classes that we can use when developing programs The Java standard class library is part of any Java development environment Its classes are not part of the Java language per se, but we rely on them heavily Various classes we've already used (System, Scanner, String) 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 import Declaration When you want to use a class from a package, you could use its fully qualified name java.util.scanner Or you can import the class, and then use just the class name import java.util.scanner; To import all classes in a particular package, you can use the * wildcard character import java.util.*; 2

The import Declaration All classes of the java.lang package are imported automatically into all programs It's as if all programs contain the following line: import java.lang.*; That's why we didn't have to import the System or String classes explicitly in earlier programs Where are the packages located? C:\Program Files\Java\jdk1.5.0\src.zip The zip file contains all libraries that ship with the java language. The Scanner class, on the other hand, is part of the java.util package, and therefore must be imported Can you add new packages? Create a directory c:\<some_path>\isu In that directory save the file Cyclone.java At the top of Cyclone.java put: package ISU; Compile Cyclone.java but don t run it. Set your CLASSPATH to c:\<some_path>\ Cyclone.java package ISU; public class Cyclone private String msg; public Cyclone (String message) msg=message; public void printmessage () System.out.println(msg); TestCyclone.java import ISU.Cyclone; public class TestCyclone public static void main(string[] args) Cyclone cy= new Cyclone("Go Cyclones!"); Chapter 3 Sections 3.4-3.5 cy.printmessage(); 3

The Random Class Methods in The Random Class The Random class is part of the java.util package It provides methods that generate pseudorandom numbers A Random object performs complicated calculations based on a seed value to produce a stream of seemingly random values Random Numbers Example The Math Class The Math class is part of the java.lang package See RandomNumbers.java (page 126) The Math class contains methods that perform various mathematical functions These include: absolute value square root exponentiation trigonometric functions Math Class The Math Class The methods of the Math class are static methods (also called class methods) Static methods can be invoked through the class name no object of the Math class is needed value = Math.cos(90) + Math.sqrt(delta); See Quadratic.java (page 129) We discuss static methods further in Chapter 6 4

Chapter 3 Sections 3.6-3.8 Run examples from the book Formatting Output Methods in NumberFormat Class Two new classes DecimalFormat NumberFormat NumberFormat Example Methods in DecimalFormat Class double dollars=5.994; NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println ( Price = + fmt.format(dollars) ); RESULT: Price = $5.99 5

DecimalFormat Example The printf Method Provided as a courtesy to C programmers double miles =.5395; DecimalFormat fmt = new DecimalFormat( 0.### ); System.out.println ( Miles = + fmt.format(miles) ); System.out.printf( ID: %5d\tName: %s, id, name); RESULT: Miles = 0.540 Miles = 0.54 The printf convention %d print an int argument in decimal %ld print a long int argument in decimal %c print a character %s print a string %f print a float or double argument %e same as %f, but use exponential notation %g use %e or %f, whichever is better %o print an int argument in octal (base 8) %x print an int argument in hexadecimal (base 16) %% print a single % Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type byte short int long float double char boolean void Wrapper Class Byte Short Integer Long Float Double Character Boolean Void [From: www.eskimo.com/~scs/cclass/notes/sx6a.html ] Integer Class Wrapper Classes The following declaration creates an Integer object which represents the integer 40 as an object Integer age = new Integer(40); An object of a wrapper class can be used in any situation where a primitive value will not suffice For example, some objects serve as containers of other objects Primitive values could not be stored in such containers, but wrapper objects could be 6

Wrapper Classes Wrapper classes also contain static methods that help manage the associated type For example, the Integer class contains a method to convert an integer stored in a String to an int value: num = Integer.parseInt(str); The wrapper classes often contain useful constants as well For example, the Integer class contains MIN_VALUE and MAX_VALUE which hold the smallest and largest int values Autoboxing Autoboxing is the automatic conversion of a primitive value to a corresponding wrapper object: Integer obj; int num = 42; obj = num; The assignment creates the appropriate Integer object The reverse conversion (called unboxing) also occurs automatically as needed Autoboxing Examples Integer obj1; int num1 = 69; obj1 = num1; // automatically creates an //integer object THE END Integer obj2= new Integer(69); int num2; num2 = obj2; // automatically extracts //the int value 7