473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Exceptions

5
Hello all! I'm relatively new to Java, so I'm sorry if this question comes off as naive. I am creating an array-based stack class. The goal of it is to work on exceptions.

I have 2 classes, a test class and a regular class. The regular class has the methods object peek(), object push(), and object pop(). For the peek and the pop methods I am using an EmptyStackException. For some reason, the EmptyStackException will not allow me to enter in parameters for it like my exception for the push method does. Therefore, when I put it in the test class and my stack is empty, it doesn't check the exception. This part of my class looks like this:
Expand|Select|Wrap|Line Numbers
  1. public Object peek() // Returns object on top of this stack without removing it from the stack. Throws an EmptyStackException if this stack is empty
  2.     {
  3.        if( top ==  -1 )
  4.        {
  5.            throw new EmptyStackException();
  6.        }
  7.        return stk[top];
  8.     }
  9.  
  10.     public Object pop() // Removes and returns object at top of this stack. Throws an EmptyStackException if the stack is empty
  11.     {
  12.         if( top == -1 )
  13.         {
  14.             throw new EmptyStackException();
  15.         }
  16.         return stk[top--];
  17.  
  18.     }
  19.  
And in the test class I put..
Expand|Select|Wrap|Line Numbers
  1.        try
  2.         {
  3.             System.out.println(newStack.push("tart"));
  4.             System.out.println(newStack.push("chocolate"));
  5.             System.out.println(newStack.pop());
  6.             System.out.println(newStack.pop());
  7.             System.out.println(newStack.pop());
  8.         }
  9.         catch(EmptyStackException emptyEmpty)
  10.         {
  11.             System.out.println("Stack is empty - could not carry out operation");
  12.         }
  13.  
When complied, the output reads
tart
chocolate
chocolate
tart
null


Could somebody please tell me if they see something wrong in my coding, and perhaps lead me to a solution? Thank you so much!
Nov 3 '08 #1
8 1310
r035198x
13,262 8TB
Most likely the top variable is not being updated properyly. What does your pop method look like?
Nov 4 '08 #2
JosAH
11,448 Expert 8TB
What happens when you do the following in your test class:

Expand|Select|Wrap|Line Numbers
  1. while (true)
  2.    System.out.println(newStack.pop());
  3.  
kind regards,

Jos
Nov 4 '08 #3
c0426
5
r, my pop method is as follows.
public Object pop() // Removes and returns object at top of this stack. Throws an EmptyStackException if the stack is empty
{
if( top == -1 )
{
throw new EmptyStackException();
}
return stk[top--];

}

Jos,
are you implying that I should put the while loop in the try/catch block?

Thank you both!
Nov 4 '08 #4
c0426
5
Oh Jos, I understand what you are saying. The goal of the assignment though is to utilize a try/catch block for an exception that is thrown. So I am supposed to handle the EmptyStackException using the try/catch block. I just don't see how this is possible if the complier will not allow me to pass a message in the exception's parameters.
Nov 4 '08 #5
JosAH
11,448 Expert 8TB
Oh Jos, I understand what you are saying. The goal of the assignment though is to utilize a try/catch block for an exception that is thrown. So I am supposed to handle the EmptyStackException using the try/catch block. I just don't see how this is possible if the complier will not allow me to pass a message in the exception's parameters.
That's because the EmptyStackException is (also?) an existing Exception.
Can you show us your entire Stack class? I suspect that the 'top' member
variable equals zero initially.

kind regards,

Jos
Nov 4 '08 #6
c0426
5
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Stack
  3. {
  4.     private Object stk[];
  5.     private int top;
  6.  
  7.  
  8.     public Stack()
  9.     {
  10.         stk= new Stack[50]; //creates a default setting of 50 objects in the stack.
  11.         top= -1; //sets top of stack to -1 to show there is no objects in the stack yet.
  12.  
  13.     }
  14.  
  15.     public Stack(int size)
  16.     {
  17.         if(size < 0) //if the size passed is less than zero, the size is set to fifty.
  18.         {
  19.           size=50; 
  20.         }
  21.  
  22.         stk= new Object[size]; //otherwise, the size is set to whatever is passed in the parameters.
  23.     }
  24.  
  25.     public boolean empty() //tests whether or not the stack is empty.
  26.     {
  27.         if(true)
  28.         {
  29.             return top==-1;
  30.         }
  31.         return false;
  32.     }
  33.  
  34.     public Object peek() //looks at the top item without discarding it.
  35.     {
  36.        if( top ==  -1 ) //if the stack is empty, throws EmptyStackException.
  37.        {
  38.            throw new EmptyStackException(); 
  39.        }
  40.        return stk[top];
  41.     }
  42.  
  43.     public Object pop() //removes and returns item on top of stack.
  44.     {
  45.         if( top == -1 ) 
  46.         {
  47.             throw new EmptyStackException();
  48.         }
  49.         return stk[top--]; 
  50.  
  51.     }
  52.  
  53.     public Object push(Object item) // pushes item on top of this stack and returns item.
  54.     {
  55.         if (top == stk.length -1) //if the stack is full, Throws IllegalStateException.
  56.         {
  57.             throw new IllegalStateException("The stack is too full!");
  58.         }
  59.  
  60.         stk[++top]= item;
  61.         return item;
  62.     }
  63.  
  64.     public int getTop() //getter method for the top item on the stack.
  65.     {
  66.         return top;
  67.     }
  68. }
Nov 5 '08 #7
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Stack
  3. {
  4.     private Object stk[];
  5.     private int top;
  6.  
  7.  
  8.     public Stack()
  9.     {
  10.         stk= new Stack[50]; //creates a default setting of 50 objects in the stack.
  11.         top= -1; //sets top of stack to -1 to show there is no objects in the stack yet.
  12.  
  13.     }
  14.  
  15.     public Stack(int size)
  16.     {
  17.         if(size < 0) //if the size passed is less than zero, the size is set to fifty.
  18.         {
  19.           size=50; 
  20.         }
  21.  
  22.         stk= new Object[size]; //otherwise, the size is set to whatever is passed in the parameters.
  23.     }
Both of these constructors are incorrect for different reasons:

1) it should be 'stk= new Object[50]' in your first constructor
2) in your second constructor you forgot to set top= -1.

Your test code most likely used the second constructor.

kind regards,

Jos
Nov 5 '08 #8
c0426
5
ah perfect! Thank you so much!
Nov 6 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem...
21
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the...
26
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal...
9
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
6
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.