modify a class to accept exceptions

Would like to know the best way to do this; have a few ideas but my te

Page 1 of 1

1 Replies - 670 Views - Last Post: 05 May 2008 - 10:02 PM Rate Topic: -----

#1 xaosotter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-May 08

modify a class to accept exceptions

Post icon  Posted 05 May 2008 - 09:25 PM

package javaapplication3;

/**
*@author Vanessa Todd
*/

import java.util.*;
import java.io.*;

public class Room{ 
	
 int iDays;
 int iRoomNumber;
 double dCostPerDay;
 String sGuest;
 Object Room;

  public Room(int iRoomNumber, String sGuest, double dCostPerDay, int iDays){ 
	 this.iRoomNumber= iRoomNumber;
	 this.iDays= iDays;
	 this.sGuest= sGuest;
	 this.dCostPerDay= dCostPerDay; 
  }//closes constructor
  
  public int getiRoomNumber(){
	 return this.iRoomNumber;
  }//closes getiRoomNumber
   
  public void setiRoomNumber(int newiRoomNumber){
	  this.iRoomNumber= newiRoomNumber;
  }//closes setiRoomNumber

  public String getsGuest(){
	 return this.sGuest;
  }//closes getsGuest method

  public void setsGuest (String newsGuest){
	 this.sGuest= newsGuest;
  }//closes setsGuest method

  public double getdCostPerDay(){
	 return this.dCostPerDay;
  }//closes getdCostPerDay
  
  public void setdCostPerDay (double newdCostPerDay){
	 this.dCostPerDay= newdCostPerDay;
  }//closes setdCostPerDay method

  public int getiDays(){
	 return this.iDays;
  }//closes getiDays method

  public void setiDays(int newiDays){
	 this.iDays= newiDays;
  }//closes setiDays method
  
	@Override
  public String toString(){
	 String s = "";
	 if(iDays != 0){
		s = s + "Room Number: " + this.iRoomNumber;
		s = s + "\nGuest Name: " + this.sGuest;
		s = s + "\nCost Per Day: " + this.dCostPerDay;
		s = s + "\nDays: " + this.iDays;
		return s;
	 }//closes if
	 else{
		s = s + "Room Number: " + this.iRoomNumber;
		s = s + "Cost Per Day: " + this.dCostPerDay;
		return s;
	 }//closes else 
  }//closes toString method 
  
}//closes Room class



Ok, that is my Room class and I know that it works because I ran a separate class with a main method off of it and it was fine.
The next step is to do the exceptions to the set methods.
Now here is where it gets tricky--for me at least--and please excuse any ignorance on my part in advance...
The instruction sheet states that I need to build 3 classes; the Room class, the RoomException class, and a Hotel class(this one holding my main method) however, the instructions are divided into Hotel.java and Room.java. Also in her tips/steps she states that after the Room class is built to modify the Room class to accept Exceptions and to have the constructor call the set method.

So my questions are:
1. Can I build a separate public RoomException class{ like I did with the Room class instead of modifying the Room class, and if so how would I call the constructor?
2. If that is not possible or practical and I do need to modify the Room class which of the following would be the best way to go about it?
a. modify each set method w/ a try catch statement
b. or can I add a class RoomException{ and then call each set method ??? in that block with a try catch (again I am unsure how I would call the set methods to do this)

If I were doing this for the Hotel class I would do it off my input readers but this class does not have all that.
<puzzled>
Thanks for any and all help

Is This A Good Question/Topic? 0
  • +

Replies To: modify a class to accept exceptions

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: modify a class to accept exceptions

Posted 05 May 2008 - 10:02 PM

OK I'll try to make it short:

There basically 2 type of Exceptions:
- Exceptions
- and RunTimeExceptions which is a subclass of Exceptions

The big difference between the 2 is that Exceptions has to be try and catch the RunTimeException don't

Lets look at the second one first
There is a class IllegalStateException that you can use anywhere to trap a bug in your program this class extends RunTimeException.

So lets imagine you have a method that copy an array to another array and you want to throw an exception if (it is a bug in your code) the 2 arrays don't have the same sise:

void copyArray(int[] a, int[b]) {
   if(a.length != b.length)
	  throw new IllegalStateException("Method copyArray called with 2 arrays not the same length");
   for(int i = 0; i < a.length; i++)
	  b[i] = a[i];



now in your code you can call copyArray without try/catch clause:

int[] x = {1,2,3,4,5};
int[] y = new int[5];
copyArray(x, y);



will work without problem and if you call copyArray with 2 arrays not the same size... your program will stop... you have a bug fix it !!!

Now if your code throw an Exception (not a RunTime one) you'll have to use the throw/try/catch clauses

void copyArray(int[] a, int[b]) {
   if(a.length != b.length)
	  throw new Exception("Method copyArray called with 2 arrays not the same length");
   for(int i = 0; i < a.length; i++)
	  b[i] = a[i];



won't compile... you have to say that the method might throw an Exception:

void copyArray(int[] a, int[b]) throws Exception {
   if(a.length != b.length)
	  throw new Exception("Method copyArray called with 2 arrays not the same length");
   for(int i = 0; i < a.length; i++)
	  b[i] = a[i];



and the old code calling the method also has to be changed

int[] x = {1,2,3,4,5};
int[] y = new int[5];
try {
	copyArray(x, y);
}
catch(Exception e) {
	System.out.println("Oups try to copy 2 arrays of not different sizes");
}



Now back to your question....

Yes you can write a RoomException class just have to determine if you extends Exception on RunTime exception

If you use that one the method throwing will have the "throws RoomException" in its definition
The code calling that method will have to
try {
}
catch (RoomException e) {
}
class RoomException extends Exception {
   // constructor
   RoomException(String message) {
	  super(message);
   }
}



If you use that one, no throws clause and no need for the try/catch block

class RoomException extends RunTimeException {
   // constructor
   RoomException(String message) {
	  super(message);
   }
}



Hopes this help

This post has been edited by pbl: 05 May 2008 - 10:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1