Help with Java while loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danbuttercup
    New Member
    • Oct 2007
    • 2

    Help with Java while loops

    Hi everyone

    I just recently learnt how to do while loops in my java class and I am completely lost. I have to make programs for the following questions but I have no idea were to start.

    1.Several pairs (x,y) of numbers are to keyed in. Any pair with x=y terminates the loop. Determine and display counts of how many pairs satisfy x<y and how many pairs satisfy x>y.

    2.Two numbers x and y are to be keyed in. If the sum of x and y is greater than 42, display 42. If not, increase x by 10 and y by 3, display the new x and y and again check to see if the sum is greater than 42. Continue until the sum is greater than 42.

    3.One number is to be typed. If it is between 7 and 35, inclusive display BETWEEN and stop. If it is less than 7, increase it by 5; if it is greater than 35, decrease it by 5. In either case display the value obtained. The program should continue with this new value until it is between 7 and 35.

    4.You’ve been hired by a law firm that is working on a sex discrimination case. Your firm has obtained a file of incomes which contains the salaries of every employee in the company. Each salary amount is preceded by ‘F’ for female or ‘M’ for male. As a first pass in the analysis of these data, you’ve been asked to compute the average income for females and the average income for males.

    5.A heating oil company uses the temperature range for each day to determine its customers’ typical oil use and to schedule deliveries. The firm has hired you to take hourly outdoor temperature readings for each 24 hour period and find the day’s high and low temperatures from this data. Because you won’t be getting much sleep on this job, you decide that it would be a good idea to have the computer keep track of the maximum and minimum values.

    6.Many scored from 0 to 100 are to be read from a file. Determine the average of all numbers less than 60, the average of those from 60 up to but not including 90, and the average of those 90 or more.

    7.A young person agrees to begin working for a company at the very modest salary of a penny per week, with the stipulation that their salary will double each week. What is their weekly salary at the end of 6 months and how much has that person earned.

    8.Find the total amount credited to an account after 4 years if $25.00 is deposited each month at an annual interest rate of 5.5 percent compounded monthly.

    9.Write a program that accepts characters and only displays them if they are upper case letters. The program ends when an exclamation mark is entered.

    10.Write a program to do factorials. The program should only accept the numbers from 2 to 10. A factorial is a number multiplied by itself and all numbers below it.
    5! = 5 * 4 * 3 * 2 * 1

    11.Write a program to produce bar charts for numbers that have been entered. The program ends with zero. The bar chart for a number is three rows of stars, with the number of stars the same as the number, with the number itself printed on the middle row.
    *********
    ********* 9
    *********

    12.Write a program that counts and displays numbers entered that are exactly divisible by three and in the range 50-100. Program stops if a number not meeting the condition is entered and the count of acceptable numbers is printed out.

    I know I should probably about specific questions about what I need help with, but like I said before, I really don;t know were to start.

    Thank you very much to nayone who is willing to help me!

    P.S. I know that Im supposed to use if function and J0ption pane instead of strings and that I am not aloud to use for loops.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by danbuttercup
    Hi everyone

    I just recently learnt how to do while loops in my java class and I am completely lost. I have to make programs for the following questions but I have no idea were to start.

    1.Several pairs (x,y) of numbers are to keyed in. Any pair with x=y terminates the loop. Determine and display counts of how many pairs satisfy x<y and how many pairs satisfy x>y.

    2.Two numbers x and y are to be keyed in. If the sum of x and y is greater than 42, display 42. If not, increase x by 10 and y by 3, display the new x and y and again check to see if the sum is greater than 42. Continue until the sum is greater than 42.

    3.One number is to be typed. If it is between 7 and 35, inclusive display BETWEEN and stop. If it is less than 7, increase it by 5; if it is greater than 35, decrease it by 5. In either case display the value obtained. The program should continue with this new value until it is between 7 and 35.

    4.You’ve been hired by a law firm that is working on a sex discrimination case. Your firm has obtained a file of incomes which contains the salaries of every employee in the company. Each salary amount is preceded by ‘F’ for female or ‘M’ for male. As a first pass in the analysis of these data, you’ve been asked to compute the average income for females and the average income for males.

    5.A heating oil company uses the temperature range for each day to determine its customers’ typical oil use and to schedule deliveries. The firm has hired you to take hourly outdoor temperature readings for each 24 hour period and find the day’s high and low temperatures from this data. Because you won’t be getting much sleep on this job, you decide that it would be a good idea to have the computer keep track of the maximum and minimum values.

    6.Many scored from 0 to 100 are to be read from a file. Determine the average of all numbers less than 60, the average of those from 60 up to but not including 90, and the average of those 90 or more.

    7.A young person agrees to begin working for a company at the very modest salary of a penny per week, with the stipulation that their salary will double each week. What is their weekly salary at the end of 6 months and how much has that person earned.

    8.Find the total amount credited to an account after 4 years if $25.00 is deposited each month at an annual interest rate of 5.5 percent compounded monthly.

    9.Write a program that accepts characters and only displays them if they are upper case letters. The program ends when an exclamation mark is entered.

    10.Write a program to do factorials. The program should only accept the numbers from 2 to 10. A factorial is a number multiplied by itself and all numbers below it.
    5! = 5 * 4 * 3 * 2 * 1

    11.Write a program to produce bar charts for numbers that have been entered. The program ends with zero. The bar chart for a number is three rows of stars, with the number of stars the same as the number, with the number itself printed on the middle row.
    *********
    ********* 9
    *********

    12.Write a program that counts and displays numbers entered that are exactly divisible by three and in the range 50-100. Program stops if a number not meeting the condition is entered and the count of acceptable numbers is printed out.

    I know I should probably about specific questions about what I need help with, but like I said before, I really don;t know were to start.

    Thank you very much to nayone who is willing to help me!

    P.S. I know that Im supposed to use if function and J0ption pane instead of strings and that I am not aloud to use for loops.
    You should work on them one at a time starting with the first one of course. You can start by learning how to use the Scanner class to get input from the user.

    Edit: If you are sure you should be using JOptionPane then for the first problem you should try to figure out how to allow the user to enter the values and have a functionality to indicate that they are done entering their values and would now want to see the results.

    Comment

    • danbuttercup
      New Member
      • Oct 2007
      • 2

      #3
      I know how to prompt the user and everything up to the point were I have to start my while loop.
      I guess I should of made that clearer.
      Sorry

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by danbuttercup
        I know how to prompt the user and everything up to the point were I have to start my while loop.
        I guess I should of made that clearer.
        Sorry
        Post what you have and the errors/problems you have with it then,

        P.S Don't forget to use code tags when posting code.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Again, you need to work on these, one at a time. Let's start with the first one.

          1.Several pairs (x,y) of numbers are to keyed in. Any pair with x=y terminates the loop. Determine and display counts of how many pairs satisfy x<y and how many pairs satisfy x>y.
          So let's assume you have already gotten the x and y from the user. You know how to check if x < y, or if x > y, right? Since you say you are having trouble with while loop,s I'll just assume you can handle this part out.

          Now, since x != y, you want to keep going. This indicates a loop. What has to happen inside the loop? Well,

          1) You need to get new x and y values.
          2) You need to compare x and y (and do something appropriate for x < y or x > y)

          When will the loop stop? Your assignment says, "Any pair with x=y terminates the loop." This is the only stopping condition given, so you know that your loop will only stop when x=y. Then when should it continue?

          These are the general steps I take when designing a loop of any kind:

          1) Do I even need a loop? Are there executions that I need to repeat, and can be generalized?
          2) When should the loop stop (or, When should the loop keep going)?
          3) What should happen inside the loop? One of the events in the loop must change the 'end condition'.

          If you follow these steps, you should have no trouble with your homework.

          Comment

          Working...