Java Midterm Slides - 600.107 Spring 2015 Java Midterm Review 1 By Katie Calabro Midterm Details Wednesday March 11th in normal classroom Bring J-Cards

Java Midterm Slides - 600.107 Spring 2015 Java Midterm...

This preview shows page 1 - 12 out of 34 pages.

600.107 Spring 2015Java Midterm ReviewBy: Katie Calabro1
Background image
Midterm DetailsWednesday March 11th in normal classroomBring:J-CardsPencil/PenNo Calculators
Background image
Primitive TypesPrimitive types are the most basic types in Java and they show up in purple on jGrasp.byte, short, int, long, float, double, boolean, charReserved Wordsspecial words in Java with special meaning. These also show up in purple and cannot be the name of a variable (public, static, void, throws, throw, if, else, etc.)3
Background image
Variable Namesvariables can start with a letter, underscore, or $after the first character can be a number, underscore, $, or letterGood variable names: $_123, _test, abc123Bad variable names: 1num, abc#abc, int, String4
Background image
CharactersCharacters are store in memory as numbers. For example ‘a’==97 , ’A’==65 , ‘k’==107, ’n’==110. lower case letters are > then upper case lettersWhat do these print?char c1 = ‘k’;System.out.println(c1);// kSystem.out.println(c1 + 3); // 110System.out.println((char)(c1 + 3)); // n5
Background image
ArithmeticOrder of operations: (), -(negation), * / %, + -, =When we divide an integer by an integer we perform integer division. Be careful that you don’t unintentionally do thisWhat do these print?int a = 8, b = 5;double c = 4.7;System.out.println(a/b);// 1System.out.println((double)a/b);// 1.6System.out.println((double)(a/b)); // 1.0System.out.println((int)c);// 46
Background image
Arithmeticmod (%) gets the remainder when you divide two numbers. Can be used to see if a number is even.even# % 2 == 0, odd# % 2 == 1num++ vs ++num. num++ executes the line and then increments num by 1++num increments num by 1 and then executes the lineWhat do these print?int num = 5;int a = num++ + 2;System.out.println(a + “ “ + num);// 7 6System.out.println(++num * 2); // 147
Background image
StringsNot a primitive typeIndicated with double quoteslength() -> returns the length of a stringString s = “abc123”int length = s.length();System.out.println(length);// 68
Background image
StringsindexOf -> finds the first index of a character in a stringAll indices start at 0String s = “abcbc1”int i = s.indexOf(‘c’);System.out.println(i);// 2String -> a b c b c 1index -> 0 1 2 3 4 59
Background image
StringscharAt -> returns the character at an index in stringWhat happens?String s = “abcdef”char c = s.charAt(4);System.out.println(c);// ec = s.charAt(s.length());String -> a b c d e findex -> 0 1 2 3 4 510crash!
Background image
Stringssubstring -> takes the substring of a stringfirst index in inclusive, second index is exclusive
Background image
Image of page 12

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture