Results 1 to 6 of 6
  1. #1
    xx__rose is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default How can I display these objects from another class?

    I'm writing a program that reads information from three seperate classes. Here is my code:
    Java Code:
    public class Animal
    {
       protected int id;
       protected String type;
       protected double mass;
     
        //------------------------------------------------------------------------
        //  Sets up an animal with the specified ID number, type and weight.
        //------------------------------------------------------------------------
     
       public Animal (int animalID, String animalType, double weight)
       {
          id = animalID;
          type = animalType;
          mass = weight;
       }
     
       //------------------------------------------------------------------------
       //  Returns information about this animal as a string.
       //------------------------------------------------------------------------
     
       public String toString()
       {
          String description = "ID: " + id + "\n";
     
          description += "Type: " + type + "\n";
          description += "Weight: " + mass + "\n"; 
     
          return description;  
       }
    }
    Java Code:
    public class Pet extends Animal
    {
       private String title;
       private String own;
     
       //------------------------------------------------------------------------
       //  Sets up a pet using the specified information.
       //------------------------------------------------------------------------
     
       public Pet (int animalID, String animalType, double weight, String name, String owner)
       {
          super (animalID, animalType, weight);
     
          title = name;
          own = owner;
       }
     
       //------------------------------------------------------------------------
       //  Returns information about this pet as a string.
       //------------------------------------------------------------------------
     
       public String toString()
       {
          String description = super.toString();
     
          description += "Name: " + title + "\n";
          description += "Owner: " + own + "\n"; 
     
          return description;  
       }
    }
    Java Code:
    public class ZooAnimal extends Animal
    {
       private int cage;
       private String train;
     
       //------------------------------------------------------------------------
       //  Sets up a zoo animal using the specified information.
       //------------------------------------------------------------------------
     
       public ZooAnimal (int animalID, String animalType, double weight, int cageNumber, String trainer)
       {
          super (animalID, animalType, weight);
     
          cage = cageNumber;
          train = trainer;
       }
     
       //------------------------------------------------------------------------
       //  Returns information about this zoo animal as a string.
       //------------------------------------------------------------------------
     
       public String toString()
       {
          String description = super.toString();
     
          description += "Cage: " + cage + "\n";
          description += "Trainer: " + train + "\n";
     
          return description;
       }
    }
    Java Code:
    import java.util.Scanner;
    import java.util.StringTokenizer;
    import java.io.*;
    
    public class FileRead
    {
       public static void main (String[] args) throws FileNotFoundException
       {
          Scanner scan = new Scanner(new File("animal.txt"));
          String animalType = null, name = null, trainer = null, owner = null, lineIn = null;
          int animalID = 0, cageNumber = 0;
          double weight = 0.0;
          StringTokenizer st = null;
          Animal animal = null;
          Pet pet = null;
          ZooAnimal zooAnimal = null;
    
          while (scan.hasNextLine())
          {
             lineIn = scan.nextLine();
             st = new StringTokenizer(lineIn, ",");
             animalID = Integer.parseInt(st.nextToken());
             animalType = st.nextToken();
             weight = Double.parseDouble(st.nextToken());			
    
             if (animalID <= 8000 && animalID > 9999)
             {
                cageNumber = Integer.parseInt(st.nextToken());
                trainer = st.nextToken();
                zooAnimal = new ZooAnimal(animalID, animalType, weight, cageNumber, trainer);
                System.out.println(zooAnimal);
             }
    			
    	 if (animalID <= 3000 && animalID > 7999)
             {
                name = st.nextToken();
                owner = st.nextToken();
                pet = new Pet(animalID, animalType, weight, name, owner);
                System.out.println(pet);
             }
    			
             else
    	 {
                animal = new Animal (animalID, animalType, weight);
                System.out.println(animal);
             }
          }
       }
    }
    The following code segment is animal.txt

    Java Code:
    3000,Monkey,38.6
    7999,Ape,65.2,
    8000,Dog,13.4,Thomas,George,
    5252,Giraffe,130.3,103,Samuel,
    I want my program to be able to distinguish the different ID numbers from the above classes, so it knows which ID number is an animal, pet or zoo animal. The ID number between 1000 and 2999 is an animal, the ID number between 3000 and 7999 is a pet and an ID number between 8000 and 9999 us a zoo animal. Any ID number which is less than 1000 or greater than 9999 should be considered invalid records. That way, it will know when to display a "zoo animal", a "pet" or an "animal". Whenever I run my program, all it displays the ID number, the animal type and the weight. The below is what I'm trying to aim for:

    ID: 3000
    Type: Monkey
    Weight: 38.6

    ID: 7999
    Type: Ape
    Weight: 65.2

    (The above from the animal class)

    ID: 8000
    Type: Dog
    Weight: 13.4
    Name: Thomas
    Owner: George

    (The above from the pet class)

    ID: 5252
    Type: Giraffe
    Weight: 130.3
    Cage Number: 103
    Trainer: Samuel

    (The above from the zoo animal class)

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You don't need to store the type of the animal. The class used for creating the object gives you that. Set it in the toString method

    e.g toString of Animal should be
    Java Code:
    public String toString()   {
          String description = "ID: " + id + "\n";
           description += "Type: Animal\n"; //we know we are in the animal class
           description += "Weight: " + mass + "\n"; 
           return description;  
       }
    The the toString of the ZooAnimal class would have
    Java Code:
    public String toString()
       {
          String description = super.toString();
     
          description += "Cage: " + cage + "\n";
          description += "Trainer: " + train + "\n";
          description += "Type: ZooAnimal\n";
     
          return description;
       }
    I'm sure you now know what the toString of Pet should now look like.
    That way you don't lose the power of inheritance.

  3. #3
    xx__rose is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    The "Type" actually specifies what type of animal it is, hench the name "animalType". It has nothing to do with the name of the classes.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I see, well then your if statements in the main method should do the job.
    They seem to be wrong though e.g if (animalID <= 8000 && animalID > 9999) can never be true, can it?
    Let me attempt the first one for you.
    Specs say: The ID number between 1000 and 2999 is an animal.

    First a bit on the structure, since everything extends Animal, you only need one Animal variable in your main method at the top.
    so you have
    Java Code:
    Animal animal = null;
    Then you read the line details from the file and do
    Java Code:
    if (animalID > 1000 && animalID < 30000) {//we have an Animal
        animal = new Animal(/*... your animal details here*/)
    
    }
    when you encounter, say, Pet later you simply do
    Java Code:
    animal = new Pet(/*... your pet details here*/)

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Um...you don't think it might have been, you know, polite to mention on the other thread that you'd actually solved your problem?

  6. #6
    xx__rose is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    I got it, thank you for your time and patience, r035198x.

    And you're right, Tolls. Pardon my ignorance.

Similar Threads

  1. deleting objects? how to free a class (JVM)
    By Sungron in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 02:07 PM
  2. Send selective objects in a class over network
    By RDReavis in forum Advanced Java
    Replies: 2
    Last Post: 01-18-2010, 08:45 AM
  3. Replies: 1
    Last Post: 01-03-2010, 09:48 PM
  4. SWT Display class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 12:19 PM
  5. Getting objects of a class
    By ravian in forum New To Java
    Replies: 1
    Last Post: 12-04-2007, 12:23 PM

Posting Permissions

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