Lecture 2: Defining and Using Classes

8/28/2020

Compilation

Compiling and running code from the terminal: $ javac HelloWorld.java $ ls HelloWorld.java HelloWorld.class $ java HelloWorld Hello World!

Compilation

Defining and Instantiating Classes

/** Dog.java file */ public class Dog { public static void makeNoise() { System.out.println("Bark!"); } } /** Can't be run directly, no main method */ /** DogLauncher.java file */ /** The DogLauncher class will test drive the Dog class */ public class DogLauncher { public static void main(String[] args) { Dog.makeNoise(); } } $ java DogLauncher Bark!

Dog

Object Instantiation

public class Dog { public int weightInPounds; // An instance variable /** One integer constructor for dogs. */ public Dog(int w) { // Constructor (similar to __init__) weightInPounds = w; } public void makeNoise() { // Non-static method, instance method if (weightInPounds < 10) { System.out.println("yip!"); } else if (weightInPounds < 30) { System.out.println("bark."); } else { System.out.println("wooooof!"); } } } /** DogLauncher.java file */ /** The DogLauncher class will test drive the Dog class */ public class DogLauncher { public static void main(String[] args) { Dog smallDog; // Declaration of a Dog instance new Dog(20); // Instantiation of a Dog instance smallDog = new Dog(5); // Instantiation and Assignment Dog mediumDog = new Dog(25); // Declaration, Instantiation, and Assignment d.makeNoise(); // Invocation of the 25 lb Dog's makeNoise method } } $ java DogLauncher bark.

Defining a Typical Class (Terminology)

Arrays of Objects

Dog[] dogs = new Dog[2]; // Creates an array of Dogs of size 2 dogs[0] = new Dog(8); dogs[1] = new Dog(20); dogs[0].makeNoise(); // Yipping occurs

Static vs Instance Methods

Static vs Non-static

Why Static Methods?

public class Dog { public int weightInPounds; // An instance variable /** A static variable that applies to all dog instances */ public static String binomen = "Canis familiaris"; /** One integer constructor for dogs. */ public Dog(int w) { // Constructor (similar to __init__) weightInPounds = w; } public void makeNoise() { // Non-static method, instance method if (weightInPounds < 10) { System.out.println("yip!"); } else if (weightInPounds < 30) { System.out.println("bark."); } else { System.out.println("wooooof!"); } } public static Dog maxDog(Dog d1, Dog d2) { if (d1.weightInPounds > d2.weightInPounds) { return d1; } return d2; } public Dog maxDog(Dog d2) { if (this.weightInPounds > d2.weightInPounds) { return this; } return d2; } } public class DogLauncher { public static void main(String[] args) { Dog d = new Dog(15); Dog d2 = new Dog(100); Dog bigger = Dog.maxDog(d, d2); bigger.makeNoise(); Dog bigger2 = d.maxDog(d2); bigger2.makeNoise(); System.out.println(d.binomen); System.out.println(Dog.binomen); } } $ java DogLauncher woooooof! woooooof! Canis familiaris Canis familiaris

Static vs. Non-static

public static void main(String[] args)

One Special Role for String: Command Line Arguments

public class ArgsDemo { /** Prints out the -th command line argument. */ public static void main(String[] args) { System.out.println(args[0]); } } $ java ArgsDemo hello some args hello

ArgsSum Exercise

public class ArgsSum { public static void main(String[] args) { int N = args.length; int i = 0; int sum = 0; while (i < N) { sum = sums + Integer.parseInt(args[i]); i = i + 1; } } }

Using Libraries (e.g. StdDraw, In)

Java Libraries