Presentation is loading. Please wait.

Presentation is loading. Please wait.

The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.

Similar presentations


Presentation on theme: "The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy."— Presentation transcript:

1 The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy Code”); Type Variable new class parameter Name name

2 String Class String is a special class. Sometimes JAVA treats it as a primitive. For instance, you can declare a String like you do a primitive: String myString = “Crazy Code”; ************************************************************ The string class has many methods. For instance, myString.length() returns the length of the string; System.out.println(myString.length()); //outputs 10 here

3 String Class Strings are also a special type of object because each time we change a string, we actually create a new one. This doesn’t matter much now, but will be a concern when we start making comparisons or passing parameters. *************************************************** Strings are indexed from 0 to length-1. Index 0 1 2 3 4 5 HELLO!

4 String Class Look at the methods on page 84 and try to determine what this code does: String name = “Hello!”; name = name.substring(0, 5); name = name + “ Dolly”; System.out.println(name); HELLO!

5 String Class a) Listing 2.9 for practice b) Write a program that declares a string, prints out the string length using length(), finds the index of a substring within the string, and returns a substring from the beginning of the original string to the found index.

6 Wrappers Integer(int value); Double(double value); These store primitives as an object. You are expected to know the methods: comparTo; equals, intValue, toString and doubleValue for these. We’ll use these more later.

7 Random Class Used to generate psuedorandom numbers. First we create a new Random object. Random generator = new Random;

8 Random Class Here are some examples of using the generator: Random r = new Random(); int num; double dNum; num = r.nextInt(10) //generates between 0-9 num = r.nextInt(10)+1 // 1 – 10 dNum = r.nextDouble() // between 0 to 1 Dnum = r.nextDouble()*6 // between 0.0 and 5.999… Dnum = (int) r.nextDouble()*6 + 1 // 1 to 6

9 Random Class Write a program that simulates rolling a pair of dice and outputting the sum.

10 The Scanner Class Creates Scanner objects that input data from a device Declaration: Scanner scan = new Scanner(System.in); //inputs from the default input system

11 The Scanner Class nextInt() returns next integer value from the device. int I = scan.nextInt(); //reads an integer from the input device double r = scan nextDouble(); //reads a double Make sure you print something to open a window before calling Scanner methods for keyboard input.

12 The Scanner Class Example Scanner scan = new Scanner(System.in); System.out.println(“Enter an integer: “); int first = scan.nextInt(); System.out.println(“Enter another integer :”); int second = scan.nextInt(); System.out.println(“The sum is :” + (first+second));

13 The Scanner Class Example import java.util.Scanner; public class AddEm() { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println(“Enter an integer: “); int first = scan.nextInt(); System.out.println(“Enter another integer :”); int second = scan.nextInt(); System.out.println(“The sum is :” + (first+second)); }

14 Assignment Page 119 Programming projects. 2.2 – 2.4

15 The Math Class Accesses various mathematical methods: Absolute value, trig functions, inverse trig functions, exponential, ceiling, floor, powers, random (different from Random class) and square root. The Math class is accessed through static calls, for example Math.sqrt(4)

16 The Math Class Example: double a = 3; double b = 4; double c = sqrt(Math.pow(a,2)+Math.pow(a,2)); What is the value of c?

17 The Math Class Assignment: Page 120 2.8

18 DecimalFormat Class DecimalFormat formats decimal output: DecimalFormat hundredths = new DecimalFormat(“0.##”); //formats output to two decimal places. System.out.println(“Answer :”+hundredths.format(3.23456)); //prints out 3.23

19 DecimalFormat Class import java.text.DecimalFormat; public class TestFormat { public static void main(String[] args) { DecimalFormat hundredths = new DecimalFormat(“0.##”): System.out.println(hundredths.format(2.32456)); }

20 DecimalFormat Class Programming projects page 120 2.9, 2.10

21 NumberFormat Class Prints numbers in different formats: getCurrencyInstance() used to generate dollar format getPercentInstance() used to generate percent format Import java.text.NumberFormat Do initialize using new. Do this instead: NumberFormat money = NumberFormat.getCurrencyInstance() NumberFormat percent = NumberFormat.getPercentInstance();

22 NumberFormat Class NumberFormat money = NumberFormat.getCurrencyInstance(); System.out.println(“Ten Dollars :”+money.format(10.00)); Prints out $10.00 NumberFormat perc = NumberFormat.getPercentInstance(); System.out.println(perc.format(10.00)); Prints 1,000%

23 NumberFormat Class import java.text.NumberFormat; public class StringMeths { public static void main(String[] args) { NumberFormat cash = NumberFormat.getCurrencyInstance(); System.out.println(cash.format(10.00)); NumberFormat perc = NumberFormat.getPercentInstance(); System.out.println(perc.format(10.00)); }

24 NumberFormat Class Page 120 2.12- 2.13


Download ppt "The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy."

Similar presentations


Ads by Google