Commit 6e75be9d authored by cosmina.dunca's avatar cosmina.dunca

Commit

parent ceb5fc22
File added
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
File added
File added
File added
File added
public abstract class Animal {
// attributes
String name;
String sound;
Habitat habitat;
Omnivore foodtype; // omnivore means both carnivore and herbivore (these are subclasses)
// descriptive attributes
double speed;
int lifespan;
String colour;
//constructor
public Animal(String name, String sound, double speed, int lifespan, String colour) {
this.name = name;
this.sound = sound;
this.speed = speed;
this.lifespan = lifespan;
this.colour = colour;
}
// returns the sound made by this animal
public void makeSound() {
System.out.println(sound + " is the sound made by this animal.");
System.out.println(sound + "!" + sound + "!" + sound + "!" + "...");
}
public double increaseSpeedWhenAttacked() {
// return the speed of the animal when it's attacked (by 20%)
return speed + (speed * 0.2);
}
public abstract void Sleep();
}
File added
import java.util.Scanner;
import java.sql.ShardingKey;
import java.util.ArrayList;
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
public class App {
public static void displayDetails(Animal animal) {
System.out.println(animal.name);
animal.makeSound();
System.out.println("Speed: " + animal.speed + " km/h, Lifespan: " + animal.lifespan + " years, Colour: " + animal.colour);
System.out.println();
}
public static void main(String[] args) throws Exception {
// an ArrayList of animals
ArrayList<Animal> animals = new ArrayList<Animal>();
// Adding all the animals to the Zoo - this way or using a CSV file
// Mammals
Animal giraffe = new Mammals("Giraffe", "Hum", 60, 26, "Yellow");
giraffe.foodtype = new Herbivore("Leaves", 50, 75, "Acacia Trees");
giraffe.habitat = new Habitat("Desert", 36);
animals.add(giraffe);
Animal lion = new Mammals("Lion", "Roar", 80, 13, "Brown");
lion.foodtype = new Carnivore("Meat", 5, 16, "Pounce");
lion.habitat = new Habitat("Grassland", 25);
animals.add(lion);
Animal elephant = new Mammals("Elephant", "Pawoo", 40, 48, "Grey");
elephant.foodtype = new Herbivore("Leaves and grass", 4, 272, "Flowers and wild fruits");
elephant.habitat = new Habitat("Desert", 35);
animals.add(elephant);
// Birds
Animal owl = new Birds("Owl", "Hoot", 65, 12, "White");
owl.foodtype = new Carnivore("Invertebrates", 3, 4, "Perch and Pounce");
owl.habitat = new Habitat("Forest", 10);
animals.add(owl);
Animal beeEater = new Birds("BeeEater", "Canto", 35, 6, "Green with red");
beeEater.foodtype = new Carnivore("Bees", 2, 2, "Swipes the insect against the tree");
beeEater.habitat = new Habitat("Grassland", 20);
animals.add(beeEater);
// Fish
Animal shark = new Fish("Shark", "Arr", 50, 30, "Grey");
shark.foodtype = new Carnivore("Other fish", 4, 6, "Stingray");
shark.habitat = new Habitat("Aquatic", 22);
animals.add(shark);
// Reptiles
Animal tortoise = new Reptiles("Tortoise", "Aaa", 1, 80, "Yellow");
tortoise.foodtype = new Omnivore("Fruit", 1, 3);
tortoise.habitat = new Habitat("Grassland", 24);
animals.add(tortoise);
Animal crocodile = new Reptiles("Crocodile", "Bellows", 32, 70, "Green");
crocodile.foodtype = new Carnivore("Frogs", 4, 5, "Drawning their prey");
crocodile.habitat = new Habitat("Aquatic", 25);
animals.add(crocodile);
// There might be an easier way to store the animals (CSV file). I chose the procedural code here
//Application
Scanner reader = new Scanner(System.in);
System.out.println("Hello! Welcome to Cosmina's Zoo. Please choose a number from 1 to 7: ");
int num = reader.nextInt();
if (num == 1) {
System.out.println("This will display all the animals in the zoo and their details.");
System.out.println();
for(Animal animal : animals)
{
displayDetails(animal);
}
} else if (num == 2) {
System.out.print("This will display the details of a specific animal. Please select an animal that exists in the zoo: ");
for(Animal animal : animals)
{
System.out.print(animal.name + " ");
System.out.println();
}
String a = reader.next();
for(Animal animal: animals){
if(animal.name.equals(a)){displayDetails(animal);}
}
} else if (num == 3){
System.out.println("Choose a category to be displayed: Mammals, Birds, Fish or Reptiles");
String c = reader.next();
if(c.equals("Mammals")){
for(Animal animal : animals){
if(animal instanceof Mammals) {
System.out.print(animal.name + " ");
}
}
} else if (c.equals("Birds")) {
for(Animal animal : animals){
if(animal instanceof Birds) {
System.out.print(animal.name + " ");
}
}
} else if (c.equals("Fish")) {
for(Animal animal : animals){
if(animal instanceof Fish) {
System.out.print(animal.name + " ");
}
}
} else if (c.equals("Reptiles")) {
for(Animal animal : animals){
if(animal instanceof Birds) {
System.out.print(animal.name + " ");
}
}
} else {
System.out.println("Double check your spelling.");
}
} else if (num == 4) {
System.out.println("You chose to see feeding info.");
for(Animal animal : animals){
System.out.println(animal.name);
animal.foodtype.provideFeedingInfo();
}
} else if (num == 5) {
System.out.println("You chose to see all animals from a specifict environnment. Choose an environment: Desert, Aquatic, Forest, Grassland");
Boolean rightAns = false;
String e = reader.next();
for(Animal animal : animals) {
if(animal.habitat.type.equals(e)){System.out.print(animal.name + " "); rightAns = true;}
}
if(rightAns != true) {System.out.println("Double check your spelling.");}
} else if (num == 6) {
System.out.println("This will dispaly each animal's favourite plant if they are herbivorous.");
for(Animal animal : animals) {
if(animal.foodtype instanceof Herbivore) {
Herbivore herbi;
herbi = (Herbivore) animal.foodtype;
System.out.println(animal.name + " loves " + herbi.favouritePlant + ".");
herbi.plantFavouritePlant();
}
}
System.out.println("It also dispalys each animal's hunt method if they are carnivorous.");
for(Animal animal : animals) {
if(animal.foodtype instanceof Carnivore) {
Carnivore carni;
carni = (Carnivore) animal.foodtype;
System.out.println(animal.name + ": " + carni.getHuntMethod());
}
}
} else if(num == 7) {
System.out.println("You chose to see their speed increase when attacked.");
for(Animal animal : animals) {
System.out.print("The average speed of " + animal.name + " is: " + animal.speed + " km/h. When attcked the speed is: " + animal.increaseSpeedWhenAttacked() + " km/h");
System.out.println();
}
} else {
System.out.println("You didn't choose a number from 1 to 7.");
}
reader.close();
}
}
File added
public class Birds extends Animal {
//constructor
public Birds(String name, String sound, double speed, int lifespan, String colour) {
super(name, sound, speed, lifespan, colour);
}
@Override
public void Sleep() {
System.out.println("Birds also sleep unihemispherically, with one side of the brain asleep while the other stays awake. As they sleep, only the eye associated with the sleeping hemisphere of their brain is closed. Unihemispheric sleep allows birds to protect themselves from predators. For example, mallard ducks can sleep in a row.");
}
public void fly() {
System.out.println("I love flying!");
}
}
public class Carnivore extends Omnivore {
private String huntMethod;
public Carnivore(String mainSource, int times, int quantity, String huntMethod) {
super(mainSource, times, quantity);
this.huntMethod = huntMethod;
}
public String getHuntMethod() {return huntMethod;}
public void setNewHuntMethod(String newHuntM) {this.huntMethod = newHuntM;}
}
File added
public class Fish extends Animal {
//constructor
public Fish(String name, String sound, double speed, int lifespan, String colour) {
super(name, sound, speed, lifespan, colour);
}
@Override
public void Sleep() {
System.out.println("Do fish sleep? Sort of, but it’s probably more appropriate to call what fish do “rest.” When fish are resting, they slow down their activity level and metabolism29 while remaining alert enough to protect themselves from danger. They float in place, like zebrafish do30, or find themselves a safe spot in the mud, sand, or coral to rest.");
}
public void swim() {
System.out.println("I love swimming");
}
}
public class Habitat {
String type;
int temperature;
//constructor
public Habitat(String type, int temperature){
this.type = type;
this.temperature = temperature;
}
public void increaseTemp(int x) {
temperature += x;
System.out.println("The temperature has been increased by " + x);
}
public void decreaseTemp(int x) {
temperature -= x;
System.out.println("The temperature has been decreased by " + x);
}
}
public class Herbivore extends Omnivore {
String favouritePlant;
public Herbivore(String mainSource, int times, int quantity, String favouritePlant) {
super(mainSource, times, quantity);
this.favouritePlant = favouritePlant;
}
public void plantFavouritePlant() {
System.out.println("Let's try to cultivate as many " + favouritePlant + " as we can!");
}
}
public class Mammals extends Animal {
//constructor
public Mammals(String name, String sound, double speed, int lifespan, String colour) {
super(name, sound, speed, lifespan, colour);
}
@Override
public void Sleep() {
System.out.println("Mammals sleep to save their energy and restore mental and physical energy. The amount of sleep a mammal needs depends on several factors, including age, body size, environment, diet, and the safety of its sleep site. Different mammals spend different amounts of time in non-REM sleep and REM sleep. However, all mammals studied thus far do exhibit signs of REM sleep, suggesting that mammals dream, just like humans do.");
}
public void beHappy() {
System.out.println("Main goal: be happy!");
}
}
public class Omnivore {
private String mainSource;
private int times;
private int quantity;
public Omnivore(String mainSource, int times, int quantity) {
this.mainSource = mainSource;
this.times = times;
this.quantity = quantity;
}
public String getMainSource() {return mainSource;}
public int getTimes() {return times;}
public int getQuanity() {return quantity;}
public void provideFeedingInfo(){
System.out.println("Its main source of food is: " + mainSource + ". It eats " + times + " times a day, " + quantity + " pounds.");
if(quantity > 10 && times >= 5) {
System.out.println("It's a huge animal. It's always hungry! :)");
}
else if(quantity > 10 && times < 5) {
System.out.println("It's a big animal. Food is important to it.");
} else if(quantity > 5 && times > 3) {
System.out.println("It's a medium animal. It eats enough to survive.");
} else {
System.out.println("It's a small one. It doesn't eat too much.");
}
}
}
public class Reptiles extends Animal {
//constructor
public Reptiles(String name, String sound, double speed, int lifespan, String colour) {
super(name, sound, speed, lifespan, colour);
}
@Override
public void Sleep() {
System.out.println("Reptiles are some of the least studied animals when it comes to sleep. Historically, REM and slow-wave sleep were thought to exclusively be sleep patterns of mammals and birds. However, emerging research indicates that reptiles such as lizards may also experience these stages of sleep, even in sleep cycles as short as 80 seconds.");
}
public void camouflage() {
System.out.println("I need to camouflage");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment