Young Coders: Week 3 Notes
Yesterday, we learned about classes and some data structures.
public class Pet {
//fields
public String name;
public String color;
public int legs;
public int speed;
private int pinNum;
//constructor
public Pet (String name, String color, int legs, int speed) {
this.name = name;
this.color = color;
this.legs = legs;
this.speed = speed;
pinNum = 1111;
}
//methods
public int getPin() {
return pinNum;
}
public void setColor(String color) {
this.color = color;
}
public void exercise(int increase) {
speed += increase;
color = "red";
}
public void sleep() {
speed = 0;
color = "blue";
}
public void wakeUp() {
speed = 5;
color = "green";
}
public String toString() {
return "this works";
}
public static void main(String[] args) {
Pet myFirstPet = new Pet("Finn", "purple", 0, 5);
myFirstPet.exercise(3);
myFirstPet.sleep();
System.out.println(myFirstPet);
}
}
Classes
A “specification” or “blueprint” that says how to construct an object. Here is the Pet class we made:public class Pet {
//fields
public String name;
public String color;
public int legs;
public int speed;
private int pinNum;
//constructor
public Pet (String name, String color, int legs, int speed) {
this.name = name;
this.color = color;
this.legs = legs;
this.speed = speed;
pinNum = 1111;
}
//methods
public int getPin() {
return pinNum;
}
public void setColor(String color) {
this.color = color;
}
public void exercise(int increase) {
speed += increase;
color = "red";
}
public void sleep() {
speed = 0;
color = "blue";
}
public void wakeUp() {
speed = 5;
color = "green";
}
public String toString() {
return "this works";
}
public static void main(String[] args) {
Pet myFirstPet = new Pet("Finn", "purple", 0, 5);
myFirstPet.exercise(3);
myFirstPet.sleep();
System.out.println(myFirstPet);
}
}
Subclasses
Why use subclasses?
The Pet class already has some fields and methods that overlap with our new class, Dog. We don't want to rewrite all that code!
Some things to know:
The Pet class already has some fields and methods that overlap with our new class, Dog. We don't want to rewrite all that code!
Some things to know:
-
A subclass constructor can invoke the
constructor of the superclass. (Example: the Dog constructor)
-
Declare new methods in the subclass. (Example: trainDog() method)
-
Use the fields and methods of the superclass. A
subclass inherits fields and methods from its
superclass. (Example: the field, "color")
-
Override a method in the superclass. (Example: wakeUp() method)
-
Block the subclass from inheriting members of
the superclass by using private. The subclass can
access those members if you write
public/protected accessor methods. (Example: our dog's pin number and the Pet class's method getPin())
Here is our subclass example, Dog:
public class Dog extends Pet {
//field
public boolean sit;
//constructor
public Dog (String name, String color, int speed, boolean sit) {
super(name, color, 4, speed);
this.sit = sit;
}
//method
public void trainDog() {
sit = true;
}
public void wakeUp() {
speed = -5;
color = "brown";
}
public static void main(String[] args) {
Dog myFirstDog = new Dog("Molly", "black", 20, false);
//myFirstDog.trainDog();
//System.out.println(myFirstDog.sit);
//myFirstDog.sleep();
//System.out.println(myFirstDog.color);
//myFirstDog.wakeUp();
//System.out.println(myFirstDog.color);
System.out.println(myFirstDog.getPin());
}
}
Stacks
A stack is a data structure (a collection of elements). It's LIFO, or "last in first out". Imagine a stack of dishes or papers. The last dish or paper on the stack is on the top, so it's also the first one you remove.In our Stack class, two of the methods are "push" and "pop". When we add an element to a stack, we say we "push" it. When we remove an element from a stack, we "pop" that element.To implement a Stack (of integers), we made a pointer that points to the top of the stack. We also used an array of integers:
import java.util.Arrays;public class Stack {//fieldsprivate int[] array;private static final int CAPACITY = 10;private int topOfStack = -1;//constructorpublic Stack() {array = new int[CAPACITY];}//methodspublic void push(int element) {if(array.length == CAPACITY)reallocate();topOfStack++;array[topOfStack] = element;}public int pop() {if(empty())throw new RuntimeException("stack is empty - nothing to pop");int removed = array[topOfStack];topOfStack--;return removed;}public boolean empty() {if(topOfStack == -1)return true;elsereturn false;}private void reallocate() {int length = array.length;array = Arrays.copyOf(array, 2*length);}public static void main(String[] args) {Stack myFirstStack = new Stack();//myFirstStack.pop();myFirstStack.push(100);myFirstStack.push(200);myFirstStack.push(99);int removedElement = myFirstStack.pop();System.out.println(removedElement);}}
ArrayList
Arrays have a fixed length. You have to know beforehand how many elements the array will contain. On the other hand, ArrayLists have an initial size, but can grow and shrink when you add or remove objects. Here are some examples we tried yesterday:import java.util.ArrayList;public class ArrayListEx{public static void main(String[] args) {ArrayList<String> colors = new ArrayList<String>();System.out.println(colors.size());colors.add("brown");colors.add("blue");colors.add("magenta");colors.add("red");colors.add("pink");System.out.println(colors.size());colors.add(3, "gray");System.out.println(colors);}}Queue
A queue is FIFO (first in first out). We thought of a queue as a line of people waiting at Disneyland. If you are the first one to line up, you should be the first one out of the line and on the ride!import java.util.ArrayList;public class Queue {//fieldsprivate ArrayList<String> array;//constructorpublic Queue () {array = new ArrayList<String>();}public void enq(String element) {array.add(element);}public String deq() throws Exception {if(array.size() < 0)throw new Exception("nothing to deq");String element = array.remove(0);return element;}public int size() {return array.size();}public static void main(String[] args) throws Exception {Queue animals = new Queue();animals.enq("Tiger");animals.enq("Pig");animals.enq("Zebra");animals.enq("Lion squirrel");animals.enq("Anteater");animals.enq("My brother");//System.out.println(animals);System.out.println(animals.size());System.out.println(animals.deq());}}A reminder: the Candy Challenge for this week is to write a toString() method for our queue class that returns its elements!Good job yesterday! Next week will be our final class. Same time, same place: Sunday, 1-3 pm. We'll be looking at sorting and searching algorithms.