week 1 wednesday what did we talk about last time
play

Week 1 -Wednesday What did we talk about last time? Syllabus - PowerPoint PPT Presentation

Week 1 -Wednesday What did we talk about last time? Syllabus Course policies Java basics In Java, every variable and every literal has a type A type says what kind of data it is In Python, you could just assign a value to


  1. Week 1 -Wednesday

  2.  What did we talk about last time?  Syllabus  Course policies  Java basics

  3.  In Java, every variable and every literal has a type  A type says what kind of data it is  In Python, you could just assign a value to a variable  In Java, every variable must be declared with its type before you use it: double x;  Java is also strongly typed, meaning that type errors will prevent your program from compiling int y = 4.0; //illegal, since 4.0 is a double

  4.  Five basic types of data in Java let you get most things done  These are:  int For whole numbers  double For rational numbers  boolean For true or false values  char For single characters  String For words  String is a little different from the rest, since you can call methods on it (and for other reasons)  There are also byte , short , and long versions of int , which use 1, 2, and 8 bytes, respectively

  5. Type Kind of values Sample Literals -5 int 0 Integers 900031 3.14 Floating-point double -0.6 Numbers 6.02e23 true boolean Boolean values false 'A' char 'Z' Single characters '&' "If you dis Dr. Dre" Sequences of String "10 Sesquipedalians" characters

  6.  The int type is used to store integers (positive and negative whole numbers and zero)  Examples:  54  -893992  0  Inside the computer, an int takes up 4 bytes of space, which is 32 bits (1's and 0's)

  7.  You will use the int type very often  Sometimes, however, you need to represent numbers with a fractional part  The double type is well suited to this purpose  Declaration of a double variable is just like an int variable: double x;

  8.  Numbers are great  But, sometimes you only need to keep track of whether or not something is true or false  This is what the boolean type is for  Hopefully you have more appreciation for boolean s now  Declaration of a boolean variable is like so: boolean value;

  9.  Sometimes you need to deal with characters  This is what the char type is for  The char type only allows you to store a single character like '$' or 'q'  Declaration of a char variable is like so: char c;

  10.  The String type is different from the other types in several ways  The important thing for you to focus on now is that it can hold a large number of char s, not just a single value  A String literal is what we used in the Hello, World program String word;

  11. There are three parts to using Scanner for input Include the appropriate import statement so that your program 1. knows what a Scanner object is 2. Create a specific Scanner object with a name you choose Use the object you create to read in data 3.

  12.  Lots of people have written all kinds of useful Java code  By importing that code, we can use it to help solve our problems  To import code, you type import and then the name of the package or class  To import Scanner , type the following at the top of your program (before the class!) import java.util.Scanner;

  13.  Once you have imported the Scanner class, you have to create a Scanner object  To do so, declare a reference of type Scanner , and use the new keyword to create a new Scanner with System.in as a parameter like so: Scanner in = new Scanner(System.in);  You can call it whatever you want, I chose to call it in

  14.  Now that you've got a Scanner object, you can use it to read some data  It has a method that will read in the next piece of data that user types in, but you have to know if that data is going to be an int , a double , or a String  Let's say the user is going to input her age (an int ) and you want to store it in an int variable called years  We'll use the nextInt() method to do so: int years; years = in.nextInt();

  15.  Scanner has a lot of methods (ways to accomplish some tasks)  For now, we're only interested in three  These allow us to read the next int , the next double , and the next String , respectively: Scanner in = new Scanner(System.in); int number = in.nextInt(); double radius = in.nextDouble(); String word = in.next();

  16.  + adds  - subtracts  * multiplies  / divides (integer division for int type and fractional parts for double type)  % finds the remainder

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

  18.  ! NOT  Flips value of operand from true to false or vice versa  && AND  true if both operands are true  || OR  true if either operand is true  ^ XOR  true if operands are different

  19.  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

  20.  char values can be treated like an int int number; number = 'a'; // number contains 97  It can be more useful to get the offset from a starting point char letter = 'r'; int number; number = letter – 'a' + 1; //number is 18

  21.  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? '\t'  tab: '\n'  newline:  The backslash is a message that a special command called an escape sequence is coming  These can be used in String literals as well:  "\t\t\t\nThey said, \"Wow!\""

  22.  The only operator that we will use directly with String s is the + (concatenation) operator  This operator creates a new String that is the concatenation of the two source String s  Concatenation can be used to insert the values of other types into String s as well String word; word = "tick" + "tock"; // word is "ticktock"

  23.  equals()  Tests two String s to see if they are the same  compareTo()  Returns a negative number if the first String comes earlier in the alphabet, a positive number if the first String comes later in the alphabet, and 0 if they are the same  length()  Returns the length of the String  charAt()  Returns the character at a particular index inside the String  substring()  Returns a new String made up of the characters that start at the first index and go up to but do not include the second index

  24.  Each primitive data type in Java has a wrapper class  Integer ▪ Allows String representations of integer values to be converted into int s  Double ▪ Allows String representations of floating point values to be converted into double s  Character ▪ Provides methods to test if a char value is a digit, is a letter, is lower case, is upper case ▪ Provides methods to change a char value to upper case or lower case

  25.  The if -statement: int x = 4; if( x < 5 ) System.out.println("x is small!");  x is small will only print out if x is less than 5  In this case, we know that it is, but x could come from user input or a file or elsewhere

  26. The if part Any boolean expression if( condition ) statement; Any single executable statement

  27.  Any statement that evaluates to a boolean is legal  Examples:  x == y  true  Character.isDigit('r')  s.equals("Help me!") && (z < 4)

  28.  The most common condition you will find is a comparison between two things  In Java , that comparison can be:  == equals  != does not equal  < less than  <= less than or equal to  > greater than  >= greater than or equal to  These are called relational operators

  29.  You can use the == operator to compare any two things of the same type  Different numerical types can be compared as well ( 3 == 3.0 )  Be careful with double types, 0.33333333 is not equal to 0.33333332 int x = 3; if( x == 4 ) System.out.println("This doesn't print");

  30.  Any place you could have used the == operator, you can use the != operator  If == gives true, the != operator will always give false, and vice versa  If you want to negate a condition, you can always use the ! as a not if( x != 4 ) is the same as if( !(x == 4) )

  31.  Remember, a single equal sign ( = ) is the assignment operator (think of a left-pointing arrow)  A double equals ( == ) is a comparison operator int y = 10; if( y = 6 ) //compiler error! boolean b = false; if( b = false ) //no compiler error but wrong

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend