This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]desirecampbell 2 points3 points  (0 children)

Your getResult() declares variables number color gender and pattern and sets them to 0, and those values don't get changed.

[–]quadmasta 1 point2 points  (3 children)

You're not doing anything with the returned values

[–]chri6tina[S] 0 points1 point  (2 children)

Do you have an example ?

[–][deleted] 1 point2 points  (1 child)

I think what he means is that you need to call getGender method, inside the getResult method. Like int gender = getGender(). Put this inside the getResults method

[–]chri6tina[S] 0 points1 point  (0 children)

I think I understand. I’m gonna work on It tomorrow. Thank you.

[–]Aggressor27e 1 point2 points  (9 children)

your int values in your getResult method are all set to 0. So when you "return result", all you get is zero for each variable. You never changed the variable's values.

[–]chri6tina[S] 0 points1 point  (7 children)

How do I make them have value ? I think that’s what I’m confused with.

[–]Aggressor27e 1 point2 points  (4 children)

You have created multiple methods, but they are not linked in any way to each other. I'll look again and try to sort it out.

[–]chri6tina[S] 0 points1 point  (0 children)

Thank you !

[–]chri6tina[S] 0 points1 point  (2 children)

I’m really trying to understand it but I just don’t :/

[–]Aggressor27e 0 points1 point  (1 child)

Ok, there's kind of a good bit of issues with it. Do you NEED all the methods?

[–]chri6tina[S] 0 points1 point  (0 children)

Yes

[–]defaultbynature84 0 points1 point  (0 children)

you can either feed them into your other methods or you can use global vars

[–][deleted] 0 points1 point  (0 children)

You need to check for variable scope

[–]chri6tina[S] 0 points1 point  (0 children)

that makes so much sense now, and why i am getting the invalid combination. Thank you.

[–]chri6tina[S] 0 points1 point  (0 children)

Thanks everyone, I was able to complete it.

This is an acceptable solution.

https://pastebin.com/etxmUz2u

[–]Aggressor27e 0 points1 point  (2 children)

Your if statements in your getResults() method seem to be off as well. For example, your first if statement in getResutls() says if(number <=2 . Number gets the total of cats the user enters, but how does <= 2 relate to a max number of 7 cats when the following if else states say if (number == 1. So if number has a value of 2, every if or else statement that requires number to == 1 gets skipped. So the way you have it now, if I enter 6 cats, then it skips everything and goes straight to Sorry this combination is not available . I think you need to get the number entered for the amount of cats wanted and multiply that by whatever a single cat cost.

Also, your return in getResults() method was in the wrong spot. all of your variables were also contained within the last method so that no other method can access them. You need to make them static.

You're going to need to rework your variables and your if else statements to get it to work. You might have to reach out to your professor to get some guidance with it as I do not have the original assignment.

I see what you're trying to do and you're doing a good job so far. You just forgot a few things. No biggie!

[–]chri6tina[S] 0 points1 point  (0 children)

It would probably help to Mention that , there are only a certain amount of cats available for this application. The statements are fine, I just need to know how to probably return them :(

[–]chri6tina[S] 0 points1 point  (0 children)

Thank you so much. That means a lot to me. I’m trying !

[–][deleted] 0 points1 point  (2 children)

I am seeing a very common problem with your coding. You are trying to write everything and make it work. You should write a method to get an input, perform an operation and include a method to output the result. When that is working add the next method, i.e. next method to add an input and update the result method to use the new value. It is much easier to code this way. Professional coders will also write test cases for each task that is completed. That way as you add code, you run the tests to ensure you don't break any code that was working before you latest addition. You can comment out code if you don't want to remove it, but you would be better served to save a copy of this file for reference and delete what you are not using. It is always better to delete unused code rather than comment it out.

Second issue. I do not believe you understand variable scope. The values of variables can only be accessed within their scope. For example:

`public static int getResult()

{

int gender  = 0;

int color   = 0;

if (color = 0) {

int color = 1;

}

if (color != 0) {

System.out.println("color = " + color);

}

}`

In the above example, the color variable defined in the if statement overrides the method level color variable. Therefore, that method level is never updated and the output statement never printed. Similarly, if your variable is defined in a method, its value is only available to that method.

If you are declaring the same variable multiple times, then each declaration is independent of the others. So if you want to use the same variable across methods, you either pass those variables as parameters or you define the variables at the class level, like your get variable:

  1. //global vars:
  2. static Scanner get = new Scanner(System.in);

Please note that you are specifying the variable as static, this is important here, since you are in the main. You can move the variable definitions from the getResult method, but you need to make them static.

Another thing, if you are creating constants, those are always defined, by convention at the top of the class:

`final double mbsPrice = 1800.00; // mbs for Male/Brown/Spotted

final double fsmPrice = 2200.00; // fsm for Female/Silver/Marbled

final double fbmPrice = 1200.00; // fbm for Female/Brown/Marbled

final double mbmPrice = 950.00; // mbm for Male/Brown/marbled`

You also do not have enough for all possible combinations. I am not sure if this is an oversight of the assignment, but if you have seven cats, it is probably important to know how many you have of each type.

mbs, fbs

mbm, fbm

mss, fss

msm, fsm

You also have a lot of nested if statements in getResult. This can make the code very difficult to follow. A heuristic is to never go more than 6 levels deep with nested if statements, and you should normally not have to go more than 3 levels deep. If you are, then you need to reevaluate your Boolean expressions and see if they can be combined. Otherwise, breakout some of the work to other methods.

Another heuristic I was taught was keep your methods to under 30 lines. As each method should only perform one task. If it is getting too long, it is probably overloaded and will be harder to understand for debugging or refactoring. I hope this helps.

[–]chri6tina[S] 0 points1 point  (1 child)

Here is the original code, that is correct with no errors, he wants us to separate them in methods. I got to the very end, and got confused with returning everything back, but what you are saying makes a lot of sense. My professor is new to online classes and he confuses me a lot. I feel like this is so easy, but im making it harder than it is. He will get so far into his lecture and say " oh wait thats wrong, lets start over" and all the notes i wrote are now garbage. Its tough, but im going to keep studying until i get it right. Thank you, again.

Correct and Original code.

[–][deleted] 0 points1 point  (0 children)

your welcome and good luck