constructor


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: constructor

  1. #1
    Join Date
    Apr 2004
    Posts
    5

    Question constructor

    hey...

    i am working on a program in which i have to write a program that shows a constructor passing information about constructor failure to an exception handler. i have my code compiling and running....but for some reason it wont print the information in my catch block. i thought my try block's info was false so it should get into the catch block. plz help asap!!! i'm stuck.
    here is my code....

    public final class InRange {

    // aName has content.
    // exception IllegalArgumentException if a param does not comply.
    public InRange ( String aName, double aSpeed ) {
    if ( !textHasContent(aName) ) {
    throw new IllegalArgumentException("Name has no content.");

    }

    if ( aSpeed < 0.0 || aSpeed > 1.0 ) {
    throw new IllegalArgumentException("Speed not in range [0..1]: " + aSpeed);
    }
    fName = aName;
    fSpeed = aSpeed;
    }

    private String fName;
    private double fSpeed;

    // Returns true if aText is non-null and has visible content.
    private boolean textHasContent( String aText )
    {
    String emptyString = "";
    return (aText != null) && (!aText.trim().equals(emptyString));
    }

    public static void main(String args[])
    {
    // InRange part;
    IllegalArgumentException row=null;
    String row1 = null;
    String string1="Sabeen";
    String emptyString1 = "";
    try
    {
    row1 = string1;
    }
    catch(IllegalArgumentException ex)
    {
    System.out.println("Empty string causes an exception.");
    }

    System.out.println("main: " + InRange.class.getName() + " ended");
    }
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    This stumped me for a second because your code for InRange seemed ok, but then i noticed that you were never running any of it!! Nowhere in main to you make a call to InRanges constructor, so there is no chance of ever catching an exception.
    Code:
    public static void main(String args[])
    {
        // InRange part;
        IllegalArgumentException row=null;
        String row1 = null;
        String string1="Sabeen";
        String emptyString1 = "";
        try
        {
            row1 = string1;
        }
        catch(IllegalArgumentException ex)
        {
            System.out.println("Empty string causes an exception.");
        }
    
        System.out.println("main: " + InRange.class.getName() + " ended");
    }
    Firstly, why do you make an IllegalArgumentException and set it to null? You don't need to make an Exception unless you're going to throw it. Secondly, in the try block all you do is take row1 (which == null), and then make it equal string1 (which == "Sabeen"). I'm not sure why you did that, but what you want to be doing in the try block is making an InRange object and supplying it parameters that will cause it to throw an exception. Finally, in the catch block instead of printing out a message of your own I suggest you write System.out.println(ex) which will print out the message that is sent with the exception.

  3. #3
    Join Date
    Apr 2004
    Posts
    5

    Question tried to fix but still getting errors..plz help!!!

    thanx for the help...
    i tried to make the changes u suggested. i am getting an error related to the object i'm creating in main. please help me!!!

    here is my new program....

    public final class InRange {

    //param aName has content.
    //param aSpeed is in the range 0 (inclusive) to 1 (inclusive), and
    //exception IllegalArgumentException if a param does not comply.

    public InRange ( String aName, double aSpeed ) {
    if ( !textHasContent(aName) ) {
    throw new IllegalArgumentException("Name has no content.");
    }
    if ( aSpeed < 0.0 || aSpeed > 1.0 ) {
    throw new IllegalArgumentException("Speed not in range [0..1]: " + aSpeed);
    }
    fName = aName;
    fSpeed = aSpeed;
    }

    private String fName;
    private double fSpeed;

    // Returns true if aText is non-null and has visible content.

    private boolean textHasContent( String aText )
    {
    String emptyString = "";
    return (aText != null) && (!aText.trim().equals(emptyString));
    }

    public static void main(String args[])
    {
    InRange part;
    String row="";
    String row1 = "Siddiqui";
    String string1="Sabeen";
    double number=2.0;
    double num=1.5;
    try
    {
    part(string1, num);
    System.out.println("Lookng for exception");
    }
    catch(IllegalArgumentException ex)
    {
    System.out.println(ex);
    //System.out.println("Empty string causes an exception.");
    }

    System.out.println("main: " + InRange.class.getName() + " ended");
    }

    }



    thanx
    sabeen

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    you're not creating your object correctly. In java you can declare an object or instantiate an object. You're declaring the object properly when you type InRange part; but you never instantiate it properly. The code part(string1 num) is not instantiating it correctly, instead write part = new InRange(string1, num).

  5. #5
    Join Date
    Apr 2004
    Posts
    5

    Thumbs up thanx, that did id

    thanx a bunch mike, that was my problem.....hopefully the rest should go well now. i'll be back with a post if i still have trouble but i think i got it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center