java - How do I define the toString in this program. It is supposed to return the super hero based on the favorite color -


i need finish programming assignment in 1 hour. have done of it, cannot understand how complete tostring @ end. program find users favorite super hero.

here driver class

    /*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */  package superherotester; import java.util.scanner;  /**  *  * @author charters  */ public class superherotester {     public static superhero asuperhero;      /**      * program       */     public static void main(string[] args)     {         getdatafromuser();         determinesuperheroidentity();     }      public static void getdatafromuser()     {         scanner keyboard = new scanner(system.in);         system.out.println("what first name?");         string firstname = keyboard.nextline();         system.out.println("what last name?");         string lastname = keyboard.nextline();         system.out.println("what super power?");         string superpower = keyboard.nextline();         system.out.println("what favorite color?");         string favoritecolor = keyboard.nextline();          asuperhero = new superhero(firstname, lastname, superpower, favoritecolor);      }      public static void determinesuperheroidentity()     {         system.out.println(asuperhero);          if (asuperhero.getfavoritecolor().equalsignorecase("red"))         {             system.out.println("you must spiderman!");         }         else if (asuperhero.getfavoritecolor().equalsignorecase("green"))         {             system.out.println("you must green goblin!");         }         else if (asuperhero.getfavoritecolor().equalsignorecase("black"))         {             system.out.println("you must batman");         }         else if (asuperhero.getfavoritecolor().equalsignorecase("blue"))         {             system.out.println("you must captain america");         }         else if (asuperhero.getfavoritecolor().equalsignorecase("purple"))         {             system.out.println("you must magneto");         }      }  } 

and here domain class need finish. tostring @ end of domain class.

/*  * describe program does.  */ package superherotester;  /**  *  * @author   */ public class superhero {    //define attributes here: string firstname; string lastname; string superpower; string favcolor;       //define constructor here: public superhero(string firstname, string lastname, string superpower, string favcolor)  { this.firstname = firstname; this.lastname = lastname; this.superpower = superpower; this.favcolor = favcolor; }          //define setters , getters here:  public string getfirstname() { return firstname; } public string getlastname() { return lastname; } public string getsuperpower() { return superpower; } public string getfavcolor() { return favcolor; }   public void setfirstname(string afirstname) {     firstname = afirstname; }  public void setlastname(string alastname) {     lastname = alastname; }  public void setsuperpower(string asuperpower) {     superpower = asuperpower; }  public void setfavcolor(string afavcolor) {     favcolor = afavcolor; }        //define tostring here:    } 

you're mistaken on tostring() supposed do.

how define tostring in program. supposed return super hero based on favorite color

the tostring() method has nothing searching superhero.

the program find users favorite super hero.

yes, program this, again tostring() has nothing this.


instead tostring() supposed return string representation of superhero object, no more , no less. output string holds contents of key fields of class:

string firstname; string lastname; string superpower; string favcolor; 

and that's it.

myself, i'd create stringbuilder object , append variable name followed variable state, , return tostring() stringbuilder.


Comments

Popular posts from this blog

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

wordpress - (T_ENDFOREACH) php error -

Using django-mptt to get only the categories that have items -