Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions BarkBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

public class BarkBehavior implements TalkBehavior{
public String talk()
{
return "Woof Woof Woof";
}

}
8 changes: 8 additions & 0 deletions CrawlBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

public class CrawlBehavior implements MoveBehavior{

public String move()
{
return "Crawling playfully";
}
}
29 changes: 29 additions & 0 deletions DigitalWorldDriver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

public class DigitalWorldDriver {
public void runWorld() {
Robot[] robots = new Robot[4];
robots[0] = new DogRobot("Tipsy");
robots[1] = new HumanRobot("Sam");
robots[2] = new iRobot("Will");
robots[3] = new WallERobot("Wall-E");

for(Robot robot : robots) {
displayRobot(robot);
}

}

private void displayRobot(Robot robot) {
System.out.println("\n----------------");
System.out.println(robot);
System.out.println(robot.move());
System.out.println(robot.talk());
System.out.println("----------------");
}


public static void main(String[] args) {
DigitalWorldDriver world = new DigitalWorldDriver();
world.runWorld();
}
}
13 changes: 13 additions & 0 deletions DogRobot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

public class DogRobot extends Robot{
public DogRobot(String name)
{
super(name);
moveBehavior = new CrawlBehavior();
talkBehavior = new BarkBehavior();
}
public String toString()
{
return name + " is a happy Dog Robot";
}
}
13 changes: 13 additions & 0 deletions HumanRobot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

public class HumanRobot extends Robot{
public HumanRobot(String name)
{
super(name);
moveBehavior= new WalkBehavior();
talkBehavior= new SpeakBehavior();
}
public String toString() {
return name + " is a typical human robot";
}

}
4 changes: 4 additions & 0 deletions MoveBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public interface MoveBehavior {
public String move();
}
31 changes: 31 additions & 0 deletions Robot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

public abstract class Robot {
protected String name;
MoveBehavior moveBehavior;
TalkBehavior talkBehavior;

public Robot(String name)
{
this.name = name;
}

public abstract String toString();

public String move()
{
return moveBehavior.move();
}
public String talk()
{
return talkBehavior.talk();
}
public void setMoveBehavior(MoveBehavior mb)
{
moveBehavior= mb;
}
public void setTalkBehavior(TalkBehavior tb)
{
talkBehavior = tb;
}

}
7 changes: 7 additions & 0 deletions RunBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

public class RunBehavior implements MoveBehavior{
public String move()
{
return "Running super agressivelly!!! AAAAHHHHH";
}
}
12 changes: 12 additions & 0 deletions SimpleSpeakBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.Random;

public class SimpleSpeakBehavior implements TalkBehavior{
String[] randSSB = {"Wall-E","EVA!","Pop","Eva?","Eee-va?"};

public String talk()
{
Random r = new Random();
return randSSB[r.nextInt(randSSB.length)];
}

}
12 changes: 12 additions & 0 deletions SpeakBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.Random;

public class SpeakBehavior implements TalkBehavior{

String[] randSB = {"Hello","How are you","Good Day"};

public String talk()
{
Random r = new Random();
return randSB[r.nextInt(randSB.length)];
}
}
4 changes: 4 additions & 0 deletions TalkBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public interface TalkBehavior {
public String talk();
}
8 changes: 8 additions & 0 deletions WalkBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

public class WalkBehavior implements MoveBehavior{
public String move()
{
return "Walking Gleefully";
}

}
13 changes: 13 additions & 0 deletions WallERobot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

public class WallERobot extends Robot{
public WallERobot(String name)
{
super(name);
moveBehavior = new WalkBehavior();
talkBehavior = new SimpleSpeakBehavior();
}
public String toString()
{
return name + " is an environmentally friendly Wall-E Robot";
}
}
14 changes: 14 additions & 0 deletions iRobot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

public class iRobot extends Robot{
public iRobot(String name)
{
super(name);
moveBehavior = new RunBehavior();
talkBehavior= new SpeakBehavior();
}
public String toString()
{
return name + " is a scary iRobot!!!";
}

}