473,796 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Java while loops

2 New Member
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.
Oct 30 '07 #1
4 3517
r035198x
13,262 MVP
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.
Oct 30 '07 #2
danbuttercup
2 New Member
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
Oct 30 '07 #3
r035198x
13,262 MVP
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.
Oct 30 '07 #4
Ganon11
3,652 Recognized Expert Specialist
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.
Oct 30 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

114
9889
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
1
2152
by: pawel | last post by:
I have made some comparision C# to Java RegularExpression. The problem was to find out if the rule match some text. Matching were done for precompiled regular expressions, in 100000 iterations loop. Those loops were executed 11 times and average value of consumend time was calculated. Below are codes for both classes. And I found, that Java implementation is 2 to 5 times faster than C# (it depends on complexity of expression). Maybe my...
133
8605
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
17
4345
by: Allerdyce.John | last post by:
Hi, I am trying to compare the amount of work between using STL algorithm VS a plain Java loop. Let's say the class Rect has 2 attributes: area, and areaPerCent. In Java, I just write a plain for loop with a list: public static void calculateAreaPerCent(List rectList, float containerArea) {
13
4279
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am not that good at programming but what I need to know is in java how would i do this.................. menu appears, user selects menu option (1,2,3,4,5,6) a new menu appears after selecting option 1 asking the user to select another option...
2
1841
by: shblack | last post by:
I need help with a program I am writing for school. The program has to do the following: /* 1. Character string must be a minimium of 15 charactors. 2. If not 15 characters long it must give error and send you back to orginal question. 3. After entering 15 characters it must count the CAPTIAL letters and tell you how many there are. 4. Need a loop to count and produce error and take you back to first screen. */
0
1372
by: Sudz28 | last post by:
Greetings! I am attempting to write a program that will allow a user to manipulate data read from a file, and am obviously in no way near finished. However, one problem I'm having that I don't understand is with my switch subroutine. In the below code, if I select "d" or anything that is an invalid response, it loops correctly back to the menu. However, if I attempt to insert (i), find (f), or delete (d) it loops back to the menu *but*...
1
1234
by: tkempy | last post by:
okay so i have a CIS class called abstraction and design. i'm completely new to this "java" stuff. I've never seen any of it in my life. our second homework is to create a program that produces a "ASCII art" version of the lincoln financial field where the eagles play. anyone who can help???? These are all the guidelines and what not... Assignment Description Your second assignment will provide you with practice using static methods,...
350
11907
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.