I need to write an Array List of Pets. An item list is either a Dog or a Cat. For each pet enter a name and type (C for cat or D for dog). Input should stop when the word STOP is entered for the name. I need to also have the Dog class to include an instance variable for weight(double) and Cat to include an instance variable coatColor(string). The user will basically enter name and type. If it is cat they input its coat color. If it is a dog input its weight for each pet. Output into a list.

This is what I have so far I need help finishing this and the main:

import  java.io.*;
import  util.*;

    class Pet  {
           public static void main(String[ ] args)  throws IOException {

     class Cat extends Pet {

      Cat [ ]    cat = new Cat[2];    
      public String      coatColor(), name();
      public char          type("c");
   }

     class Dog extends Pet{

     Dog[ ]   dog = new Dog[2];
     public String     name();
     public char        type("d");
     public double    weight();
  }

    public int        getWeight( ) {
      return weight;
   }

    public char    getType( ) {
       return type;
  }

    public String  getName( ),  getCoatColor( ) {
       return name, coatColor;
  }
 
    public void     setWeight( double weight) {
        weight = newWeight;
   }

    public void     setType(char type) {
       type = newType;
   }

    public void     setName(String name), setCoatColor(String coatColor) {
         name = newName
            coatColor = newCoatColor;
   }

    for (int i = 0;  i < pet.length; i ++);

I need help finishing this and the main

Please ask questions about your problems.
What errors do you get when you try to compile your program?
Please copy and paste the full text here.

A comment on the code shown:
for (int i = 0; i < pet.length; i ++); // this loop does nothing???

You can not define a class inside of a method.

Yes, this is where I am getting stuck. I need help with my main.

Please list the steps that need to be done in the main method.
Make a simple list, sort of like pseudo code. Then try to write the code for each step.
Do one step at a time, get it to compile and execute, and then move to the next one.

Okay. I have changed this around. It is messy with lots of errors. Can you help with my errors?

import java.io.*; 
import java.util.*;

public class Pet{ 
protected String name;
protected int weight;

class Cat extends Pet {

Cat [ ] cat = new Cat[2];
public Cat(String Catname, coatColor){ //error on coatColor Do not understand why
protected String coatColor;
protected String Catname;

public char type("c");// error "over rides Pet.type" 
cat c;

}

class Dog extends Pet{
Dog[ ] dog = new Dog[2];
public Dog(String name, int weight){
}
protected int weight; 	
}
}

public char type("d"); // error same as above on "c"


public double weight(){

}
public int getweight() {
return weight;
}
public char getType() {
return type; //error can not be resolved to a variable
}

public String getName(), getcoatColor() { // error on getcoatColor()
return name, coatColor; //error on coatColor is not a field
}
public void setweight(double weight) {
weight = newweight; //error newweight can not be resolved to a variable
}

public void setType(char type) {
type = newType; //error newType can not be resolved to a variable

}
public void setName(String name), setcoatColor(String coatColor) { //error secoatColor() return type method missing
name = newname; //can not be resovled as a variable
coatColor = newcoatColor; // can not be resovled as a variable



public static void main(String[ ] args) 
throws IOException {
	int count = 0;
	Pet myPet = new Pet();
	Pet[] PetArray = new Pet[100];
	
	while(Type != stop){ //error stop and type can not be resovled
		if //I want it to be if its a dog??
			PetArray[count++] = new Dog(name, weight);//error dog resolved as a type & static non-static name
		else // if it is a cat??
		PetArray[count++] = new Cat(name, coatColor); //error static ref to non-static 
	}
	
	Scanner scan = new Scanner(System.in);
	System.out.println("Please enter d for dog or c for cat");
	choice = Keyboard.readChar(); //error choice and Keyboard
	switch(choice){ //error choice
	case 'd':
		System.out.println("Adding a Dog");
		addDog();
		break;
	case 'c':
	System.out.println("Adding a Cat");
	addCat();
	break;}
	}
	public static void addDog(){
		if(Type==d){ //error on Type
			System.out.println ("Please enter name:  ");
			String name = Keyboard.readString();
			System.out.println("Please enter weight of dog:  ");
			int weight = Keyboard.readint();
		}
	}
public static void addCat(){
	if(Type ==c){
		System.out.println("Please enter name:   ");
		String nameCat = Keyboard.readString();
		System.out.println("Please enter the coat color of the cat:  ");
		String coatColor = Keyboard.readString();
	}
}

}

help with my errors

yes. You need to post them.

Your code looks like you wrote each line on a piece of paper and then shuffled them before typing them in. There is no order to any of the statements.

Start with one class:
Enter the definition line ending with an open {
Enter all the class varibles.
Enter the constructor(s).
Enter the classes methods with beginning and ending {}s.
Enter the ending } for the class.

Repeat for all the classes in the program.

Edited 6 Years Ago by NormR1: n/a

NormR1 wrote:

You can not define a class inside of a method.

Sorry Norm, that's wrong. Just try this:

void m() {
       class C {
       }
       new C();
    }

You can define a class in a method, and its scope is the method, just like a variable defined in the method. It's not often done with named classes, but it's very common with anonymous inner classes.

I will see what I can do. I am just brand new to Java and programming in general. I am trying....

Can you tell me why it says in the errors (my errors are pointed out as comments after each error) I keep getting cant be resolved as a variable. It is mainly on "Type"

why it says in the errors

There is useful information in the text of the error messages. You've left off some of it when you added the comments to your code.

Please copy and paste here the FULL text of the error messages.

OK, how about THIS!?! Please help with my double method error in public Cat() please. And adding an instance to dog. I show it in quotes. PLEASE!!

import java.util.*;


public class Pet {
    private static Scanner scan = new Scanner(System.in);
    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;
    }

    public static void main(String[] args) {
        Pet pets = new Pet();
        Pet[] PetArray = new Pet[100];
        int count = 0;

        boolean finished = false;

        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;
            }
        }
    }

    public static void addDog() {
        System.out.println("Please enter name:  ");

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

        double weight = scan.nextDouble();
	
// ADD A NEW INSTANCE OF Dog TO ARRAY
    }

    public static void addCat() {
        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 Cat extends Pet {
        private String coatColor;

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

        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;
        }
       
          public Cat(String cName){ //duplicate method HOW DO I CHANGE THIS? 
           super();
           setType("c");
           setName(cName);
           }
          
    }

    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;
        }
    }
}

Edited 6 Years Ago by rowley4: n/a

You need to make each constructor's arguments unique. Otherwise the compiler is confused. How can the compiler tell if the String you are passing contains the name of a color or the name of the pet? That's the problem. A String is a String. The compiler is not able to look at the contents of the String because it is not set until execution.
The compiler would not be able to know which constructor to use if there are two with the same args.

Change the design so that every constructor has unique arguments.

To print in a list(I assume you mean a vertical column)
create a loop and print one item on each iteration of the loop with an ending newline character

Edited 6 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.