• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED Java - Exceptions (super, null constructors)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Xtreme Barton

Member
Joined
Jan 17, 2004
Ok so here i am again :D
Create a new class named NoSuchCustomerException.
A. Extends Exception.
B. It has a private String data member named customerNumber, not initialized.
C. Create a null constructor (nothing in the argument, nothing in the body of code).
D. Write a second constructor that accepts a String parameter that receives the customer number. This constructor performs the following.
1.) Should pass this message to its parent class, “The customer number [insert uppercase customer number variable here] does not exist.” Yes, this is a super(…).
2.) Assign the customerNumber value received and initialize the customerNumber data member.
3.) Watch for the two space left margin; it’s often ignored at an 8-point penalty for (not) offside’s.


I left out E as it wasnt included in my copy paste attempt but it doesnt mean much as its an easy step ..

this is what i have so far and im hung up on D 1. the super part :bang head


Code:
public class NoSuchCustomerException extends Exception
{
    private String customerNumber;
    
    public NoSuchCustomerException(){}
    
    public NoSuchCustomerException(String customerNumber)
    {
    

    }
    
    
}
 
Code:
public class NoSuchCustomerException extends Exception
{
    private String customerNumber;
    
    public NoSuchCustomerException(){}
    
    public NoSuchCustomerException(String customerNumber)
    {
        super(“The customer number " + customerNumber.toUpperCase() + " does not exist.”);
        this.customerNumber = customerNumber;
    }
}
?
 
so where is the super sending this statement ? just to the display ? or does it actually move the output up the class hierarchy and then output it ?

or
C. None of the above :D
 
To the superclass, which in this case is Exception. As in 1) "Should pass this message to its parent class".

Homework assignment?
 
It is a homework assignment :)

When you extend a class, you can refer to that class with super() as shown in in the example.

Code:
public NoSuchCustomerException(String customerNumber)
    {
        super(“The customer number " + customerNumber.toUpperCase() + " does not exist.”);
        this.customerNumber = customerNumber;
    }

Will call:

Code:
public Exception(String message) {}

So you don't need to rewrite everything in the Exception class, rather you just format the message like you want it, and then call the ready parent method. You can also access variables in the parent class if the permissions just allow it.
 
Homework assignment?

yes sir... and thanks for the help... i went back and looked through more examples in my book to try and refresh my memory some. I think I need to read again this chapter because I a bit confused why you wouldnt just want to display it there since its there rather than send it back and display it ..
 
dropadrop hey howd you know :D

i forgot i extended it too .. i was thinking that super would just refer to the parent class it was in rather than that .. lol

thanks for the pointer outer ..


It is a homework assignment :)

When you extend a class, you can refer to that class with super() as shown in in the example.

Code:
public NoSuchCustomerException(String customerNumber)
    {
        super(“The customer number " + customerNumber.toUpperCase() + " does not exist.”);
        this.customerNumber = customerNumber;
    }
Will call:

Code:
public Exception(String message) {}
So you don't need to rewrite everything in the Exception class, rather you just format the message like you want it, and then call the ready parent method. You can also access variables in the parent class if the permissions just allow it.
 
Back