Introduction to Java using NetBeans

Section 3 : Capturing and Displaying Numeric Data


Input of Numeric Data

Applications often require that users fill out forms that contain both text and numeric data. Text fields will only accept String data. It is therefore necessary to read in numeric data as a String and then convert it to a numeric data type.

Java provides methods that you can use to convert data from a string to a number. Below are a couple of those methods.

The Double.parseDouble() Method
Converts a String to a Double value.

The Integer.parseInt() Method
Converts a String to an Integer value

Example Icon Example

int num1, num2;

num1 = Double.parseDouble("5.5"); //     puts a numeric value of 5.5 in the num1 variable

num2 = Integer.parseInt("3"); //     puts a numeric value of 3 in the num2 variable

Sample Program – RectangleArea


Student Task:

    The code below shows how to read length and width to calculate the area of a rectangle. Complete the form using text boxes for input of both length and width. .

This is code that runs when the Calculate button is pressed.

Getting Integers and Double values from Strings

intnumber =Integer.parseInt(strnum); // Where strnum is an integer value entered as a string
doublenumber = Double.parseDouble(strnum); /// Where strnum is a decimal value entered as a string

Create GUI Programs for the Following Assignments. You must add upgrades to these programs.

Assignment 3.1 - Pizza Anyone!

This is an image of a pizza shop.

The cost of making pizza at a local shop is as follows:

  • Labour cost is $1.00 per pizza, regardless of size.
  • Store cost is $1.50 per pizza, regardless of size.
  • Materials is $0.05 * diameter* diameter(diameter is measured in inches).

Create a Java application that prompts the user for the size of a pizza and then displays the cost of making the pizza.

The application should look similar to the image below. Notice that the output is formatted to 2 decimal places with a dollar sign in front. Remember that this can be accomplished using DecimalFormat as seen in previous activities.

Assignment 3.2 - Fast Food


A fast food restaurant charges $2.49 for burgers, $1.89 for fries, and $0.99 for soft drinks.

Create an order application that provides the employee with input fields to enter the number of burgers, fries, and soft drinks that a customer orders.

The program is to display the total (before tax), the tax amount @ 13% (HST=13%), and the final cost.

The application output should look similar to:

Enter the number of burgers: 2

Enter the number of fries: 5

Enter the number of soft drinks: 5

Total before taxes: $19.38

Tax: $2.52

Final total: $21.50

This image is a cartoon of a hamburger, fries, and drink. Each item has legs and is running.

Modify the program to include another input field that the employee uses to enter the amount s/he received from the customer. This is called the amount tendered. Create a button. The button should be grayed out (not enabled) until the first button has been pressed.

A message is then to be displayed indicating the correct amount of change.

Enter amount tendered:                   $25.00
Change:                                            $2.91


Assignment 3.3 - Add Coins

Create an Add Coins application that prompts the user for the number of quaters, dimes nickels, and pennies and then displays the total dollar amount.





For these assignments post the following:

1. Insert the Drive Folder
2. Link to the jar file
3. The code for the JFrame Form
(Use to keep the code in the proper format)

Copy the Rubric to your Google Drive and add a link on your website