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

29
CECS 220 CECS 220 Java Test Java Test Review Review

Transcript of CECS 220 Java Test Review. Variable Names 0 May Contain: a-z, A-Z, 0-9, _, $, characters from other...

Page 1: 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.

CECS 220 Java CECS 220 Java Test ReviewTest Review

Page 2: 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.

Variable Names0 May Contain: a-z, A-Z, 0-9, _, $, characters from other languages.0 May not start with 0-9.0 Legal:

0 MyVariable0 myVariable0 MYVARIABLE0 x0 _my_variable0 $myvariable0 ανδρος0 This_is_an_insanely_long_variable_name_that_never_ends

0 Not Legal:0 My Variable0 9pins0 a+c0 #include0 O’Reilly0 Oreilly_&_Associates0 @hotmail

Page 3: 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.

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);}

Page 4: 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.

Testing

0 JUnit vs not using an automated framework0Reference utilities.CalculatorTest

vs utilities.testing.TestCalculator

Page 5: 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.

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;}

Page 6: 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.

To be or not to be static?

0Same applies to functions and procedures0Should eat() be a function of apples in general, or a

specific one?0Should getNumberOfLegs() be a function of cats in

general, or a specific one?

Page 7: 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.

Packages0 For organization0 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() { }}

Page 8: 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.

Packages

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

Page 9: 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.

Class, instances of class

0 JFrame f = new JFrame();f.add(...);JFrame.add(...); ???

0Double num = new Double(5.5);double value = num.doubleValue();double value2 = Double.doubleValue(); ???

0double value = Double.parseDouble("5.5");double value2 = value.parseDouble("5.5"); ???

Page 10: 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.

this

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

Page 11: 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.

this

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

Page 12: 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.

Extending classes0 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()); // ???

Page 13: 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.

Scope0 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;  }}

Page 14: 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.

Scope0 Where do variables live? Inside curly braces0 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; // ???  }}

Page 15: 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.

Scope

0Where do variables live?0void foo() {

  int a = 5;  { int b = 6; a = b; }  b = a; // ???}

Page 16: 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.

If0 These are identical:0 int a = ...;

if (a < 0) {  ...}if (a == 0) {  ...}if (a > 0) {  ...}

0 int a = ...;if (a < 0) {  ...} else if (a == 0) {  ...} else {  ...}

Page 17: 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.

For

0These 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);}

Page 18: 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.

Casting

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

Page 19: 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.

Casting0 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) ..."

Page 20: 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.

Wrapper classes

0ArrayList<int> aListOfNumbers = ... // ???0ArrayList<Integer> aListOfNumbers = ...

0Remember "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

Page 21: 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.

Debugging0 It's why there are best practices and good habits . You'll learn them over time0 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???

Page 22: 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.

Arrays

02D 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 Name1            Bob2            Sue3            Ann

Page 23: 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.

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]

03D arrays are an array of arrays of arrays. Etc0You can have "jagged" arrays in Java:

int[,] array = new int[,] {{0}, {0, 1, 2}};string[] months = new string[] {"January", "Feb"};

Page 24: 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.

GUI and ActionListeners

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

Page 25: 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.

Exceptions0 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());}

Page 26: 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.

Making Exceptions

0 Just extend from the Throwable class:0 class MeaningLessException extends Throwable {

  ...}

0 if (...) {  throw new MeaningLessException(...);}

Page 27: 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.

vs Error

0Errors represent something more critical, something "unrecoverable"

Page 28: 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.

Garbage collection

0PPT 3 Slide 16

Page 29: 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.

Questions?