Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

COMP122 Assessment 3 Worth 25% of the final course mark. Submission Deadline Friday, 27 April 2018, 5:00pm (Friday of Week 10) Learning Outcomes. This assessment addresses the following learning...

1 answer below »
COMP122 Assessment 3
Worth 25% of the final course mark.
Submission Deadline
Friday, 27 April 2018, 5:00pm (Friday of Week 10)
Learning Outcomes. This assessment addresses the following learning outcomes of COMP122:
• Describe object hierarchy structure and how to design such a hierarchy of related classes.
• Describe the concept of object polymorphism in theory and demonstrate this concept in practice.
• Identify and describe the task and issues involved in the process of developing interactive products fo
people, and the techniques used to perform these tasks.
Part I (50% of assessment mark)
Introduction
Note: This problem is inspired by a problem encountered on DataCamp’s “Data Science with Python” caree
course. (http”
www.datacamp.com)
Figure 1: A reference to Franz
Kafka’s The Metamorphosis. Illustra-
tion by Jir̆ı́ho Slı́vy.
Random walks. We have previously encountered the idea of a
andom walk in this module in the course notes and practical ses-
sions. We’re going to explore this concept in this part of the assess-
ment.
Cockroaches at work (or play?). Consider two cockroaches that
are moving up and down the staircase visiting the 102 floors of the
Empire State Building (the iconic building in New York City). In
case you don’t know, (some) cockroaches can fly, so can sometimes
move very quickly. We are going to assume that these cockroaches
move (somewhat) at random, and we want to examine some aspects
of their movement.
In particular, suppose that each cockroach starts on the ground
floor of the Empire State Building, which we will label as “floor 1”.
During a single timestep of movement, each cockroach could move
up or down one or more floors, or could possibly stay stationary. Their movement is governed by a random
process, which can be different for each cockroach. (These details are given below for the two cockroaches
we will consider here.)
The types of questions we are usually interested in when considering random walks include:
1. How long does it take a cockroach to reach the top floor?
2. After walking for a certain number of timesteps, what is the highest floor that has been reached during
that period?
1
http:
www.datacamp.com
3. How often are the two cockroaches on the same floor during their random walks (assuming they both
start in the same place)?
4. How long is it necessary for the cockroach to walk before it is at a “random floor” of the building?
(This depends upon trying to more precisely define what we mean by this statement.)
Since we’re dealing with random processes, we really want to consider averages for these answers, i.e.
if we repeat the process a large number of times (with different random inputs), what is, say, the average
maximum height reached during a fix time period?
Even the use of the word “average” should really be clarified. Does this word refer to the “mean”, the
“mode”, or the “median” (or some other concept)? If you aren’t aware of these different meanings, you
should really learn what they are as sometimes the word “average” is used in (say) news reports without
eally specifying which one is meant, or the word “average” is used in a misleading way. If I were to tell you
that the mean salary of employees in a company is £60,000 and the mode of the salaries is £25,000, what
might this suggest about the salary of the “average employee” of that company?
In any event, the goal of this part of the assignment is to write code to answer these types of questions
posed about the random walks of the cockroaches in question. (Don’t wo
y, I will define what “average”
efers to when we get to the questions we want to answer. . . )
Ideally we can use Java’s object-oriented capabilities (and maybe some polymorphism) to help us write
our code as we are dealing with cockroaches all the time, but ones with some slightly different behavior in
how they move about the Empire State Building.
Figure 2: The Empire State Build-
ing. Photo by Sam Valadi
https:
www.flickr.com/photos/ XXXXXXXXXX@N05/ XXXXXXXXXX,
CC BY 2.0, https:
commons.wikimedia.org/w/index.php?curid= XXXXXXXXXX
Terminology. In what follows, I will use the word
“timestep” or simply “step” to refer to one “unit of
time”, which is the amount of time necessary for a
cockroach to move exactly once. It is possible that
during this single move the cockroach could traverse
several floors, or possibly remain stationary, but at
the beginning of a time step the cockroach is on some
floor, and at the end of the timestep it has moved
once (or possibly remained stationary) and is again
on some floor of the building (i.e. not “in between”
floors).
The floors of the Empire State Building building
are numbered sequentially starting from the “ground
floor” (with value 1) to the “top floor” (numbered
102). (Note that while many tall American buildings
don’t have a floor numbered “13”, the builders of
the Empire State Building weren’t superstitious, so
it does have a “13th floor”. Note also that the cock-
oaches don’t have access to the secret 103rd floor of
the Empire State Building.)
A cockroach can never go below the ground
floor, nor above the top floor, so if it was to try a
move that would take them too low or too high, it
can instead move to the ground floor or top floor,
ut not beyond. (For example, if a cockroach was
on floor 101 and was going to move up 3 floors,
it would instead move to floor 102 and stop. If it
started the timestep on the ground floor and wanted
to move down, it would instead remain stationary fo
that step.)
2
Our subjects. We are going to consider two cockroaches, named Don and Bella. Don likes to show off his
ig wings and, thus, generally likes to move up more than down, but unfortunately Don is also a bit clumsy.
In any single time step, this is how Don moves:
(1) With a 0.1% chance, Don flies into the center of the stairwell and ends up falling all the way back to
the ground floor.
(2) Otherwise, Don rolls his 6-sided die (since we all know cockroaches love to ca
y around and use
dice) and does the following:
(a) On a 1 or a 2, Don moves down one floor.
(b) On a roll of a 3, 4, or 5, Don moves up one floor.
(c) On a roll of a 6, Don re-rolls his six-sided die and moves up the number of floors equal to this
second roll of the die.
Figure 3: An artist’s
endition of Don (o
maybe Bella, I for-
get. . . ).
Our other cockroach, Bella, has wings that are just as big as Don’s and she also
generally likes to move up rather than down, but Bella is lazy and doesn’t usually
move as fast as Don. Fortunately for Bella, she isn’t clumsy like Don, but she does
like to stop at the observation deck on the 86th floor to enjoy the view. Bella moves
as follows:
(1) If Bella is pausing for a view on the 86th floor, she doesn’t move at all fo
one step, and in the next step will roll her die (after all, she is a cockroach
too!) to (possibly) move.
(2) Otherwise, Bella rolls her 6-sided die and does the following:
(a) On a 1, 2, or 3, she moves down one floor.
(b) On a 4, she moves up 2 floors.
(c) On a 5, she moves up 3 floors.
(d) On a roll of a 6, Bella doesn’t move.
(e) However, if Bella moves onto (or tries to move through) the 86th floor,
she will stop on floor 86, and will pause for a view on the next step. (So
if, for example, Bella was on floor 85 and rolled a 4, she would stop on
floor 86 for a view on the next step, instead of progressing to floor 87.)
If Bella rolled a 6, and was already on floor 86, she will still pause for a view on the next step.
(Note that this means Bella will spend two time steps on floor 86, one for the step when she
olled a 6, and another when she pauses for the view, as described in (1) above.)
Remember that neither Don nor Bella can ever go below the ground floor (floor 1) nor above the top
floor (floor 102), so if they try to do that, they will move as far as they are able and then stop.
Requirements
Your goal is to implement Java classes to represent the cockroaches, so that we can gather some emprical
knowledge about the random walks in the Empire State Building. You should note the following:
(1) (Requirement) Develop an abstract class called “Cockroach.java”. This class should have an abstract
method called takeStep() (taking no parameters), which will be implemented by subclasses. This
method defines what a cockroach does in a single timestep of movement. The Cockroach class should
have an attribute for a name of the cockroach (a String), and can have other class attributes, con-
stants, and methods as you deem appropriate. You can, for example have an attribute that keeps track
of the number of steps the cockroach has taken since it started its random walk. You will, of course,
need an attribute that keeps track of its cu
ent location (floor) in the Empire State Building, which the
takeStep() method should update.
3
Be sure to include appropriate “get” or “set” methods as you need them and/or other methods you
want to include in the Cockroach class.
(2) (Requirement) Each individual cockroach should be a subclass of “Cockroach.java”. Only include
additional attributes in the subclasses as you need them (i.e. in good object-oriented programming
style, put as many of the attributes, methods, and constants into the base “Cockroach.java” class as
you can).
Obviously each subclass will need to implement the takeStep() method above which determines
how the cockroach moves in a single timestep of its movement. As mentioned, you can include
additional atributes, constants, or methods beyond those in “Cockroach.java”, but these should be
integeral to a particular cockroach and not ones that could be included in that base class.
(3) (Requirement) You are going to output some summary statistics that you gather after repeating some
experiments.
DO NOT USE an external li
ary to compute these statistics! Write some (short)
methods to compute these numbers as appropriate. Make certain the values that you output are double
values.
Do the following experiments:
(a) For each of our two cockroaches, starting from the ground floor, have them each walk for 100
timesteps. (By “walk” I mean execute their takeStep() method.) Record the maximum floo
that each cockroach reaches during the course of their walk.
Answered Same Day Apr 21, 2020 COMP122

Solution

Snehil answered on Apr 23 2020
141 Votes
Part 1/Bella.class
public synchronized class Bella extends Cockroach {
private boolean pauseOnStep;
private final int PAUSE_FLOOR;
public void Bella(String);
public void takeStep();
private void move(int);
}
Part 1/Bella.java
Part 1/Bella.java
public class Bella extends Cockroach 
{   
    private boolean pauseOnStep;
    private final int PAUSE_FLOOR = 86;
    public Bella(String name) 
    {
        super(name);
    }
    @Ove
ide
    public void takeStep() 
    {
        if(pauseOnStep==false)
        {
            switch(rollDie())
            {
            case 1:
            case 2:
            case 3:
                move(-1);
                
eak;
            case 4:
                move(2);
                
eak;
            case 5:
                move(3);
                
eak;
            case 6:
                
 No movement on roll 6, additionally bella skip next turn by pausing if cu
ently on pause floor 
                if(getCurFloor()==PAUSE_FLOOR)
                {
                    pauseOnStep=true;
                }
            }
        }
        else
        {
            pauseOnStep=false;
        }
        setStepsTaken(getStepsTaken()+1);       
    }
    private void move(int moves)
    {
        int curFloor = getCurFloor();
        
checking two conditions of pause floo
        
if bella is below pause floor and moving takes her equal to or above pause floor   OR
        
if bella is above pause floor and moving takes her equal to or below pause floo
        if((curFloo
PAUSE_FLOOR && curFloor+moves>=PAUSE_FLOOR) ||(curFloo
PAUSE_FLOOR && curFloor+moves<=PAUSE_FLOOR))
        {
            curFloor=PAUSE_FLOOR;
            pauseOnStep=true;
 set boolean to true, takeStep method will check it and make bella skip turn (pause on floor)
        }
        else
        {
            curFloor+= moves;
            if(curFloo
GROUND_FLOOR)
            {
                curFloor = GROUND_FLOOR;
            }
            else if(curFloo
TOP_FLOOR)
            {
                curFloor=TOP_FLOOR;
            }
        }
        setCurFloor(curFloor);
    }
}
Part 1/Cockroach.class
public abstract synchronized class Cockroach {
private String name;
private int curFloor;
private int stepsTaken;
protected java.util.Random rand;
public static final int GROUND_FLOOR = 1;
public static final int TOP_FLOOR = 102;
public void Cockroach(String);
public String getName();
public void setName(String);
public int getCurFloor();
public void setCurFloor(int);
public int getStepsTaken();
public void setStepsTaken(int);
public abstract void takeStep();
protected int rollDie();
}
Part 1/Cockroach.java
Part 1/Cockroach.java
import java.util.Random;
public abstract class Cockroach
{
    private String name;
    private int curFloor;
    private int stepsTaken;
    protected Random rand;  
    public static final int GROUND_FLOOR=1;
    public static final int TOP_FLOOR=102;
    public Cockroach(String name)
    {
        this.name = name;
        this.curFloor = GROUND_FLOOR;
        this.stepsTaken = 0;
        rand = new Random();
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getCurFloor()
    {
        return curFloor;
    }
    public void setCurFloor(int curFloor)
    {
        this.curFloor = curFloor;
    }
    public int getStepsTaken()
    {
        return stepsTaken;
    }
    public void setStepsTaken(int stepsTaken)
    {
        this.stepsTaken = stepsTaken;
    }
    public abstract void takeStep();
    protected int rollDie()
    {
        
 rand.nextInt(6) returns a random numbe
        
 between 0 and 5 (6 is not included). we
        
 add 1 to it to make the range 1 to 6
        return rand.nextInt(6) + 1;
    }
}
Part 1/Don.class
public synchronized class Don extends Cockroach {
public void Don(String);
public void takeStep();
private void move(int);
}
Part 1/Don.java
Part...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here