Firstly thanks taking the time to help me out.
I can compile and run this program but where I want to print the Dogs name and age (with the toString method) it prints only the age, returning "null" for name.
//Dog.java //represents a dogs age and name and contains methods to do several //things. public class Dog { private String name; private int age; //constructor: initializes the instance data name and age: public Dog () { age = 14; } //getter public int getAge () { return age; } //setter public void setAge (int value) { value = age; } //getter public String getName () { return name; } //setter public void setName (String words) { name = words; } //toString method public String toString() { return name + "\t" + age; } }
and the driver class:
//Kennel.java //main method instantiates and updates several Dog objects. public class Kennel { public static void main (String[] args) { Dog Scruffy, Jade; Scruffy = new Dog (); Scruffy.getName(); System.out.println ("Scruffys age in human years: " + Scruffy.getAge()); System.out.println (Scruffy); } }
and when running the program i get:
I want the dogs name to show as 'Scruffy'.craig@craig-laptop:~/Documents/panda/java2$ java Kennel Scruffys age in human years: 14 null 14