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;
}
}
Tuesday, October 26, 2010
LabTest#2: Revised Solution -- Section 5 -- Class: Ghost
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;
}
}
LabTest#2: Revised Solution -- Section 6 -- Class: Shopping Cart
public class ShoppingCart
{
public Item[] theItems;
public int item_count;
public int TotalPrice;
public ShoppingCart()
{
theItems = new Item[1000];
item_count = 0;
TotalPrice = 0;
}
/* ****************************************************************************************** */
// Setters and Getters
public int getItemCount()
{
return item_count;
}
public void setItemCount(int ic)
{
item_count = ic;
}
public void setTotalPrice(int t)
{
this.TotalPrice = t;
}
public int getTotalPrice()
{
return TotalPrice;
}
public void setTheItems(Item[] t)
{
this.theItems = t;
}
public Item[] getTheItems()
{
return theItems;
}
/* ****************************************************************************************** */
public void addItems(Item anItem)
{
theItems[item_count] = anItem;
System.out.println("Item Added:" + anItem);
item_count++;
}
/* ****************************************************************************************** */
public boolean removeItem(Item e)
{
boolean returnValue = false;
int matched_item;
if((matched_item = this.contains(e)) >= 0)
{
for(int i = matched_item; i < item_count - 1; i++)
{
theItems[i] = theItems[i+1];
}
item_count--;
this.setTotalPrice(this.getTotalPrice() - e.getPrice());
returnValue = true;
}
if(returnValue == false)
{
System.out.println("Item not found");
}
else
{
System.out.println("Item Removed:" + e);
}
return returnValue;
}
/* ****************************************************************************************** */
public int contains(Item e)
{
int item_match = -1;
for(int j = 0; j < this.getItemCount(); j++)
{
if(theItems[j].getName().equals(e.getName()) && theItems[j].getPrice() == e.getPrice())
{
item_match = j;
}
}
return item_match;
}
/* ****************************************************************************************** */
public int calculateTotalPrice()
{
this.setTotalPrice(0);
for(int i = 0 ; i < this.getItemCount(); i++)
{
TotalPrice += theItems[i].getPrice();
}
return this.getTotalPrice();
}
/* ****************************************************************************************** */
public void clear()
{
for(int j = 0; j < this.getItemCount(); j++)
{
theItems[j] = new Item("", 0);
}
// Create an empty array of Item type : Item[]
Item[] freshEmptyItems = new Item[1000];
// Set theItems to this freshEmptyItems
this.setTheItems(freshEmptyItems);
this.setItemCount(0);
this.setTotalPrice(0);
System.out.println("Cart Cleared");
}
/* ****************************************************************************************** */
public String toString()
{
boolean encountered_before;
int the_item_count;
String str = "";
for(int i = 0; i < this.getItemCount(); i++)
{
encountered_before = false;
// Check to see if encountered before
for(int j = 0; j < i; j++)
{
if(theItems[i].getName().equalsIgnoreCase(theItems[j].getName()))
{
encountered_before = true;
}
}
// if not encountered before then calculate its count
if(encountered_before == false)
{
// calculate the item_counts of each item
the_item_count = 1;
for(int k = i+1; k < this.getItemCount(); k++)
{
if(theItems[i].getName().equalsIgnoreCase(theItems[k].getName()))
{
the_item_count++;
}
}
// display
//System.out.println("Quantity = " + the_item_count + " " + theItems[i].getName());
//str += "Quantity = " + the_item_count + " " + theItems[i].getName() + "\n";
str += String.format("Quantity = %d %s %n", the_item_count, theItems[i].getName());
}
}
return str;
}
/* ****************************************************************************************** */
}
LabTest#2 -- Section 6 -- Shopping Cart of Items
1) Create an Item class with a price and name attributes.
2) Create Shirt, Pants, and Shoes classes, with added size attribute. Shirt sizes are S,M,L,XL. Pant and shoe sizes are integers.
3) Create a shopping cart which can hold up to 1000 Items,
with methods :
a) addItem(Item e)
b) removeItem(Item e)
c) int calculateTotal() ;returns the total price
d) clear(); removes all items from the shopping cart
e) toString(); each item in one line.
If an item is there more than once it should say
quantity=4 blue shirt
rather than "blue shirt" in 4 separate lines
LabTest#2 -- Section 6 -- Driver Class ( You can use this to test your program )
public class Driver
{
public static void main(String[] args)
{
Shirt shirt1 = new Shirt("blue shirt", 100, Shirt.Size.M);
Shirt shirt2 = new Shirt("blue shirt", 120, Shirt.Size.S);
Shirt shirt3 = new Shirt("maroon shirt", 130, Shirt.Size.L);
Pant pant1 = new Pant("brown pant", 65, 32);
Pant pant2 = new Pant("black pant", 130, 34);
Pant pant3 = new Pant("brown pant", 100, 33);
Shoes shoe1 = new Shoes("black shoe", 200, 8);
Shoes shoe2 = new Shoes("black shoe", 200, 8);
Shoes shoe3 = new Shoes("grey shoe", 200, 8);
/* **************************************************************** */
ShoppingCart Cart = new ShoppingCart();
Cart.addItems(shirt1);
Cart.addItems(shirt2);
Cart.addItems(shirt3);
Cart.addItems(pant1);
Cart.addItems(pant2);
Cart.addItems(pant3);
Cart.addItems(shoe1);
Cart.addItems(shoe2);
Cart.addItems(shoe3);
System.out.println("TOTAL PRICE = $" + Cart.calculateTotalPrice());
System.out.println("ITEMS BOUGHT: \n" + Cart.toString());
/* **************************************************************** */
Cart.removeItem(shirt3);
Cart.removeItem(pant2);
Cart.removeItem(shoe2);
System.out.println("TOTAL PRICE = $" + Cart.calculateTotalPrice());
System.out.println("ITEMS BOUGHT: \n" + Cart.toString());
/* ****************************************************************** */
Cart.clear();
System.out.println("TOTAL PRICE = $" + Cart.calculateTotalPrice());
System.out.println("ITEMS BOUGHT: \n" + Cart.toString());
/* ******************************************************************* */
Shirt shirt4 = new Shirt("green shirt", 130, Shirt.Size.L);
Pant pant4 = new Pant("green pant", 130, 34);
Shoes shoe4 = new Shoes("green shoe", 200, 8);
Cart.addItems(shirt4);
Cart.addItems(pant4);
Cart.addItems(shoe4);
System.out.println("TOTAL PRICE = $" + Cart.calculateTotalPrice());
System.out.println("ITEMS BOUGHT: \n" + Cart.toString());
/* ******************************************************************** */
}
}
LabTest#2-Section 6 -- Sample Output ( in accordance to above Driver Class)
Sorry about the minor glitch in the TotalPrice Calculation. I had forgotton to reset TotalPrice to 0 : It should be ok now
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
Item Added: Item Name: blue shirt Item Price: $100 Item Size:M
Item Added: Item Name: blue shirt Item Price: $120 Item Size:S
Item Added: Item Name: maroon shirt Item Price: $130 Item Size:L
Item Added: Item Name: brown pant Item Price: $65 Item Size:32
Item Added: Item Name: black pant Item Price: $130 Item Size:34
Item Added: Item Name: brown pant Item Price: $100 Item Size:33
Item Added: Item Name: black shoe Item Price: $200 Item Size:8
Item Added: Item Name: black shoe Item Price: $200 Item Size:8
Item Added: Item Name: grey shoe Item Price: $200 Item Size:8
TOTAL PRICE = $1245
ITEMS BOUGHT:
Quantity = 2 blue shirt
Quantity = 1 maroon shirt
Quantity = 2 brown pant
Quantity = 1 black pant
Quantity = 2 black shoe
Quantity = 1 grey shoe
Item Removed: Item Name: maroon shirt Item Price: $130 Item Size:L
Item Removed: Item Name: black pant Item Price: $130 Item Size:34
Item Removed: Item Name: black shoe Item Price: $200 Item Size:8
TOTAL PRICE = $785
ITEMS BOUGHT:
Quantity = 2 blue shirt
Quantity = 2 brown pant
Quantity = 1 black shoe
Quantity = 1 grey shoe
Cart Cleared
TOTAL PRICE = $0
ITEMS BOUGHT:
Item Added: Item Name: green shirt Item Price: $130 Item Size:L
Item Added: Item Name: green pant Item Price: $130 Item Size:34
Item Added: Item Name: green shoe Item Price: $200 Item Size:8
TOTAL PRICE = $460
ITEMS BOUGHT:
Quantity = 1 green shirt
Quantity = 1 green pant
Quantity = 1 green shoe
LabTest#2 -- Walk through GRID Dungeons
1) Create a class called Monster, with attribute name. You can have other enum variables too like :
public enum MonsterType { GOBLIN, ZOMBIE, GHOST };
public MonsterType type;
Feel free to create other enum variables ( if you feel you need more )
2) Create classes Goblin, Zombie, and Ghost which extends the Monster Class. Can have attributes int x, int y.
3) Create Dungeon which is a (10x10) array of Tiles.
4) A Tile has a 1-D array of Monsters, which starts out empty.
5) The Dungeon class should have methods
addMonster(Monster, x, y);
deleteMonsters(x,y); //deletes all monster in x,y
toString();// prints out the dungeon, and its inhabitants
You can add other functions if you want to ( according to need )
6) The Monsters should all have:
moveNorth(); //moves north one
moveSouth();
moveEast();
moveWest();
except that:
the goblin will continue moving as long as there is someone else in its destination spot
the zombie kills (deletes) everyone else in its destination spot.
if a ghost lands in a Tile with another ghost, if keeps moving to the next spot.
LabTest#2 -- Sample Output ( In Accordance to the Main Program given below )
~~OUTPUT 1:~~
DUNGEON[1][0] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
DUNGEON[1][1] CONTAINS ::
Cell[0]: Ghost1 GHOST
DUNGEON[2][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
~~OUTPUT 2:~~
DUNGEON[1][1] CONTAINS ::
Cell[0]: Ghost1 GHOST
DUNGEON[1][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
DUNGEON[2][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
~~OUTPUT 3:~~
DUNGEON[1][1] CONTAINS ::
Cell[0]: Ghost1 GHOST
DUNGEON[1][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
DUNGEON[1][3] CONTAINS ::
Cell[0]: Ghost2 GHOST
DUNGEON[2][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
~~OUTPUT 4:~~
DUNGEON[1][1] CONTAINS ::
Cell[0]: Ghost1 GHOST
DUNGEON[1][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
Cell[1]: Ghost2 GHOST
DUNGEON[2][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
~~OUTPUT 5:~~
DUNGEON[1][0] CONTAINS ::
Cell[0]: Ghost2 GHOST
DUNGEON[1][1] CONTAINS ::
Cell[0]: Ghost1 GHOST
DUNGEON[1][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
DUNGEON[2][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
~~OUTPUT 6:~~
DUNGEON[1][0] CONTAINS ::
Cell[0]: Ghost2 GHOST
DUNGEON[1][1] CONTAINS ::
Cell[0]: Ghost1 GHOST
Cell[1]: Goblin2 GOBLIN
DUNGEON[1][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
DUNGEON[2][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
~~OUTPUT 7:~~
Monster: Ghost1 GHOST AT DUNGEON[1][1] GETS EATEN UP
Monster: Goblin2 GOBLIN AT DUNGEON[1][1] GETS EATEN UP
DUNGEON[1][0] CONTAINS ::
Cell[0]: Ghost2 GHOST
DUNGEON[1][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
DUNGEON[1][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
~~OUTPUT 8:~~
DUNGEON[1][0] CONTAINS ::
Cell[0]: Ghost2 GHOST
DUNGEON[1][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
DUNGEON[2][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
~~OUTPUT 9:~~
Goblin2 GOBLIN IS DEAD
DUNGEON[1][0] CONTAINS ::
Cell[0]: Ghost2 GHOST
DUNGEON[1][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
DUNGEON[2][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
~~OUTPUT 10:~~
Ghost1 GHOST IS DEAD
DUNGEON[1][0] CONTAINS ::
Cell[0]: Ghost2 GHOST
DUNGEON[1][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
DUNGEON[2][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
~~OUTPUT 11:~~
DUNGEON[1][1] CONTAINS ::
Cell[0]: Zombie1 ZOMBIE
DUNGEON[1][9] CONTAINS ::
Cell[0]: Ghost2 GHOST
DUNGEON[2][2] CONTAINS ::
Cell[0]: Goblin1 GOBLIN
Subscribe to:
Posts (Atom)