Introduction to Java using NetBeans

Section 1.2 : Variables and Formating

Learning Goals

We are Learning .......

- variable types
- arithmetic operations
-formating numbers
- strings

Success Criteria

What I'm Looking For......

- bug free java applications that efficiently solve the problems presented
- problem solving (try before you ask)
- efficient algorithm implementation
- formated and documented code
- organized website with code and executables

Curriculum Expectations

Programming Concepts and Skills
- demonstrate the ability to use different data types and expressions
- use proper code maintenance techniques when creating computer programs.

Software Development
- manage the software development process effectively

What is a Variable?

A variable is a symbolic name for a location in memory. Memory is used to store string or numeric data. The value stored in memory (represented by a variable) can be modified and read during the execution of the Java program. To use a variable in Java, it must first be defined.

Defining a variable in Java is done by stating the variable type and by giving it a variable name (identifier).

Declaring Variables

Integer (whole number)

int intAge; // Declare the variable
intAge = 25; // Give the variable a value


double dblSalary; // Declare the variable
dblSalary = 95,000.52 // Give the variable a value


boolean blnHappy; // Declare the variable
blnHappy = true; // Give the variable a value (true or false)


String strName; // Declare the variable

strName = "Gravelle"; //Give the variable a value (note double quotes)


char chrInitial; // Declare the variable
chrInitial = 'D'; //Give the variable a value (note single quotes)

We will look at more variable types later.

You can create many variables of the same type at once, as shown below:

int intQuotient, intRemainder,intTotal;

You can assign values of variables in the declaration statement

int intScore = 5; // Declare the variable and give it a value


Simple Arithmetic in Java

Using the +, -, * and / operators (plus, minus, times and divide).

Generally, you will store the result of the mathematical expression in a memory location represented by a variable.


Example Icon Example


The expressions...

intAge = 21;

intAvg = (80 + 56 + 75 + 82)/4;


The Modulus Operator

The division of two integers like 27 divided by 5 will result in an answer of 5.

The modulus operator tells you the remainder. To find the remainder of the division of two integers like 27 divided by 5, you would use the modulus division operator %.

Example Icon Example

intQuotient = 27 / 5 ; // divides 27 by 5 and determines the whole value of the division, quotient = 5

intRemainder = 27 % 5; // divides 27 by 5 and determines the remainder, remainder = 2


Formatting Numbers

When you are outputting to the console screen you may notice that decimal numbers may or may not print out the way you want them to.   The Java language has a system (the java.text library) for formatting objects before they are printed.

Formatting a number before it is printed involves following a number of steps.

  1. Before you can use the functions for formatting you must import the "Java text" library. This is done only once in your program (above your class and main function) using the code:

import java.text.*;

  1. Next you must create a "DecimalFormat" object that is used for formatting the number according to a pattern that you must specify.
  1. Finally use the DecimalFormat object that was created to display the decimal number using the format defined.

Formatting to 2 Decimal Places

Formatting with Commas, Zeros, and Dollar Signs


Visit Sun Microsystems for more on Java Operators.

Strings in Java


Example Icon Example

// creates a String variable called teamName
String strTeam;

// set the value of the String variable, string value is in quotes
strTeam = "Ottawa Senators ";          

// displays "Ottawa's team is the Ottawa Senators"
System.out.println("Ottawa's team is the " + strTeam);        

Different ways to give strings values

  • a string literal (e.g., strFirst = "Dianna";)
  • another string (e.g., strLast = Gravelle;)
  • a joining of other strings (e.g., strName = strFirst + strSecond;)

Example Icon Example

strCity = "Ottawa";
strTeam = "Senators";
strTeamName = name1 + name2;
//Displays Ottawa Senators




These assignments will be marked for code format, documentation (every line), variable names, variable types, and output format.

Assignment 1.5  - Area of a Circle
Find the area of a circle with a radius of 15 cm.
Use variables to store the dblRadius, dblPi, and dblArea.
Calculate the Area using the formula dblArea = dblRadius * dblRadius * dblPi

Display the answer in sentence form using the variables

The area of a circle with a radius of 15 cm is 706.5 cm(2)

Now Change the radius to 25, using the same variable.

Recalculate and Print.

Assignment 1.6 - Craig Anderson

Create a program that stores firstname, lastname, middle initial, number and team in appropriate variables. The program should display:
Craig P. Anderson is number 41 on the Ottawa Senators (using the variables)

Now change the Variables values (do not make new variables) to another player.

Assignment 1.7 - Area of a Rectangle
Find the area of a 5.7 by 4.8 rectangle. Use variables to store the width, height, and area. Calculate the area using a formula. Display the width, height, and area all on separate lines using variables.

Area of a Rectangle
width= 4.8 cm
height= 5.7 cm
The area is 27.36 cm(2)

Now change the Variables to different values and reprint.

Assignment 1.8- Bill of sale
Write a Java program to calculate and print a bill of sale based on the following:

A shirt was purchased for $12.49 and was paid with a $20 bill.
Assign variables for the purchase price and amount given to the clerk.

Calculate taxes, (HST = 13%), total bill, amount paid, and change.

Add 3 additional items to your bill

Output the entire bill showing purchase price, taxes, total, and change. The print format should look like a bill you would receive in a store.

Add a message from your cashier

Make sure the date and company name are on the bill







Posting your work

1. Upload the Java Projects (Lessons and Assignments) to your Gravelle Folder (Share with me only) and insert the Drive Folder (Insert > Drive Folder) Make sure projects are named correctly.

2. Upload your code image to your Gravelle Folder and embed on your site

3. Take a screenshot of your console output and embed on your site

4. Upload the rubric to your Google Drive Gravelle Folder and post at the top of your subpage

JAR files which are executable java files do not work on projects without a display so we will not use them in this assignment.