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

Similar documents
Packages & Random and Math Classes

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Using Classes and Objects. Chapter

Formatting Output & Enumerated Types & Wrapper Classes

Using Classes and Objects

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

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

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

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

Java Basic Introduction, Classes and Objects SEEM

Java Basic Introduction, Classes and Objects SEEM

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Anatomy of a Class Encapsulation Anatomy of a Method

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Using Classes and Objects

Where do objects come from? Good question!

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

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

Code Listing H-1. (Car.java) H-2 Appendix H Packages

Using Classes and Objects

Classes and Objects Part 1

Designing Classes part 2

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

Object Oriented Programming. Java-Lecture 1

CS 112 Introduction to Programming

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

CSE1720 Delegation Concepts (Ch 2)

CS 211: Existing Classes in the Java Library

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

CS 335 Lecture 02 Java Programming

Objects and Classes -- Introduction

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

Data Representation Classes, and the Java API

Chapter 2: Basic Elements of Java

Data Conversion & Scanner Class

Introduction to Java Unit 1. Using BlueJ to Write Programs

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Basic Computation. Chapter 2

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

Introduction to Computer Science Unit 2. Exercises

Chapter 4 Classes in the Java Class Libraries

1 Shyam sir JAVA Notes

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

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Chapter 4: Writing Classes

COMP-202 More Complex OOP

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CS 335 Java Programming Controls. Fall 2007

AP Computer Science Unit 1. Programs

Introduction to Software Development (ISD) David Weston and Igor Razgon

PIC 20A The Basics of Java

Chapter Goals. Chapter 7 Designing Classes. Discovering Classes Actors (end in -er, -or) objects do some kinds of work for you: Discovering Classes

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

System.out.print(); Scanner.nextLine(); String.compareTo();

Object-Based Programming. Programming with Objects

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

COMP 202. Java in one week

Lecture 5: Methods CS2301

Introduction to Computer Science I

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

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

Computer Programming I - Unit 2 Lecture 1 of 13

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

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

H212 Introduction to Software Systems Honors

Class Libraries and Packages

Chapter 4. Mathematical Functions, Characters, and Strings

CSCE3193: Programming Paradigms

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

Example: Tax year 2000

CS111: PROGRAMMING LANGUAGE II

Elementary Programming

COMP 202 Java in one week

Using Java Classes Fall 2018 Margaret Reid-Miller

Decisions: Logic Java Programming 2 Lesson 7

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

Full file at

Flexible data exchange with a method Parameters and return value

Introduction to Computer Science Unit 2. Notes

Example packages from Java s standard class library:

CSC 1051 Data Structures and Algorithms I

Chapter VI. Using Object Methods. Chapter VI Topics

Overview. Software Code is Everywhere. Software Crisis. Software crisis Development approaches Reusability Java packages API Sample classes:

Packages. Examples of package names: points java.lang com.sun.security drawing.figures

Chapter 4 Writing Classes

Full file at

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal

AP CS Unit 3: Control Structures Notes

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

Chapter 2: Data and Expressions


COMP-202 Unit 8: Basics of Using Objects

AP Computer Science A

Transcription:

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

From last time: Account Declaring an Account object: Account acct1 = new Account ("Ted Murphy", 72354, 102.56); Now acct1 is a usable object reference variable. Compare to: Scanner scan = new Scanner(System.in); acct1 Ted Murphy, 72354, 102.56 Object ref Object itself Using Account methods: use dot on object ref variable acct1.deposit (25.85); // changes balance in object acct1.addinterest(); Here we are calling methods of Account, namely deposit and addinterest 2

From last time: Account Account acct1 = new Account ("Ted Murphy", 72354, 102.56); Now acct1 is a usable object reference variable. Picture: blue areas depict memory areas acct1 Ted Murphy, 72354, 102.56 Variable Name: acct1, value: pointer to object Note how the object content is in a different memory area Compare to int variable: An int variable is self-contained in one memory area int height = 60; height 60 Variable Name: height, value: 60 3

Sec. 3.2 The String class We can create a String object using its constructor, listed on pg. 119: String greeting = new String( Hi! How are you ); But we usually use the more convenient form allowed by Java String greeting = Hi! How are you? ; Either way, we end up the a String object with contents Hi!, and an object reference named greeting greeting Hi! How are you? 4

Sec. 3.2 The String class greeting Hi! How are you? Now that we have an object variable (AKA reference), we can call String methods: int len = greeting.length(); // length method if (greeting.equals( Hello )) // equals method System.out.println(greeting.toUpperCase()); The last prints HI! HOW ARE YOU? 5

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 (Look them up on Sun website) Other class libraries can be obtained through third party vendors, or you can create them yourself 6

Packages The classes of the Java standard class library are organized into packages Some packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing 7

The import Declaration When you want to use a class contained in a package, you can use its fully qualified name java.util.scanner scan =... Or you can import the package containing the class and just use the class name Scanner import java.util.scanner; Scanner scan =... To import all classes in a particular package, you can use the * wildcard character import java.util.*; 8

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 The Scanner class, on the other hand, is part of the java.util package, so that class must be imported as part of its package 9

The Random Number Generator Math.random() Needed for project 2, throwing dice Computer-generated truly random numbers are impossible We can call them pseudorandom numbers But they are such good fakes they can be used with confidence 10

Tiny Math.random() Example public class Random1 { public static void main (String[] args) { double num1 = Math.random(); // one random no. (< 1.0 double num2 = Math.random(); // another one System.out.println ("Random nos: " + num1 +" " + num2); } } Note: random() is a static method of the Math class So we use <classname>.<methodname> to call it. 11

Some generated numbers > run Random1 Random nos: 0.01538480070275039 0.33946741237265876 > run Random1 Random nos: 0.8105596566230356 0.8913714271446472 > run Random1 Random nos: 0.40997968141459673 0.893665312675838 We can turn these into ints using a little arithmetic 12

Math.random(): Random ints public class Random2 { public static void main (String[] args) { // Using *20, then convert to int // to generate random ints 0-19 int num1 = (int)(math.random()*20);// one random no. int num2 = (int)(math.random()*20);// another one System.out.println ("Random ints: " + num1 + " " + num2); } } 13

Random2 results: random ints 0-19 > run Random2 Random ints: 8 12 > run Random2 Random ints: 17 12 > run Random2 Random ints: 0 8 Throwing dice: 6 possibilities so use (int)math.random()*6) for 6 outcomes 0, 1, 2, 3, 4, 5 Add one to shift to 1, 2, 3, 4, 5, 6 14

Formatting Output: Sec 3.6 Look at NumberFormat and DecimalFormat classes in the text They provide you with ways to output numbers with a predefined precision For example: Printing double value of Pi Printing only 2 decimal digits 3.14 3.141592 15