• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help writing a program  RSS feed

 
Ryan Korte
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, my professor gave me these problems to program on Eclipse and I have no clue where to start... Can someone help me?

A. Unit Pricing Write a Java program to read the unit price of an item and the quantity ordered, and
then calculate the total amount of the purchase (all integers). You must give the EXACT input and
output as shown below. (Bold-face is what the user enters)
Please enter the Quantity desired: 5
Please enter the Unit price: 10
The Quantity desired is : 5
The Unit Price is : $10
The Total Amount is : $50

B. Modulo Division Write a program to read in an integer and print out the sum of the digits of the
integer. Use the % operator to extrct the digits and use the / operator to extract the digit. Use the
following format for input/output:
Enter an integer: 744
The sum of the digits is: 15

C. Approximating Pi Pi can be computed using the formula
4 * (1.0 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - . . .)
Write a program to display the result of
4 * (1.0 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13)
Note: be sure to use 1.0 not 1 in your program.

D. Wind Chill The National Weather Service has a relatively new formula to measure the wind chill
temperature. The formula is
Wind Chill = 35.74 + 0.6215T - 35.75V0.16 + 0.4275TV0.16
where T = outside temperature (°F) and V = wind velocity (mph). The formula cannot be used for
wind speeds below 2 mph, temperatures below -58°F, or temperatures above 41°F.
Write a program that prompts the user to enter a temperature and a wind speed, and then displays the
wind chill temperature. You may assume the values entered are valid.
Enter temperature(Fahrenheit): 5.3
Enter wind speed(mph): 6
The wind chill index is -5
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan, and welcome to the Ranch!

Ryan Korte wrote:So, my professor gave me these problems to program on Eclipse and I have no clue where to start.

Have you discovered the Oracle tutorials?
http://docs.oracle.com/javase/tutorial/index.html
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with the equivalent of Hello World

Write a program that outputs a message "Please enter the Quantity desired:"

Then extend that program to read in the value the user types.

Then extend that program to print out the value the user just entered.

 
Ryan Korte
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i got this far and i'm stuck on how I get the users input:
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help.

When you post a program, please UseCodeTags (← click). I've done it for you this time.

And Welcome to the Ranch.
 
Ryan Korte
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright thanks, Knute. That's a lot easier to read lol. So, when the output says (for example) "Please enter the Quantity desired:" I have to place a 0 after the number i type in. How do i get it so I only have to type in any number? Also I used some of this code from a previous project we had so I tried to manipulate it to the unit pricing project. How can I get the numbers that I type in for the quantity and unit price not be 5 and 10? I want to be able to type in any numbers and get the total amount.



This is the output:
Please enter the Quantity desired:
6 (it automatically detects this as 5 and ignores the users input)
0
Please enter the Unit price:
3 (it automatically detects this as 10 and ignores the users input)
0
The Quantity desired is:
5
The Unit Price is :
10
The Total Amount is: $
50
 
Mark King
Ranch Hand
Posts: 55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all your variable names should be more descriptive, count1 should be quantity, and userinput should be price. Second of all you can use stdin.nextInt() to get ints instead of doubles. Third you are using int for price when you probably really want to use double or BigDecimal. Finally it seems from your code that you understand that nextDouble() returns only 1 digit, in fact it returns the whole number.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan,

Also, think about negative inputs (lines: 14, 15, 24, 25).
Probably you don't want them.

And welcome to the Ranch
 
Ryan Korte
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys! I finally got this code to work! Now I need to do 2 more... My friend helped me do the approximation of pi one. They're both working very well.

This is the unit pricing:


This is approximation of pi:
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your Pi program, you can get rid of the if-else statement. Consider this:

 
Ryan Korte
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help with this project:

b. Modulo Division Write a program to read in an integer and print out the sum of the digits of the
integer. Use the % operator to extrct the digits and use the / operator to extract the digit. Use the
following format for input/output:
Enter an integer: 744
The sum of the digits is: 15

Does anyone know of any tutorial videos that show how to use the % operator?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, indent your code properly. It makes it a lot easier to see how the logic flows.

Your code will be easier to read if you line things up properly and indent consistently. Adding white space (blank lines) also helps make it easier to make out the different sections of code, like how spaces between paragraphs in books give the reader little "breaks".
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Korte wrote:Does anyone know of any tutorial videos that show how to use the % operator?

One thing we encourage here is showing a little effort. Doing a little search in Google can go a long way. Experimenting with whatever language feature you're trying to learn more about is also a very effective way to learn. The Java Tutorials are an excellent resource for beginners.
 
Paul Clapham
Sheriff
Posts: 22816
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also encourage you not to restrict yourself to video tutorials. The net is absolutely full of text-based tutorials about every possible aspect of Java, and likewise there's a ton of documentation in print form. So you're really going to have to become accustomed to reading text if you want to get ahead in programming.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!