• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with if/else  RSS feed

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am suppose to print the type of flooring based on the users input. The output that displays is correct however it displays all of the options. For example if the budget is : $300.00. The out print would come out as  carpet: $400, hardwood: $350, carpet: $325, tile:$290. Though for this example only the tile should the out print. Please explain what I am doing wrong.


 
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's floortype 0? I just see 1, 2, 3 and 4.
Let's say we enter budget as 300 dollars and for floortype we enter 1 then we have the following happening in your program.
floortype==1 && budget >= floorspace*1 (since floortype is 1)
So the budget depends on floorspace and how you defined Ceramic Tile, Carpet etc.
Maybe the budget should depend on something else, maybe just on floorspace.
If the budget is bigger than the floorspace, what should happen?
Also analyze, why do you have so many if conditions.


 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an assignment in school I am suppose to ask the user the number of doors, windows, and bookshelves which I have done below. The part which I don't understand is my professor says You must use separate Methods to calculate the change for each the Door, Window and Bookcase (Hint – you may want to use 2 methods Thanks!


 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid Mahmud wrote:The part which I don't understand is my professor says You must use separate Methods
Seems like you do not understand every part your professor asks you.

Do you have some material from the class (slides, books, notes) so you could come back to it and revise what you missed when it was explained in the class?

Use google, for instance:
1. What is method in Java?
2. Why use methods in Java?
3. How to write method in Java?

After you get initial understanding, try to ask again, what's exactly unclear from what you found.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay I will
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I have. The problem I am getting is that It always prints the doorspace as 0.0. Pleas explain why. Thanks

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the error has to do with something change1 because it is not being used. Please explain what I am doing wrong. Thanks!


p
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid Mahmud wrote:I know the error has to do with something change1 because it is not being used. Please explain what I am doing wrong. Thanks!


Well, simply, you set the value of doorspace to zero here...



And there isn't any code, anywhere else in your program that changes that. So ... when you print the value of doorspace, well, it has a value of zero.

Henry
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You get the length and the height but then you never call itemd() to get the area before printing.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I CAL itemd before it prints I am a little confused. I tried to put it the for loop but still got 0.0. Thanks
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the updated code. Thanks


 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call itemd() right after you have gotten the length and width, not up in the initialization section.

Secondly, you don't need doorspace as a parameter in the itemd() declaration:
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay Thanks I got it. Here is the code let me know if this is correct. Thanks!

 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the doors meassurements for instance are 2.5 x 1.5 metres, calculate how much material you would need to make them?
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The measurements just have to be in feet, But have I done the program correctly?
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid Mahmud wrote:The measurements just have to be in feet, But have I done the program correctly?

How you could test it? You need to find the way.

Get calculator and do next:
2.5 x 1.5 and tell us what it shows?

Now you wrote a program which meant to be a calculator in some sense, right?

Try out some methods and see if matches with calculator's figures.
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read what Knute wrote lately? What you concluded from information you have been given?
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I calculate the doorspace and wall space because it depends on the number of doors and windows the user enters as well as the bookshelves. Thanks!



 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I calculate the doorspace and wall space because it depends on the number of doors and windows the user enters as well as the bookshelves. Thanks!

Well, you have the number of doors and windows.  You have the dimensions of the doors and windows.  How would you use them together?
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid Mahmud wrote:How would I calculate the doorspace and wall space because it depends on the number of doors and windows the user enters as well as the bookshelves.

You're trying to avoid to answer to my quesiton. But you still missing my point.

What happens when we execute code snippet below?

And now see what you're returning from methods as a results.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a little confused
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid Mahmud wrote:I am a little confused

No worries. Ok, in your methods you have statements:

Please explain each of three parts what they are and what they do in your code?
1. return ?
2. (int) ?
3. someVariableName ?

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry that is the outdated code. The new code is below and the problems I am having is that I need help on calculating the wall space and floor space. The reason is because it is calculated based on the amount of doors, windows, and bookshelves that user inputs and their measurements. That is the only thing I have issues on. My professor said I have everything else correct. I hope this makes sense. Thanks!

This is the code I need help on. This code below is from number of doors, windows, and bookshelves that I knew, but know has to be changed where I don't know the number of each and their measurements.

ld - length door

hd - height door

lw - length window

hw - height window

lb - length bookshelf

hb - height bookshelf

wb - width bookshelf




This is the full code. Thanks
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:
How would I calculate the doorspace and wall space because it depends on the number of doors and windows the user enters as well as the bookshelves. Thanks!

Well, you have the number of doors and windows.  You have the dimensions of the doors and windows.  How would you use them together?

Did you see this post?  What's the answer?
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have figured it out. Thanks for all the help though!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!