CanCode Hacks
// Unit 5 Hack
class Rectangle{
private static int area;
private static int perimeter;
private static int sides;
//public
public static boolean quadrilateral;
public Rectangle(int area, int perimeter, int sides){
this.area = area;
this.perimeter = perimeter;
this.sides = sides;
}
public int getArea(){
return this.area;
}
public int getSides(){
if(sides == 4){
isQuadrilateral();
}
return sides;
}
public int getPerimeter(){
return this.perimeter;
}
public static boolean isQuadrilateral(){
quadrilateral = true;
System.out.println("Is it a quadrilateral: " + quadrilateral);
return quadrilateral;
}
public String toString(){
return ("Perimeter: " + this.perimeter + " Area: " + this.area);
}
public static void main(String args[]){
Rectangle rect = new Rectangle(16,16,4);
System.out.println(rect.toString());
System.out.println(rect.getPerimeter());
System.out.println(rect.getSides());
}
}
Rectangle.main(null);
//Unit 9
//Hack 1
class Vehicle {
public String name;
public Vehicle(String name){
this.name = name;
}
public void honk() {
System.out.println("honk");
}
// methods and attributes
}
class Car extends Vehicle {
public Car(String name) {
super(name);
}
public String toString(){
return name;
}
public static void main(String[] args) {
Car car1 = new Car("big car");
car1.honk();
System.out.println(car1.toString());
}
}
Car.main(null);
//Hack 2
public class animal {
public String color;
public animal(String color){
this.color = color;
}
public void talk() {
System.out.println("hi");
}
}
public class rabbit extends animal{
public int height;
public rabbit(String color, int height) {
super(color);
this.height = height;
}
public void talk(){
super.talk();
System.out.println("hello");
}
public String toString() {
return color + " " + height;
}
}
public class pig extends animal {
public int weight;
public pig(String color, int weight) {
super(color);
this.weight = weight;
}
public void talk(){
super.talk();
System.out.println("oink");
}
public String toString(){
return color + " " + weight;
}
}
public class Main {
public static void main(String[] args) {
rabbit rabbit1 = new rabbit("white", 12);
pig pig1 = new pig("pink", 1000);
System.out.println(rabbit1.toString());
System.out.println(pig1.toString());
rabbit1.talk();
pig1.talk();
}
}
Main.main(null);
// Unit 10 Hacks
public class Fibonacci{
public static int fibonacci(int n){
if(n == 0){
return 0;
}
else if(n == 1){
return 1;
}
else{
return fibonacci(n-1) + fibonacci(n-2);
}
}
public static void main(String args[]){
System.out.println("Fibonacci of 10: " + fibonacci(10));
}
}
Fibonacci.main(null);