diff --git a/Week3/Abstraction/README.md b/Week3/Abstraction/README.md new file mode 100644 index 0000000..42b4eab --- /dev/null +++ b/Week3/Abstraction/README.md @@ -0,0 +1,29 @@ +# Abstraction 🍵 + +### Task 🐧 + - Create an abstract class called Shirt: + - Make an instance variable called color + - Make a constructor that takes a String for the color & sets it to the instance variable + - Make a public method called `getColor()` that returns the name of the color + - Make an empty abstract method called `getDescription()` + + - Create a default class called `T_Shirt` that extends Shirt: + - Make an instance variable called size + - Make an instance variable called price + - Make a constructor that takes in a color, size, price and sets the instance variables (Use super() to call the constructor of Shirt) + - Override the method called `getDescription()` that returns the color, size, and price of the T_Shirt + + - Create another default class called 'Jacket' that extends Shirt: + - Make an instance variable called price + - Make an instance variable called brand + - Make a constructor that takes in a color, and price, and sets the instance variables (Use super() to call the constructor of Shirt) + - Override the method called `getDescription()` that returns the color, brand, and price of the T_Shirt + +### Requirements 🏫 +``` +1. A .java file +2. Prints the color & size of a T_Shirt +3. Prints the color & size of a Jacket +4. Prints the isZip of a Jacket +5. Prints the total price of the items +``` \ No newline at end of file diff --git a/Week3/Abstraction/Shirt.java b/Week3/Abstraction/Shirt.java new file mode 100644 index 0000000..b09cbaa --- /dev/null +++ b/Week3/Abstraction/Shirt.java @@ -0,0 +1,53 @@ +// Abstract means that you can't create a object of this class, only it's children +public abstract class Shirt { + public String color; + + public Shirt(String color) { + this.color = color; + } + + public String getColor() { + return this.color; + } + + abstract void getDescription(); + + public static void main(String[] args) { + Shirt[] myShirt = {new T_Shirt("Blue", "XL:", 29.99), new Jacket("Black", "Adidas", 49.99)}; + for (int i = 0; i < myShirt.length; i++) { + myShirt[i].getDescription(); + } + } +} + +class T_Shirt extends Shirt { + private String size; + private double price; + + public T_Shirt(String color, String size, double price) { + super(color); + this.size = size; + this.price = price; + } + + @Override + public void getDescription() { + System.out.println("\n\nT-Shirt:\nColor: " + color + "\nSize: " + size + "\nPrice: $" + price); + } +} + +class Jacket extends Shirt { + private double price; + private String brand; + + public Jacket(String color, String brand, double price) { + super(color); + this.brand = brand; + this.price = price; + } + + @Override + public void getDescription() { + System.out.println("\n\nJacket:\nColor: " + color + "\nBraand: " + brand + "\nPrice: $" + price); + } +} \ No newline at end of file diff --git a/Week3/Arrays/Array.java b/Week3/Arrays/Array.java new file mode 100644 index 0000000..3f132ba --- /dev/null +++ b/Week3/Arrays/Array.java @@ -0,0 +1,10 @@ +public class Array { + public static void main(String[] args) { + String[] randomRoboticInformation = {"Software", "blah blah blah"}; + + for (int i = 0; i < randomRoboticInformation.length; i++) { + System.out.println(randomRoboticInformation[i]); + } + } +} + diff --git a/Week3/Arrays/README.txt b/Week3/Arrays/README.txt new file mode 100644 index 0000000..a732edc --- /dev/null +++ b/Week3/Arrays/README.txt @@ -0,0 +1,16 @@ +# Arrays 🍵 + +### Task 🐧 +Create code that consists of an array with the following elements: + - An element containing the name of your department + - An element contianing the name of the oldest person in the world + - An element contianing the name of the Rapid React world champion alliance captains + - An element containing the name of the 2019 FRC game + + - Then print each element, from last to first. + +### Requirements 🏫 +``` +1. A .java file +2. Creates an array and prints the elements as specified +``` diff --git a/Week3/Classes&Objects/Me.java b/Week3/Classes&Objects/Me.java new file mode 100644 index 0000000..1849d80 --- /dev/null +++ b/Week3/Classes&Objects/Me.java @@ -0,0 +1,21 @@ +public class Me { + String name; + int age; + + Me(String name, int age) { + this.name = name; + this.age = age; + } + void name() { + System.out.println(this.name); + } + void age() { + System.out.println(this.age); + } + + public static void main(String[] args) { + Me justin = new Me("Justin", 17); + justin.name(); + justin.age(); + } +} diff --git a/Week3/Classes&Objects/Pokemon.java b/Week3/Classes&Objects/Pokemon.java new file mode 100644 index 0000000..ee9ab4e --- /dev/null +++ b/Week3/Classes&Objects/Pokemon.java @@ -0,0 +1,30 @@ +public class Pokemon { + String name; + int level; + + + Pokemon(){ + level = 1; + } + + Pokemon(String pName, int pLevel){ + // Use variables defined in class (at top of page) rather than local variables (this statements) + this.name = pName; + this.level = pLevel; + } + + void attack(){ + System.out.println(name + "attack!"); + } + + public static void main(String[] args) { + + Pokemon p1 = new Pokemon("Pikachu", 15); + + System.out.print(p1.name + "\nLevel:" + p1.level); + + Pokemon p2 = new Pokemon("Vaporeon", 69); + + p2.attack(); + } +} diff --git a/Week3/Classes&Objects/README.md b/Week3/Classes&Objects/README.md new file mode 100644 index 0000000..7b8465a --- /dev/null +++ b/Week3/Classes&Objects/README.md @@ -0,0 +1,13 @@ +# Classes and Objects 🍵 + +### Task 🐧 + - Create a class called "Me" & intialize variables name and age + - Create parameters for these variables and set them to the variables in the constructor + - Create 2 method that returns your name and age + - Create an object for this class in the main method, & use the method to print! + +### Requirements 🏫 +``` +1. A .java file +2. Prints your name and age from the Me object +``` \ No newline at end of file diff --git a/Week3/Encapsulation/Information.java b/Week3/Encapsulation/Information.java new file mode 100644 index 0000000..98ad6e7 --- /dev/null +++ b/Week3/Encapsulation/Information.java @@ -0,0 +1,37 @@ +public class Information { + // Private means variable cannot be accessed outside of class + private String name; + private int age; + private String gender; + + Information() { + this.name = "Jimmeh"; + this.age = 21; + this.gender = "Male"; + } + Information(String name, int age, String gender) { + this.name = name; + this.age = age; + this.gender = gender; + } + // Public means that there is permission for other classes to use this + public String getName() {return name;} + public void setName(String newName) {this.name = newName;} + // Get Age + public int getAge() {return age;} + public void setAge(int newAge) {this.age = newAge;} + // Get gender + public String getGender() {return gender;} + public void setGender(String newGender) {this.gender = newGender;} + // Check older than 21 + public boolean getOlderThan21() { + if (this.age > 21) {return true;} else {return false;} + } + public static void main(String[] args) { + Information justin = new Information("Justin", 17, "Male"); + Information weston = new Information("Weston", 22, "Mouse"); + + System.out.println(justin.getOlderThan21()); + System.out.println(weston.getOlderThan21()); + } +} \ No newline at end of file diff --git a/Week3/Encapsulation/README.md b/Week3/Encapsulation/README.md new file mode 100644 index 0000000..a66d63a --- /dev/null +++ b/Week3/Encapsulation/README.md @@ -0,0 +1,14 @@ +# Encapsulation 🍵 + +### Task 🐧 + - Create a class callsed Information: + - Make private variables for name, age, and gender + - Create getters and setters for each of the private variables + - Create a boolean getter for if they are above the age of 21 + - Create two instances of Information with different names & genders & ages either above or below 21 + +### Requirements 🏫 +``` +1. A .java file +2. Print out if people are above the age of 21 with their name and gender +``` diff --git a/Week3/Inheritance/Child.java b/Week3/Inheritance/Child.java new file mode 100644 index 0000000..b4599bd --- /dev/null +++ b/Week3/Inheritance/Child.java @@ -0,0 +1,16 @@ +public class Child extends Parent { + String first_name; + + Child(String first_name, String lastName, String eyeColor) { + super(lastName, eyeColor); + this.first_name = first_name; + } + + void getFullName() { + System.out.println("This is" + first_name + " his last name is " + last_name + "."); + } + + void parentEyeColor() { + System.out.println("The parent has " + super.eye_color + " eyes."); + } +} diff --git a/Week3/Inheritance/Main.java b/Week3/Inheritance/Main.java new file mode 100644 index 0000000..dc22c54 --- /dev/null +++ b/Week3/Inheritance/Main.java @@ -0,0 +1,38 @@ +public class Main { + + public static void main(String[] args) { + Child Jim = new Child("Jim", "Smithson", "blue"); + Jim.getLastName(); + Jim.parentEyeColor(); + } +} + +// class Parent { +// String last_name; +// String eye_color; + +// Parent(String last_name, String eyeColor) { +// this.last_name = last_name; +// this.eye_color = eyeColor; +// } +// void getLastName() { +// System.out.println("This person's last name is " + this.last_name); +// } +// } + +class Child extends Parent { + String first_name; + + Child(String first_name, String lastName, String eyeColor) { + super(lastName, eyeColor); + this.first_name = first_name; + } + + void getFullName() { + System.out.println("This is" + first_name + " his last name is " + last_name + "."); + } + + void parentEyeColor() { + System.out.println("The parent has " + super.eye_color + " eyes."); + } +} \ No newline at end of file diff --git a/Week3/Inheritance/Parent.java b/Week3/Inheritance/Parent.java new file mode 100644 index 0000000..d668216 --- /dev/null +++ b/Week3/Inheritance/Parent.java @@ -0,0 +1,12 @@ +public class Parent { + String last_name; + String eye_color; + + Parent(String last_name, String eyeColor) { + this.last_name = last_name; + this.eye_color = eyeColor; + } + void getLastName() { + System.out.println("This person's last name is " + this.last_name); + } +} diff --git a/Week3/Inheritance/README.md b/Week3/Inheritance/README.md new file mode 100644 index 0000000..72f1e3d --- /dev/null +++ b/Week3/Inheritance/README.md @@ -0,0 +1,21 @@ +# Inheritance 🍵 + +### Task 🐧 + - Create a class called Parent: + - Make an instance variable called last_name + - Make an instace variable called eye color + - Create a constructor and set all the instace varaiables to the parameters passed in + - Make a public method called `getLastName()` that returns the last name + + - Create a class called 'Child' that inherits (extends) Parent: + - Make an instance variable called first_name + - Make a constructor that first, sets first_name, and second, calls the constructor of Parent (Make sure to pass in the last name and eye color) + - Create a method that returns the first and last name in a String + - Create a method that returns the eye color color of the parent + +### Requirements 🏫 +``` +1. A .java file +2. Prints the first and last name of the child +3. Prints the eye color of the parent +``` \ No newline at end of file diff --git a/Week3/Inheritance/Vehicle.java b/Week3/Inheritance/Vehicle.java new file mode 100644 index 0000000..770d1fc --- /dev/null +++ b/Week3/Inheritance/Vehicle.java @@ -0,0 +1,23 @@ +public class Vehicle { + double speed; + + void go() { + System.out.println("This vehicle is moving!"); + } + + void stop() { + System.out.println("This vehicle is stopped!"); + } + public static void main(String[] args) { + Car ferrari = new Car(); + ferrari.go(); + } +} + +class Car extends Vehicle { + +} + +class Bicycle extends Vehicle { + +} \ No newline at end of file diff --git a/Week3/Polymorphism/Calculator.java b/Week3/Polymorphism/Calculator.java new file mode 100644 index 0000000..615f68e --- /dev/null +++ b/Week3/Polymorphism/Calculator.java @@ -0,0 +1,20 @@ +public class Calculator { + public Integer division(int num1, int num2) { + return num1/num2; + } + + public double division(double num1, double num2) { + return num1/num2; + } + + public Integer multiply(int num1, int num2) { + return num1*num2; + } + + public static void main(String[] args) { + Calculator jim = new SlowCalculator(); + jim.multiply(10, 4); + jim.division(4.5, 1.5); + jim.division(4, 2); + } +} \ No newline at end of file diff --git a/Week3/Polymorphism/README.md b/Week3/Polymorphism/README.md new file mode 100644 index 0000000..9fb38a6 --- /dev/null +++ b/Week3/Polymorphism/README.md @@ -0,0 +1,17 @@ +# Polymorphism 🍵 + +### Task 🐧 + - Create a class called 'Calculator' + - Make the a method division that divides two ints & overload it to divide two doubles + - Make the a method multiplication that multiplies two ints + + - Create a class called 'SlowCalculator' that extends Calculator: + - Override the multiply method and use a loop to calculate the product of the two ints + +### Requirements 🏫 +``` +1. A .java file +2. Prints the exact qutient of 39 and 49 +3. Prints the rounded quotient of 37 and 49 +4. Prints the product of 37 and 49 +``` \ No newline at end of file diff --git a/Week3/Polymorphism/Sample.java b/Week3/Polymorphism/Sample.java new file mode 100644 index 0000000..d277a94 --- /dev/null +++ b/Week3/Polymorphism/Sample.java @@ -0,0 +1,28 @@ +public class Sample { + String ice; + Sample(String ice) { + this.ice = ice; + } + + void saySomething() { + System.out.println("HEY!"); + } + + public static void main(String[] args) { + Sample[] bob = {new Polymorphism("ice"), new Sample("ice"), new Polymorphism("ice"), new Sample("Bob")}; + for (int i = 0; i < bob.length; i++) { + bob[i].saySomething(); + } + } +} + +class Polymorphism extends Sample { + Polymorphism(String ice) { + super(ice); + } + + @Override + void saySomething() { + System.out.println("Hey?"); + } +} diff --git a/Week3/Polymorphism/SlowCalculator.java b/Week3/Polymorphism/SlowCalculator.java new file mode 100644 index 0000000..3eb4748 --- /dev/null +++ b/Week3/Polymorphism/SlowCalculator.java @@ -0,0 +1,18 @@ +// import java.lang.Math; + +class SlowCalculator extends Calculator { + int num3; + + public Integer multiply(int num1, int num2) { + this.num3 = 0; + for (int i = 0; i < num2; i++) { + System.out.println(num3); + num3 = num1 + num3; + } + return this.num3; + } + + public void exponent(int x, int y) { + System.out.println("I don't work"); + } +} \ No newline at end of file diff --git a/pokemon/Jigglypuff.java b/pokemon/Jigglypuff.java new file mode 100644 index 0000000..2057c7e --- /dev/null +++ b/pokemon/Jigglypuff.java @@ -0,0 +1,16 @@ +package pokemon; + +public class Jigglypuff extends Pokemon { + public Jigglypuff(int health, String name, int attack) { + super(health, name, attack); + } + + private Jigglypuff() { + super(50, "Jigglypuff", 8); + } + + @Override + public void attack(Pokemon target) { + target.damageTaken(this.attack); + } +} diff --git a/pokemon/Main.java b/pokemon/Main.java new file mode 100644 index 0000000..8679311 --- /dev/null +++ b/pokemon/Main.java @@ -0,0 +1,18 @@ +package pokemon; + +public class Main { + public static void main(String[] args) { + Pokemon pikachu = new Pikachu(100, "Ash's Pikachu", 20); + Pokemon jigglypuff = new Jigglypuff(121, "Demon Slapping Singer", 6); + + while (pikachu.getIsFainted() || jigglypuff.getIsFainted()) { + pikachu.attack(jigglypuff); + jigglypuff.attack(pikachu); + } + if (pikachu.getIsFainted()) { + System.out.println(jigglypuff.getName() + " wins"); + } else { + System.out.println(pikachu.getName() + " Wins"); + } + } +} diff --git a/pokemon/Pikachu.java b/pokemon/Pikachu.java new file mode 100644 index 0000000..97b5cda --- /dev/null +++ b/pokemon/Pikachu.java @@ -0,0 +1,16 @@ +package pokemon; + +public class Pikachu extends Pokemon { + public Pikachu(int health, String name, int attack) { + super(health, name, attack); + }; + + private Pikachu() { + super(50, "Pikachu", 12); + }; + + @Override + public void attack(Pokemon target) { + target.damageTaken(this.attack); + }; +} diff --git a/pokemon/Pokemon.java b/pokemon/Pokemon.java new file mode 100644 index 0000000..3901190 --- /dev/null +++ b/pokemon/Pokemon.java @@ -0,0 +1,28 @@ +package pokemon; + +abstract public class Pokemon { + private int health; + private String name; + public int attack; + // Sets a default value for all Pokemon of 50 + public Pokemon(int health, String name, int attack) { + this.health = health; + this.name = name; + this.attack = attack; + } + // Abstract method to reprogram for attack + abstract void attack(Pokemon target); + // Takes a number it deals that much damage to the Pokemon + public void damageTaken(int dmg) { + this.health = this.health - dmg; + } + // Checks the status of isFainted to see if Pokemon is fainted + public boolean getIsFainted() { + boolean isFainted = health <= 0; + return isFainted; + } + // Returns the value of Pokemon's name + public String getName() { + return this.name; + } +} \ No newline at end of file diff --git a/pokemon/README b/pokemon/README new file mode 100644 index 0000000..1886181 --- /dev/null +++ b/pokemon/README @@ -0,0 +1,19 @@ +a. Explain why Pokemon should be an abstract class + Since it doesn't make sense for there to be a Pokemon class since it is a concept + +b. Explain why inheriting from Pokemon is useful + Inheriting from Pokemon means you require much less work as it all already exists in Pokemon + +c. Explain an example of method polymorphism in your code + There is an example of method Polymorphism in my code when I override the attack function + +d. Explain an example of subtype polymorphism in your code + When I create the default constructor, due to it having different parameters it does not throw an error + +e. Explain why you made one of your methods public + I made the attack method public so that the main file could reference it + +f. Explain why you made one of your instance variables private + I made health and name private because health and name do not need to be changed or referenced by other classes just set in the super. + +