CECS 220 Java Test Review - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CECS 220 Java Test Review

Description:

Would the computer just make up ... Cambria Arial Rage Italic Calibri Courier New Sketchbook 1_Sketchbook 2_Sketchbook 3_Sketchbook CECS 220 Java Test ... – PowerPoint PPT presentation

Number of Views:211
Avg rating:3.0/5.0
Slides: 31
Provided by: Matth523
Category:
Tags: cecs | computer | java | review | test

less

Transcript and Presenter's Notes

Title: CECS 220 Java Test Review


1
CECS 220 Java Test Review
2
About me
  • Matthew Thomas
  • Senior
  • CECS
  • From Utica, KY

3
Variable Names
  • May Contain a-z, A-Z, 0-9, _, , characters from
    other languages.
  • May not start with 0-9.
  • Legal
  • MyVariable
  • myVariable
  • MYVARIABLE
  • x
  • _my_variable
  • myvariable
  • a?d???
  • This_is_an_insanely_long_variable_name_that_never_
    ends
  • Not Legal
  • My Variable
  • 9pins
  • ac
  • include
  • OReilly
  • Oreilly__Associates
  • _at_hotmail

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

5
Testing
  • JUnit vs not using an automated framework
  • Reference utilities.CalculatorTest
    vs utilities.testing.TestCalculator

6
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

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

8
Packages
  • For organization
  • package toolspublic class Calculator  public
    static int multiply(int a, int b) return a b
  • package toolspublic class AnotherTool  public
    static void function()
  • package stuffpublic class Calculator  public
    static void doSomething()

9
Packages
  • package com.example.toolspublic class
    Calculator   public static int multiply(int a,
    int b)     return a b 
  • com.examples.tools.Calculator.multiply(4, 5)
  • import com.examples.tools.Calculator...Calculat
    or.multiply(4, 5)

10
Class, instances of class
  • JFrame f new JFrame()f.add(...)JFrame.add(..
    .) ???
  • Double num new Double(5.5)double value
    num.doubleValue()double value2
    Double.doubleValue() ???
  • double value Double.parseDouble("5.5")double
    value2 value.parseDouble("5.5") ???

11
this
  • class Foo   private static final int DEFAULT
    5  private int myNumber  public Foo()    
    this(DEFAULT) // ??    public Foo(int number)
        myNumber number 

12
this
  • class Foo   private static final int DEFAULT
    5  private int number  public Foo()    
    this(DEFAULT)     public Foo(int number)  
      this.number number // ?? 

13
Extending classes
  • class Animal   private string name "animal" 
    public Animal(string name)     this.name
    name    public string getName()     return
    myName 
  • class Dog extends Animal   private string name
    "dog"  public Dog(string name)    
    super(name) // ?? 
  • System.out.println(new Dog("Caesar").getName())
    // ???

14
Scope
  • For a gun? For a clean mouth?
  • // 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 

15
Scope
  • Where do variables live? Inside curly braces
  • 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 // ??? 

16
Scope
  • Where do variables live?
  • void foo()   int a 5  int b 6 a b
      b a // ???

17
If
  • These are identical
  • int a ...if (a lt 0)   ...if (a 0)  
    ...if (a gt 0)   ...
  • int a ...if (a lt 0)   ... else if (a
    0)   ... else   ...

18
For
  • These are identical
  • int array ...
  • for (int i 0 i lt array.length i)   int
    element arrayi  System.out.println(element)
  • for (int element array)   System.out.println(e
    lement)

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

20
Casting
  • What about "casting down the class hierarchy"?
  • 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? /
  • 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
    /
  • Protect with "if (c instanceof HondaCivic) ..."

21
Wrapper classes
  • ArrayListltintgt aListOfNumbers ... // ???
  • ArrayListltIntegergt aListOfNumbers ...
  • 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

22
Debugging
  • It's why there are best practices and good habits
    . You'll learn them over time
  • Maybe most common
  • 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()  
  • System.out.println(new Foo(4).isSameAs(5)) //
    Prints "true"???
  • System.out.println(new Foo(4).isSameAs2(10)) //
    Prints "true"???
  • new Foo(4).theProcedureIForgotToComplete() //
    Won't work???

23
Arrays
  • 2D array
  • "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

24
Arrays
  • In Java, a 2D array is actually a 1D array of
    arrays
  • int, array new int, 0, 2, 4, 1, 3, 5
  • 0, 1 2  3 4  5
  • 3D arrays are an array of arrays of arrays. Etc
  • You can have "jagged" arrays in Javaint,
    array new int, 0, 0, 1, 2string
    months new string "January", "Feb"

25
GUI and ActionListeners
  • See project set option in main() to
    DisplayOptions.ShowForms and run

26
Exceptions
  • 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) 
  • try   int i Int.Parse("5.5") catch
    (Exception e)   System.out.println(e.getMessage(
    ))

27
Making Exceptions
  • Just extend from the Throwable class
  • class MeaningLessException extends Throwable  
    ...
  • if (...)   throw new MeaningLessException(...)

28
vs Error
  • Errors represent something more critical,
    something "unrecoverable"

29
Garbage collection
  • PPT 3 Slide 16

30
Questions?
Write a Comment
User Comments (0)
About PowerShow.com