java abstract data types?

java abstract data types?
Hi I am writing a program for class using abstract data types,instance variables,constructor and mutator methods. The first part dogs compiles fine the second kennel gives me can not find symbol errors for all instances of dog and any place i have keyboard.nextInt; Thanks for any help!
{CODE}
public class Dogs2
{

private String name;
private String owner;
private String breed;
private int age;

public Dogs2()
{
name=”";
owner=”";
breed=”";
age=0;
}

public Dogs2(String name, String owner, String breed, int age)
{
this.name=name;
this.owner=owner;
this.breed=breed;
this.age=age;
}

public Dogs2(Dogs2 someDog)
{
this.name=someDog.getName();
this.owner=someDog.getOwner();
this.breed=someDog.getBreed();
this.age=someDog.getAge();
}

public String getName()
{
return name;
}
public String getOwner()
{
return owner;
}
public String getBreed()
{
return breed;
}
public int getAge()
{
return age;
}

public int computeYearsToLive()
{
int toLive;
if(breed.equalsIgnoreCase(“german”))
{
toLive=15-age;
}
else if(breed.equalsIgnoreCase(“boxer”))
{
toLive=11-age;
}
else if(breed.equalsIgnoreCase(“terrier”))
{
toLive=8-age;
}
else if(breed.equalsIgnoreCase(“doberman”))
{
toLive=13-age;
}
else
{
toLive=14-age;
}
return toLive;
}
}
{CODE}

{CODE}

import java.util.Scanner;

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

String Name=”",Owner=”", Breed=”", output=”";
int Age, c, counter;
dog dogArray[] = new dog[10];

for( c=0 ; c <10; c++)
{
Name=keyboard.nextInt.System.out.println("Enter name of dog, type quit when done.");
if(Name.equalsIgnoreCase("quit"))
{
break;
}
Owner=keyboard.nextInt.System.out.println("Enter name of owner of dog");
Breed=keyboard.nextInt.System.out.println("Enter breed of dog");
Age=Integer.parseInt(keyboard.nextInt.System.out.println("Enter age of dog"));
dogArray[c] = new Dogs2(Name, Owner ,Breed, Age);
}
while(true)
{
Name=keyboard.nextInt.System.out.println("Enter name of dog to display, type quit to exit.");
if(Name.equalsIgnoreCase("quit"))
{
break;
}
for( counter =0 ; counter < c; counter++)
{
if(Name.equalsIgnoreCase(dogArray[counter].getName()) )
{
keyboard.nextInt.System.out.println(null, "Your dog info is:\n"
+ "Name: "+dogArray[counter].getName() +
"\nOwner Name: "+dogArray[counter].getOwner() +
"\nBreed: "+dogArray[counter].getBreed()+
"\nAge: "+dogArray[counter].getAge()+
"\nYear Left to Live: "+
dogArray[counter].computeYearsToLive() );
break; //exit for loop
}//end if
}
if (counter==c)
{
keyboard.nextInt.System.out.println(null, "Dog not found! He must have died.");
}
}//end while
keyboard.nextInt.System.out.println(null, "So long!");
System.exit(0);
}
}
[CODE]

Best answer:

It’s quite obvious by your code that you have not been attending class. Pardon my pun, but your code is a bunch of dog shit thrown together. Probably in the hopes that someone will rewrite it all for you. But since I wasted 5 minutes of my life looking at your garbage, the least I could do is give you a starting point on where to go from here…

First of all, you declared Name, Owner, and Breed as strings, yet your miserably failed attempts to receive input for those variables calls the keyboard.nextInt….You can not put an integer into a string obviously. The only one that would work for is age. Second of all, leave the System.out.println’s on their own line. Thirdly, it is good practice to follow standard coding principles…variable names should start with lowercase letters (eg. owner, breed, age)…You have a random “type quit when done”…Well the loop will finish after 10 iterations anyway unless you want to give the user an option to exit before 10 iterations are complete…If that’s the case, than it could be handled more appropriately. I did not even look beyond that code. In the mean time, I have provided a little reworking of that loop. It should hopefully be enough to help you get moving again. But seriously, go back to class…You need it.

for( c=0 ; c < 10; c++)
{
Name=keyboard.next();
System.out.println(Name + " type quit when done.");
if(Name.equalsIgnoreCase("quit")) break;
Owner=keyboard.next();
System.out.println(Owner);
Breed=keyboard.next();
System.out.println(Breed);
Age=keyboard.nextInt();
System.out.println();
dogArray[c] = new Dogs2(Name, Owner, Breed, Age);
}

Tags: , , ,

Under Forum

2 Comments for java abstract data types?

  • 1. deonejuan  |  April 23rd, 2007 at 12:58 am

    Abstract Data TYPEs is another concept in CIS. You are faced with abstract CLASS. Start over. I used pastebin.com because Y! Answers does not seem to like long listings.

    Basically if we
    abstract class Fish {var,var,var}
    class FreshwaterFish extends Fish{ we have vars of Fish}
    class SaltwaterFish extends Fish{ we have vars of Fish }
    then…
    Fish[] fishes = new Fish[22];
    we can put both kinds into an array and then…
    loop through fishes with one operation

    good luck

  • 2. Stardawg  |  April 23rd, 2007 at 1:24 am

    It is obvious you missed a ton of class or slept through it. you arent even touching on abstract data types in your code which i would assume is the entire idea of the homework assignment. you should be asking a teacher or classmate to help catch you up, but i’ll give you a few pointers.

    you are calling a System.out.println as if its part of the scanner.nextInt method thats the main cause of your problems. a good fix to this would be something like this:

    System.out.println(“Age of the dog: “);
    int age = keyboard.nextInt() ;

    you will also encounter another problem when you try to get input for your strings owner, name and breed. use something like:
    String owner = keyboard.nextLine();

    your other problems stem from you not having a dog class, which is because you aren’t approaching this having learned abstract data types. create a dog class with the mutator methods and the one abstract method computeYearsToLive() and then have your sub classes being the breeds implementing the one and only abstract method ie:

    public abstract class Dog
    {
    private int age;
    private String owner, name, breed;
    // Dont forget your constructor and mutators
    public abstract int computeYearsToLive();
    }

    public class Doberman extends Dog
    {
    public Doberman(String name, String owner, int age)
    {
    super(name,owner,”Doberman”,age);
    }
    public int computeYearstoLive()
    {
    return 13 – getAge();
    }
    }

    so now when you get all the input from the user you can insert a new Doberman into the dog array. like so:

    if (breed.equalsIgnoreCase(“doberman”))
    {
    dog[counter] = new Doberman(name, owner, age);
    }

    an abstract class like Dog in my example is simply a class that has both set methods and abstract methods, which must be implemented inside the sub classes one of which will be Doberman.

    an abstract method is just a rough idea of what the method is, for example with computeYearsToLive() you are telling the sub class that you don’t care how it ends up computing the years to live just return it as an int. so we can store all of the types of dogs in a Dog array that will end up being able to use this abstract method

Leave a Comment for java abstract data types?

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories