Notes and Definitions
Classes (in object oriengted programming)
- a blueprint/template for creating objects, defines the properties and methods that objects of the class can ahve
- objects will share common properties and behaviors
Access Modifiers
- keywords that determine accessibility of class, inclduing its members and methods
- 4 types of access modifiers in Java : public, protected, private, and package-private (default)
- Public: A public class or member is accessible from any other class in any package
- Protected: A protected class or member is accessible within its own package or by a subclass in a different package
- Private: A private class or member is accessible only within the same class
- Package-Private: A package-private class or member is accessible only within its own package
Constructors
- speacial method that is called when an object of the class is created
- initiliazes object's properties / sets up its encironemt
- has the same name as the class and does not ahve a return type
- paramters are optional
Modifiers/Setters
- methods used to modify/set value of object's property
- usually used to ensure data integrity and encapsulation
- can be public, private, protected, or package-private
- any return type
Getters
- methods used to get value of object's property
- used to access private or protected properties
- should be public and have return type that matches the type of the proeprty taht are retrieving Getters are methods used to retrieve the value of an object's properties. They are used to access private or protected properties of an object. Getters should be public and have a return type that matches the type of the property they are getting.
Example of a class that incorporates access modifiers, constructors, modifiers/setters, and getters:
public class Cat {
private String name;
private int age;
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
In this example, the class is called "Cat". It has two private properties, name and age. There is one constructor that takes the two paramerters, name and age. There are two modifier methods, setName and setAge. Two getter methods, getName and getAge.
public class Node<T> {
private T value;
private Node<T> next;
public Node(T value) {
this.value = value;
this.next = null;
}
public T getValue() {
return this.value;
}
public Node<T> getNext() {
return this.next;
}
public void setNext(Node<T> next) {
this.next = next;
}
}
public class LinkedList<T> {
private Node<T> head;
public LinkedList() {
this.head = null;
}
public void add(T value) {
Node<T> newNode = new Node<>(value);
if (head == null) {
head = newNode;
} else {
Node<T> current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
}
public void remove(T value) {
if (head == null) {
return;
}
if (head.getValue().equals(value)) {
head = head.getNext();
return;
}
Node<T> current = head;
while (current.getNext() != null) {
if (current.getNext().getValue().equals(value)) {
current.setNext(current.getNext().getNext());
return;
}
current = current.getNext();
}
}
public void print() {
Node<T> current = head;
while (current != null) {
System.out.print(current.getValue() + " ");
current = current.getNext();
}
System.out.println();
}
}
public class Queue<T> {
private LinkedList<T> list;
public Queue() {
this.list = new LinkedList<>();
}
public void enqueue(T value) {
list.add(value);
}
public T dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
T value = list.getValue();
list.remove(value);
return value;
}
public boolean isEmpty() {
return list == null || list.getValue() == null;
}
}
/* This is wrapper class...
Objective would be to push more functionality into this Class to enforce consistent definition
*/
public abstract class Generics {
public final String masterType = "Generic";
private String type; // extender should define their data type
// generic enumerated interface
public interface KeyTypes {
String name();
}
protected abstract KeyTypes getKey(); // this method helps force usage of KeyTypes
// getter
public String getMasterType() {
return masterType;
}
// getter
public String getType() {
return type;
}
// setter
public void setType(String type) {
this.type = type;
}
// this method is used to establish key order
public abstract String toString();
// static print method used by extended classes
public static void print(Generics[] objs) {
// print 'Object' properties
System.out.println(objs.getClass() + " " + objs.length);
// print 'Generics' properties
if (objs.length > 0) {
Generics obj = objs[0]; // Look at properties of 1st element
System.out.println(
obj.getMasterType() + ": " +
obj.getType() +
" listed by " +
obj.getKey());
}
// print "Generics: Objects'
for(Object o : objs) // observe that type is Opaque
System.out.println(o);
System.out.println();
}
}
public class Alphabet extends Generics {
// Class data
public static KeyTypes key = KeyType.title; // static initializer
public static void setOrder(KeyTypes key) {Alphabet.key = key;}
public enum KeyType implements KeyTypes {title, letter}
private static final int size = 26; // constant used in data initialization
// Instance data
private final char letter;
/*
* single letter object
*/
public Alphabet(char letter)
{
this.setType("Alphabet");
this.letter = letter;
}
/* 'Generics' requires getKey to help enforce KeyTypes usage */
@Override
protected KeyTypes getKey() { return Alphabet.key; }
/* 'Generics' requires toString override
* toString provides data based off of Static Key setting
*/
@Override
public String toString()
{
String output="";
if (KeyType.letter.equals(this.getKey())) {
output += this.letter;
} else {
output += super.getType() + ": " + this.letter;
}
return output;
}
// Test data initializer for upper case Alphabet
public static Alphabet[] alphabetData()
{
Alphabet[] alphabet = new Alphabet[Alphabet.size];
for (int i = 0; i < Alphabet.size; i++)
{
alphabet[i] = new Alphabet( (char)('A' + i) );
}
return alphabet;
}
/*
* main to test Animal class
*/
public static void main(String[] args)
{
// Inheritance Hierarchy
Alphabet[] objs = alphabetData();
// print with title
Alphabet.setOrder(KeyType.title);
Alphabet.print(objs);
// print letter only
Alphabet.setOrder(KeyType.letter);
Alphabet.print(objs);
}
}
Alphabet.main(null);
public class Cupcake extends Generics {
// Class data
public static KeyTypes key = KeyType.title; // static initializer
public static void setOrder(KeyTypes key) {Cupcake.key = key;}
public enum KeyType implements KeyTypes {title, flavor, frosting, sprinkles}
// Instance data
private final String frosting;
private final int sprinkles;
private final String flavor;
// Constructor
Cupcake(String frosting, int sprinkles, String flavor)
{
this.setType("Cupcake");
this.frosting = frosting;
this.sprinkles = sprinkles;
this.flavor = flavor;
}
/* 'Generics' requires getKey to help enforce KeyTypes usage */
@Override
protected KeyTypes getKey() { return Cupcake.key; }
/* 'Generics' requires toString override
* toString provides data based off of Static Key setting
*/
@Override
public String toString() {
String output="";
if (KeyType.flavor.equals(this.getKey())) {
output += this.flavor;
} else if (KeyType.frosting.equals(this.getKey())) {
output += this.frosting;
} else if (KeyType.sprinkles.equals(this.getKey())) {
output += "00" + this.sprinkles;
output = output.substring(output.length() - 2);
} else {
output = super.getType() + ": " + this.flavor + ", " + this.frosting + ", " + this.sprinkles;
}
return output;
}
// Test data initializer
public static Cupcake[] cupcakes() {
return new Cupcake[]{
new Cupcake("Red", 4, "Red Velvet"),
new Cupcake("Orange", 5, "Orange"),
new Cupcake("Yellow", 6, "Lemon"),
new Cupcake("Green", 7, "Apple"),
new Cupcake("Blue", 8, "Blueberry"),
new Cupcake("Purple", 9, "Blackberry"),
new Cupcake("Pink", 10, "Strawberry"),
new Cupcake("Tan", 11, "Vanilla"),
new Cupcake("Brown", 12, "Chocolate"),
};
}
public static void main(String[] args)
{
// Inheritance Hierarchy
Cupcake[] objs = cupcakes();
// print with title
Cupcake.setOrder(KeyType.title);
Cupcake.print(objs);
// print flavor only
Cupcake.setOrder(KeyType.flavor);
Cupcake.print(objs);
}
}
Cupcake.main(null);
public class Pets extends Generics {
// Class data
public static KeyTypes key = KeyType.name; // static initializer
public static void setOrder(KeyTypes key) { Pets.key = key; }
public enum KeyType implements KeyTypes { name, age, animal }
// Instance data
private final String name;
private final int age;
private final String animal;
// Constructor
public Pets(String name, int age, String animal) {
this.setType("Pets");
this.name = name;
this.age = age;
this.animal = animal;
}
/* 'Generics' requires getKey to help enforce KeyTypes usage */
@Override
protected KeyTypes getKey() { return Pets.key; }
/* 'Generics' requires toString override
* toString provides data based off of Static Key setting
*/
@Override
public String toString() {
String output = "";
if (KeyType.name.equals(this.getKey())) {
output += this.name;
} else if (KeyType.age.equals(this.getKey())) {
output += "Age: " + this.age;
} else if (KeyType.animal.equals(this.getKey())) {
output += "Animal: " + this.animal;
} else {
output = super.getType() + ": " + this.name + ", Age: " + this.age + ", Animal: " + this.animal;
}
return output;
}
// Test data initializer
public static Pets[] pets() {
return new Pets[]{
new Pets("Spot", 1, "Ladybug"),
new Pets("Luca", 2, "Cat"),
new Pets("Apollo", 3, "Dog"),
new Pets("Binger", 4, "Cat"),
new Pets("Mart", 5, "Rat"),
new Pets("Kevin", 6, "Parrot"),
new Pets("Bartholomew", 7, "Hamster"),
new Pets("Slugzie", 8, "Slug"),
new Pets("Fattie", 9, "Dog"),
new Pets("Squish", 10, "Squid"),
};
}
public static void main(String[] args) {
// Inheritance Hierarchy
Pets[] objs = pets();
// print with title
Pets.setOrder(KeyType.name);
Generics.print(objs);
// print difficulty only
Pets.setOrder(KeyType.age);
Generics.print(objs);
// print question only
Pets.setOrder(KeyType.animal);
Generics.print(objs);
}
}
Pets.main(null);