1 chap. 3 creating objects the string class java class library (packages) math.random() reading for...

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

Upload: simon-wells

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

From last time: Account Account acct1 = new Account ("Ted Murphy", 72354, ); Now acct1 is a usable object reference variable. Picture: blue areas depict memory areas 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; Variable Name: height, value: 60 3 “Ted Murphy”, 72354, acct1 height60

TRANSCRIPT

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

1

Chap. 3

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

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

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);

Object ref Object itself

• Using Account methods: use dot on object ref variableacct1.deposit (25.85); // changes balance in objectacct1.addInterest();Here we are calling methods of Account, namely deposit and addInterest

2

“Ted Murphy”, 72354, 102.56acct1

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

From last time: AccountAccount acct1 = new Account ("Ted Murphy", 72354, 102.56); Now acct1 is a usable object reference variable. Picture: blue areas depict memory areas

Variable Name: acct1, value: pointer to objectNote how the object content is in a different memory area

Compare to int variable: An int variable is self-contained in one memory areaint height = 60;

Variable Name: height, value: 60

3

“Ted Murphy”, 72354, 102.56acct1

height 60

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

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

JavaString greeting = “Hi! How are you?”;

• Either way, we end up the a String object with contents “Hi! …”, and an object reference named greeting

4

Hi! How are you?greeting

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

Sec. 3.2 The String class

• Now that we have an object variable (AKA reference), we can call String methods:

int len = greeting.length(); // length methodif (greeting.equals(“Hello”)) … // equals methodSystem.out.println(greeting.toUpperCase());

The last prints HI! HOW ARE YOU?

5

Hi! How are you?greeting

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

6

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

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

7

Packages

• The classes of the Java standard class library are organized into packages

• Some packages in the standard class library are:

Package

java.langjava.appletjava.awtjavax.swingjava.netjava.utiljavax.xml.parsers

Purpose

General supportCreating applets for the webGraphics and graphical user interfacesAdditional graphics capabilitiesNetwork communicationUtilitiesXML document processing

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

8

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.*;

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

9

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

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

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

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

Tiny Math.random() Examplepublic 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 classSo we use <className>.<methodName> to call it.

11

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

Some generated numbers> run Random1Random nos: 0.01538480070275039 0.33946741237265876

> run Random1Random nos: 0.8105596566230356 0.8913714271446472

> run Random1Random nos: 0.40997968141459673 0.893665312675838

We can turn these into ints using a little arithmetic…

12

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

Math.random(): Random intspublic 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

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

Random2 results: random ints 0-19

> run Random2Random ints: 8 12> run Random2Random ints: 17 12> run Random2Random ints: 0 8

Throwing dice: 6 possibilities so use (int)Math.random()*6) for 6 outcomes 0, 1, 2, 3, 4, 5Add one to shift to 1, 2, 3, 4, 5, 6

14

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

15

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 3.141592…Printing only 2 decimal digits 3.14