Note - Variables in Java

advertisement
ICS3U1 – Unit 1
Name: _______________
Variables in Java
Syntax:
// To declare
int num;
char ch
double angle;
String msg;
a variable, use the following statements.
// Integer variable.
// Character variable.
// Decimal number.
// A String instance.
// To assign a value to a variable, simply use the ‘=’ operator.
num = 3;
ch = ‘b’;
// Character values are expressed between single quotes.
angle = 45.29;
msg = “Computer Science rocks!”; // String values are expressed between double
// quotes.
What is a variable?
Variables are placeholders in memory that can be used to store information. We can think of
them as “boxes” that we as programmers can use to store data relevant to our program.
Each variable must have a different identifier (or name), and that name cannot be any of the
reserved words for Java such as class, public, void, etc.
ICS3U1 – Unit 1
Name: _______________
We can only store one piece of information at a time in a variable. If you try to store a
second piece of information, it overwrites the previous information.
Larger pieces of information require larger boxes of memory. In order to allocate an
appropriate amount of memory, variables are also assigned a type. Data types vary from
language to language but generally include integers, floating point numbers (decimals), and
strings (text).
Data Types in Java
In the Java programming language, there are 8 data types. For our purposes, we will focus on
the following:
Type
int
double
char
boolean
String
Meaning
Integer in the range -2,147,483,648 to
2,147,483,647
A decimal number
A single character. This can be a letter, a
number, a space, or special characters like
punctuation.
A type that can only hold the value true or
false.
Not a true data type, but actually a class.
Strings are used to hold text such as
sentences, serial numbers, etc.
Example Values
4, -6, 0, 10000000
-0.5643, 3.145829, 100000.0
‘a’, ‘1’, ‘.’
true, false
“Bob”
“Holy cow I love milk!”
“My phone number is 911!”
Declaring and Initializing Variables
Variables are declared using the following template within the body of a program.
<type> <identifier>;
Once a variable is declared within your program, it does not have to be declared again!
For example:
String name;
int num;
// A String variable called name is now in memory.
// An integer called num is now in memory.
ICS3U1 – Unit 1
Name: _______________
Assigning values to variables is done using the assignment operator ‘=’.
name = “Bob”;
// name now holds the value “Bob”
num = 8675309;
// num now holds Jenny’s phone number.
Variables can also be declared AND initialized to a value in one line using the template below:
<type> <name> = <value>;
The previous variables could be declared alternatively as follows:
String name = “Bob”;
int num = 8675309;
Although a variable can only be declared once, it can be assigned new values multiple times.
Examples: The following are valid variable declarations in Java.
int num;
// Note that num has no value assigned to it yet!
final double PI = 3.141728; // Note that this declares PI as a constant!
char initial = ‘Z’;
// Note the single quotes!
String message = “Warning! Don’t push that button again!”;
// Note the double quotes!
You can declare multiple variables with the same type on the same line by separating them
with commas:
ex.
int num1, num2, num3;
However, each one has to be assigned a value individually!
You must use a separate line for each different type of variable (ex. String & int).
Outputting the Value of a Variable
To print a variable to screen, simply put its identifier in brackets as an argument to the
print() function:
int num = 3;
System.out.print(“The value of the variable num is “);
System.out.print(num);
You can also print both text and variable values using one print statement by using the
concatenation operator ‘+’:
System.out.println(“Thee value of the variable num is “ + num);
Joining text (in quotations) with variable identifiers can be done multiple times per print
statement.
ICS3U1 – Unit 1
Name: _______________
Variables in Java – Problem Set
1. Create a program in Java that uses variables to store the following information: your name,
your age, and your gender. Use suitable data types! Print the values of your variables to
screen in a tidy fashion. Save your program as Bio.java.
2. Variables of type int can be used in the basic mathematical operations of addition,
subtraction, multiplication, and division. For instance:
int a = 2;
int b = 3;
int sum = a + b;
int diff = a – b;
int product = a * b;
int quotient = b / a;
// sum is now 5
// diff is now -1
// product is now 6
// quotient is now 1 because Java always
// rounds down with integer division.
Create a program in Java that includes the following:
- at least 4 variables
- at least 2 different types of variables
- at least 2 calculations
- at least 3 output statements.
Your program should perform a simple task! For example, it could calculate the tax on an
item, outputting the name of the item and information about its cost (all stored in variables).
Or it could calculate the perimeter, area and volume of a geometric shape, with variables to
hold the name of the shape, the information needed for the calculations, and the results of
the calculations. Save your program as Calculations.java.
3. Mr. Zimny travels to Mountain Equipment Co-op and buys a new disc (frisbee) for $9.99, a
canoe paddle for $74.99, and a map of Algonquin Park for $8.49. He pays with a $100 and $20
bill.
Write a program that prints a receipt to screen for this purchase. Your receipt should display:
-
the address of the store (look it up online!)
a subtotal of the items purchase
the HST on the subtotal
the grand total of the items
the money exchanged (change and all)
Save your program as Purchase.java.
Download