Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 3 - Wednesday CS 121.

Similar presentations


Presentation on theme: "Week 3 - Wednesday CS 121."— Presentation transcript:

1 Week 3 - Wednesday CS 121

2 Last time What did we talk about last time? Math methods
boolean operations char operations

3 Questions?

4 Project 1

5 System.out.format() For Project 1, the easiest way to print out data with 2 decimal places is put "%.2f" in the formatting string for System.out.format() If you want, you can include other things in the formatting string double x = ; System.out.format("%.2f", x); //prints 5.75 System.out.format("Total = $%.2f", ); //prints Total = $15.78

6 Review of math methods

7 Methods A method is a piece of Java code that has been packaged up so that you can use it over and over Usually, a method will take some input and give some output System.out.println() is an example of a method Using a method (calling a method) always requires parentheses

8 Method example with sin()
The sin() method allows you to find the sine of an angle (in radians) This method is inside the Math class The answer that it gives back is of type double To use it, you might type the following: double value = Math.sin( 2.4 );

9 result = class.method( input );
Method syntax Unless the method is inside your class, you must supply a class name and a dot If your method takes input, you put it inside the parentheses, if not, you leave them empty result = class.method( input ); You can store the result of the method, as long as the variable matches the type that the method gives back Next, you must give the method name that you are calling

10 Other Math methods Return type Name Job double sin( double theta )
Find the sine of angle theta cos( double theta ) Find the cosine of angle theta tan( double theta ) Find the tangent of angle theta exp( double a ) Raise e to the power of a (ea) log( double a ) Find the natural log of a pow( double a, double b ) Raise a to the power of b (ab) long round( double a ) Round a to the nearest integer random() Create a random number in [0, 1) sqrt( double a ) Find the square root of a toDegrees( double radians ) Convert radians to degrees toRadians( double degrees ) Convert degrees to radians

11 Example Write a program that takes a base b and an exponent x
Print the result of raising bx

12 Operations on String values

13 Concatenation The only operator that we will use directly with String values is the + (concatenation) operator This operator creates a new String that is the concatenation of the two source Strings As with numerical types, the + operator does not change the two Strings being concatenated String word; word = "tick" + "tock"; // word is "ticktock"

14 Concatenation with other types
Concatenation is a great tool for merging lots of different types into a String Confusion can arise: String word; word = 99 + " problems"; // word is // "99 problems" String word; word = "love potion #" ; // word is "love potion #45" word = "love potion #" + (4 + 5); // word is "love potion #9"

15 Strings are objects Objects have data inside of them but also have the ability to do things with methods Among other things, a String can: Compare itself with other Strings Find its length Say which character is located at position i Generate a substring

16 String comparison To see if two Strings are identical, use the equals() method: If they are the same (including case), the method will return true If they are not, the method will return false String word1 = "lettuce"; String word2 = "let us"; boolean same = word1.equals ( word2 ); // false

17 String comparison To see which String goes first in the dictionary, use the compareTo() method: If word1 comes first, value will be a negative number If word2 comes first, value will be a positive number If they are the same, value will be 0 String word1 = "hard work"; String word2 = "success"; int value = word1.compareTo( word2 ); // < 0

18 String length To find the length of a String, use the length() method:
It is possible to have a String of length 0: String word = "a mile long"; int length = word.length(); // length = 11 String nothing = ""; int length = nothing.length(); // length = 0

19 char at position i To find the char at position i in a String, use the charAt() method: Woe betide the man (or woman) who asks for a character out of range: String word = "walnut"; char c = word.charAt(3); // c = 'n' String word = "short"; char c = word.charAt(10); // ouch!

20 Getting a substring To get a substring of a String, use the substring() method: The first int tells which char to start on, the second int says which char to stop before String word1 = "disco fever"; String word2 = word1.substring(3,7); //word2 = "co f"

21 Example Write a program that reads a first and a last name
Then, output only the person's initials

22 Wrapper Classes

23 Classes and objects are useful
There are certain things that are difficult to do with the operations we've shown you For example, how do you turn a String representation of a number like "847" into the actual int 847? Wrapper classes!

24 Wrapper classes Each primitive data type in Java has a wrapper class
We will focus on 3: Integer Double Character

25 Integer class The main uses of the Integer class are converting ints to and from Strings To convert a String to an int, use the parseInt() method To convert an int to a String, use the toString() method (or just concatenate) String number = "345"; int value = Integer.parseInt(number); int value = 543; String number = Integer.toString(value);

26 Double class The Double class is much like the Integer class
To convert a String to a double, use the parseDouble() method To convert a double to a String, use the toString() method (or just concatenate) String number = " "; double value = Double.parseDouble(number); double value = 6.02e23; String number = Double.toString(value);

27 Character class The Character class is mostly useful for getting information about a particular char For example, you can find out whether a char is a digit, is a letter, is uppercase, or is lowercase by calling the isDigit(), isLetter(), isUpperCase(), or isLowerCase() methods, respectively char c = '8'; boolean value = Character.isDigit(c); //true

28 Quiz

29 Upcoming

30 Next time… Introduction to if-statements Lab 3

31 Reminders Keep reading Chapter 3 of the textbook
Keep working on Project 1 Due next Friday


Download ppt "Week 3 - Wednesday CS 121."

Similar presentations


Ads by Google