0

Before I begin I'd like to state that I'm very much a beginner when it comes to writing code in general so I do apologise if what I'm asking may seem extremely basic.

With that being said I'm struggling with returning methods in my code. I am able to write this program without splitting it into methods as I have but I am told that when coding it is good practice to split it up in methods so that debugging can be easier.

The following code seems to have a 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 I 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 I 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 some reason the methods hybridNot and solarNot seem to repeat themselves before moving on to the next method. To me it seems that I might have a problem with what I'm returning at the end of the method but I honestly can't figure out what's wrong. The method totalCost seems to ignore the if statement in the method discountNot and the boolean values aren't passed correctly to totalCost and I only get the the value when finalPrice >= 26500.

Again, I'm new to Java in general and I'm also new to stackoverflow (so hi!!) so please tell me if I'm doing something wrong and I'll look to correctly next time round! Thank you!!

6
  • 1
    I'm perplexed that you say the methods do anything, as this code doesn't actually compile. You can't write a return statement in a method that has a return type declared (boolean for example) without accopmanying that return with the value you wish to return. Commented Nov 1, 2015 at 13:00
  • @RealSkeptic: To be fair, that's precisely the problem. However when I attempt to actually put a return value there the program gets worse and worse. Either it begins to loop or the compiler complains that I'm returning the wrong type. Commented Nov 1, 2015 at 13:21
  • That means that there is a logic issue. And that makes sense, because you have three possibilities (electric, hybrid, bad input) but you chose a return type that only gives you two possibilities. You need to change the logic (maybe put the validation loop inside the method? Throw an exception?) Commented Nov 1, 2015 at 13:25
  • @RealSkeptic Okay so I think I may have managed to fix the logic error (thank you!) but the if statement in totalCost(); is still being completely ignored. Commented Nov 1, 2015 at 13:40
  • You should pay attention to Roman Pustylnikov's answer, the part where you assign instead of compare the booleans. Commented Nov 1, 2015 at 13:50

2 Answers 2

2

You're supposed to return the value according to the method return type.

public static boolean hybridNot()
{
  String typeCar = input("Hybrid or Electric?");     
   return (typeCar.equalsIgnoreCase("Electric"))
}

I'm not talking about getting the input in the function that checks the state - it's evil on it's own.

Now here:

  if ((solarPanel = true) || (electric = true))

you do the assignment, not comparement. To compare you can do:

  if (solarPanel || electric)

since the variables are boolean already.

Here:

  boolean hybrid = hybridNot();
   boolean solarPanel = solarNot();
   int finalPrice = 0;

   finalPrice = discountNot();

discountNot will call the hybridNot and solarNot again:

public static int discountNot()
{
   ...
    boolean electric = hybridNot();
    boolean solarPanel = solarNot();
Sign up to request clarification or add additional context in comments.

5 Comments

Note that OP initializes the booleans as false, so if the "I don't know" option is reached it still would return false (if the function were corrected)
You're right, it looked like the attempt to have a tri-state on boolean from the first glance. I'll update.
Thank you for your reply. I have a few questions, however. I don't understand why you would return what you have, isn't that classed as a string? Also comparing causes the compiler to complain that || is an unexpected type.
equalsIgnoreCase returns boolean. Regarding the second issue, please see this example: ideone.com/DMFRmp
Thank you for this. I don't really understand how to apply this to my code but it occurs to me that this might be because I'm lacking knowledge of this topic. I do appreciate all the help, though.
1

You aren't returning any values in your functions. Use:

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

to return the boolean value electric. Use similar syntax in your other functions to return the relevant variables.

Also, the reason the methods are repeating themselves is because you 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();
    //...
}

1 Comment

I actually had that in a previous iteration of my code but all it did was go into a loop asking me if I wanted hybrid or electric again and again. Apologies if I've misunderstood you. Ah sorry I didn't see your reason as to why they repeat themselves. That solves the repeating problem then, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.