aspose file tools*
The moose likes Beginning Java and the fly likes Me and my dad's java journey. (there is a question here) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Me and my dad Watch "Me and my dad New topic
Author

Me and my dad's java journey. (there is a question here)

Bob Shangles
Greenhorn

Joined: Feb 03, 2013
Posts: 2
My dad and I are currently using the book called Blue Pelican Java.(he also purchased the answer book too) We also supplement our reading by visiting this website and working on there exercises. We are currently working on the if Statements.

http://programmedlessons.org/java5/Notes/chap12/progExercises12.html

For exercise 2 called: Order checker. Is there a way to solve this problem without using the && statements?
Also, before I used the && statements I was unable to print the result out every time I ran this program;

System.out.println("Total price is " + (totalBoltPrice + totalNutPrice + totalWasherPrice));
This is the program I have now it runs and seems to solve the question on exercise 2.




I hope I was clear (I'm only 11 ) I like java and I think it's fun but I need some help so I came here to the pro's
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38897
    
  23
Welcome to the Ranch
You ought to use the code button; I have been back to your post and you can see how much better it looks Also check your indentation, which is inconsistent; if you indent consistently you will be better able to see mistakes before they become problems. There are some suggestions about indenting here.
To get rid of the && operator, you would have to consider the logic of the application. You can nest if statements inside each other, but I suspect that will give you worse‑looking code than using the && operator.
Another way to do it is to look at the app’s logic and decide that nuts and bolts are sold in groups; if you order a bolt you automatically get two washers and a nut with it. So you only order bolts and the nuts are automatically supplied by the application.
You need to check your arithmetic and logic carefully. You have an else on line 23. Is that correct? You have washers * 2 in one place and bolts * 2 in another. Is that what you want?
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100



If you dont want to use && It could be as such.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38897
    
  23
Is that equivalent to what OP wrote? It is very difficult to tell because of OP’s inconsistent indentation.
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

Yes you can also refer to the link he provided.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Singh Anisha wrote:

If you dont want to use && It could be as such.


Not quite: what happens when bolts==washers but washes != bolts*2?


Steve
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

sorry my typing mistake..


This is only to replace &&
Second else will remain same.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

You should get into the habit of breaking your code up into a bunch of small methods that each does one and only thing. For example, I would write a single method to get input from the user and call it something like 'inputOrderQuantities()'. This lets you write code that tells a clearer story of what's going on. Here's what your higher level code might/should look like:


If you read that code, doesn't it clearly tell the story of what you're doing? Break down the big problem into lots of smaller, simpler problems. It's much easier to solve small, simple problems. How do you get the values of boltsOrderNotOk and the other variables? At least two ways: you could use an inline boolean expression and assign it to the appropriate variable or you could write a method that returns the value of the boolean expression. I prefer the method approach since it helps me keep each solution to a small problem separate from the others.

Junilu - [How to Ask Questions] [How to Answer Questions]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Bob Shangles wrote:My dad and I are currently using the book called Blue Pelican Java.(he also purchased the answer book too) We also supplement our reading by visiting this website and working on there exercises. We are currently working on the if Statements.

http://programmedlessons.org/java5/Notes/chap12/progExercises12.html

...


Here is the pertinent requirement from the link:
A correct order must have at least as many nuts as bolts and at least twice as many washers as blots


There are three key requirements here:
1) There must be at least as many nuts as bolts
2) and
3) There must be at least twice as many washers as bolts.

The second requirement indicates both of the other two requirements must be met. In the code you have (if I read it correctly) you are requiring just one of them to be correct. Here is your code (reformatted with proper indenting to make it easier to read):

If I were to put that into English it would be:
If there are exactly as many nuts as bolts, then the order is okay. Otherwise, if there exactly twice as many washers as bolts, then the order is okay. If neither of those situations is true, then check the order.

In Java, == means exactly equal, but the requirement is 'at least', so you need a different comparison. Do you know which one?

Also, you would need to change the nested if/else structure so that "Order is okay" is printed only when both conditions are true.

Also, line 15 of my code is copied from line 32 of your code. It doesn't actually do anything, since there is a ; at the end of the if statement. This means there is no code run on condition that the if statement is true. It is a common mistake that you can usually avoid (or at least identify easier) by getting in the habit of always using {} brackets for the code in if statements:

This makes it much easier to see nothing is happening, which makes it easier to identify if you did it by accident.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Singh Anisha wrote:sorry my typing mistake..


This is only to replace &&
Second else will remain same.


But this is not a good replacement for && because it does not cover the possible conditions the same way. Let's say you have this condition using &&:

There are 4 possible conditions:
1) (bolts == nuts) is true, and (washers == bolts*2) is true
-- In this case, both comparisons are made, and "Okay" is printed
2) (bolts == nuts) is true, and (washers == bolts*2) is false
-- In this case, both comparisons are made, and "Check order" is printed
3) (bolts == nuts) is false, and (washers == bolts*2) is true
-- In this case, only the first comparison is made, and "Check order" is printed
4) (bolts == nuts) is false, and (washers == bolts*2) is false
-- In this case, only the first comparison is made, and "Check order" is printed

--- Edit ---
Sorry, I was mistaken, because of the poor indenting.
p.s. Follow Campbell's advice - proper indenting makes like easier!

The below statement is not true when you see that the else belongs to the second if statement, and you keep the else from your previous post
-----------

In your code, the conditions would be as follows:
1) (bolts == nuts) is true, and (washers == bolts*2) is true
-- In this case, both comparisons are made, and "Okay" is printed
2) (bolts == nuts) is true, and (washers == bolts*2) is false
-- In this case, both comparisons are made, but nothing is printed
3) (bolts == nuts) is false, and (washers == bolts*2) is true
-- In this case, only the first comparison is made, and "Check order" is printed
4) (bolts == nuts) is false, and (washers == bolts*2) is false
-- In this case, only the first comparison is made, and "Check order" is printed

The difference is in the second condition. The && takes it into account, your code does not. I think a better replacement for the && would be something like this:

---- Edit ----
I still think this is a better, easier to read solution which uses less repeated code, which means fewer possible bugs.
-------------


Of course, it is more work to replace an && operator, so I would suggest not doing so... but this would be a fair replacement.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

The exercise says that there are two order rules that need to be checked and both errors are to be displayed if appropriate. Otherwise, display the "Order is OK." message. Strive for clarity before efficiency. Define a method for each order rule, then use them accordingly to tell a clear and concise story.

Which of the two versions below is easier to read and figure out if it's wrong?



IMO, version 2 tells the story better and you know to check the code in notEnoughNuts() if there is something wrong with this part of the program. Version 1 takes just a little bit more effort to find the bug; just a little bit but this is just a very simple case. The method 'notEnoughNuts()' takes the place of the boolean expression and clearly explains the intent rather than the way it is calculated.
Bob Shangles
Greenhorn

Joined: Feb 03, 2013
Posts: 2
Hello, again . I've been really busy with school and thought no one else responded but 1 person, but you guys did!
I almost gave up on it until I saw all the helpful post and I decided to give another stab at it.
all the post are a great addition to my program. Thank you, for all the great post and will give another post if it worked or not.


THANK YOU
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Me and my dad's java journey. (there is a question here)