Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pet.java

Posted on 2010-08-28
5
288 Views
Last Modified: 2013-11-23
I need to write a Java program that inputs in a list a dog (name and weight) and a cat (name and coat color). Program will stop in put when word Stop is entered.Having issues and some errors. Please help. I have entered comments where my errors are occuring.

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;
}
public  static void main(String[] args){
	Pet pets = new Pet();
	Pet[] PetArray = new Pet[100];
	int count = 0;
	//insert pets
	while(Type != stop){ //error stop and type can not be resolved
		if //I want it to be if its a dog??
			PetArray[count++] = new Dog(dname, weight);//error "dog can not be resolved to type 
		else // if it is a cat??
		PetArray[count++] = new Cat(cName, cColor); // error cName, cColor resolved as a variable
	}
	
	Scanner scan = new Scanner(System.in);
	System.out.println("Please enter d to add a dog or c to add a cat");
	choice = Keyboard.readChar(); //error choice and Keyboard
	switch(choice){ //error 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;}
	}
	public static void addDog(){
		if(type==d){ //error on Type
			System.out.println ("Please enter name:  ");
			String dName = Keyboard.readString(); //error on keyboard
			System.out.println("Please enter weight of dog:  ");
			double weight = Keyboard.readdouble(); //error on keyboard
		}
	}
public static void addCat(){
	if(type ==c){ //error on type c
		System.out.println("Please enter name:   ");
		String cName = Keyboard.readString(); //error on keyboard
		System.out.println("Please enter the coat color of the cat:  ");
		String cColor = Keyboard.readString(); //error on keyboard
}
}
}

class Cat extends Pet {
private String coatColor;
public String getCoatColor(){
	return coatColor;
}
public void setCoatColor(String coatColor){
	this.coatColor = coatColor;
}
public Cat(){
	super();
	setType("c");
}
public Cat(String cColor){ //duplicate method
	super();
	setType("c)");
	setCoatColor(cColor);
}
public Cat(String cName){ //duplicate method
	super();
	setType("c");
	setName(cName);
}
class Dog extends Pet{

private double weight;

public double getWeight(){
	return weight;
}
public void setWeight (double weight){
	this.weight = weight;
}
public Dog(double weight){
	super();
	setType("d");
	setWeight(weight);
}
public Dog(){
	super();
	setType("d");}
}

Open in new window

0
Comment
Question by:Rowley4
  • 3
5 Comments
 
LVL 1

Expert Comment

by:ifoxbin
ID: 33552110
I suggest you write separet classes to do this job.

you can write
Pet.java
Cat.java
Dog.java
and
mainClass
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 33552411
This should get you started. Study what i wrote in capitals for what to do next
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();
	// NOW ADD A NEW INSTANCE OF Dog TO YOUR 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
            super();
            setType("c)");
            setCoatColor(cColor);
        }

        public String getCoatColor() {
            return coatColor;
        }

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

        /*
           public Cat(String cName){ //duplicate method
           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;
        }
    }
}

Open in new window

0
 

Author Comment

by:Rowley4
ID: 33554140
Now how would I get this to print the input in a list form?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33554419
You should implement toString in each class, then print the array
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33665377
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 63
HashTable highest marks enumeration alternative 9 43
how to debug htl and js pages 8 39
Tagging and Merging on Branch 1 29
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question