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

Thread: how can i pass objects into a parameter and display them in the method

  1. #1
    Junior Member em1980's Avatar
    Join Date
    Dec 2011
    Location
    Bethlehem, Pa
    Posts
    8
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how can i pass objects into a parameter and display them in the method

    this is what i have coded so far
    public class Animals {


    public static void main(String args[]){

    Dog dog = new Dog();
    dog.setAnimalType("dog");
    Cat cat = new Cat();
    cat.setAnimalType("cat");
    Fish fish = new Fish();
    fish.setAnimalType("fish");
    }


    public static void displayObject(Dog dog, Cat cat, Fish fish){
    System.out.println("this a " +dog.getAnimalType());
    System.out.println("this a " +cat.getAnimalType());
    System.out.println("this a " +fish.getAnimalType());
    }

    }
    this is one of the classes dog cat and fish look the same
    public class Dog {
    public String animaltype1;

    public String getAnimalType(){
    return animaltype1;
    }
    public void setAnimalType(String name){
    animaltype1 = name;
    }

    }
    Last edited by em1980; December 4th, 2011 at 09:02 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    Do you have a question or problem? Please explain.

  3. #3
    Junior Member em1980's Avatar
    Join Date
    Dec 2011
    Location
    Bethlehem, Pa
    Posts
    8
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    i am doing an exercise in a book and the book is telling me to make 3 objects cat dog and fish then to pass them into into a method. the method must then display the name of each object

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    Please explain what your problem is? Where are you stuck?

  5. #5
    Junior Member em1980's Avatar
    Join Date
    Dec 2011
    Location
    Bethlehem, Pa
    Posts
    8
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    i figured out how to pass objects into the parameters as shown in the code above i cannot get the method to display the objects on the console i tried putting the displayObject method into the main method and i get multiple syntax errors. i pretty much am trying to use the class animal to println the classes dog cat and fish on the command prompt(in textpad) or console ( in eclipse). i am pretty much stuck on the whole thing the code i wrote i posted i could be going in right direction or wrong direction i need help basically and lots of it
    Last edited by em1980; December 4th, 2011 at 10:44 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    i get multiple syntax errors.
    Please post the full text of errors you are getting.

    display the objects on the console
    You would use the print or println methods to display Strings on the console. The contents of the objects must be converted to Strings to be printed. What data variables do you want to display on the console?
    Show the println statement you are using to print on the console.

  7. The Following User Says Thank You to Norm For This Useful Post:

    em1980 (December 4th, 2011)

  8. #7
    Junior Member em1980's Avatar
    Join Date
    Dec 2011
    Location
    Bethlehem, Pa
    Posts
    8
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    forget it i will do it on my own u confusing me even more

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    If you don't post the errors you are getting how do you expect anyone to help you solve the problem?
    We expect you to write your code. If you have problems we'll help you solve them.

  10. #9
    Junior Member em1980's Avatar
    Join Date
    Dec 2011
    Location
    Bethlehem, Pa
    Posts
    8
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    this is the code
    public class Animals {


    public static void main(String args[]){

    Dog dog = new Dog();
    dog.setAnimalType("dog");
    Cat cat = new Cat();
    cat.setAnimalType("cat");
    Fish fish = new Fish();
    fish.setAnimalType("fish");



    public static void displayObject(Dog dog, Cat cat, Fish fish){
    System.out.println("this a " +dog.getAnimalType());
    System.out.println("this a " +cat.getAnimalType());
    System.out.println("this a " +fish.getAnimalType());
    }

    }
    this is the error in textpad
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:17: illegal start of expression
    public static void displayObject(Dog dog, Cat cat, Fish fish){
    ^
    1 error

    Tool completed with exit code 1

  11. #10
    Junior Member em1980's Avatar
    Join Date
    Dec 2011
    Location
    Bethlehem, Pa
    Posts
    8
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    this is the code another way
    package com.emcher.animal;

    public class Animals {


    public static void main(String args[]){

    Dog dog = new Dog();
    dog.setAnimalType("dog");
    Cat cat = new Cat();
    cat.setAnimalType("cat");
    Fish fish = new Fish();
    fish.setAnimalType("fish");

    }

    public static void displayObject(Dog dog, Cat cat, Fish fish){
    System.out.println("this a " +dog.getAnimalType());
    System.out.println("this a " +cat.getAnimalType());
    System.out.println("this a " +fish.getAnimalType());
    }

    }

    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:17: cannot find symbol
    symbol : class Dog
    location: class com.emcher.animal.Animals
    public static void displayObject(Dog dog, Cat cat, Fish fish){
    ^
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:17: cannot find symbol
    symbol : class Cat
    location: class com.emcher.animal.Animals
    public static void displayObject(Dog dog, Cat cat, Fish fish){
    ^
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:17: cannot find symbol
    symbol : class Fish
    location: class com.emcher.animal.Animals
    public static void displayObject(Dog dog, Cat cat, Fish fish){
    ^
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:8: cannot find symbol
    symbol : class Dog
    location: class com.emcher.animal.Animals
    Dog dog = new Dog();
    ^
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:8: cannot find symbol
    symbol : class Dog
    location: class com.emcher.animal.Animals
    Dog dog = new Dog();
    ^
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:10: cannot find symbol
    symbol : class Cat
    location: class com.emcher.animal.Animals
    Cat cat = new Cat();
    ^
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:10: cannot find symbol
    symbol : class Cat
    location: class com.emcher.animal.Animals
    Cat cat = new Cat();
    ^
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:12: cannot find symbol
    symbol : class Fish
    location: class com.emcher.animal.Animals
    Fish fish = new Fish();
    ^
    C:\Users\Emery\workspace\CertPractice\src\com\emch er\animal\Animals.java:12: cannot find symbol
    symbol : class Fish
    location: class com.emcher.animal.Animals
    Fish fish = new Fish();
    ^
    9 errors

    Tool completed with exit code 1
    Last edited by em1980; December 4th, 2011 at 11:20 AM.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    You can not define one method within another. There must be an ending } for the main() method BEFORE you can define another method: displayObject()

    Animals.java:17: cannot find symbol
    symbol : class Dog
    Where are the class definitions for Dog, Cat etc. The compiler can not find them.

  13. #12
    Junior Member em1980's Avatar
    Join Date
    Dec 2011
    Location
    Bethlehem, Pa
    Posts
    8
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    ok i figured it out on my own thought it was way more complicated than what it was i madethe display method i just never used it
    public class Animals {


    public static void main(String args[]){

    Dog dog = new Dog();
    dog.setAnimalType("dog");
    Cat cat = new Cat();
    cat.setAnimalType("cat");
    Fish fish = new Fish();
    fish.setAnimalType("fish");
    displayObject(dog, cat, fish);
    }
    public static void displayObject(Dog dog, Cat cat, Fish fish){
    System.out.println("this a " +dog.getAnimalType());
    System.out.println("this a " +cat.getAnimalType());
    System.out.println("this a " +fish.getAnimalType());
    }




    }
    sorry if i was coming off like an ahole i am very very new to programming. i have no previous experience and am trying to teach myself with a little help from a couple of friends
    Last edited by em1980; December 4th, 2011 at 12:02 PM.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    The way to get better and learn is to write lots of code.
    One practice that is useful is to compile frequently. Type in some code and compile it, fix the problems and continue. Don't type in too much code so you get so many errors all at once.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Tjstretch (December 4th, 2011)

  16. #14
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: how can i pass objects into a parameter and display them in the method

    Quote Originally Posted by Norm View Post
    The way to get better and learn is to write lots of code.
    One practice that is useful is to compile frequently. Type in some code and compile it, fix the problems and continue. Don't type in too much code so you get so many errors all at once.
    Don't know why nobody does this anymore.

  17. #15
    Junior Member em1980's Avatar
    Join Date
    Dec 2011
    Location
    Bethlehem, Pa
    Posts
    8
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how can i pass objects into a parameter and display them in the method

    well i know for myself i am learning from this website The Java™ Tutorials and the scja and scjp
    books and they give u all this code a small explanation on how to code it, then u get to the exercises and have no clue what to do i read a couple of things word for word 10 times like varargs and branching statements still don't have any clue how to code them i. i can answer a bunch of questions i just can't code

Similar Threads

  1. Replies: 3
    Last Post: November 25th, 2011, 02:02 PM
  2. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM
  3. how to pass value of table cell as parameter/attribute
    By ajincoep in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: February 8th, 2011, 09:51 AM
  4. How do you pass an Array as a Parameter?
    By Arius in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2010, 09:36 PM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM