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

Thread: Help with storing objects in array? :(

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with storing objects in array? :(

    I am trying to store object pet into an array then I'll make an arraylist out of the array, is there a better way to do this or is there any way
    someone can help me make this program work at all? thanks!

    Write a program that creates an ArrayList of pets. An item in the list is either a Dog or a Cat, For each pet, enter its name and type('c' for
    cat and 'd' for dog). Stop the input when the string stop is entered for the name. After the input is complete, output the name and type for
    each pet in the list.

    import java.util.*;

    public class Chapter13Ex4{
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    Pet pets = new Pet();

    int count =0;

    //System.out.println("How many animals would you like to add?");
    //int N = scan.nextInt();

    boolean finished = false;

    Pet[] PetArray = new Pet[100];

    while (!finished) {
    System.out.println(
    "Please enter d to add a dog or c to add a cat ('Stop' to end)");

    char choice = scan.next().charAt(0);

    switch (choice) {

    case 'd':
    System.out.println("You want to add a Dog");
    addDog();

    break;

    case 'c':
    System.out.println("You want to add a Cat");
    addCat();

    break;

    default:
    finished = true;
    }


    count = count + 1;

    }



    System.out.println(PetArray[0]);




    }

    public static void addDog() {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter name: ");

    String dName = scan.next();
    System.out.println("Please enter weight of dog: ");

    double weight = scan.nextDouble();

    }

    public static void addCat() {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter name: ");

    String cName = scan.next();
    System.out.println("Please enter the coat color of the cat: ");

    String cColor = scan.next();
    }


    class Dog extends Pet {
    private double weight;

    public Dog(double weight) {
    super();
    setType("d");
    setWeight(weight);
    }

    public Dog() {
    super();
    setType("d");
    }

    public double getWeight() {
    return weight;
    }

    public void setWeight(double weight) {
    this.weight = weight;
    }


    }

    class Cat extends Pet {
    private String coatColor;

    public Cat() {
    super();
    setType("c");
    }

    public Cat(String cName){ //duplicate method HOW DO I CHANGE THIS?
    super();
    setType("c");
    setName(cName);
    }

    public Cat(String cColor) { //duplicate method HOW DO I CHANGE THIS?
    super();
    setType("c");
    setCoatColor(cColor);
    }

    public String getCoatColor() {
    return coatColor;
    }

    public void setCoatColor(String coatColor) {
    this.coatColor = coatColor;
    }



    }



    }

    __________________________________

    import java.util.*;
    public class Pet {
    private String name;
    private String type;
    private Pet[] pets;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getType() {
    return type;
    }

    public void setType(String type) {
    this.type = type;
    }

    public Pet[] getPets() {
    return pets;
    }

    public void setPets(Pet[] pets) {
    this.pets = pets;
    }



    }


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

    Default Re: Help with storing objects in array? :(

    store object pet into an array then I'll make an arraylist out of the array,
    Do you need the array or could you store the pet objects into the arraylist directly?

    If you have any specific questions or problems please post them.
    If any errors, please copy and paste the full text here.

Similar Threads

  1. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM
  2. Storing data from a file in array and setting it to textbox
    By macko in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 13th, 2011, 09:17 PM
  3. Reading .txt and storing into an array
    By vluong in forum Collections and Generics
    Replies: 1
    Last Post: January 4th, 2010, 02:07 PM
  4. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM

Tags for this Thread