Class Notes (807,938)
COMP 202 (186)
Lecture

# COMP 202 study sheet.docx

2 Pages
251 Views

School
McGill University
Department
Computer Science (Sci)
Course
COMP 202
Professor
Daniel Pomerantz
Semester
Fall

Description
Unit 2  3) Inside method, variables defined in the method header will Variables have value of expression passed to them: Math.pow(3, 4.0 + 3) o Give a variable a name and a type  a=3.0, b=7.0  Type = kind of data (int, double, string etc.)Example : int name o Using a result from method in an expression = 3;  1) Change method header 2) Add a return statement o Reading from keyboard  3) Call method Example : public static double  Java.util.Scanner keyboardReader = new convert(double fahrenheit) {return (fahrenheit – 32) * 5.0 / 9.0;} double boilPoint = convert(88) java.util.Scanner(System.in);  Int variable = keyboardReader.next.Int(); o Return Statement o A variable you create inside a block of code is not the same outside  Public static double foo(double a){ return a*2; that block return a*3;}  a*3 will not be seen Types of Variables  If Statements o Boolean (<= 1byte) : store true or false o Format of if : if(confition){ o Byte(1) : can only store between( -128 and 127) condition happens if true} code here happens no matter what o Short(2) : Small numbers (-32,768 < x < 32,767) o Format of if/else: Happens when you need two conditions. Like if o Char(2) : stores one character (0 < x < 2 – 1) (condition) { //happens when condition is true} else { //happens when  Each character corresponds to a unique number condition is false}  Delimited by apostrophes (like : „a‟) o Format of if/else if/else : if inside an if. The „else if‟ is only entered if  To store „ you need to do char x = „\‟‟  (\) is also used to store condition was false new line (\n)  Boolean Expressions o Int (4) : integer (2 < x < -2 -1 )  size in byte 4 o Comparing strings (use method)  int double no: info lost : doubl int: some info lost  String one = “a String”; /n String two = “a diff string”; o long (8): long numbers (pi) (-2 < x < 2 -1) boolean areEqual = one.equal(two); o Comparing char : boolean lessThan = „+‟ < „J‟ ; o float(4) : like double stores numbers; although 32bit  Example: 3.0f  (approx) +/- 3.4 * 10 ) o Creating Boolean exp. From other boo exp. o Double (8) : stores fractions (approx) +/- 1.8 * 10  && : needs two true to be true  || : true iff one operand is true (false if 2 are false  3.0 is considered a double; 64 bits mem o String : letters Unit 4: Programming with iterations  Int greetingLength = greeting.length();  While loops (Use when there is indefinite # of iterations) Using Expressions o While(condition){ //block of code} //Go here only after condition is o Thing you assign to a type and its variable false : Iterations: number of times loop goes around  Class Variable and Constants o Different sort of expressions  Literals : 10, “hello”, true, 3.0 o Use the final operator before the type (final int)  Variables : someVariable  For loops o Having many methods o for(initialization; condition; finalization){body}  Need to specify the type of your method  Nested loops (Loop within loop)  Needs to be out of another method o Example: for(int y = 0; y < 10 ; y++){  Example: int anotherMethod() for (int x = 0 ; x < 10 ; x++){ System.out.print(“(“+ y + “,” + x + “), o + operator “);} System.out.print(“\n”);}  Int + int  int Unit 5: Loops in Practice (ARRAYS)  Double + double  double Arrays (They are objects)  Int + double  double o A container object that holds fixed numbers of values in a single type  String + string  string (length established at beginning, then it remains fixed)  type[ ]  String + int  String o Creating an array: (3 ways) don‟t tech need name o * operator  type[ ] variableName = {one,two}  String * String  ERROR  type[ ] Name = new type[size];  Double * int  ERROR  type[] Name = new type[]{1,2,3}; o / division o Each spot has a value (what is inside each box) and an index (integer  Int/int = int that starts counting at 0)  When you divide you need 2.0/3.0, not 2/3 for double o Accessing values: variableName[index] o Modulo o Length of an array: int arraySize = variable.length  % will take the remainder of a division o Any object type does not store data; stores an address in the memory.  If right number> left than left (2%5 = 2) Remember that when y[0] also changes x[0] when int[ ] y = x o Type casting o IMPORTANT EXAMPLE: int[x] !(==) int[y] even if they have the  From double to int same indexes and values  Int x = (int)*7.5 : X = 7 (round down) Two dimensional arrays  (Double x = 1/2; = 0.0) or (int x = 2.2;) error. You need o Creating a 2d array (2 ways) int x = (int) 2.2; (loss of precision, manual cast)  int[] [] board = { {1,2,3} , {1,4} , {6,2} };  Casting is temporary : doesn‟t change the type for rest of code  type[] [] variableName = new type[size1][size2]; o ++ and – operator good example at bottom o Accessing values of a 2d array  x++increments after getting value : same for --  Int[] [] x = new int[3][4];(creates array which contains 3[] size 4)  ++x increments before getting value :same for - Useful String Methods .charAt(int i) – gets the ith symbol of the String (count from 0) o Final operator (Declares a constant) .substring(a,b) – gets String consisting of from a of the original up to but not including a+b  Final double PI = 3.14; .equals(String other) – compare two Strings values to each other (since == compares addresses) .compareTo(String other) – tells you which String is larger Unit 3: Methods and Conditions  Library Methods .indexOf(char c):returns the index of the first instance of c o Math library (Math.method to access) .toLowerCase() - returns a lowercase version of String Good examples of data types:  Contains constants like „e‟ or pi byte y=2345; Compilation Error  BE CARFUL: Error: int blah = Math.tan(Math.PI); because tan Double x = 3.45F; No error is a double Int x = 10/3; No error Char y = “hey”; Compilation Error o String library Int x=10,y=x+5,z=3 No error o Static methods String s =“hello” + 345+x No error  Call doing .nextInt Good example of ++ and – operators Int i = 10; , int n = i++; print(“i =” +i+ “and n=” +n);  i=11, n=10Errors  To import just : import java.util.Scanner;  Creating your own methods Static/compiler Runtime Type match, (ex: string as int) Array out of bounds o Methods that don‟t return values : Void methods Forget to declare variable Division by 0 o Passing data to a method Syntax error (forget the ; or ( ){}) Mismatch type  1) Start by what will be pas
More Less

Related notes for COMP 202

OR

Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Join to view

OR

By registering, I agree to the Terms and Privacy Policies
Just a few more details

So we can recommend you notes for your school.