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 13 of 13

Thread: issues with Class

  1. #1
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default issues with Class

    Ok so this is the stuff I am working on right now:
    2. Create a class named NoSuchCustomerException.java. Without this already in place, you will not be able to compile your CustomerIO class.

    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). NAMI.

    D. It should have a second constructor that accepts a String parameter that receives the customer number. This constructor performs the fowllowing.

    1.) Should pass the 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 offsides.

    E. Create an String method named getCustomerNumber. Its only job is to return the customerNumber value.

    and this is all I have so far:
    public class NoSuchCustomerException extends Exception {
    	private String customerNumber;
     
    public void Constructor(){
    }
     
    public String notFound(String customerNumber){
    	return "  The customer number " + customerNumber.toUpperCase() + " does not exist";
    }
    }
    I Don't understand what the book means by passing the message to the parent class... and also #2..

    Any help going in the right direction would be deeply appreciated, thank you!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: issues with Class

    Accessing stuff from the parent class can be done using the super construct. By default Java will always try to call super(), but if you provide your own super declaration you can over-ride this behavior.
    public class MyException extends Exception
    {
        public MyException(String message)
        {
            super(message); // calls the constructor Exception(String message) with parameter message
        }
    }

    I'm not sure what your second question is asking, though I suspect it's some variation of this idea put together with the regular constructor functions.

  3. #3
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: issues with Class

    So when it says to pass the string to the parent class, just put the message inside super(...) ? My second question was about:
    2.) Assign the customerNumber value received and initialize the customerNumber data member.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: issues with Class

    You can use the super construct in conjunction with any regular constructor code. The only pre-requisite is that the super construct must be the first line.

    public MyClass(String message, int param)
    {
        super(message);
     
        // everything below is just like a normal constructor
        this.parameter = param;
    }

  5. #5
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: issues with Class

    public String notFound(String customerNumber){
    	super("  The customer number " + customerNumber.toUpperCase() + " does not exist");
    }

    I tried to use the super() and it keeps saying this:

    NoSuchCustomerException.java:15: call to super must be first statement in constructor

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: issues with Class

    the notFound method is not your constructor. You only use super(params) as the first line of the constructor.

    Constructors are defined as a method with the class name, not the word "constructor". Also, constructors have no declared return type.

  7. #7
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: issues with Class

    I'm still a bit confused on it, how would I fix this then?
    public class NoSuchCustomerException extends Exception {
    	private String customerNumber;
     
    public void Constructor(){
    }
     
    public String notFound(String customerNumber){
     
    	super("  The customer number " + customerNumber.toUpperCase() + " does not exist");
    }
     
     
    }

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: issues with Class

    Here's an example of what I'm talking about. I'll leave it up to you to figure out how to apply this to your problem.

    public class MyClass extends JFrame
    {
        int parameter;
     
        public MyClass(String title, int param)
        {
            // calls to a super constructor must be the first statement of the constructor method declaration
            super(title); // calls the super constructor JFrame(String title)
     
            // everything below is just like a normal constructor
            this.parameter = param;
        }
    }

    Notice how the constructor has no return type declared (not even null). Also, notice that the constructor's name exactly matches the name of the class.

  9. #9
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: issues with Class

    EDIT:::

    Thank you for your help! I believe I've got, it has compiled with no errors this is what I did:

    public class NoSuchCustomerException extends Exception {
     
    private String customerNumber;
     
    public NoSuchCustomerException(String customerNumber){
    	super("  The customer number " + customerNumber.toUpperCase() + " does not exist");
    }
     
    public void Constructor(){
    }
     
    }

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: issues with Class

    mmm, you've partially got it. You still have the empty method "Constructor". This is not needed (unless you're planning on doing something else with this method). Secondly, You still haven't set the field customerNumber to anything.

    public class NoSuchCustomerException extends Exception {
     
    private String customerNumber;
     
    public NoSuchCustomerException(String customerNumber){
        super("  The customer number " + customerNumber.toUpperCase() + " does not exist");
        this.customerNumber = customerNumber.toUpperCase();
    }
     
    }

  11. #11
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: issues with Class

    Yeah, its says to create a null constructor which im still lost at how to do, This is the books requirements:

    2. Create a class named NoSuchCustomerException.java. Without this already in place, you will not be able to compile your CustomerIO class.

    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). NAMI.
    D. It should have a second constructor that accepts a String parameter that receives the customer number. This constructor performs the fowllowing.
    1.) Should pass the 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 offsides.

    E. Create an String method named getCustomerNumber. Its only job is to return the customerNumber value.


    Which I am now having issues when running the application, I search for a product and it ALWAYS comes back with saying no customer was found. This may be a pretty big post so I'm sorry in advance but it's the only way to demonstrate what i mean with my code:
    import java.util.Scanner;
     
    public class CustomerApp	{
        public static void main(String args[])	    {
            // display a welcome message
            System.out.println("Welcome to the Customer application");
            System.out.println();
     
            // create the Scanner object
            Scanner sc = new Scanner(System.in);
     
            // continue until choice isn't equal to "y" or "Y"
            String choice = "y";
            while (choice.equalsIgnoreCase("y"))	{
                String custNo = Validator.getString(sc, "Enter a customer number: ");
     
                try	{
                    Customer cust = CustomerIO.getCustomer(custNo);
                    System.out.println("\n" + cust.getNameAndAddress());
                }
                catch (NoSuchCustomerException e)
                {	System.out.println(e.getMessage());	  }
     
                choice = Validator.getRequiredString(sc, "\nDisplay another customer? (y/n): ");
                System.out.println();
     
            }
     
        }
    }
    public class CustomerIO
    {
        public static Customer getCustomer(String custNo)
            throws NoSuchCustomerException
        {
            Customer cust = new Customer();
            if (custNo == "Z1110")
            {
                cust.name = "Andrew Antosca";
                cust.address = "485 Duane Terrace";
                cust.city = "Ann Arbor";
                cust.state = "MI";
                cust.zipCode = "48108";
                return cust;
            }
            else if (custNo == "Z1111")
            {
                cust.name = "Barbara White";
                cust.address = "3400 Richmond Parkway #3423";
                cust.city = "Bristol";
                cust.state = "CT";
                cust.zipCode = "06010";
                return cust;
            }
            else if (custNo == "Z1112")
            {
                cust.name = "Karl Vang";
                cust.address = "327 Franklin Street";
                cust.city = "Edina";
                cust.state = "MN";
                cust.zipCode = "55435";
                return cust;
            }
            else if (custNo == "Z1113")
            {
                cust.name = "Ronda Chavan";
                cust.address = "518 Comanche Dr.";
                cust.city = "Greensboro";
                cust.state = "NC";
                cust.zipCode = "27410";
                return cust;
            }
            else if (custNo == "Z1114")
            {
                cust.name = "Sam Carol";
                cust.address = "9379 N. Street";
                cust.city = "Long Beach";
                cust.state = "CA";
                cust.zipCode = "90806";
                return cust;
            }
            else if (custNo == "Z1115")
            {
    			cust.name = "Alex Stevens";
    			cust.address = "8281 Main Street";
    			cust.city = "Bellevue";
    			cust.state = "NE";
    			cust.zipCode = "68123";
    			return cust;
    		}
    		else if (custNo == "T2000")
    		{
    			cust.name = "Computers 2 Go";
    			cust.address = "19123 Never Lane";
    			cust.city = "San Diego";
    			cust.state= "CA";
    			cust.zipCode = "23144";
    			return cust;
    		}
    		else if (custNo == "T2100")
    		{
    			cust.name = "Worlds Best Advertising";
    			cust.address = "10029 S Street";
    			cust.city = "Denver";
    			cust.state = "CO";
    			cust.zipCode = "29181";
    			return cust;
    		}
    		else if (custNo == "T2200")
    		{
    			cust.name = "Tims Web Design LLC";
    			cust.address = "2558 McNeilly Road";
    			cust.city = "Chicago";
    			cust.state = "IL";
    			cust.zipCode = "60601";
    			return cust;
    		}
    		else if (custNo == "T2300")
    		{
    			cust.name = "PayCo Systems LLC";
    			cust.address = "12304 Very Important Drive";
    			cust.city = "Los Angeles";
    			cust.state = "CA";
    			cust.zipCode = "90210";
    			return cust;
    		}
    		else if (custNo == "T2400")
    		{
    			cust.name = "Xtreme Web Design LLC";
    			cust.address = "98232 Parch Drive";
    			cust.city = "Branson";
    			cust.state = "MO";
    			cust.zipCode = "87644";
    			return cust;
    		}
    		else if (custNo == "T2500")
    		{
    			cust.name = "DeedsTech LLC";
    			cust.address = "10929 Best Drive";
    			cust.city = "Bellevue";
    			cust.state = "NE";
    			cust.zipCode = "68123";
    			return cust;
    		}
    		else if (custNo == "Q5000")
    		{
    			cust.name = "Sally Sallerson";
    			cust.address = "1923 Old Drive";
    			cust.city = "Toronto";
    			cust.state = "CA";
    			cust.zipCode = "19233";
    			return cust;
    		}
    		else if (custNo == "Q5001")
    		{
    			cust.name = "Butch Salak";
    			cust.address = "19192 Military Road";
    			cust.city = "Las Vegas";
    			cust.state = "NV";
    			cust.zipCode = "18882";
    			return cust;
    		}
    		else if (custNo == "Q5002")
    		{
    			cust.name = "Elvis Presley";
    			cust.address = "Elvis Presley Blvd.";
    			cust.city = "Memphis";
    			cust.state = "TN";
    			cust.zipCode = "61723";
    			return cust;
    		}
     
     
            else
                throw new NoSuchCustomerException(custNo);
        }
    }
    import java.util.Scanner;
     
    public class Validator
    {
    	public static String getString(Scanner sc, String prompt){
    		System.out.print(prompt);
    		String s = sc.next();
    		sc.nextLine();
    		return s;
    	}
    	public static int getInt(Scanner sc, String prompt)
    	{
    		int i = 0;
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			System.out.print(prompt);
    			if (sc.hasNextInt())
    			{
    				i = sc.nextInt();
    				isValid = true;
    			}
    			else
    			{
    				System.out.println("Error! Invalid integer value. Try again.");
    			}
    			sc.nextLine();  // discard any other data entered on the line
    		}
    		return i;
    	}
     
    	public static int getIntWithinRange(Scanner sc, String prompt,
    	int min, int max)
    	{
    		int i = 0;
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			i = getInt(sc, prompt);
    			if (i <= min)
    				System.out.println(
    					"Error! Number must be greater than " + min);
    			else if (i >= max)
    				System.out.println(
    					"Error! Number must be less than " + max);
    			else
    				isValid = true;
    		}
    		return i;
    	}
     
    	public static double getDouble(Scanner sc, String prompt)
    	{
    		double d = 0;
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			System.out.print(prompt);
    			if (sc.hasNextDouble())
    			{
    				d = sc.nextDouble();
    				isValid = true;
    			}
    			else
    			{
    				System.out.println("Error! Invalid decimal value. Try again.");
    			}
    			sc.nextLine();  // discard any other data entered on the line
    		}
    		return d;
    	}
     
    	public static double getDoubleWithinRange(Scanner sc, String prompt,
    	double min, double max)
    	{
    		double d = 0;
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			d = getDouble(sc, prompt);
    			if (d <= min)
    				System.out.println(
    					"Error! Number must be greater than " + min);
    			else if (d >= max)
    				System.out.println(
    					"Error! Number must be less than " + max);
    			else
    				isValid = true;
    		}
    		return d;
    	}
     
    	public static String getRequiredString(Scanner sc, String prompt)
    	{
    		String s = "";
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			System.out.print(prompt);
    			s = sc.nextLine();
    			if (s == null || s.equals(""))
    			{
    				System.out.println("Error! This entry is required. Try again.");
    			}
    			else
    			{
    				isValid = true;
    			}
    		}
    		return s;
    	}
    }
    public class Customer{
    	String name;
    	String address;
    	String city;
    	String state;
    	String zipCode;
    public String getNameAndAddress(){
    	return "  " + name + "\n\t\t   "
    	            + address + "\n\t\t   "
                    + city + ", " + state + " " + zipCode;
    }
     
    }
    public class NoSuchCustomerException extends Exception {
    private String cusNumber;
     
    public void empty(){
    }
     
    public NoSuchCustomerException(String customerNumber){
    	super("  The customer number " + customerNumber.toUpperCase() + " does not exist");
    	cusNumber = customerNumber;
    }
    public String getCustomerNumber(){
    	return cusNumber;
    }
     
     
    }

  12. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: issues with Class

    I believe you're running into issues with using == on Strings (didn't take a closer look, but this is usually the problem).

    See: Common Java Mistakes: == operator or equals() method

    Again, you're not naming your constructors properly. They must have the exact same name as the class name and no declared return type (not even void). See the code I provided for the constructor with a string parameter and modify to have no parameters, as well as removing all code from the body.

  13. The Following User Says Thank You to helloworld922 For This Useful Post:

    _lithium_ (December 20th, 2010)

  14. #13
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: issues with Class

    Thank you so much, I actually fixed the constructor after posting all that code and have it set to what it is supposed to be. Thank you for alllllll the help! It works now!

Similar Threads

  1. Aligning Issues in the Output
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2010, 05:29 AM
  2. Triangle issues
    By FrEaK in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2010, 08:49 AM
  3. Having Issues Past this
    By baGoGoodies111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2009, 08:19 PM
  4. Replies: 0
    Last Post: October 2nd, 2009, 10:51 PM
  5. Maven Issues - mvn install
    By Paolo Futre in forum Java Theory & Questions
    Replies: 5
    Last Post: August 26th, 2009, 05:07 AM