Showing posts with label lab test. Show all posts
Showing posts with label lab test. Show all posts

Thursday, March 15, 2012

Test 2 and Lab Test 2

Here are the Test 2s and the answers:

  1. (25%) What does the following program print out when run?

    public class Boat extends Ship {
    
     private int seaWorthinessRating;
    
     public Boat(double maxSpeed) {
      super(maxSpeed);
      seaWorthinessRating = 33;
     }
    
     public Boat(double maxSpeed, int rating) {
      super(maxSpeed);
      seaWorthinessRating = rating;
     }
    
     public String toString(){
      return "mp=" + maxSpeed + " seaworth=" + seaWorthinessRating;
     }
    
    }
    
    public class SailBoat extends Boat {
    
      public String toString(){
        return "Sailboat";
      }
    }
    
    public class Ship {
    
     protected double maxSpeed;
    
     public Ship(double maxSpeed){
      this.maxSpeed = maxSpeed;
     }
    
     public String toString(){
      return "maxSpeed="  + maxSpeed;
     }
    
     public static void main(String[] args){
      Ship s1 = new Ship(1);
      System.out.println(s1);
      Ship s2 = new Boat(2,66);
      Ship s22 = new Boat(8);
      System.out.println(s2);
      System.out.println(s22);
      SailBoat s3 = new SailBoat(3,77);
      System.out.println(s3);
      Ship s4 = (Ship)s3;
      System.out.println(s4);
     }
    }

  2. (25%) What is wrong with the program below? Explain.
    public interface Drinkable {
     public void drink();
    
     public boolean isEmpty(){ return false;}
    }
    
    public class RedBull implements Drinkable {
    
     private int size;
    
     public RedBull(int size){
      this.size = size;
     }
    
     public void drink(){
      System.out.println("Yuuuuuckkkkk");
     }
    
     public boolean isEmpty(){
      return true;
     }
    
    }
    Answer: An interface cannot implement a method.
  3. (25%) Implement a static method which takes as an argument an array of integers a and returns a new array, lets call it r, where r[0] has the sum of a[0] + a[1], and r[1] has the sum of a[2] + a[3], and so on. You can assume that the length of a is even.
  4. Answer:
    public static int[] everyOther(int[] a){
      int[] result = new int[a.length/2];
      for (int i = 0; i < a.length; i+=2) {
       result[i/2] = a[i] + a[i+1];
      }
      return result;
     }
            
  5. (25%) Take a look at this code:
    public class Plant {
    
     private String name;
    
     public Plant(String name){
      this.name = name;
     }
    
     public String getName(){
      return name;
     }
    
     public static void main(String[] args){
      Plant[] garden = new Plant[3];
      garden[0] = new Plant("Vine");
      //Create a Rosemary plant with the name Vanity
      // it will have 1024 leaves by default
      garden[1] = new Rosemary("Vanity");
      //This one will have 2048 leaves
      garden[2] = new Rosemary("Pride", 2048);
    
      for (Plant p: garden){
       System.out.println(p.getName()); //print their names
      }
      //getRosemaries() returns the total number of rosemaries
      //  that have been created.
      System.out.println("There are " + Rosemary.getNumRosemaries()
        + " rosemary plants.");
    
      Rosemary r = new Rosemary("Loathing");
      System.out.println(r.getName());
      System.out.println("There are " + Rosemary.getNumRosemaries()
        + " rosemary plants.");
    
     }
    }
            
    Implement the missing Rosemary class so that when we run the code above it will print:
    Vine
    Rosemary:Vanity numLeaves=1024
    Rosemary:Pride numLeaves=2048
    There are 2 rosemary plants.
    Rosemary:Loathing numLeaves=1024
    There are 3 rosemary plants.

    See the comments in the code for hints on what the various
    methods and attributes Rosemary must have.

    Answer:

    public class Rosemary extends Plant {
    
     private int numLeaves;
    
     private static int numCreated = 0;
    
     public Rosemary(String name) {
      super("Rosemary:" + name );
      numLeaves = 1024;
      numCreated++;
     }
    
     public Rosemary(String name, int numLeaves) {
      super("Rosemary:" + name );
      this.numLeaves= numLeaves;
      numCreated++;
     }
    
     public String getName(){
      return super.getName() + " numLeaves=" + numLeaves;
     }
    
     public static int getNumRosemaries(){
      return numCreated;
     }
    
    
    }
and the other one
  1. (25%) What does the following program print out when run?

    public class Boot extends Shoe {
    
     protected int height;
    
     public Boot(int height){
      super(9);
      this.height = height;
     }
    
     public Boot(int height, int size){
      super(size);
      this.height = height;
     }
    }
    
    public class Ugg extends Boot {
    
     public Ugg(int height, int size) {
      super(height,size);
      height--;
     }
    
     public String toString(){
      return "Ugg size=" + super.toString() + " height=" + height;
     }
    }
    public class Shoe {
    
     private int size;
    
     public Shoe(){
      size = 7;
     }
    
     public Shoe(int size) {
      this.size = size;
     }
    
     public String toString() {
      return "Shoe [size=" + size + "]";
     }
    
     public static void main(String[] args) {
      Shoe s1 = new Shoe();
      System.out.println(s1);
      Boot s3 = new Ugg(7,9);
      System.out.println(s3);
      Boot s4 = new Boot(5,15);
      System.out.println(s4);
      Ugg s5 = new Ugg(9,16);
      System.out.println(s5);
      Boot s6 = (Boot)s5;
      System.out.println(s6);
     }
    }

  2. (25%) What is wrong with the program below? Explain.
    public interface Playable{
    
     public void play() {
      super();
     };
    
     public int timeLeft();
    }
    
    public class Media {
    
     protected String title;
    
     public Media(String title){
      this.title = title;
     }
    }
    
    public class Song extends Media implements Playable{
    
     protected int runningTime;
    
     public Song(String name, int runningTime){
      super(name);
      this.runningTime = runningTime;
     }
    
     public void play(){
      System.out.println("Playing....");
     }
    
     public int timeLeft(){
      return runningTime - 10;
     }
    }
            
    Answer: An interface cannot implement a method.

  3. (25%) Implement a static method which takes as an argument an array of integers a and returns a new array, lets call it r, where r[0] contains the sum of a[0] and a[last index in a], r[1] contains the sum of a[1] and a[last index in a minus 1], and so on. You can assume that the length of a is even.

    Answer:

    public static int[] fold(int[] a){
      int[] result = new int[a.length/2];
      for (int i = 0; i < a.length / 2; i++) {
       result[i] = a[i] + a[a.length - 1 - i];
      }
      return result;
     }
                                        
  4. (25%) Take a look at this code:
    public interface LeafEater {
              public void eatLeaf();
    }
    
    public class Mammal {
    
     protected int numToes;
    
     protected static int count = 0;
    
     public Mammal(){
      numToes = 5;
      count++;
     }
    
     public String toString(){
      return "Mammal with " + numToes + " toes.";
     }
    
     public static void main(String[] args) {
      Mammal[] animals = new Mammal[4];
      animals[0] = new Mammal();
    
      //Creates a giraffe with 8 toes and height of 10.0 feet.
      animals[1] = new Giraffe();
    
      //Creates a giraffe with 5 toes and height of 14.6 feet.
      animals[2] = new Giraffe(14.6);
    
      //Creates a giraffe with 8 toes and height of 10.0 feet.
      animals[3] = new Giraffe();
      for (Mammal m : animals){
       System.out.println(m);
      }
    
      //getCount() returns the number of Mammals that have been created.
      System.out.println("There are " + Giraffe.getCount() + " mammals");
     }
    }

    Implement the missing Giraffe class so that when we run the code above it will print:
    Mammal with 5 toes.
    Mammal with 8 toes.-- Giraffe: 10.0 feet.
    Mammal with 5 toes.-- Giraffe: 14.6 feet.
    Mammal with 8 toes.-- Giraffe: 10.0 feet.
    There are 4 mammals
    See the comments in the code for hints on what the various methods and attributes Giraffe must have.

    Answer:

    public class Giraffe extends Mammal implements LeafEater{
    
     private double height;
    
     public Giraffe(){
      super();
      height = 10;
      numToes = 8;
     }
    
     public Giraffe(double height){
      super();
      this.height = height;
     }
    
     public String toString(){
      return super.toString() + "-- Giraffe: " + height + " feet.";
     }
    
     public static int getCount(){
      return count;
     }
    
     public void eatLeaf() {
      System.out.println("Yummm, leaves are good.");
    
     }
    
    }            
And, here is my solution to the Lab Tests.

Wednesday, February 1, 2012

Lab 8: Words and Vowels

In this lab you will write a program that asks the user to type in some sentences and then tells him how many words and how many vowels (a, e, i, o, u) there are in the text he typed in. Here is a sample transcript:
I will count your vowels. Enter your sentences.
Hit enter when done
Sentence (hit ENTER when done):Once upon a midnight dreary
Sentence (hit ENTER when done):while I pondered
Sentence (hit ENTER when done):weak
Sentence (hit ENTER when done):and
Sentence (hit ENTER when done):weary
Sentence (hit ENTER when done):
There are 20 vowels.
There are 11 words.

You can assume that there is always a space between every two words, but remember that there could be a line with just one word, and no spaces, as the above example shows.

Hint: use Scanner.nextLine() to read the whole line.

Hint: use String.charAt

As always, turn it in at the dropbox.cse.sc.edu, Lab 8.

Tuesday, November 23, 2010

Lab test 3 - send your program to these email ids

jmvidal@gmail.com, shamik.usc@gmail.com, humayun.k1@gmail.com

Lab Test 3 - section 6 - To do list

INPUT FILE FORMAT
(1) First Line : 10 -- Number of Items to be added to an ArrayList
(2) 10 Lines follow -- each line corresponds to data of an Item
Item Data Format :
--------------------------
Name;Color;Size;Price
(3) Line 12 : 3 -- Number of Items to search in ArrayList
(4) 3 Items' Name, Color and Size follows
----------------------------------------------------------------
Things To Do:
--------------------
(a) Read the data from the Input File
(b) Store information about each item in an ArrayList
(c) Read the names, colors and sizes from the file which are to be searched
(d) If items are there in the list corresponding to each Name,Color, and Size, then display their average price

Lab Test 3 - Section 6 - Input File

10
Shirt;blue;S;$40
Shirt;yellow;M;$50
Pant;black;M;$100
Shirt;blue;S;$60
Pant;grey;L;$80
Jacket;black;M;$50
Pant;grey;S;$60
Pant;black;M;$110
Shirt;blue;S;$80
T-Shirt;red;S;$60
3
Shirt;blue;S
Pant;black;M
T-Shirt;green;M

LabTest3 -- Section 5


INPUT FILE FORMAT
--------------------------------
1) First Line : 10 -- Number of Employees to be added to an ArrayList
2) 10 Lines follow -- each line corresponds to data on an Employee
    Employee Data Format :
    ----------------------------------- 
    Name;Age;Salary;Office
3) Line 12 : 4 -- Number of names to search in ArrayList
4) 4 Names follow 
5) Line 17 : 3 -- Number of names to remove from the ArrayList
6) 3 Names follow

7) Check the Output File to see what to display.

-------------------------------------------------------------------------------------------------------
Things To Do:
--------------------
a) Read the data from the Input File
b) Store informattion about each employee in an ArrayList
c) Calculate the Average Salary
d) Read the names which are to be searched to see if they are contained in the list
    If contained in the list then print whether salary above or below average salary
e) Read the names which are to be removed from the ArrayList
   if contained in the list then remove them.
f) Finally display the Final List.

** Check the Output File for the Output Format

LabTest3 -- Output


********* READING EMPLOYEE DATA **************

Employee Data Added: Herbert 32 10000 2A41

Employee Data Added: Lander 45 15000 2A47

Employee Data Added: Winthrop 27 0 3A71

Employee Data Added: Marion 23 3000 4B25

Employee Data Added: Adams 31 4765 5A67

Employee Data Added: Richard 38 20000 6A11

Employee Data Added: Jason 34 12456 4C32

Employee Data Added: Karthik 39 14567 3B33

Employee Data Added: Bernard 40 34234 4A22

Employee Data Added: Sofia 26 45676 3C56
********* EMPLOYEE DATA READ **************

********* BEGIN SEARCH **************
Lander is contained in the list
Employee Details :Lander 45 15000 2A47
Below Average Salary

Arnold is not contained in the list

Marion is contained in the list
Employee Details :Marion 23 3000 4B25
Below Average Salary

Shenoy is not contained in the list

********* END SEARCH **************

********* REMOVING SPECIFIC EMPLOYEE DATA **************
Lander is contained in List - hence removing ...
Jason is contained in List - hence removing ...
Sopna not contained in list
********* SPECIFIC EMPLOYEE DATA REMOVED **************

********** THE FINAL LIST IS: **************
Herbert 32 10000 2A41 
Winthrop 27 0 3A71 
Marion 23 3000 4B25 
Adams 31 4765 5A67 
Richard 38 20000 6A11 
Karthik 39 14567 3B33 
Bernard 40 34234 4A22 
Sofia 26 45676 3C56 

LabTest3 -- Input File


10
Herbert;32;$10000;2A41
Lander;45;$15000;2A47
Winthrop;27;7000;3A71
Marion;23;$3000;4B25
Adams;31;$4765;5A67
Richard;38;$20000;6A11
Jason;34;$12456;4C32
Karthik;39;$14567;3B33
Bernard;40;$34234;4A22
Sofia;26;$45676;3C56
4
Lander
Arnold
Marion
Shenoy
3
Lander
Jason
Sopna

Wednesday, October 27, 2010

Revised Solutions for LabTest#2 - Section 5/6 Posted

I have posted the revised solutions of LabTest#2 - Section 5/6 having replaced the ('instanceof' keyword && SuperClass to SubClass typecast ) and making accompanying modifications. ( after Prof. Vidals comments .. :) .. ). My apologies. Hope this helps.

LabTest#2: Revised Solution -- Section 6 -- Class: Item

I had forgotton to include this before
public class Item 
{
 private String name;
 private int Price;
 
 public Item()
 {
  name = "";
  Price = 0;
 }
 
 public Item(String n, int p)
 {
  this.name = n;
  this.Price = p;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getPrice() {
  return Price;
 }

 public void setPrice(int price) {
  Price = price;
 }
 
}

Tuesday, October 26, 2010

LabTest#2: Revised Solution -- Section 5 -- Class: Dungeon

public class Dungeon 
{
 
 private Tile[][] theDungeon;
 
 public Dungeon()
 {
  theDungeon = new Tile[10][10];
  
  for(int i = 0; i < 10; i++)
  {
   for(int j = 0; j < 10; j++)
   {
    theDungeon[i][j] = new Tile();
   }
  }
 }
 
 /* ************************************************************* */
 
 public void addMonsters(Monster m, int x, int y)
 {
  if(x < 0 || x > 10)
   System.out.println("Out of Range");
  
  if(y < 0 || y > 10)
   System.out.println("Out of Range");
  
  // Add the monster 
  theDungeon[x][y].addTheMonster(m);
 }
 
 /* ***************************************************************** */
 
 public void deleteMonsters(int x, int y)
 {
  // Gets the array of monsters at Dungeon[x][y]
  Monster[] m_xy = theDungeon[x][y].getMonsters();
  
  for(int i = 0; i < theDungeon[x][y].getSize(); i++)
  {
   m_xy[i].setLiveStatus(Monster.LiveStatus.DEAD);
   System.out.println("Monster: " 
     + m_xy[i] + " AT DUNGEON[" + x + "][" + y + "] GETS EATEN  UP");
  }
  
  // Creating an empty array of Monster[] type:
  Monster[] newMonsterArray = new Monster[100];
  
  // Replace the Monster[] array of this TILE with an empty one
  theDungeon[x][y].setMonsters(newMonsterArray);
  theDungeon[x][y].setSize(0);
 }
 
 /* ***************************************************************** */
 
 // Returns the 2D Array
 public Tile[][] getTheDungeon()
 {
  return theDungeon;
 }
 
 /* ***************************************************************** */
 
 public String toString()
 {
  String Str = "";
  String contents = "";
  
  for(int i = 0; i < 10; i++)
  {
   for(int j = 0; j < 10; j++)
   {
    
    contents = theDungeon[i][j].toString();
    
    if(!contents.equals(""))
    {
     Str += String.format("DUNGEON[%d][%d] CONTAINS :: %n %s %n",i, j, contents);
    }
    else
    {
     //Str += String.format("DUNGEON[%d][%d] CONTAINS :: %n EMPTY %n",i, j);
    }
   }
  }
  
  return Str;
 }
 
 /* ***************************************************************** */
 
 
}

LabTest#2: Revised Solution -- Section 5 -- Class: Tile

public class Tile 
{
 private Monster[] monsters;
 private int size;
 
 public Tile()
 {
  monsters = new Monster[100];
  size = 0;
 }
 
 /* ******************************************************************* */
 
 public void setSize(int s)
 {
  this.size = s;
 }
 
 /* ******************************************************************* */
 
 public int getSize()
 {
  return this.size;
 }
 
 /* ******************************************************************* */
 
 public void addTheMonster(Monster m)
 {
  monsters[size] = m;
  size++;
 }
 
 /* ******************************************************************* */
 
 public void deleteTheMonster(Monster m)
 {
  int index;
  
  // If match found then delete
  if((index = this.contains(m)) >= 0)
  {
   for(int i = index; i < this.getSize() - 1; i++)
   {   
    // The shift after deletion
    monsters[i] = monsters[i+1];
   }
   size--;
  }
 }
 
 /* ******************************************************************* */
 
 public int contains(Monster m)
 {
  int match_index = -1;
  
  for(int i=0; i < this.getSize(); i++)
  {
   if(monsters[i].getName().equalsIgnoreCase(m.getName()) && monsters[i].getMonsterType() == m.getMonsterType())
   {
    match_index = i;
    break;
   }
  }
  
  return match_index;
 }
 
 /* ******************************************************************* */
 
 // returns the array of monsters at the particular tile
 public Monster[] getMonsters()
 {
  return monsters;
 }
 
 /* ******************************************************************* */
 
 // set the array of monsters for a particular tile
 public void setMonsters(Monster[] m)
 {
  this.monsters = m;
 }
 
 /* ******************************************************************* */
 
 // Checks to see if this CHANNEL contains anyone of Monster.MonsterType monsType
 public boolean contains(Monster.MonsterType monsType)
 {
  boolean returnValue = false;
  
  for(int i = 0; i < this.getSize(); i++)
  {
   if(monsters[i].getMonsterType() == monsType)
   {
    returnValue = true;
    break;
   }
  }
  return returnValue;
 }
 
 /* ******************************************************************* */
 
 // 
 public String toString()
 {
  String str = "";
  
  if(this.getSize() > 0)
  {
   for(int i = 0; i < this.getSize(); i++)
   {
    str += String.format("Cell[%d]: %s %n",i, monsters[i]);
   }
  }
  return str;
 }
 
 /* ******************************************************************* */

}

LabTest#2: Revised Solution -- Section 5 -- Class: Monster

public class Monster 
{
 private String name;
 public enum MonsterType { GOBLIN, ZOMBIE, GHOST };
 public MonsterType type;
 
 public enum LiveStatus { LIVING, DEAD };
 public LiveStatus liveStat;
 
 public Monster()
 {
  name = "";
 }
 
 public Monster(String n)
 {
  this.setName(n);
 }
 
 public void setName(String n)
 {
  this.name = n;
 }
 
 public String getName()
 {
  return this.name;
 }
 
 public void setMonsterType(MonsterType m)
 {
  type = m;
 }
 
 public MonsterType getMonsterType()
 {
  return type;
 }
 
 public void setLiveStatus(LiveStatus l)
 {
  liveStat = l;
 }
 
 public LiveStatus getLiveStatus()
 {
  return liveStat;
 }
 
}

LabTest#2: Revised Solution -- Section 5 -- Class: Goblin


public class Goblin extends Monster 
{
 private int x;
 private int y;
 
 public Goblin(String n, MonsterType m, int x, int y, Dungeon theD)
 {
  super(n);
  super.setMonsterType(m);
  this.setX(x);
  this.setY(y);
  super.setLiveStatus(Monster.LiveStatus.LIVING);
  
  theD.addMonsters(this, this.getX(), this.getY());
  
 }
 
 /* ************************************************************* */
 
 // Setters and Getters
 
 public int getX() {
  return x;
 }

 public void setX(int x) {
  this.x = x;
 }

 public int getY() {
  return y;
 }

 public void setY(int y) {
  this.y = y;
 }
 
 
 /* ************************************************************** */
 
 // Movements
 
 // move North
 public void moveNorth(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes North till it finds an empty space
   do
   {
    this.setY(this.getY() + 1);
    
    // Wraps around ( if it goes beyond GRID)
    if(this.getY() > 9)
     this.setY(0);
    
   }while(theD.getTheDungeon()[this.getX()][this.getY()].getSize() > 0);
   
   // when it has found an empty space --> it then adds itself there
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move South
 public void moveSouth(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes on till it finds an empty space
   do
   {
    this.setY(this.getY() - 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getY()) < 0)
     this.setY(9);
    
   }while(theD.getTheDungeon()[this.getX()][this.getY()].getSize() > 0);
   
   // when it has found an empty space --> it then adds itself there
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move West
 public void moveWest(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes on till it finds an empty space
   do
   {
    this.setX(this.getX() - 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getX()) < 0)
     this.setX(9);
    
    
   }while(theD.getTheDungeon()[this.getX()][this.getY()].getSize() > 0);
   
   // when it has found an empty space --> it then adds itself there
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move West
 public void moveEast(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes on till it finds an empty space
   do
   {
    this.setX(this.getX() + 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getX()) > 9)
     this.setX(0);
    
   }while(theD.getTheDungeon()[this.getX()][this.getY()].getSize() > 0);
   
   // when it has found an empty space --> it then adds itself there
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 
 /* **************************************************************** */
 
 public String toString()
 {
  String str;
  //str = super.getName() + " " + super.getMonsterType().toString() + "\n";
  str = String.format("%s %s", super.getName(), super.getMonsterType().toString());
  return str;
 }
}

LabTest#2: Revised Solution -- Section 5 -- Class: Zombie

public class Zombie extends Monster 
{
 private int x;
 private int y;
 
 public Zombie(String n, MonsterType m, int x, int y, Dungeon theD)
 {
  super(n);
  super.setMonsterType(m);
  this.setX(x);
  this.setY(y);
  super.setLiveStatus(Monster.LiveStatus.LIVING);
  
  theD.addMonsters(this, this.getX(), this.getY());
 }
 
 /* ****************************************************************** */
 
 // Setters and Getters
 
 public int getX() {
  return x;
 }

 public void setX(int x) {
  this.x = x;
 }

 public int getY() {
  return y;
 }

 public void setY(int y) {
  this.y = y;
 }
 
 
 /* ************************************************************** */
 
 // Movements
 
 // move north
 public void moveNorth(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   this.setY(this.getY() + 1);
   
   // Wraps around ( if it goes beyond GRID)
   if((this.getY()) > 9)
    this.setY(0);
   
   this.TheZombieAct(theD, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move south
 public void moveSouth(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   this.setY(this.getY() - 1);
   
   // Wraps around ( if it goes beyond GRID)
   if((this.getY()) < 0)
    this.setY(9);
   
   
   this.TheZombieAct(theD, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // Move west 
 public void moveWest(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   this.setX(this.getX() - 1);
   
   // Wraps around ( if it goes beyond GRID)
   if((this.getX()) < 0)
    this.setX(9);
   
   this.TheZombieAct(theD, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // Move east
 public void moveEast(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   this.setX(this.getX() + 1);
   
   // Wraps around ( if it goes beyond GRID)
   if((this.getX()) > 9)
    this.setX(0);
   
   this.TheZombieAct(theD, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 
 /* **************************************************************** */
 
 // The Zombie's Act
 
 public void TheZombieAct(Dungeon theD, int x, int y)
 {
  // The Zombie now deletes all monsters at Dungeon[x][y + 1]
  theD.deleteMonsters(x, y);
  
  // And it adds itself at Dungeon[x][y + 1] after having eaten everyone at Dungeon[x][y + 1]
  theD.addMonsters(this, x, y);
 }
 
 /* **************************************************************** */
 
 public String toString()
 {
  String str;
  //str = super.getName() + " " + super.getMonsterType().toString() + "\n";
  str = String.format("%s %s", super.getName(), super.getMonsterType().toString());
  return str;
 }
 
}

LabTest#2: Revised Solution -- Section 5 -- Class: Ghost


public class Ghost extends Monster 
{
 private int x;
 private int y;
 
 public Ghost(String n, MonsterType m, int x, int y, Dungeon theD)
 {
  super(n);
  super.setMonsterType(m);
  this.setX(x);
  this.setY(y);
  super.setLiveStatus(Monster.LiveStatus.LIVING);

  theD.addMonsters(this, this.getX(), this.getY());
  
 }
 
 /* ************************************************************** */
 
 // Getters and Setters
 
 public int getX() {
  return x;
 }

 public void setX(int x) {
  this.x = x;
 }

 public int getY() {
  return y;
 }

 public void setY(int y) {
  this.y = y;
 }
 
 
 /* ************************************************************** */
 
 // Movements
 
 // move north
 
 public void moveNorth(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes North till it finds a TILE without another GHOST
   do 
   {
    this.setY(this.getY() + 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getY()) > 9)
     this.setY(0);
   
   }while(theD.getTheDungeon()[this.getX()][this.getY()].contains(Monster.MonsterType.GHOST));
   
   // theD.getTheDungeon() --> returns Tile[][] array
   // theD.getTheDungeon()[x][y] --> returns a Tile Object --> containing Monsters[]
   
   // Now it adds itself to this CHANNEL
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move south 
 public void moveSouth(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
  
  
   // Goes south till it finds a TILE without another ghost
   do 
   {
    this.setY(this.getY() - 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getY()) < 0)
     this.setY(9);
   
   }while(theD.getTheDungeon()[this.getX()][this.getY()].contains(Monster.MonsterType.GHOST));
   
   // theD.getTheDungeon() --> returns Tile[][] array
   // theD.getTheDungeon()[x][y] --> returns a Tile Object --> containing Monsters[]
   
   // Now it adds itself to this TILE
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move west
 public void moveWest(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes west till it finds a TILE without another ghost
   do 
   {
    this.setX(this.getX() - 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getX()) < 0)
     this.setX(9);
   
   }while(theD.getTheDungeon()[this.getX()][this.getY()].contains(Monster.MonsterType.GHOST));
   
   // theD.getTheDungeon() --> returns Tile[][] array
   // theD.getTheDungeon()[x][y] --> returns a Tile Object --> containing Monsters[]
   
   // Now it adds itself to this CHANNEL
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move west
 public void moveEast(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes EAST till it finds a TILE without another ghost
   do 
   {
    this.setX(this.getX() + 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getX()) > 9)
     this.setX(0);
   
   }while(theD.getTheDungeon()[this.getX()][this.getY()].contains(Monster.MonsterType.GHOST));
   
   // theD.getTheDungeon() --> returns Tile[][] array
   // theD.getTheDungeon()[x][y] --> returns a Tile Object --> containing Monsters[]
   
   // Now it adds itself to this CHANNEL
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 
 
 /* **************************************************************** */
 
 public String toString()
 {
  String str;
  //str = super.getName() + " *** " + super.getMonsterType() + "\n";
  str = String.format("%s %s", super.getName(), super.getMonsterType().toString());
  return str;
 }
 

}

LabTest#2: Revised Solution -- Section 6 -- Class: Shoes

public class Shoes extends Item
{
 private int theSize;
 
 public Shoes()
 {
  this.setSize(0);
 }
 
 public Shoes(String n, int p, int s)
 {
  super(n, p);
  this.setSize(s);
 }
 
 public void setSize(int s)
 {
  this.theSize = s;
 }
 
 public int getSize()
 {
  return theSize;
 }
 
 public String toString()
 {
  String str;
  str = String.format("Item Name:%s Item Price:%d Item Size:%d", super.getName(), super.getPrice(), this.getSize());
  return str;
 }
}

LabTest#2: Revised Solution -- Section 6 -- Class: Shirt

public class Shirt extends Item
{
 public enum Size { S, M, L, XL };
 private Size theSize;
 
 public Shirt()
 {
  this.setSize(Size.S);
 }
 
 public Shirt(String n, int p, Size s)
 {
  super(n, p);
  this.setSize(s);
 }
 
 public void setSize(Size s)
 {
  theSize = s;
 }
 
 public Size getSize()
 {
  return theSize;
 }
 
 public String toString()
 {
  String str;
  str = String.format("Item Name:%s Item Price:%d Item Size:%s", super.getName(), super.getPrice(), this.getSize().toString());
  return str;
 }
 
}

LabTest#2: Revised Solution -- Section 6 -- Class: Pant

public class Pant extends Item 
{
 private int theSize;
 
 public Pant()
 {
  this.setSize(0);
 }
 
 public Pant(String n, int p, int s)
 {
  super(n, p);
  this.setSize(s);
 }
 
 public void setSize(int s)
 {
  this.theSize = s;
 }
 
 public int getSize()
 {
  return theSize;
 }
 
 public String toString()
 {
  String str;
  str = String.format("Item Name:%s Item Price:%d Item Size:%d", super.getName(), super.getPrice(), this.getSize());
  return str;
 }
}