Hacks Unit 5,9,10 - Rohit, Nathan, Jun
// UNIT 5
import java.util.*;
public class Car {
private String carBrand;
private String modelName;
private String modelYear;
private int carMileage = 0;
private int carId = 0;
private static int carsForSale = 0;
private static ArrayList<Car> carsInMarket = new ArrayList<Car>();
/**
* Constructor with 3 arguments
* @param carBrand
* @param modelName
* @param modelYear
*
* everytime this method is called, car id is added and a new car is added to the array
*/
public Car(String carBrand, String modelName, String modelYear, int carMileage){
this.carBrand = carBrand;
this.modelName = modelName;
this.modelYear = modelYear;
this.carMileage = carMileage;
carId = uniqueID();
Car.carsInMarket.add(this);
}
/**
*
* @return number of cars
*/
public static int getCarsCount(){
return Car.carsInMarket.size();
}
/**
*
* @return unique id for each car
*/
private int uniqueID(){
Random randid = new Random();
carId = randid.nextInt(9999);
return carId;
}
/**
*
* @return calling the car array
*/
public static List<Car> getCarsInMarket(){
return Car.carsInMarket;
}
/**
* printing the object
*/
public String toString(){
return "Id: " + carId + " Brand: " + carBrand + " Model: " + modelName + " Year: " + modelYear + " Mileage: " + carMileage;
}
public static void main(String[] args){
System.out.println("Car available: " + Car.getCarsCount());
Car BMWX1 = new Car("BMW","X1","2002",30);
Car HondaCRV = new Car("Honda","CRV","2015",40);
for(int i = 0; i < Car.getCarsCount(); i++){
System.out.println((Car.getCarsInMarket().get(i)));
}
System.out.println("Car available: " + Car.getCarsCount());
}
}
Car.main(null);
// UNIT 9 PART 1
class Vehicle {
public String name;
public Vehicle(String name){
this.name = name;
}
public void honk(){
System.out.println("HONK MEEP BEEEEEEEP!");
}
}
class Car extends Vehicle {
public Car(String name){
super(name);
}
// methods and attributes
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Lexus GS430");
car1.honk();
System.out.println(car1.name);
}
}
Main.main(null);
// UNIT 9 PART 2
// Parent class
public class Animal {
protected String name;
protected int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void speak() {
System.out.println("I AM AN ANIMALIO!!!!!!!!");
}
}
// Subclass 1
public class Turtle extends Animal {
private String breed;
public Turtle(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}
@Override
public void speak() {
System.out.println("Turtle Noises Commence");
}
public void eat() {
System.out.println("I nomp lettuce.");
}
}
// Subclass 2
public class Cat extends Animal {
private boolean isIndoor;
public Cat(String name, int age, boolean isIndoor) {
super(name, age);
this.isIndoor = isIndoor;
}
@Override
public void speak() {
System.out.println("Meow!");
}
public void scratch() {
System.out.println("I am scratching the furniture.");
}
}
// Tester method
public class AnimalTester {
public static void main(String[] args) {
Animal animal1 = new Turtle("Shellby", 3, "Russian Tortoise");
Animal animal2 = new Cat("Whiskers", 2, true);
animal1.speak();
((Turtle) animal1).eat();
System.out.println(animal1.name);
animal2.speak();
((Cat) animal2).scratch();
System.out.println(animal2.age);
}
}
AnimalTester.main(null);
// UNIT 10
public class Fibonacci {
public static long fib(long n) {
if ((n == 0) || (n == 1))
return n;
else
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
System.out.println("The 0th fibonacci number is: " + fib(0));
System.out.println("The 7th fibonacci number is: " + fib(7));
System.out.println("The 12th fibonacci number is: " + fib(12));
}
}
Fibonacci.main(null);