[RESOLVED] How to use these Constructor parameters?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] How to use these Constructor parameters?

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Posts
    15

    [RESOLVED] How to use these Constructor parameters?

    I am having difficulty trying to get passed this bump in the road in a program I am trying to build, mainly regarding an overloaded Constructor and an objects creation. Here it is:

    The Constructor:

    Whale(String name, int age, String[] whaleType, String[] fishOrigin){
    this.name = name;
    this.age = age;
    this.whaleType = whaleType;
    this.fishOrigin = fishOrigin;
    }


    The Object Creation:

    Whale w = new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");


    There is a compiler error stating that I need a String, int, String, String, but I need them to be String, int, String[], String[]. Is this possible? Can anyone offer any guidance to this, it's been a frustrating few hours!

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,426

    Re: How to use these Constructor parameters?

    There is a compiler error
    Please copy and paste here the full text of the error message.

    The code for the constructor leaves off the data type for the class variables whaleType and fishOrigin.
    Are they String or String[]?

    Double check your code. What you have posted does not make sense.
    Last edited by Norm; June 1st, 2011 at 08:08 PM.
    Norm

  3. #3
    Join Date
    Mar 2011
    Posts
    15

    Re: How to use these Constructor parameters?

    Here is the code in which I need to work properly:

    Code:
    class Whale extends Aqualife{
    
        private String[] whaleType = {"Killer Whale", "Blue Whale"};
        
        Whale(String name, int age, String[] whaleType, String[] fishOrigin){
           this.name = name;
           this.age = age;
           this.whaleType = whaleType;
           this.fishOrigin = fishOrigin;
       }

    Here is where the compiler error comes up:


    Code:
    Whale w = new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");

    Here is the compiler error:


    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: constructor Fish(java.lang.String,int,java.lang.String,java.lang.String)
    location: class Fish
    at TestAqualife.main(TestAqualife.java:3)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)


    I am trying to pass a String, int, String array, and String array. The parameter has a String, int, and String Array taken from a parent class and a single String array taken from this class being passed into the constructor. I need these arrays to be used in the overloaded constructor to be able to assign certain values to an Object from the constructor. Sorry if I am being confusing! Thanks!

  4. #4
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,426

    Re: How to use these Constructor parameters?

    The error message shows the constructor for the Fish class. Your previous code was the Whale class.
    Where did that come from?

    A Whale is NOT a fish. A whale is a mammal.

    I cant make any sense from what you are showing. You have single items here and arrays there????
    Can you show your whole code? Or at least the constructors and the calls to the constructors.


    Given the constructor:
    Whale(String name, int age, String[] whaleType, String[] fishOrigin){

    this call to the constructor is wrong:
    ..new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");

    The 3rd and 4th arguments are NOT arrays.
    You can create an array in your call this way:
    ..new Whale ("asdf",33, new String[] {"item 1", "item 2"}, new String[] {"item 1", "item 2"});
    Last edited by Norm; June 1st, 2011 at 09:03 PM.
    Norm

  5. #5
    Join Date
    Mar 2011
    Posts
    15

    Re: How to use these Constructor parameters?

    Code:
    public abstract class Aqualife {
    
        String name;
        int age;
        String fishOrigin[] = {"Lake-fish", "Ocean-fish"};
    
        abstract void eat();
        abstract void procreates();
    
        public void breathes() {
            System.out.println("These creatures breathe from gills.");
        }//End Breathes method
    }//End Aqualife class
    Code:
    class Fish extends Aqualife{
    
       private String[] eatPreference = {"Carnivore", "Herbivore"};
      
       Fish(String name, int age, String[] eatPreference, String[] fishOrigin){
           this.name = name;
           this.age = age;
           //this.eatPreference = eatPreference;
           //this.fishOrigin = fishOrigin;
       }
    
        @Override
        void eat() {
    
            int i = 0;
            if(eatPreference[i].equals("Carnivore")){
                System.out.println("Carnivore - eats meat.");
            } else if (eatPreference[i].equals("Herbivore")){
                System.out.println("Herbivore - eats veggies.");
            }
        }//End eat method
    
        @Override
        void procreates() {
            System.out.println("Fish lay eggs");
        }//End procreates method
    }//End Fish class
    Code:
    class Whale extends Aqualife{
    
        private String[] whaleType = {"Killer Whale", "Blue Whale"};
        
        Whale(String name, int age, String[] whaleType, String[] fishOrigin){
           this.name = name;
           this.age = age;
           this.whaleType = whaleType;
           this.fishOrigin = fishOrigin;
       }
    
        @Override
        void eat() {
    
            int i = 0;
            if (whaleType[i].equals("Killer Whale")) {
                System.out.println("Killer Whale - eats meat.");
            } else if (whaleType[i].equals("Blue Whale")) {
                System.out.println("Blue Whale - eats veggies.");
            }
        }//End eat method
    
        @Override
        void procreates() {
           System.out.println("Whales are mammals.");
        }//End procreates method
    
        @Override
        public void breathes() {
            System.out.println("These mammals breathe above water.");
        }//End Breathes method
    }//End Whale class
    Code:
    public class TestAqualife {
        public static void main(String[] args) {
            Fish f = new Fish("Harold", 13, "Carnivore", "Lake-fish");
            Whale w = new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");
    
            //Harold Fish Information
            System.out.println("Fish Name: " + f.name);
            System.out.println("Age: " + f.age);
            System.out.println("Fish Origin: " + f.fishOrigin);
            f.eat();
            f.procreates();
            f.breathes();
            System.out.println();
    
            //Erwin Whale Information
            System.out.println("Whale Name: " + w.name);
            System.out.println("Age: " + w.age);
            System.out.println("Fish Origin: " + w.fishOrigin);
            w.eat();
            w.procreates();
            w.breathes();
        }//End main method
    }//End TestAqualife class

  6. #6
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,426

    Re: How to use these Constructor parameters?

    These two calls to the Constructors look wrong:
    Fish f = new Fish("Harold", 13, "Carnivore", "Lake-fish");
    Whale w = new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");

    Neither of them have arrays.

    Both of the constructors have arrays:
    Fish(String name, int age, String[] eatPreference, String[] fishOrigin){
    Whale(String name, int age, String[] whaleType, String[] fishOrigin){

    You MUST change your calls to the constructors to use the same data types as are used in their definitions. You MUST use arrays where there are arrays.

    Look at post #4. I gave you the syntax for creating arrays when calling a constructor/method.
    Norm

  7. #7
    Join Date
    Mar 2011
    Posts
    15

    Re: How to use these Constructor parameters?

    Thanks for clearing that up!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center