Presentation is loading. Please wait.

Presentation is loading. Please wait.

CECS 220 Java Test Review. Variable Names 0 May Contain: a-z, A-Z, 0-9, _, $, characters from other languages. 0 May not start with 0-9. 0 Legal: 0 MyVariable.

Similar presentations


Presentation on theme: "CECS 220 Java Test Review. Variable Names 0 May Contain: a-z, A-Z, 0-9, _, $, characters from other languages. 0 May not start with 0-9. 0 Legal: 0 MyVariable."— Presentation transcript:

1 CECS 220 Java Test Review

2 Variable Names 0 May Contain: a-z, A-Z, 0-9, _, $, characters from other languages. 0 May not start with 0-9. 0 Legal: 0 MyVariable 0 myVariable 0 MYVARIABLE 0 x 0 _my_variable 0 $myvariable 0 ανδρος 0 This_is_an_insanely_long_variable_name_that_never_ends 0 Not Legal: 0 My Variable 0 9pins 0 a+c 0 #include 0 O’Reilly 0 Oreilly_&_Associates 0 @hotmail

3 Function, or procedure? void calculate() { int value = 5 * 4; return; } int calculate() { int value = 5 * 4; return value; } void calculate(int a, int b) { if (a == 5 && b == 4) { System.out.println(20); return; } System.out.println(a * b); }

4 Testing 0 JUnit vs not using an automated framework 0 Reference utilities.CalculatorTest vs utilities.testing.TestCalculator

5 To be or not to be static? It's a matter of belonging. Static = belongs to the class, not static = belonging to an instance of the class. class Vehicle{ // Do these variables apply to vehicles in general, or to a specific one? private int numWheels= 4 ; private static int numPeople = 5; } class Motorcycle{ private static int numWheels= 2 ; private int numPeople = 2; }

6 To be or not to be static? 0 Same applies to functions and procedures 0 Should eat() be a function of apples in general, or a specific one? 0 Should getNumberOfLegs() be a function of cats in general, or a specific one?

7 Packages 0 For organization 0 package tools; public class Calculator{ public static int multiply(int a, int b) { return a * b; } } 0 package tools; public class AnotherTool{ public static void function() { } } 0 package stuff; public class Calculator{ public static void doSomething() { } }

8 Packages 0 package com.example.tools; public class Calculator { public static int multiply(int a, int b) { return a * b; } } 0 com.examples.tools.Calculator.multiply(4, 5); 0 import com.examples.tools.Calculator;... Calculator.multiply(4, 5);

9 Class, instances of class 0 JFrame f = new JFrame(); f.add(...); JFrame.add(...); ??? 0 Double num = new Double(5.5); double value = num.doubleValue(); double value2 = Double.doubleValue(); ??? 0 double value = Double.parseDouble("5.5"); double value2 = value.parseDouble("5.5"); ???

10 this 0 class Foo { private static final int DEFAULT = 5; private int myNumber; public Foo() { this(DEFAULT); // ?? } public Foo(int number) { myNumber = number; } }

11 this 0 class Foo { private static final int DEFAULT = 5; private int number; public Foo() { this(DEFAULT); } public Foo(int number) { this.number= number; // ?? } }

12 Extending classes 0 class Animal { private string name= "animal"; public Animal(string name) { this.name = name; } public string getName() { return myName; } } 0 class Dog extends Animal { private string name= "dog"; public Dog(string name) { super(name); // ?? } } 0 System.out.println(new Dog("Caesar").getName()); // ???

13 Scope 0 For a gun? For a clean mouth? 0 // Which variable is which?? class Foo { private double variable; public Foo(double variable) { this.variable = variable; // "this"?? // variable = variable; // Why is this incorrect? } public boolean compare(double variable) { if (this.variable == variable) { return true; } return false; } }

14 Scope 0 Where do variables live? Inside curly braces 0 class Foo { private double variable1 = 1.0; void function1() { double variable2 = 2.0; variable2 = variable1; } void function2() { double variable3 = 3.0; variable1 = variable3; variable3 = variable2; // ??? } }

15 Scope 0 Where do variables live? 0 void foo() { int a = 5; { int b = 6; a = b; } b = a; // ??? }

16 If 0 These are identical: 0 int a =...; if (a 0) {... } 0 int a =...; if (a < 0) {... } else if (a == 0) {... } else {... }

17 For 0 These are identical: 0 int[] array =...; 0 for (int i = 0; i < array.length; i++) { int element = array[i]; System.out.println(element); } 0 for (int element : array) { System.out.println(element); }

18 Casting 0 Bronze? For an arm? Throwing? 0 Object o = (Object)(new Integer(5)); 0 Animal a = new Dog(); // Dog extends Animal 0 This is "casting up the class hierarchy"

19 Casting 0 What about "casting down the class hierarchy"? 0 HondaCivic h= new Car(); // extends Car /* Wrong, and will fail at runtime! All HondaCivics have all the parts of a general car because HondaCivic extends Car, but there might be things (i.e. fields) that the HondaCivic class has in addition. Would the computer just make up things to fill these gaps? */ 0 HondaCivic h= (HondaCivic)((Car)(new HondaCivic(1994, "blue"))); /* This works though, and won't fail at runtime. The "real type" stays the same even though the "apparent type" changes */ 0 Protect with "if (c instanceof HondaCivic)..."

20 Wrapper classes 0 ArrayList aListOfNumbers =... // ??? 0 ArrayList aListOfNumbers =... 0 Remember "all classes are subclasses of Object"? Actually, the primitives aren't; without wrappers, this causes problems for things like ArrayLists that only work for subclasses of Object

21 Debugging 0 It's why there are best practices and good habits. You'll learn them over time 0 Maybe most common: 0 class Foo { int DEFAULT = 5; private int value = DEFAULT; public Foo(int value) { value = value; this.value == value; value = this.value; } public boolean isSameAs(int otherValue) { return value == otherValue; } public boolean isSameAs2(int otherValue) { return value = otherValue; } public void theProcedureIForgotToComplete() { } } 0 System.out.println(new Foo(4).isSameAs(5)); // Prints "true"??? 0 System.out.println(new Foo(4).isSameAs2(10)); // Prints "true"??? 0 new Foo(4).theProcedureIForgotToComplete(); // Won't work???

22 Arrays 0 2D array: 0 "Student ID" and "Student Name" aren't actually a part of it, that's just what we're arbitrarily labeling the columns: Student ID Student Name 1 Bob 2 Sue 3 Ann

23 Arrays 0 In Java, a 2D array is actually a 1D array of arrays: 0 int[,] array = new int[,] {{0, 2, 4}, {1, 3, 5}}; 0 [[0], [1]] [2] [3] [4] [5] 0 3D arrays are an array of arrays of arrays. Etc 0 You can have "jagged" arrays in Java: int[,] array = new int[,] {{0}, {0, 1, 2}}; string[] months = new string[] {"January", "Feb"};

24 GUI and ActionListeners 0 See project; set option in main() to DisplayOptions.ShowForms and run

25 Exceptions 0 class Int { public static int Parse(string s) { if (the string can't be turned into an int) { throw new Exception("Invalid string"); } return (the string as an int); } } 0 try { int i = Int.Parse("5.5"); } catch (Exception e) { System.out.println(e.getMessage()); }

26 Making Exceptions 0 Just extend from the Throwable class: 0 class MeaningLessException extends Throwable {... } 0 if (...) { throw new MeaningLessException(...); }

27 vs Error 0 Errors represent something more critical, something "unrecoverable"

28 Garbage collection 0 PPT 3 Slide 16

29 Questions?


Download ppt "CECS 220 Java Test Review. Variable Names 0 May Contain: a-z, A-Z, 0-9, _, $, characters from other languages. 0 May not start with 0-9. 0 Legal: 0 MyVariable."

Similar presentations


Ads by Google