java - Creating an object from another class -


i've spent time trying solve this, i'm missing because can't code work correctly. have program 3 class - person, doctor , consoleapplication. person holds constructor information firstname, lastname, phonenumber etc. doctor subclass of person information need medical specialty - i'm aiming inherit information in person object.

the conesoleapplication class handles creation of objects person , doctor classes, in addition giving user way modify objects through input. i've managed spawn objects of persons in class , store them in array, can't figure out how same doctor objects.

here's example of code addnewdoctor method in conesoleapplication class. don't provide inputs fields or store object in array because i'm trying test if works.

    private static void addnewdoctor() {     int personid = 0;     person person;          system.out.print("please enter identification number of person becoming doctor: ");      try {         personid = input.nextint();         input.nextline();     }     catch (inputmismatchexception e) {             system.out.println("oops!! please enter integral numbers");             system.out.println(input.next() + " not valid input.");     }      person = getpersonid(personid);      if (null == person)         system.out.println ("this person cannot found \n");     else {          string spec = null;         string firstname = null;         string lastname = null;         string homeaddress = null;         string phonenumber = null;          firstname = person.getfirstname(firstname);          system.out.print ("enter speciality of doctor: ");           spec = input.nextline();          doctor b = new doctor(firstname, lastname, homeaddress, phonenumber, spec);         system.out.println(b);      }  } 

this code result in

identification number: 0 name: null surname: null address: null mobile/telephone: null 

i hoping object created correct firstname , specialty, apparently i've done wrong because don't appear there. don't understand why speciality (spec) field doesn't appear either.

here's working code have creating objects of person class - thought i'd include reference.

        private static void addnewperson() {         string firstname, lastname, homeaddress, phonenumber;         input.nextline();         system.out.print ("enter first name of person: ");         firstname = input.nextline();              system.out.print ("enter last name of person: ");         lastname = input.nextline();          system.out.print ("enter home address of person: ");         homeaddress = input.nextline();          system.out.print ("enter phone number of person: ");         phonenumber = input.nextline();          persons[amountpersons] = new person (firstname, lastname, homeaddress, phonenumber);         amountpersons++;          system.out.print ("the new person has been added. " + "\n"                             + "" + "\n" );                } 

here's constructors person , doctor classes.

person

// default constructor initialize default instance variables public person() { firstname = " "; lastname = " "; homeaddress = " "; phonenumber = " "; personnumber = 0; }  //overloaded constructor designed objects spawn   public person (string firstname, string lastname, string homeaddress, string phonenumber) { // initialize instance variables this.firstname = firstname; this.lastname = lastname; this.homeaddress = homeaddress; this.phonenumber = phonenumber; personnumber = numbersystem; numbersystem = numbersystem + 1; } 

doctor

    public doctor(string firstname, string lastname, string homeaddress, string phonenumber, string spec) {     //constructor inherited person information , new doctor information     super(firstname, lastname, homeaddress, phonenumber);     spec = speciality;     int doctorid = 0;     personnumber = doctorid;     } 

is there brave soul out there willing read , put me on correct path? i've gotten frustrated trying figure out why can't create objects of doctor correctly. :(

you haven't posted fields doctor class, seems speciality field , spec parameter passed constructor. instead of

spec = speciality; 

it should be:

speciality = spec; 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -