Monday, February 19, 2007

Lecture 15

Things to know for the next Lab:

Inheritance, Arrays, Interface

Things to know for the Midterm:


everything we have covered until this class (Feb 19 2007) is fair game for the midterm
this is unofficially true but the only thing Andrew has told pertaining to Test Content.


Aggregation
  • keeps a reference to an external object
  • external object outlives the aggregate
Composition
  • keeps a copy of object it contains
  • contained object lives and dies with the composition
Polymorphism and Arrays

public class Parent{}

public class Child extends Parent{}

Parent P;
Child c;
c = new Child();
p = c;

  • this is legal
  • P is a reference to C
  • P is of type parent
  • P is a reference to C and is treated like an object of type Parent
  • remember
    • C inherits all of P's methods
    • C can use anything inherited from P
      • public methods ...etc
  • but when p=c
    • p can only access inherited methods from C
      • in other words only methods which exist in both classes can be used in p
  • suppose C has an overridden method
public class Parent
{
public void myMethod()
{
System.out.println("parent");
}
}
public class Child extends Parent()
{
public void myMethod()
{
System.out.println("child");
}
}
-------------------------------------------------

Child c;
Parent p;
c = new Child();
p = c
c.myMethod(); /* prints "child" */
p.myMethod(); /* prints "child" */
  • p.myMethod prints "child"
    • this is because java remembers that P started out as C which is type child
      • this is called "Late Binding"
Summary

Child c;
Parent p;
c = new Child();
c.myMethod(); /* prints "child" because of overriding*/
p = c;
p.myMethod(); /* prints "child" because of polymorphism / late binding*/
p = new Parent();
p.myMethod(); /* prints "parent" because it is not an object reference anymore*/

Wednesday, February 14, 2007

More On Arrays

Review of Arrays

  • Declaring
    • double[] testmark;
  • Assign Space
    • testMark = new double[5]
  • Use Elements in Array
    • testMark[3] = 720;
  • an array is a table of values stored in memory
  • the array name (testMark) is a reference to the array, just like object references
  • to refer the ith member of the array
    • testMark[i]
  • Very Important
    • array indices start at zero, not one.
  • use testMark.length
    • length is the keyword to get the length of the array
  • arrays are objects
    • they should be treated as such
  • because we can change the elements of an array and because arrays are objects we have to treat them as mutable objects
    • deep copy, defensive copy
  • look again at testMark

double[]copyMarks; // create a copy of testMarks
copyMarks = testmarks //BAD!!!
  • both references (copyMarks and testMarks) point to the same thing
  • remember this also happens with object references
  • how do we create a copy of an array?
    1. allocate new space
    2. individually copy elements

testMark = new double[5]
int i;
for (i=0, i < testMark.length, i++)
{
copyMark[i] = testMark[i];
}
  • Summary
    • Arrays are mutable objects which means Defensive copying
    • copy on object containing an array
      • deep copy
    • so i do deep copy test mark
    • testMark == copyMark
      • this is not the same reference
    • unfortunately equals() does not exist for arrays
    • there exist java utilities for array equals
      • java.util.arrays
        • equals
        • fill
        • sort

Lecture 14

Midterm Feb 25 2007


Command Line Arguments

public static void main (String[]args)
{
}


  • argument passed to main is an ARRAY of STRINGS
  • args is an array of Strings consisting of arguments typed on the command line when Java was executed
console - java -cp . ArgsTest one two three
  • that is 3 arguments
    • one
    • two
    • three
      • NOTE: args are seperated by empty spaces, NOT COMMAS, i tested this code myself after class
  • how many parameters?
    • args.length
  • this is useful for automation and for quick input to a program
  • can you pass array objects to a method as a parameter?
    • yes
public void method (double[]myArray)
{}
  • can you return an array?
    • yes
public double[] method()
{
}
  • remember to defensively copy

Aggregates and Compositions
  • a composition is like an aggregate but stronger
  • here the parts only exist in the context of the whole and vice versa
    • a car is not a car without an engine
    • a book is not a book without pages
  • An aggregation relationship
    • aggregate does not keep a private copy of its member objects
    • refers to them by reference
      • investments which contain stocks
public class Investment
{
private Stock x;
private int num;
}
public class Stock
{
private String name;
private double price;
}

  • who creates the stock?
    • investments constructor should not create a stock
    • should already exist when Investment is created
  • should Investment keep a local copy of the Stock?
    • No
      • the stock is under outside control
  • stock outlives the Investment and exists outside the Investment
  • this is an aggregation
  • a composition relationship
    • private copies are kept
    • contained objects do not exist outside of the composition
    • objects live and die with composition
public class CreditCard
{
private String name;
private int cardNum;
private GregorianCalendar dateOfIssue;

//this is a composition relationship

Wednesday, February 7, 2007

Lecture 13

Inner Classes

  • an inner class is a class defined within another class

Example

public class outerClass
{
private class innerClass
{
}
}
  • inner classes provide extra 'class-like' functionality to your class
    • makes your class look self contained
  • inner classes have access to private members of the outer class
    • and vice versa
  • inner classes can be made public and are then visible outside of the outer class

Access

public class outerClass
{
private int y;
private int x;

private class innerClass
{
private int y;

public void method()
{x=0 //if no private member in inner class named x
}
}
  • if no private member in innerClass is named x , it can refer to x in the outer class by name
  • to gain access to the outer class y
    • outerClass.this.y

public class outerClass
{
private int x;
private int y;
private class innerClass
{
private int y;
public void method()
{
x = 0; // refers to the outerClass x
y = 0; //refers to the innerClass y
outerClass.this.y; // refers to outerClass y
}
}
}

  • creating a public Object of an inner class type
public class outerClass
{
public class innerClass
{
}
}
  • first create an object of type outerClass
  • then create an object of type innerClass
outerClass a = new outerClass();
outerClass.innerClass b = a.new innerClass();

Aggregates Compositions and Arrays


Arrays

  • in undergrad say we want to store the grades for all the term tests instead of just the final raw grade
  • an array lets us refer the collections of variables of the same type using a single name and number
Very Important

  • Arrays in java always have their first object at zero (0)
  • if you don't know the length of an array
    • all arrays have a special public field called 'length'
testArray.length
  • Alternative Array Declaration
    • (with initialization)
double[]testArray = {0.0,0.0,0.0,0.0,0.0}
  • this array has 5 elements

Monday, February 5, 2007

Lab 3

Please Post your ideas for Lab 3 here! I haven't started yet, my labs are on tuesday!

For Lab Instructions: Click

Friday, February 2, 2007

SSH Tips and Tricks

I'm tired of programming in prism, i want to program at home.

Andrew uses an SSH client in class to mimic a Linux environment in Windows and to access example files from his login...

I want to do this, but i don't know how!

I spoke to Andrew he said any SSH client will work (putty...etc)
and that everything has to be saved and edited at york to do it through that client

does anyone use SSH at home?
if so can you put the URL for YorkU's SSH portal in the comments of this post?
as well anything you know about SSH that others will find useful would be great...

Later
Jake

Lecture 12

Abstract Classes
  • Abstract class is a class that cannot be instantiated
    • only its children can be instantiated


Abstract Method
  • a method that is not defined in the abstract class
    • only its head is given

Interfaces
  • can be thought of as an extreme abstract class
  • used to group together classes with similar features into a 'type'
  • interfaces contain only abstract method headers
    • no instance fields
  • an interface is a description of how to interact with a group of related classes

Syntax

public interface myInterface

{
/*any method in here is abstract, it is assumed that it is abstract so you don't have to write the keyword 'abstract'*/

public void method(int a);
public int method2();
}

  • any class that implements the interface must override method, and method2

public class MyClass implements MyInterface
{

private int f;
public void method1(int a)
{

System.out.println(a)

}
public int method2()
{
return f;
}

}

  • you can create objects of type MyClass and treat them as type myInterface if necessary

public void interfaceMethod(myInterface in, int a)
{

int b;
in.method1(a);
b = in.method2();

}

  • it is possible to implement multiple interfaces

public class MyClass implements interface1, interface2
{
}

/* this would force MyClass to have all methods from both interface before it will compile */


  • it is possible to have inheritance within interfaces

public interface MyInterface

{

public void M1();
public void M2();

}

public interface myChild extends myInterface

{
public void M3();
}

  • a class that implements an interface but doesn't override one or more interface methods must be an abstract class
  • all unimplemented methods are automatically abstract