Variables in Java
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).
Integer (whole number)
// Declare the variable
boolean blnHappy; // Declare the variable
String strName; // Declare the variable
strName = "Gravelle"; //Give the variable a value (note double quotes)
char chrInitial; // Declare the variable
We will look at more variable types later.
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.
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 %.
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
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.
Formatting to 2 Decimal Places
Formatting with Commas, Zeros, and Dollar Signs
Visit Sun Microsystems for more on Java Operators.
Strings in Java
creates a String variable called teamName
// set the value
of the String variable, string value is in quotes
// displays "Ottawa's team is the Ottawa Senators"
System.out.println("Ottawa's team is the " + strTeam);
Different ways to give strings values
strCity = "Ottawa";
These assignments will be marked for code format, documentation (every line), variable names, variable types, and output format.
Area of a Circle
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
Now change the Variables values (do not make new variables) to different player, team, and number and reprint (note: I should see both printed in the console)
Assignment 1.7 - Area of a
Area of a Rectangle
Now change the Variables to different values and reprint.
Assignment 1.8- Bill of sale
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 purchased price, taxes, total, and change. The print format should look like a bill you would receive in a store.
Add a message from your server
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.