• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having trouble understanding part of my java loop assignment  RSS feed

 
Pat Savij
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my teacher assigned a project on a day that I missed, and i'm having trouble understanding what I need to accomplish for part of it. The instructions are as follows:



Project 5A&B Java 1 Winter 2009
Write an application for the School Lunch Program Monthly Report, a report that surveys the costs of luncheon main dishes at various public schools within the country.

PARTA Write an application called Project5A.java

1. In the main
a. It will prompt the reviewer to enter a main course
Example:
Enter the luncheon main course: Burger, Hotdog, Pizza or Grilled Cheese:
Grilled Cheese //user input
b. It will declare appropriate variables to store his data
i. A double cost //the average cost of that lunch
ii. A double total collected //total of all money from all schools surveyed
iii. A double amount //cost of each lunch
iv. An int numItems // a counter of all lunches entered
v. A String name //the main dish name
c. It will create a Scanner object


2. write a while loop controlled on the amount entered being greater or equal to 0 (or not a -1)

3. then write a while loop to prompt the reviewer to enter all lunch costs as surveyed separated by spaces, terminated with a -1
Example:
Enter all costs for the luncheon main dish Grilled Cheese separated by spaces, terminate with a -1:
1.95 2.55 0.99 -1

4. within the loop, add the amounts to the accumulating total, increment the numItems counter and read in the next amount using the scanner

5. when the loop ends (the terminal -1 was read), decrease the counter so that the -1 does not count as a lunch, calculate the average cost and output in a System.out.printf statement the name, number of lunches surveyed, total money collected and the average cost of that lunch. Do the same output in a JOptionPane – see sample execution below


PartB
Copy your Project5A.java file into one named Project5B.java and change the header line to read Project5B
Modify the code so that it prompts the reviewer to enter the maximum number of lunches in his survey using a JOptionPane
It will now need variables for a counter for the actual number of lunches and a maximum number of lunches

It will enclose the prompt for the luncheon main dish name and the while loop above in an outer loop controlled on the maximum number of lunches to be surveyed

When the inner loop ends, increment the number of lunches counter and reset the average, total and numItem back to zero, then end the outer loop.



Now, I have part A completely down. My issue is with part B. I'm not sure what I need to do to incorporate part A into part B. Basically, I don't understand what he's actually asking me to do. My part A is here(which has the while loop he referenced in part B):





Here is my code for part B:

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Your code reads very strangely, with the combination of JOptionPane and Scanner for input. Similarly you have a strange combination of println and printf for output. Use printf, which means you can get rid of the currency format class.
JOptionPane is very old‑fashioned programming. If you are using it, however, you will want to replace all the inputs with option panes. I presume you know that option panes always return Strings, so you will need to parse them into numbers. I presume you are aware of the parse methods in each of the “wrapper” classes?
The description of the loop in B is very explicit, but not actually clear. Is that exactly what you were told? You will ask for up to maybe 10 lunch prices to be entered, but I cannot tell whether you are going to terminate the loop after 8, or whether it means up to 10 cheeses and up to 10 burgers. I think you may have to ask somebody else.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for using code tags, but you had some long lines which I broke, so you can see how to do it correctly. Have a look here, but note they use a different convention for {} in that style guide. Some of your indentation is inconsistent: 4 spaces at the top and 2 spaces at the bottom. Choose one number and stick to it.
 
Pat Savij
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason i'm using JOptionPane is because that's what i'm told to do :/ I'm not entirely sure if my teacher is good or not since I have nobody to compare him to, so you tell me. The instructions he gave me are listed at the top, and I tried to follow them the best I could. I've been doing java for about 3 weeks now, so i'm still pretty new at it.

As for the poor indentation I apologize, i've erased/rewrote and copied/pasted that code so many times tonight I can't count, so it may have gotten a little out of whack. Also, there were a few lines I commented out that I need to delete entirely I just forgot.

Anyways, I appreciate the responses. As currently written, it gives the monetary output at the end of each iteration of the loop, which is the closest I could get to what he wants. He wants us to output the final results of all the lunches combined using JOptionPane(not my choice) and I simply have no idea how to do that as currently written since the variables write over themselves after each iteration. Perhaps if I made several loops and methods, but he definitely didn't tell us to do that so I have no idea what to do
 
Pat Savij
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it didn't update or something. I edited in my code for project B but it isn't showing up in my post. I'll try again.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Savij wrote:I . . . I edited in my code for project B but it isn't showing up in my post. I'll try again.
No, don't edit an old post like that. Post it as a new post.

And where on earth does the System.exit call come from? Don't use System.exit because it can be too vicious, and it won't compile if it is simply sitting out on its own.
Apart from your not having sorted out the line lengths and indentation, what is wrong with the second version? Does it work?
 
Pat Savij
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The System.exit, I was taught, is used to close JOptionPane. I'm brand new to this man. Please don't be so hard on me. I still have a whole lot to learn.

The program works, but it's not the final product he wants, as I stated above, and again. He wants the final output of ALL the lunches surveyed to be in a JOptionPane message format, but I have no idea how to do that, because variables like name, and their amounts, average, ect get written over when the loop reiterates. So, if I wanted to output everything is JOptionPane, as HE wants me to, only the last item(Grilled Cheese, Pizza, whatever) in the loop would show up, and not all of them, or 2, or 3....

Anyways, I think i'm just going to turn it in as is. Apart from telling me everything I do weird or incorrect, I never really got the actual help I needed. Thanks anyway.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.exit will close the JOptionPane. However, it will also close the entire JVM currently running your program.
 
Pat Savij
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip! I'm only on chapter 5 of my intro book, so i'm sure I would have *hopefully* read that eventually, but it's always nice to know some things ahead of time. I just started learning how to write multiple methods and such, so i'm sure that will come in handy.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't men to be hard; sorry.
 
Pat Savij
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's ok. Im sure it can be frustrating dealing with people new to programming, especially when there seems to be so many different ways to start off. I appreciate the input, i'm just really new to this, so i'm definitely learning as I go everyday.

I turned the project in as it was posted, which did work for all intended purposes, I just didn't have the final message with all the lunch types surveyed output in JOptionPane. Oh well. The instructions were very vague, as many people on many programming forums have pointed out, so I feel like I did the best I could given the current situation. The school's teacher email system has been messed up for a while now, otherwise I probably would have emailed my teacher and asked for some clarification.

Anyways, I can't get enough of this programming stuff, so you guys will probably be seeing more of me as I move along. It's definitely kinda overwhelming at times, but it's nice to know there are sources for learning out there other than the classroom.

Thanks again for responding to my issue!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!