Wednesday, February 14, 2007

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

No comments: