cecs 220 java test review. variable names 0 may contain: a-z, a-z, 0-9, _, $, characters from other...

Post on 20-Jan-2016

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CECS 220 Java CECS 220 Java Test ReviewTest Review

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

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

Testing

0 JUnit vs not using an automated framework0Reference utilities.CalculatorTest

vs utilities.testing.TestCalculator

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

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?

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

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

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"); ???

this

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

this

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

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

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

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

Scope

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

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

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 {  ...}

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

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"

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

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

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???

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

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

GUI and ActionListeners

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

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

Making Exceptions

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

  ...}

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

vs Error

0Errors represent something more critical, something "unrecoverable"

Garbage collection

0PPT 3 Slide 16

Questions?

top related