programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# May be somebody could help me with this exercise

Greenhorn
Posts: 5
Recently, I am trying to learn Java. However. i am stuck with one exercise. I would appreciate very much if somebody could help me.

Exercise:

Pickering Manufacturing Company randomly selects one of its four factories to inspect each week. Write an application that determines which factory will be selected each week for the next 52 weeks. Use the Math.random() function explained in Appendix D to generate a factory number between 1 and 4; you use a statement similar to:
factory = 1 + (int) (Math.random() * 4);
After each selection, display the factory to inspect, and after the 52 selections are complete, display the percentage of inspections at each factory for the year. Run the application several times until you are confident that the factory selection is random.

My code with explanations:

Rancher
Posts: 2240
28
• 1
Welcome to the forum.

Do you have any specific questions about the exercise?

it does not work.

Please explain.  What happens when you compile and execute the code.  If there are error messages, copy the full text and paste it here.

Could you post the full code?  The bits and pieces of code that were posted don't show enough.

Sheriff
Posts: 11498
180
• 1
Welcome to the Ranch!

Please read our wiki page that explains why saying that "it doesn't work" is useless

Look at this code again:

I doubt it does what you intend for it to do. The for-loop will be executed exactly 4 times. Line 3 in particular makes no sense at all; how did you come up with that formula for calculating the inspection time of each factory? I think you need to go back to pen and paper and work out your logic and your formula.

Greenhorn
Posts: 5
Thank you for the good advises:

My full code:

I get such output:

On week 1 this factory will be inspected 2.
On week 2 this factory will be inspected 4.
On week 3 this factory will be inspected 2.
On week 4 this factory will be inspected 4.
On week 5 this factory will be inspected 1.
On week 6 this factory will be inspected 2.
On week 7 this factory will be inspected 1.
On week 8 this factory will be inspected 4.
On week 9 this factory will be inspected 4.
On week 10 this factory will be inspected 1.
On week 11 this factory will be inspected 3.
On week 12 this factory will be inspected 2.
On week 13 this factory will be inspected 2.
On week 14 this factory will be inspected 4.
On week 15 this factory will be inspected 2.
On week 16 this factory will be inspected 4.
On week 17 this factory will be inspected 1.
On week 18 this factory will be inspected 3.
On week 19 this factory will be inspected 4.
On week 20 this factory will be inspected 3.
On week 21 this factory will be inspected 3.
On week 22 this factory will be inspected 1.
On week 23 this factory will be inspected 4.
On week 24 this factory will be inspected 1.
On week 25 this factory will be inspected 4.
On week 26 this factory will be inspected 1.
On week 27 this factory will be inspected 1.
On week 28 this factory will be inspected 1.
On week 29 this factory will be inspected 2.
On week 30 this factory will be inspected 1.
On week 31 this factory will be inspected 3.
On week 32 this factory will be inspected 1.
On week 33 this factory will be inspected 2.
On week 34 this factory will be inspected 1.
On week 35 this factory will be inspected 1.
On week 36 this factory will be inspected 4.
On week 37 this factory will be inspected 2.
On week 38 this factory will be inspected 1.
On week 39 this factory will be inspected 2.
On week 40 this factory will be inspected 2.
On week 41 this factory will be inspected 3.
On week 42 this factory will be inspected 1.
On week 43 this factory will be inspected 2.
On week 44 this factory will be inspected 4.
On week 45 this factory will be inspected 1.
On week 46 this factory will be inspected 4.
On week 47 this factory will be inspected 2.
On week 48 this factory will be inspected 4.
On week 49 this factory will be inspected 1.
On week 50 this factory will be inspected 2.
On week 51 this factory will be inspected 1.
On week 52 this factory will be inspected 4.

The problems is here:
The factory 1 was inspected 3.85% percent.
The factory 2 was inspected 5.77% percent.
The factory 3 was inspected 7.69% percent.
The factory 4 was inspected 9.62% percent

Its all the time the same and gives not right results if i would check myself. I want that this loop: for(factory = 1; factory <= 4; factory++) would sum up all 1s, 2s, 3s and 4s. I agree that thismy formula inspectionTime = 1 + factory; probably is completely wrong.

Rancher
Posts: 2240
28
• 1
Are you allowed to use arrays?  The counts for each factory could be saved in an array.  Otherwise you will need to use one variable for each factory to hold the counts.

Greenhorn
Posts: 5
To Norm Radder: Thank you very much for your comment In this exercise i am not allowed to use arrays. I will try to use a separate variable for each factory.

Rancher
Posts: 2240
28
If you use separate variables, there will be no need for the second loop.  But there will be a need for a method to do the computations that are done inside that loop.

Junilu Lacar
Sheriff
Posts: 11498
180
Vladas Verkelis wrote:In this exercise i am not allowed to use arrays. I will try to use a separate variable for each factory.

Are you sure? This problem is something that is best solved using an array. Not using an array will actually force you to write code that is more complicated than it needs to be. I don't understand why you would be given a restriction that will make you write bad code.

Greenhorn
Posts: 5
Junilu Lacar wrote:
Vladas Verkelis wrote:In this exercise i am not allowed to use arrays. I will try to use a separate variable for each factory.

Are you sure? This problem is something that is best solved using an array. Not using an array will actually force you to write code that is more complicated than it needs to be. I don't understand why you would be given a restriction that will make you write bad code.

Thank you very much for notice. I am reading a book "Java Programming" by Joyce Farrrell.  After one more chapter i should look at the arrays concept. So, i will check it earlier. Thank you for your advice.