Week 3 - Wednesday
What did we talk about last time? Math methods Lab 2
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 double x = ; System.out.format("%.2f", x); //prints 5.75 System.out.format("Total = $%.2f", ); //prints Total = $15.78
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
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 );
If your method takes input, you put it inside the parentheses, if not, you leave them empty Next, you must give the method name that you are calling Unless the method is inside your class, you must supply a class name and a dot You can store the result of the method, as long as the variable matches the type that the method gives back result = class.method( input );
Return typeNameJob doublesin( double theta ) Find the sine of angle theta doublecos( double theta ) Find the cosine of angle theta doubletan( double theta ) Find the tangent of angle theta doubleexp( double a ) Raise e to the power of a (e a ) doublelog( double a ) Find the natural log of a doublepow( double a, double b ) Raise a to the power of b (a b ) longround( double a ) Round a to the nearest integer doublerandom() Create a random number in [0, 1) doublesqrt( double a ) Find the square root of a doubletoDegrees( double radians ) Convert radians to degrees doubletoRadians( double degrees ) Convert degrees to radians
The boolean type seems so simple What on earth would we want to do with it? Just like numerical types, we can combine boolean s in various ways You might be familiar with these operations if you have taken a course in logic
The NOT operator Changes a true into a false or a false into a true x!x truefalse true
We can combine statements in logic together to make other interesting statements The way we combine them makes a difference, e.g. Politicians lie.(True) Cast iron sinks.(True) Politicians lie in cast iron sinks.(Absurd)
The AND operator It gives back true only if both things being combined are true If I can swim AND the pool is not filled with acid, then I will survive xyx && y true false truefalse
The OR operator It gives back true if either or both things being combined are true If I get punched in the face OR kicked in the stomach, then I will be in pain xyx || y true falsetrue falsetrue false
The XOR operator, sort of like what people often mean when they say "or" in English It gives back true if one but not both things are true If I get 1 apple XOR 3 oranges, then I will have an odd number of fruit xyx ^ y true false truefalsetrue falsetrue false
(!true && (false^(false||true))) Is this expression true or false ? It's false
In some circumstances, Java doesn't check the whole expression: (true || (some complicated expression) ) Ignores everything after || and gives back true (false && (some complicated expression) ) Ignores everything after && and gives back false
Multiplication and division don't seem to make sense We can increment and decrement a char char letter; letter = 'x';// letter contains 'x' letter++;// letter contains 'y' letter++; // letter contains 'z' letter++; // letter contains ? char letter; letter = 'x';// letter contains 'x' letter++;// letter contains 'y' letter++; // letter contains 'z' letter++; // letter contains ?
It is possible to convert a char into an int It can be more useful to get the offset from a starting point int number; number = 'a';// letter contains 97 int number; number = 'a';// letter contains 97 char letter = 'r'; int number; number = letter – 'a' + 1; //number is 18 char letter = 'r'; int number; number = letter – 'a' + 1; //number is 18
Everything in the computer is 1's and 0's Each character has a number associated with it These numbers can be listed in tables The ASCII table only covers 7 bits of information (0-127) NEVER EVER TYPE THESE NUMBERS IN CODE
Remember that we use single quotes to designate a char literal: 'z' What if you want to use the apostrophe character ( ' )? apostrophe: '\'' What if you want to use characters that can't be printed, like tab or newline? tab: '\t' newline: '\n' The backslash is a message that a special command called an escape sequence is coming
You can put escape sequences into String literals as well You do not have to escape apostrophes in a String But you do have to escape quotation marks String blanks = "\t\t\t\t\n"; String quote = "He said, \"Attack!\""; String blanks = "\t\t\t\t\n"; String quote = "He said, \"Attack!\"";
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 String s As with numerical types, the + operator does not change the two String s being concatenated String word; word = "tick" + "tock"; // word is "ticktock" String word; word = "tick" + "tock"; // word is "ticktock"
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 = 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" String word; word = "love potion #" ; // word is "love potion #45" word = "love potion #" + (4 + 5); // word is "love potion #9"
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 String s Find its length Say which character is located at position i Generate a substring
To see if two String s 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 String word1 = "lettuce"; String word2 = "let us"; boolean same = word1.equals ( word2 ); // false
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 String word1 = "hard work"; String word2 = "success"; int value = word1.compareTo( word2 ); // < 0
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 word = "a mile long"; int length = word.length(); // length = 11 String nothing = ""; int length = nothing.length(); // length = 0 String nothing = ""; int length = nothing.length(); // length = 0
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 = "walnut"; char c = word.charAt(3); // c = 'n' String word = "short"; char c = word.charAt(10); // ouch! String word = "short"; char c = word.charAt(10); // ouch!
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" String word1 = "disco fever"; String word2 = word1.substring(3,7); //word2 = "co f"
Write a program that reads a first and a last name Then, output only the person's initials
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!
Each primitive data type in Java has a wrapper class We will focus on 3: Integer Double Character
The main uses of the Integer class are converting int s to and from String s 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); String number = "345"; int value = Integer.parseInt(number); int value = 543; String number = Integer.toString(value); int value = 543; String number = Integer.toString(value);
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); String number = " "; double value = Double.parseDouble(number); double value = 6.02e23; String number = Double.toString(value); double value = 6.02e23; String number = Double.toString(value);
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 char c = '8'; boolean value = Character.isDigit(c); //true
Introduction to if -statements Lab 3
Keep reading Chapter 3 of the textbook Keep working on Project 1 Due next Friday