• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Can someone help me write this program please?

 
Ranch Hand
Posts: 43
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, I hope you're doing well. Can someone help me solve this problem please? I'll attach what I've done until now. I'd really appreciate any kind of help. Thanks very much in advance.

/
 
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anduena, please edit your post to UseCodeTags (<-read this link).
 
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you indent the code correctly, as well as adding code tags, you will be able to see the structure of the program.
I am not sure whether you have got the loops where they are supposed to be, The instructions are not clear.
I suggest you find out about the printf() method; it will make printing your results easier.
 
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should first work on making this code easier to understand.  Variables are not being sent in text messages.  They should be given full, descriptive names.  When i look at your code, I see things like:

nr
tot
negnr
n

i have no clue what any of those are or represent.  If I saw code like this from a teammate, I'd immediately reject it and have them go back and make it readable.

The same for your formatting.  While Java does not require you to indent block inside of loops and conditions, your fellow programmers do.  Why is line 14 indented?  Why isn't the block starting on line 16? and then more on 17 for the if?  and so on.  

You may not think that sort of thing is important...but if you want someone to understand what you are writing, you need to follow the norms/expectations/conventions that are well established.
 
Saloon Keeper
Posts: 7175
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the code supposed to be doing, and what does it do instead? Where are you stuck making the required changes?
 
Marshal
Posts: 8132
572
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP
Even though it may look as a fairly simple exercise to you, I would not underestimate it and tackle it in a bit more disciplined way.

Create methods for each and every concrete task. For example you have a task:

Tell the user to type a number (an odd number bigger than 4...)


Now, how do you know user actually entered an odd number? Create method for that, so it tells you an answer, i.e.

Now you have something useful, and can instruct your program to keep asking user for a number until user provides you an odd one. Pseudo code:

while number is not odd
   ask number


Instructions actually prescribes you the approach to use "while" loop. Well, prescription could be better in such case, i.e. use "do while" loop. I'll leave it for you to decide how you go about that.

So, by now you'd have a method which would serve you identifying whether number entered by the user is expected (odd) or not.

Now you can create yet another method, which would actually ask user to provide/enter that number, for example:

That method getOddNumber() would have a loop and a method call isOdd() (the one we talked about few sentences back).

So, what's next? Well, read instructions:

...and using a for loop ask the user to type those number (So, if the user typed 5 - the user has to type five numbers)


Yet another method to create (think of a name and what it would return?), whose input would be an output from method getOddNumber().

And then it asks you to print some statistics. So that merits another method creation. It could be named as simple as printStatistics().

As you see, breaking down the bigger task to a bunch of smaller ones, by proxy you start dealing with simpler problems. Also your code becomes more readable and less prone to errors.
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . Create method for that . . .

I think I might put that method into a utility class and make that method static. Of course, anything with n % 2 == 1 in will not work.
 
Liutauras Vilda
Marshal
Posts: 8132
572
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Of course, anything with n % 2 == 1 in will not work.


Of course! Very good point!

It will not work one quarter of the time.

@OP
Find the book Java Puzzlers by Joshua Bloch and Neal Gafter (Campbell recommended that book to me quite some time ago. Apologies for not referencing book in the appropriate way). This problem is described in Puzzle 1.

Test method containing n % 2 == 1 on more numbers, in particular talking about actual odds (hint in instructions).
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . It will not work one quarter of the time. . . .

That is worse than not working 100% of the time. You might not notice the error until it is too late.
 
Liutauras Vilda
Marshal
Posts: 8132
572
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is worse than not working 100% of the time.


What a weird thing to say. But that's true. That's a serious potential pitfall for that type of exercise.
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, working 75% of the time and not working 25% of the time is worse than always failing to work correctly, because it can give the programmer a false sense of security.

And thank you for the cow, whoever it was
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic