Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help me with my code

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help me with my code

    I'm new to java programming and this is my assignment. This is the code I've done but couldn't able to display the error message . When I execute the program I'm only getting the error message "Class room table cannot have irregular-shape table." but was not successful in getting the error message for sides and legs which should obviously be displayed."Error in setting legs . Number of legs cannot be less than 3." "Error in setting sides, number of sides cannot be 0".

    Thanks for the help.

    //Class ClassRoomTable is to model class room table
     
     
     
     
    //Class table is to model table
    class Table {
    //Error handling class.
    //An object of this class is thrown if
    //the number of legs for a table is less than 3.
    	public class TooFewLegsError extends Exception // -- 2%
    	{
    		public TooFewLegsError(){
    		System.out.println("Error in setting legs . Number of legs cannot be less than 3.");
    		}
    	}
    //An object of this class is thrown if
    //the number of sides for a table is 0
    	public class ZeroSideError extends Exception // --- 1%
    	{
    		public ZeroSideError(){
    			System.out.println("Error in setting sides, number of sides cannot be 0");
    		}
    	}
    //By default the number of legs for a table is 4, and
    //the number of sides for a table is also 4.
    	private int legs=0; // number of legs
    	private int sides=0; // number of sides
     
    //Default constructor of the table class.
    //By default the number of legs for a table is 4, and
    //the number of sides for a table is also 4.
    public Table() // --- 1%
    { 
     
     
    }
    //A TooFewLegsError is thrown if the number of legs
    //for a table is less than 3, or a ZeroSideError is thrown
    //if the number of sides for a table is equal to 0
    	public Table(int legs1, int sides1) throws TooFewLegsError, ZeroSideError
    	//---- 3%
    	{ 
    		if(legs1==2){
    		throw new TooFewLegsError();
    		}else if(sides1==0){
    		throw new ZeroSideError();
    	}
    }
    //A TooFewLegsError error is thrown if the
    //number of legs for a table is less than 3
    public void setLegs(int legs1) throws TooFewLegsError // -- 2%
    {
    if(legs1<3){
    throw new TooFewLegsError();
    }
    }
     
    //A ZeroSideError error is thrown if the number
    //of sizes for a table is equal to 0
    	public void setSides(int sides1) throws ZeroSideError // -- 2%
    	{
    	if(sides1==0){
    	throw new ZeroSideError();
    	}
    	}
     
    }
    public class ClassRoomTable extends Table 
    {
    //Error handling class.
    //An object of this class is thrown if
    //the shape of a classroom table is irregular.
    	public class IrregularShapeError extends Exception {
    		public IrregularShapeError() {
    			System.out.println("Class room table cannot have irregular-shape table.");;
     
    		}
     
    	}
    private int shape=1; // 1: rectangular
    //2: circle
    //3: triangle
    //4: irregular
    //An error is thrown if the shape is irregular
    public ClassRoomTable(int shape1) throws IrregularShapeError
    { // -- 2%
     
    if(shape1==4){
    throw new IrregularShapeError();
    }
     
    }
     
    //An error is thrown if the shape is irregular
    public void setShape(int shape1) throws IrregularShapeError
    { // -- 2%
    if(shape1==4){
    throw new IrregularShapeError();
    }
     
    }
     
    public static void main(String argv[]) {
    try {
    ClassRoomTable p = new ClassRoomTable(2);
     
    p.setShape(4);
    p.setSides(0); // This line will cause an exception
    p.setLegs(2); // This line will not be executed.
    }
    catch (ClassRoomTable.IrregularShapeError n) {
    System.out.println(
    "Class room table cannot have irregular-shape table."); 
    }
    catch (Table.TooFewLegsError n) {
    System.out.println("Error in setting legs . Number of legs cannot be less than 3.");
    }
    catch (Table.ZeroSideError n) {
    System.out.println(
    "Error in setting sides, number of sides cannot be 0"); 
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,153
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help me with my code

    Are all The error messages you are describing generated by your program?
    Can you look at the logic of the program and see why the program thinks that there are errors.
    Add println statements to the program to show the values of all the variables that are used in making the decision that there is an error. The printed output should show you where your problem is.
    If you still have questions, copy and paste here all the printed output that your program generated and add your questions to that output.

    p.setLegs(2); // This line will not be executed.

    Have you read the comment on that line of code???
    Last edited by Norm; August 4th, 2011 at 07:02 AM.