Beginner Java: Returning values from methods -


before begin i'd state i'm beginner when comes writing code in general apologise if i'm asking may seem extremely basic.

with being said i'm struggling returning methods in code. able write program without splitting methods have told when coding practice split in methods debugging can easier.

the following code seems have few major flaws.

public static boolean hybridnot() {   string typecar = input("hybrid or electric?");   boolean electric = false;   if (typecar.equalsignorecase("electric"))  {     electric = true;  }   else if (typecar.equalsignorecase("hybrid"))  {     electric = false;  }   else  {     print("sorry didn't understand that");  }   return; }  public static boolean solarnot() {    string panelsmaybe = input("solar panel?");    boolean solarpanel = false;     if (panelsmaybe.equalsignorecase("yes"))    {       solarpanel = true;    }     else if (panelsmaybe.equalsignorecase("no"))    {       solarpanel = false;    }     else    {       print("sorry didn't understand that");    }     return;   }   public static int discountnot() {     final int basicprice = 20000;     final int electriccost = 2000;     final int solarcost = 5000;     boolean electric = hybridnot();     boolean solarpanel = solarnot();     int totalprice;      if ((solarpanel = true) || (electric = true))     {        totalprice = basicprice + solarcost + electriccost - 500;     }      else if ((solarpanel = true) || (electric = false))     {        totalprice = basicprice + solarcost;     }      else if ((solarpanel = false) || (electric = true))     {        totalprice = basicprice + electriccost;     }      else     {        totalprice = basicprice;     }      return;     }  public static void totalcost() {    final int basicprice = 20000;    final int electriccost = 2000;    final int solarcost = 5000;    final int discountcost = 500;    boolean hybrid = hybridnot();    boolean solarpanel = solarnot();    int finalprice = 0;     finalprice = discountnot();     if (finalprice >= 26500)    {       print("basic price: " + basicprice + "\n" + "electric model: " +  electriccost + "\n" + "solar panel: " + solarcost + "\n" + "discount: " + discountcost);    }     else if (finalprice >= 25000)    {       print("basic price: " + basicprice + "\n" + "solar panel: " + solarcost);    }     else if (finalprice >= 22000)    {       print("basic price: " + basicprice + "\n" + "electric model: " + electriccost);    }     else    {       print("basic price: " + basicprice);    }     print("total: " + finalprice);     } 

for reason methods hybridnot , solarnot seem repeat before moving on next method. me seems might have problem i'm returning @ end of method can't figure out what's wrong. method totalcost seems ignore if statement in method discountnot , boolean values aren't passed correctly totalcost , the value when finalprice >= 26500.

again, i'm new java in general , i'm new stackoverflow (so hi!!) please tell me if i'm doing wrong , i'll correctly next time round! thank you!!

you aren't returning values in functions. use:

public static boolean hybridnot(){     //...     return electric; } 

to return boolean value electric. use similar syntax in other functions return relevant variables.

also, reason methods repeating because call them twice:

public static int discountnot() {     //...     boolean electric = hybridnot(); //here     boolean solarpanel = solarnot();     //... }  public static void totalcost() {     //...     boolean hybrid = hybridnot(); //and here     boolean solarpanel = solarnot();     //... } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -