Results 1 to 2 of 2
Thread: Some Exceptions issues
- 11-15-2010, 07:56 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
Some Exceptions issues
Hello i should write a program which simulates a desk and you are putting workItems on it (array have to be used) and there should be used some Exceptions with which i cant move... if someone would be so kind and help me with it
<code>
public interface WorkStack
{
/**
* Checks whether the stack is empty.
*
* @return true if the stack is empty,
* false otherwise
*/
boolean isEmpty();
/**
* Inserts a new work item to the stack.
*
* @param item work item which will be inserted to the stack
* @return true if the item is successfully inserted,
* false otherwise (e.g. if the stack is full)
* @throws NullPointerException if the parameter item is null.
*/
boolean push(WorkItem item);
/**
* It retrieves and removes the first item (top item) of the stack.
* Returns null if the stack is empty.
*
* @return the top item of the stack,
* or null if the stack is empty.
*/
WorkItem pop();
/**
* It returns a position of the given work item in "to do list".
* When the work item is on the top of the stack the position is 1,
* when the work item is on the bottom of the stack, the position is number of items in the stack.
*
* @param item a work item
* @return position of the work item
* @throws NullPointerException if the parameter item is null.
* @throws IllegalArgumentException if the stack does not contain the given work item.
*/
int getItemPosition(WorkItem item);
}
</code>
<code>
public class Table implements WorkStack
{
private WorkItem toDoList[];
private int stack;
private WorkItem item;
public Table (int stack){
if (stack<0) {
throw new IllegalArgumentException("Cant be such a tiny table");
}
this.stack=stack;
}
public boolean isEmpty(){
return (toDoList[0]==null);
}
public boolean push(WorkItem item){
if (toDoList.length<=stack){
toDoList[toDoList.length+1]=item;
return true;
}else{
return false;
}
}
public WorkItem pop(){
if (isEmpty()){
return null;
}else{
return toDoList[0];
}
}
public int getItemPosition(WorkItem item){
for(int i=0;i<toDoList.length;i++){
try { //these 2 exceptions are pain in the ass
if (equals(item)){
return i+1;
}else{
return null;
}
}catch (NullPointerException npe){
System.err.println("Here should be something written");
}
}
}
}
</code>
and it would be nice if you check that code above if there arent some bullshits... this is the last part
<code>
public class WorkItem
{
private String name;
/**
* It creates a new work item with the specified name.
*
* @param name name of the work item
* @throws NullPointerException if the name is null
*/
public WorkItem(String name) {
if (name == null) {
throw new NullPointerException("The name of the work item can't be set to NULL");
}
this.name = name;
}
/**
* It returns true if the given object is instance of this class
* and has the same name.
*
* @param object
* @return true if the given object equals to this one
* false otherwise
*/
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof WorkItem)) return false;
WorkItem item = (WorkItem) o;
return item.getName().equals(getName());
}
/**
* It returns the hashcode of the name of the work item
*
* @return hash code
*/
public int hashCode() {
return getName().hashCode();
}
/**
* @return name of the work item
*/
public String getName() {
return name;
}
@Override
public String toString() {
return getName();
}
}
</code>
- 11-16-2010, 09:10 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
Similar Threads
-
Exceptions
By Nerijus in forum New To JavaReplies: 8Last Post: 05-18-2010, 01:44 PM -
Exceptions & More
By besweeet in forum New To JavaReplies: 12Last Post: 04-29-2010, 09:06 PM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 08:38 AM -
Exceptions in thread
By Goodwater in forum New To JavaReplies: 4Last Post: 03-16-2009, 03:00 AM -
Need Help With Exceptions
By maggie_2 in forum New To JavaReplies: 5Last Post: 12-15-2008, 07:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks