Skip to content
Snippets Groups Projects
Commit c26c9b13 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
class Animal:
"""
This is base class should not be directly instantiated. All subclasses should have a move() method.
"""
def __init__(self, name):
self.name = name
def make_sound(self):
print(f'{self.name} is staying quiet.')
class Fish(Animal):
def move(self):
print(f'{self.name} is swimming.')
class Footed(Animal):
def move(self):
print(f'{self.name} is walking.')
def make_sound(self):
print('Moo. Bark bark. Meow.')
class Fowl(Animal):
def walk(self):
print(f'\t{self.name} is walking.')
def fly(self):
print(f'\t{self.name} is flying.')
def move(self):
choice = input(f'Should {self.name} move with feet or wings? ')
if choice.lower()[0] == 'f':
self.walk()
elif choice.lower()[0] == 'w':
self.fly()
else:
print(f'\t{self.name} doesn\'t know how to move using {choice}.')
def make_sound(self):
print('Bok bok.')
import java.util.*;
public class Main {
public static void showAnimals(Collection<Animal> animals) {
for(Animal animal: animals) {
animal.move();
animal.makeSound();
}
}
public static void main(String[] args) {
List<Animal> animals = new ArrayList<>();
animals.add(new Fish("Mr. Trout"));
animals.add(new Footed("Dr. Spitty"));
animals.add(new Fowl("Ms. Maran"));
showAnimals(animals);
List<Animal> otherAnimals = new ArrayList<>();
otherAnimals.add(new Fish("Limpet"));
otherAnimals.add(new Animal("Pet Rock")); // Won't compile
}
public abstract static class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void makeSound() {
System.out.println(name + " is staying quiet.");
}
abstract public void move();
}
public static class Fish extends Animal {
public Fish(String name) {
super(name);
}
@Override
public void move() {
System.out.println(name + " is swimming.");
}
}
public static class Footed extends Animal {
public Footed(String name) {
super(name);
}
@Override
public void makeSound() {
System.out.println("Moo. Bark bark. Meow.");
}
@Override
public void move() {
System.out.println(name + " is walking.");
}
}
public static class Fowl extends Animal {
public Fowl(String name) {
super(name);
}
@Override
public void makeSound() {
System.out.println("Bok bok.");
}
@Override
public void move() {
System.out.print("Should " + name + " move with feet or wings? ");
Scanner scanner = new Scanner(System.in);
String choice = scanner.nextLine().toLowerCase();
switch(choice) {
case "feet":
walk();
break;
case "wings":
fly();
break;
default:
System.out.println("\t" + name + " doesn't know how to move using " + choice + ".");
}
}
private void fly() {
System.out.println("\t" + name + " is flying.");
}
private void walk() {
System.out.println("\t" + name + " is walking.");
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment