Units 6-10 Jupyter Notebook
Notes and code on units 6-10
Homeworks
- Unit 6: .9/1
- Unit 7: 1/1
- Unit 8: .9/1
- Unit 9: .95/1
- Unit 10: 1/1
- Unit 10 was probably the best homework because it had creativity and you could choose what type of recursion to show your knowledge
- Total: 4.75/5
Unit 6: Arrays
- Arrays are important to store a "list" of data in the same type
- Watch out for errors: especially ArrayIndexOutOfBoundsException
- Important to know algorithms with arrays -> using for loops
- Enhanced for loops can be utilized but has limitations
- Algorithms can be like finding max, min, average, sorting, etc.
int[] array = {1, 2, 3, 4, 5, 10, 15};
for (int num : array) {
if (num % 5 == 0 && num % 3 != 0) {
System.out.println(num);
}
}
// using "Integer" wrapper class
ArrayList<Integer> listOfIntegers = new ArrayList<>();
// Explicitly creating integer
listOfIntegers.add(new Integer(10));
// automatically converts to Integer
listOfIntegers.add(1);
// using toString method of ArrayList
System.out.println(listOfIntegers);
// defining 2d array
int[][] arr2d = {
{1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10}
};
// using nested for loops
for (int[] row : arr2d) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
public class Animal {
String color;
int age;
public Animal () {}
public Animal (String color, int age) {
this.color = color;
this.age = age;
}
public void sayHello () {
System.out.println("hello, I am " + color + " and I am " + age + " years old.");
}
public void walk () {
System.out.println("walking...");
}
public void eat () {
System.out.println("eating");
}
}
public class Cat extends Animal {
String owner;
public Cat (String color, int age, String owner) {
this.color = color;
this.age = age;
this.owner = owner;
}
public void sayOwner () {
System.out.println("my owner is " + owner);
}
}
Cat c = new Cat("green", 2, "joe");
// using method from parent class
c.sayHello();
// using method from child class
c.sayOwner();
public class Animal {
String color;
int age;
public Animal () {}
public Animal (String color, int age) {
this.color = color;
this.age = age;
}
public void sayHello () {
System.out.println("hello, I am " + color + " and I am " + age + " years old.");
}
public void walk () {
System.out.println("walking...");
}
public void eat () {
System.out.println("eating");
}
}
public class Cat extends Animal {
String owner;
public Cat (String color, int age, String owner) {
// reduce code duplication
super(color, age);
this.owner = owner;
}
public void sayOwner () {
System.out.println("my owner is " + owner);
}
public void sayOwnerAndHello() {
super.sayHello();
sayOwner();
}
}
Cat c = new Cat("green", 2, "joe");
c.sayOwnerAndHello();
public class Animal {
String color;
int age;
public Animal () {}
public Animal (String color, int age) {
this.color = color;
this.age = age;
}
public void sayHello () {
System.out.println("hello, I am " + color + " and I am " + age + " years old.");
}
public void walk () {
System.out.println("walking...");
}
public void eat () {
System.out.println("eating");
}
}
public class Cat extends Animal {
String owner;
public Cat (String color, int age, String owner) {
// reduce code duplication
super(color, age);
this.owner = owner;
}
public void sayOwner () {
System.out.println("my owner is " + owner);
}
// Adding more functionality in say hello for cat
@Override
public void sayHello() {
super.sayHello();
System.out.println("meow...");
}
}
// Cat uses cat method
Cat c = new Cat("green", 2, "joe");
c.sayHello();
// Animal uses animal method
Animal a = new Animal("blue", 3);
a.sayHello();
abstract class Animal {
String color;
int age;
public Animal () {}
public Animal (String color, int age) {
this.color = color;
this.age = age;
}
// must be defined in child class
abstract void sayHello ();
public void walk () {
System.out.println("walking...");
}
public void eat () {
System.out.println("eating");
}
}
public class Cat extends Animal {
String owner;
public Cat (String color, int age, String owner) {
// reduce code duplication
super(color, age);
this.owner = owner;
}
public void sayOwner () {
System.out.println("my owner is " + owner);
}
// Defining method that was abstract
@Override
public void sayHello() {
System.out.println("meow...");
}
}
// Cat uses cat method
Cat c = new Cat("green", 2, "joe");
c.sayHello();
abstract class Animal {
String color;
int age;
public Animal () {}
public Animal (String color, int age) {
this.color = color;
this.age = age;
}
// must be defined in child class
abstract void sayHello ();
public void walk () {
System.out.println("walking...");
}
public void eat () {
System.out.println("eating");
}
}
public class Cat extends Animal {
String owner;
public Cat (String color, int age, String owner) {
// reduce code duplication
super(color, age);
this.owner = owner;
}
public void sayOwner () {
System.out.println("my owner is " + owner);
}
// Defining method that was abstract
@Override
public void sayHello() {
System.out.println("meow...");
}
}
// Defining cat as an animal!
Animal c = new Cat("green", 2, "joe");
// can use because abstract method guarentees implentation in child class
c.sayHello();
abstract class Animal {
String color;
int age;
public Animal () {}
public Animal (String color, int age) {
this.color = color;
this.age = age;
}
// must be defined in child class
abstract void sayHello ();
public void walk () {
System.out.println("walking...");
}
public void eat () {
System.out.println("eating");
}
}
public class Cat extends Animal {
String owner;
public Cat (String color, int age, String owner) {
// reduce code duplication
super(color, age);
this.owner = owner;
}
public void sayOwner () {
System.out.println("my owner is " + owner);
}
// Defining method that was abstract
@Override
public void sayHello() {
System.out.println("meow...");
}
// same method with different arguments
public void sayHello(String person) {
System.out.println("meow... hello " + person);
}
}
Cat c = new Cat("green", 2, "joe");
// two different argument sets for same method
c.sayHello();
c.sayHello("mark")
abstract class Animal {
String color;
int age;
public Animal () {}
public Animal (String color, int age) {
this.color = color;
this.age = age;
}
// must be defined in child class
abstract void sayHello ();
public void walk () {
System.out.println("walking...");
}
public void eat () {
System.out.println("eating");
}
}
public class Cat extends Animal {
String owner;
public Cat (String color, int age, String owner) {
// reduce code duplication
super(color, age);
this.owner = owner;
}
public void sayOwner () {
System.out.println("my owner is " + owner);
}
// Defining method that was abstract
@Override
public void sayHello() {
System.out.println("meow...");
}
// same method with different arguments
public void sayHello(String person) {
System.out.println("meow... hello " + person);
}
// Overriding the to string
@Override
public String toString () {
return "[Cat]: " + "color=" + color + ", " + "age=" + age + ", " + "owner=" + owner;
}
}
// Object superclass is automatically inherited
Object c = new Cat("green", 2, "joe");
// System.out.println uses toString method internally
System.out.println(c);
public int factorial (int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n-1);
}
System.out.println(factorial(5));