Welcome to Dream.In.Code
Become a Java Expert!

Join 149,523 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,459 people online right now. Registration is fast and FREE... Join Now!




need help implementing an abstract method

 
Reply to this topicStart new topic

need help implementing an abstract method

Khazidhea
24 May, 2007 - 03:24 AM
Post #1

New D.I.C Head
*

Joined: 23 May, 2007
Posts: 4


My Contributions
Abstract class Pet has an array of Dose objects, as well as a subclass Dog

When I try to compile Dog.java, the first error i get (the problem I need the most help with) is:

Dog.java:5: cannot find symbol
symbol : constructor Pet()
location: class Pet
public class Dog extends Pet
^

I'm sorry if the answer can be found elsewhere in the forums, I've had a bit of a look, but havent seen (or at least recognised) the help i need

My code so far:

Pet.java:
CODE

public abstract class Pet
{
    private String name;
    private double weight;
    private Date dob;
    private double accumulatedDose;

    public Pet(String nameIn, double weightIn, Date dobIn, double accumulatedDoseIn)
    {
        this.name = nameIn;
        this.weight = weightIn;
        this.dob = dobIn;
        this.accumulatedDose = accumulatedDoseIn;
    }

    public abstract double getDose(double weight);

    public int getAgeInMonths(Date d)
    {
        Date now  = new Date();
        int nowYear = now.getYear();
        int nowMonth = now.getMonth();
        int thenYear = d.getYear();
        int thenMonth = d.getMonth();
        int yearDiff = nowYear-thenYear;
        int monthDiff = nowMonth-thenMonth;
        int ageInMonths = yearDiff*12 + monthDiff;
        return ageInMonths;
    }

}





Dog.java:
CODE

public class Dog extends Pet
{
    private String breed;
    
    public double getDose(double weightIn)
    {
    int age;
    Date dob = super.dob;
    double doseInMilligrams;
  
    age = super.getAgeInMonths(dob);

        if (ageInMonths < 3)
            doseInMilligrams = 0;
        else if (ageInMonths > 12 && weight < 2)
            doseInMilligrams = 6 + 0.75*weight;
        else
            doseInMilligrams = 12 + 0.65*weight;
    }


}



and Dose.java:
CODE

public class Dose
{
    private String nameOfDrug;
    private Date date;
    private double doseInGrams;

   public Dose(String nameOfDrugIn, Date dateIn, double doseInGramsIn)
    {
        nameOfDrug = nameOfDrugIn;
        date = dateIn;
        doseInGrams = doseInGramsIn;
    }

    public double getDose()
    {
        return doseInGrams;
    }

    public Date getDate()
    {
        return date;
    }

    public String toString()
    {
        return nameOfDrug;
    }

}



I realise that so far, most of my classes are incomplete, but any help and the pointing out of any mistakes so far would be appreciated.
Thanks

This post has been edited by Khazidhea: 24 May, 2007 - 04:10 AM
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Need Help Implementing An Abstract Method
24 May, 2007 - 04:09 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions

That error is just because you have not imcluded any constructor method for your dog class.
User is offlineProfile CardPM
+Quote Post

Khazidhea
RE: Need Help Implementing An Abstract Method
24 May, 2007 - 04:48 AM
Post #3

New D.I.C Head
*

Joined: 23 May, 2007
Posts: 4


My Contributions
thanks, i knew it was something simple, just a problem of staring at the code too long
User is offlineProfile CardPM
+Quote Post

Khazidhea
RE: Need Help Implementing An Abstract Method
24 May, 2007 - 07:36 AM
Post #4

New D.I.C Head
*

Joined: 23 May, 2007
Posts: 4


My Contributions
just one more problem
ive changed Dog.java to:
CODE

public class Dog extends Pet
{
    private String breed;

    public Dog(String breedIn)
    {
        this.breed = breedIn;
    }

    public double getDose(double weightIn)
    {
    int age;
    Date dob = super.dob;
    double doseInMilligrams;

    age = super.getAgeInMonths(dob);

        if (age < 3)
            doseInMilligrams = 0;
        else if (age > 12 && weight < 2)
            doseInMilligrams = 6 + 0.75*weight;
        else
            doseInMilligrams = 12 + 0.65*weight;
    }

}


and im getting the error:
Dog.java:10: cannot find symbol
symbol : constructor Pet()
location: class Pet
{
^





User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Need Help Implementing An Abstract Method
24 May, 2007 - 08:42 AM
Post #5

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
hi, I made some changes in which I import java.util.Date;
then in the constructor of the Dog class I added super();
Then I inserted get methods in the class Pet because dob and weight are private.
and here is all of it:

CODE
import java.util.Date;

public abstract class Pet
{
    private String name;
    private double weight;
    private Date dob;
    private double accumulatedDose;
    
    public Pet(){
    }

    public Pet(String nameIn, double weightIn, Date dobIn, double accumulatedDoseIn)
    {
        this.name = nameIn;
        this.weight = weightIn;
        this.dob = dobIn;
        this.accumulatedDose = accumulatedDoseIn;
    }

    public abstract double getDose(double weight);

    public int getAgeInMonths(Date d)
    {
        Date now  = new Date();
        int nowYear = now.getYear();
        int nowMonth = now.getMonth();
        int thenYear = d.getYear();
        int thenMonth = d.getMonth();
        int yearDiff = nowYear-thenYear;
        int monthDiff = nowMonth-thenMonth;
        int ageInMonths = yearDiff*12 + monthDiff;
        return ageInMonths;
    }
    public Date getDob()
    {
        return this.dob;    
    }
    public double getWeight()
    {
        return this.weight;    
    }

}

class Dose
{
    private String nameOfDrug;
    private Date date;
    private double doseInGrams;

   public Dose(String nameOfDrugIn, Date dateIn, double doseInGramsIn)
    {
        nameOfDrug = nameOfDrugIn;
        date = dateIn;
        doseInGrams = doseInGramsIn;
    }

    public double getDose()
    {
        return doseInGrams;
    }

    public Date getDate()
    {
        return date;
    }

    public String toString()
    {
        return nameOfDrug;
    }

}



class Dog extends Pet
{
    private String breed;
    private Date dob;
    private double weight;

    public Dog(String breedIn)
    {
        super();
        this.breed = breedIn;
    }

    public double getDose(double weightIn)
    {
    int age;
     dob = super.getDob();
     weight=super.getWeight();
    double doseInMilligrams;

    age = super.getAgeInMonths(dob);

        if (age < 3)
            doseInMilligrams = 0;
        else if (age > 12 && weight < 2)
            doseInMilligrams = 6 + 0.75*weight;
        else
            doseInMilligrams = 12 + 0.65*weight;
            
            return doseInMilligrams;
    }

}


User is offlineProfile CardPM
+Quote Post

Khazidhea
RE: Need Help Implementing An Abstract Method
24 May, 2007 - 06:40 PM
Post #6

New D.I.C Head
*

Joined: 23 May, 2007
Posts: 4


My Contributions
k, thanks for your help
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:45PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month