The Static Math Class Figure 5 1 is

  • Slides: 24
Download presentation

The Static Math Class Figure 5. 1 is an illustration of the Math class

The Static Math Class Figure 5. 1 is an illustration of the Math class with a small set of the actual members that are found in this static class.

Java 0501. java Output // Java 0501. java C: JavaProgs 05>javac Java 0507. java

Java 0501. java Output // Java 0501. java C: JavaProgs 05>javac Java 0507. java // This programnon-static demonstrates that the methods ofcannot a class not always Java 0507. java: 12: method change. Checking(double) be are referenced from a static context Bank. change. Checking(1000. 0); // accessible, like they were with the <Math> class. In this case an ^ Java 0507. java: 13: non-static method change. Savings(double) referenced from a static context // attempt is made to use methods of the <Bank>cannot classbewithout success. Bank. change. Savings(5000. 0); ^ Java 0507. java: 14: non-static method get. Checking() cannot be referenced from a static context System. out. println("Checking balance: " + Bank. get. Checking()); ^ Java 0507. java: 15: non-static method get. Savings() cannot be referenced from a static context System. out. println("Savings balance: " + Bank. get. Savings()); ^ 4 errors C: JavaProgs 05> public class Java 0501 { public static void main (String args[]) { System. out. println("n. JAVA 0501. JAVAn"); Bank. change. Checking(1000. 0); Bank. change. Savings(5000. 0); System. out. println("Checking balance: " + Bank. get. Checking()); System. out. println("Savings balance: " + Bank. get. Savings()); System. out. println(); } }

// Java 0502. java // This program creates two Bank objects, called tom and

// Java 0502. java // This program creates two Bank objects, called tom and sue. // Each object stores its own Bank information. public class Java 0502 { public static void main (String args[]) { Java 0501. java Output System. out. println("n. JAVA 0508. JAVAn"); Bank tom; JAVA 0502. JAVA tom = new Bank(); Bank sue; Tom's checking balance: 1000. 0 sue = new Bank(); Tom's savings balance: 5000. 0 tom. change. Checking(1000. 0); Sue's checking balance: 1500. 0 tom. change. Savings(5000. 0); Sue's savings balance: 4000. 0 sue. change. Checking(1500. 0); sue. change. Savings(4000. 0); System. out. println("Tom's checking balance: " + tom. get. Checking()); System. out. println("Tom's savings balance: " + tom. get. Savings()); System. out. println("Sue's checking balance: " + sue. get. Checking()); System. out. println("Sue's savings balance: " + sue. get. Savings()); System. out. println(); } }

Calling Class and Object Methods Class methods are called with class-identifier. method Object methods

Calling Class and Object Methods Class methods are called with class-identifier. method Object methods are called with object-identifier. method

Difference Between Class and Object Methods Class methods are used when data does not

Difference Between Class and Object Methods Class methods are used when data does not need to be stored. This occurs normally with utility-style methods like the type found in the Math class. Object methods are used when data needs to be stored in different objects. Object methods then access the appropriate data based on the object identifier.

// Java 0503. java Output // This program demonstrates how an object can be

// Java 0503. java Output // This program demonstrates how an object can be constructed with a specified // initial balance in checking and savings. Most Java classes have multiple JAVA 0503. JAVA // constructors to create objects for multiple situations. public class Java 0503 Tom's checking balance: 5000. 0 { Tom's savings balance: 10000. 0 public static void main (String args[]) Sue's checking balance: 3000. 0 { Sue's savings balance: 15000. 0 System. out. println("n. JAVA 0503. JAVAn"); Bank tom; tom = new Bank(5000. 0, 10000. 0); Tom makes a $1000. 00 checking withdrawal Bank sue; Tom makes a $2000. 00 savings withdrawal sue = new Bank(3000. 0, 15000. 0); Sue makes a $1500. 00 checking deposit System. out. println("Tom's checking balance: " + tom. get. Checking()); Sue makes a $3000. 00 savings deposit System. out. println("Tom's savings balance: " + tom. get. Savings()); System. out. println("Sue's checking balance: " + sue. get. Checking()); System. out. println("Sue's savings balance: " + sue. get. Savings()); Tom's checking balance: 4000. 0 System. out. println(); Tom's savings balance: 8000. 0 System. out. println("Tom makes a $1000. 00 checking withdrawal"); Sue's checking balance: 4500. 0 tom. change. Checking(-1000. 0); System. out. println("Tom makes a $2000. 00 savings withdrawal"); Sue's savings balance: 18000. 0 tom. change. Savings(-2000. 0); System. out. println("Sue makes a $1500. 00 checking deposit"); sue. change. Checking(+1500. 0); System. out. println("Sue makes a $3000. 00 savings deposit"); sue. change. Savings(+3000. 0); System. out. println("Tom's checking balance: " + tom. get. Checking()); System. out. println("Tom's savings balance: " + tom. get. Savings()); System. out. println("Sue's checking balance: " + sue. get. Checking()); System. out. println("Sue's savings balance: " + sue. get. Savings()); } }

// Java 0504. java Output // This program demonstrates the <get. Combined> method of

// Java 0504. java Output // This program demonstrates the <get. Combined> method of the <Bank> // class. This method returns the combined checking and savings balance. JAVA 0504. JAVA // It also shows how to close bank accounts with the <close. Checking> and the // <close. Savings> methods. Tom's checking balance: Tom's savings balance: Sue's checking balance: Sue's savings balance: public class Java 0504 { public static void main (String args[]) { System. out. println("n. JAVA 0504. JAVAn"); Tom's combined balance: Bank Tom = new Bank(5000. 0, 10000. 0); Bank Sue = new Bank(3000. 0, 15000. 0); Sue's combined balance: System. out. println("Tom's checking balance: " + tom. get. Checking()); System. out. println("Tom's savings balance: " + tom. get. Savings()); Tom's combined balance: System. out. println("Sue's checking balance: " + sue. get. Checking()); Sue's combined balance: System. out. println("Sue's savings balance: " + sue. get. Savings()); System. out. println("Tom's combined balance: " + tom. get. Combined()); System. out. println("Sue's combined balance: " + sue. get. Combined()); System. out. println(); tom. close. Checking(); tom. close. Savings(); sue. close. Checking(); sue. close. Savings(); System. out. println("Tom's combined balance: " + tom. get. Combined()); System. out. println("Sue's combined balance: " + sue. get. Combined()); System. out. println(); } } 5000. 0 10000. 0 3000. 0 15000. 0 18000. 0

Creating Objects, Approach 1 The Two-Line Method Constructing an object requires using the new

Creating Objects, Approach 1 The Two-Line Method Constructing an object requires using the new operator. Bank tom; tom = new Bank(); // default constructor Bank sue; sue = new Bank(500, 800); // parameter constructor

Creating Objects, Approach 2 The One-Line Method Constructing an object requires using the new

Creating Objects, Approach 2 The One-Line Method Constructing an object requires using the new operator. Bank tom = new Bank(); // default constructor Bank sue = new Bank(500, 800); // parameter constructor

Handling Object Data Methods like get. Checking, get. Savings and get. Combined only access

Handling Object Data Methods like get. Checking, get. Savings and get. Combined only access object data Methods like changechecking, change. Savings, close. Checking and close. Savings alter object data.

Creating Objects, Approach 2 Constructing an object requires using the new operator. Bank tom

Creating Objects, Approach 2 Constructing an object requires using the new operator. Bank tom = new Bank(); // default constructor Bank sue = new Bank(500, 800); // parameter constructor

Java 0505. java Output #1 // Java 0505. java // This program introduces the

Java 0505. java Output #1 // Java 0505. java // This program introduces the <Random> class, which is part of the <java. util> JAVA 0505. JAVA // package. The <next. Int> method returns a random integer value. Note that the // values change each time that you execute the program. 2049677383 1510369737 import java. util. Random; 307108404 Java 0505. java Output #2 1915490128 public class Java 0505 -900266109 { JAVA 0505. JAVA 1029687896 public static void main (String args[]) 595496401 { -896350403 System. out. println("n. JAVA 0511. JAVAn"); -661935151 -2088079341 1588856574 Random rand = new Random(); 74035716 -2146056710 System. out. println(rand. next. Int()); Java 0505. java Output #3 773867284 System. out. println(rand. next. Int()); 1668377022 JAVA 0505. JAVA -1059631511 System. out. println(rand. next. Int()); -169007582 System. out. println(rand. next. Int()); 1879905716 1276813836 System. out. println(rand. next. Int()); -268512612 51895689 System. out. println(rand. next. Int()); -685904662 -766131688 System. out. println(rand. next. Int()); -992927539 System. out. println(rand. next. Int()); -1109991262 System. out. println(rand. next. Int()); 13422453 2042135351 System. out. println(rand. next. Int()); 2102830989 System. out. println(); -1696084093 } 1657502352 }

Java 0506. java Output #1 // Java 0506. java // This program "seeds" the

Java 0506. java Output #1 // Java 0506. java // This program "seeds" the Random object with a specific starting seed. JAVA 0506. JAVA // Multiple executions will now display the same random numbers. import java. util. Random; public class Java 0506 { public static void main (String args[]) { System. out. println("n. JAVA 0506. JAVAn"); Random rand = new Random(12345); System. out. println(rand. next. Int()); System. out. println(); } } 1553932502 -2090749135 -287790814 Java 0506. java Output #2 -355989640 -716867186 JAVA 0506. JAVA 161804169 1402202751 1553932502 535445604 -2090749135 1011567003 -287790814 151766778 Java 0506. java Output #3 -355989640 -716867186 JAVA 0506. JAVA 161804169 1402202751 1553932502 535445604 -2090749135 1011567003 -287790814 151766778 -355989640 -716867186 161804169 1402202751 535445604 1011567003 151766778

// Java 0507. java // This program demonstrates the second "overloaded" <next. Int(n)> method,

// Java 0507. java // This program demonstrates the second "overloaded" <next. Int(n)> method, // which returns an integer x, such that 0 <= x < n. import java. util. Random; public class Java 0507 { public static void main (String args[]) { System. out. println("n. JAVA 0507. JAVAn"); Random rand = new Random(12345); System. out. println(rand. next. Int(100)); System. out. println(); } } Java 0507. java Output JAVA 0507. JAVA 51 80 41 28 55 84 75 2 1 89

// Java 0508. java // This program introduces the <set. Seed> method. This method

// Java 0508. java // This program introduces the <set. Seed> method. This method allows you to // control the "randomness" and repeat the same sequence. import java. util. Random; public class Java 0508 { public static void main (String args[]) { System. out. println("n. JAVA 0508. JAVAn"); Random rand = new Random(12345); System. out. println(rand. next. Int(100)); System. out. println(); rand. set. Seed(12345); System. out. println(rand. next. Int(100)); System. out. println(); } } Java 0508. java Output JAVA 0508. JAVA 51 80 41 28 55

// Java 0509. java // This program demonstrates the <next. Double> method, // which

// Java 0509. java // This program demonstrates the <next. Double> method, // which returns a real x, such that 0 <= x < 1. import java. util. Random; public class Java 0509 { public static void main (String args[]) { System. out. println("n. JAVA 0509. JAVAn"); Random rand = new Random(12345); System. out. println(rand. next. Double()); System. out. println(); } } Java 0509. java Output JAVA 0509. JAVA 0. 3618031071604718 0. 932993485288541 0. 8330913489710237 0. 32647575623792624 0. 2355237906476252 0. 34911535662488336 0. 4480776326931518 0. 6381529437838686 0. 1582665432952023 0. 768888060192009

// Java 0510. java // This program demonstrates how to control an Random class

// Java 0510. java // This program demonstrates how to control an Random class object so that it // generates integers in a desired range. In this example the range is [10. . 99]. import java. util. Random; public class Java 0510 { public static void main (String args[]) { System. out. println("n. JAVA 0510. JAVAn"); Random rand = new Random(12345); System. out. println(rand. next. Int(90) + 10); System. out. println(); } } Java 0510. java Output JAVA 0510. JAVA 41 50 61 28 95 14 35 82 71 79

// Java 0511. java // This program demonstrates how to control an Random class

// Java 0511. java // This program demonstrates how to control an Random class object so that it // generates random upper-case letters. Note how "type casting" with (char) is // used to change random integers in the [65. . 90] range to [A. . Z] letters. import java. util. Random; public class Java 0511 { public static void main (String args[]) { System. out. println("n. JAVA 0511. JAVAn"); Random rand = new Random(12345); System. out. println( (char) (rand. next. Int(26) + 65) ); System. out. println( (char) (rand. next. Int(26) + 65) ); } } Java 0511. java Output JAVA 0511. JAVA J U F I N Q N W P X

// Java 0512. java Output // This program demonstrates how to "right justify" integers

// Java 0512. java Output // This program demonstrates how to "right justify" integers with an object JAVA 0512. JAVA // of the <Decimal. Format> class and the <format> method. 00001 import java. text. Decimal. Format; 000123 public class Java 0512 01234 { 12345 public static void main (String args[]) 123456 { 1234567 System. out. println("n. JAVA 0512. JAVAn"); Decimal. Format output = new Decimal. Format("00000"); System. out. println(output. format(1)); System. out. println(output. format(123)); System. out. println(output. format(12345)); System. out. println(output. format(1234567)); System. out. println(); } }

// Java 0513. java Output // This program demonstrates how to insert commas in

// Java 0513. java Output // This program demonstrates how to insert commas in numerical output JAVA 0513. JAVA // with a <Decimal. Format> object. import java. text. Decimal. Format; 0, 001 0, 000, 012 0, 000, 123 0, 001, 234 0, 012, 345 0, 123, 456 1, 234, 567 public class Java 0513 { public static void main (String args[]) { System. out. println("n. JAVA 0513. JAVAn"); Decimal. Format output = new Decimal. Format("0, 000"); System. out. println(output. format(1)); System. out. println(output. format(123)); System. out. println(output. format(12345)); System. out. println(output. format(1234567)); System. out. println(); } }

// Java 0514. java Output // This program demonstrates how to display US currency

// Java 0514. java Output // This program demonstrates how to display US currency amounts. JAVA 0514. JAVA // Additionally note how the <format> methods rounds off to the nearest penny. import java. text. Decimal. Format; $1. 00 $12. 20 $123. 32 $1234. 43 $12345. 54 $123456. 65 $1234567. 76 public class Java 0514 { public static void main (String args[]) { System. out. println("n. JAVA 0514. JAVAn"); Decimal. Format output = new Decimal. Format("$0. 00"); System. out. println(output. format(1)); System. out. println(output. format(12. 2)); System. out. println(output. format(123. 32)); System. out. println(output. format(1234. 432)); System. out. println(output. format(12345. 543)); System. out. println(output. format(123456. 654)); System. out. println(output. format(1234567. 765)); System. out. println(); } }

// Java 0515. java // This program demonstrates how to control rounding off to

// Java 0515. java // This program demonstrates how to control rounding off to a specified // number of digits beyond the decimal point. Java 0515. java Output JAVA 0515. JAVA import java. text. Decimal. Format; public class Java 0515 { public static void main (String args[]) { System. out. println("n. JAVA 0515. JAVAn"); Decimal. Format output 1 = new Decimal. Format("0. 0"); Decimal. Format output 2 = new Decimal. Format("0. 00"); Decimal. Format output 3 = new Decimal. Format("0. 000"); Decimal. Format output 4 = new Decimal. Format("0. 0000"); Decimal. Format output 5 = new Decimal. Format("0. 00000"); Decimal. Format output 6 = new Decimal. Format("0. 000000"); Decimal. Format output 7 = new Decimal. Format("0. 0000000"); Decimal. Format output 8 = new Decimal. Format("0. 0000"); System. out. println(Math. PI); System. out. println(output 1. format(Math. PI)); System. out. println(output 2. format(Math. PI)); System. out. println(output 3. format(Math. PI)); System. out. println(output 4. format(Math. PI)); System. out. println(output 5. format(Math. PI)); System. out. println(output 6. format(Math. PI)); System. out. println(output 7. format(Math. PI)); System. out. println(output 8. format(Math. PI)); System. out. println(); } } 3. 141592653589793 3. 142 3. 1416 3. 141593 3. 1415927 3. 14159265

Random and Decimal. Format Chapter V introduces four classes: Math Bank Random Decimal. Format

Random and Decimal. Format Chapter V introduces four classes: Math Bank Random Decimal. Format The Bank class is a user-defined class. The other classes are part of the Java class libraries. Random and Decimal. Format will be shown and explained on the computer.