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, ); 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 variable acct1.deposit (25.85); // changes balance in object acct1.addInterest(); Here we are calling methods of Account, namely deposit and addInterest 2 “Ted Murphy”, 72354, acct1
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
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 4 Hi! How are you? greeting
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 method if (greeting.equals(“Hello”)) … // equals method System.out.println(greeting.toUpperCase()); The last prints HI! HOW ARE YOU? 5 Hi! How are you? greeting
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
7 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
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.*;
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
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. to call it. 11
Some generated numbers > run Random1 Random nos: > run Random1 Random nos: > run Random1 Random nos: 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: > 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
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 … Printing only 2 decimal digits3.14