Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
barnyard
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SOFT Core
SOFT 161 and 162
barnyard
Commits
c26c9b13
Commit
c26c9b13
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
barnyard.py
+42
-0
42 additions, 0 deletions
barnyard.py
src/Main.java
+105
-0
105 additions, 0 deletions
src/Main.java
with
147 additions
and
0 deletions
barnyard.py
0 → 100644
+
42
−
0
View file @
c26c9b13
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.
'
)
This diff is collapsed.
Click to expand it.
src/Main.java
0 → 100644
+
105
−
0
View file @
c26c9b13
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment