Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Concept check
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: Wed Jan 03, 2007 6:37 pm   Post subject: Concept check

I was fiddling around with java concepts and ran into a snag.

code:
import java.util.Scanner;

public class Main {

        public static void main(String[] args) {

                String choice = new String();
                String tempName = new String();
                String tempWeight = new String();
                String tempBreed = new String();
                String tempDogBreed = new String();
                Pet newPet = new Pet();
                boolean stop = true;

                Scanner s = new Scanner(System.in);

                while (!stop) {

                        System.out.println("Pet manager program\n");
                        System.out.println("1. Add new pet");
                        System.out.println("2. Tell pet to speak");
                        System.out.println("3. Tell pet to eat");
                        System.out.println("4. Print pet information");

                        if (s.hasNext())
                                choice = s.next();

                        if (choice == "1") {

                                System.out.println("What is the pet's name? ");
                                if (s.hasNext())
                                        choice = s.next();
                                tempName = choice;

                                System.out.println("What is the pet's weight? ");
                                if (s.hasNext())
                                        choice = s.next();
                                tempWeight = choice;

                                System.out.println("What is the pet's breed? ");
                                if (s.hasNext())
                                        choice = s.next();
                                tempBreed = choice;

                                if (tempBreed == "dog") {
                                        System.out.println("What kind of dog? ");
                                        if (s.hasNext())
                                                choice = s.next();
                                        tempDogBreed = choice;
                                       
                                        // Create new pet or new dog?
                                }

                                else {
                                        newPet = new Pet(tempName, Integer.parseInt(tempWeight),
                                                        tempBreed);
                                }
                        }

                        else if (choice == "2") {
                                newPet.speak();
                        }
                       
                        else if (choice == "3") {
                                newPet.eat();
                        }

                }

        }

}

public class Pet {

        public String name;
        public int weight;
        public String breed;

        public Pet(){
               
        }
       
        public Pet(String startName, int startWeight, String startBreed) {
                name = startName;
                weight = startWeight;
                breed = startBreed;
        }
       
        public void speak() {
                System.out.println("RAWR!");
        }
       
        public void setName(String newName) {
                name = newName;
        }
       
        public void setWeight (int newWeight){
                weight = newWeight;
        }
       
        public void eat(){
                System.out.println("My crave for sustenance has been quenched.");
                weight += 2;
        }
       
        public void printInfo(){
                System.out.println("Name: " + this.name);
                System.out.println("Weight: " + this.weight);
                System.out.println("Breed: " + this.breed + "\n");
        }
       
}

public class Dog extends Pet {

        public String dogBreed;

        public Dog(String startName, int startWeight, String startBreed, String startDogBreed) {
                super(startName,startWeight,startBreed);
                dogBreed = startDogBreed;
        }
       
        public void Speak (){
                System.out.println("BOWOWOWOWWOW!");
        }
       
        public void Eat (){
                System.out.println("Mmmmm Kibbles and Bits!");
                this.weight += 1;
        }

}




How would I use the main method to handle any type of pet? Assuming I had 10 different kinds of animals, would I have to do an if statement for each animal's speak command?
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Wed Jan 03, 2007 8:21 pm   Post subject: (No subject)

I believe you would need abstract classes for this. In your case, your class Pet would be an abstract class rather than a normal one. Rizzix wrote a nice little tutorial on abstract classes.
Tubs




PostPosted: Thu Jan 04, 2007 6:48 pm   Post subject: (No subject)

I can't create an instance of the type Pet if its an abstract class.
wtd




PostPosted: Thu Jan 04, 2007 8:29 pm   Post subject: (No subject)

A fundamental conceptual mistake is occurring here.

code:
choice == "1"


Strings in Java are objects. This means that == does not check for structural equality, but rather referential equality. This will only be true if the two objects are the same.

Two Strings can be structurally identical without being referentially identical.
Tubs




PostPosted: Thu Jan 04, 2007 11:02 pm   Post subject: (No subject)

I haven't looked at runtime logic errors yet, I'm more concerned about the abstract classes. Thanks for the tip though.
HellblazerX




PostPosted: Fri Jan 05, 2007 3:17 pm   Post subject: (No subject)

Tubs wrote:
I can't create an instance of the type Pet if its an abstract class.

I suppose you could create a separate subclass for Generic Pets, but other than that, I don't think there's any other of doing it with abstract classes.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: