Exam 1||Newest Exam
Upload||Graded A+
- The difference between a variable and a literal -CORRECT ANSWER A literal is
notation for representing a fixed ( const ) value. A variable is storage location
associated with a symbolic name (pointed to, if you'd like). In any programming
language a Literal is a constant value, where as identifiers can change their values.
Identifiers can store literals and process them further.
How to declare a variable -CORRECT ANSWER To declare (create) a variable, you will
specify the type, leave at least one space, then the name for the variable and end the
line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating
point number (a double precision number), and boolean for a Boolean value (true or
false).
int score;
How to assign a value to a variable. -CORRECT ANSWER Java is pass-by-value. That
means pass-by-copy
Declare an int variable and assign it the value '7'. The bit pattern for 7 goes into the
variable named x.
int x = 7;
How to initialize a variable. -CORRECT ANSWER Initializing a variable. Initializing a
variable means specifying an initial value to assign to it (i.e., before it is used at all).
Notice that a variable that is not initialized does not have a defined value, hence it
cannot be used until it is assigned such a value.
- What a data type is (i.e., what makes up a data type) -CORRECT ANSWER A data
type is a set of values and a set of operations defined on them. For example, we are
familiar with numbers and with operations defined on them such as addition and
multiplication. There are eight different built-in types of data in Java, mostly different
kinds of numbers. We use the system type for strings of characters so frequently that
we also consider it here.
The eight primitive data types, and which ones we are using in this course -CORRECT
ANSWER Variables and the 8 Primitive Data Types
The Purpose of a Variable (and some vocabulary)
You can think of a simple program as a list of instructions to be read and acted upon
sequentially
,Example:
Read a value representing the radius of a circle from the standard input source/stream
Compute the area of a circle with this radius
Print the area to the standard output stream (i.e., the console window)
Remember: a computer will read and act upon these instructions one at a time - it is not
aware of what is coming up until it gets there!
Looking at step 1 in the program above, we will need to tell the computer that it needs to
remember the value it is reading in - it needs to store this value in its memory
somewhere so we can use it in a computation later.
We need to tell the computer how much memory will be needed to store the value in
question. Different kinds of numbers require different amounts of memory (more on this
in a minute). Of course, we sometimes need to store things other than numbers. These
things too, come in different sizes. For example, it will certainly take more memory to
store the Declaration of Independence than it will to store a single letter (i.e., a
"character"). Further, we also need to tell the computer how the value should be stored
in memory (i.e., what method of "encoding" should be employed to turn the value into a
string of 1's and 0's). Examples of types of encodings used include Two's Complement,
IEEE 754 Form, ASCII, Unicode, etc...
The computer also needs to have some reference to where it stored the value, so it can
find it again.
The concept of a variable solves all of our problems here. A variable in Java gives us a
way to store values (or other kinds of information) for later use, addressing all of the
aforementioned considerations.
The amount of memory allocated for a given v
What the arithmetic operators are, and how they work at each data type. -CORRECT
ANSWER Arithmetic Operators
The Java programming language supports various arithmetic operators for all floating-
point and integer numbers. These operators are + (addition), - (subtraction), *
(multiplication), / (division), and % (modulo). The following table summarizes the binary
arithmetic operations in the Java programming language.
Operator Use Description
+ op1 + op2 Adds op1 and op2; also used to concatenate strings
- op1 - op2 Subtracts op2 from op1
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing op1 by op2
Here's an example program, ArithmeticDemo (in a .java source file), that defines two
integers and two double-precision floating-point numbers and uses the five arithmetic
,operators to perform different arithmetic operations. This program also uses + to
concatenate strings. The arithmetic operations are shown in boldface:
public class ArithmeticDemo {
public static void main(String[] args) {
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y);
//adding numbers
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
//subtracting numbers
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
//multiplying numbers
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
//dividing numbers
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
//computing the remainder
Understand operator precedence and associativity -CORRECT ANSWER Associativity
is used when two operators of same precedence appear in an expression. Associativity
can be either Left to Right or Right to Left. For example '*' and '/' have same
precedence and their associativity is Left to Right, so the expression " * 10" is
treated as "() * 10".
, How to use a combined assignment operator -CORRECT ANSWER Variables and
Combined Assignment Operators
You'll commonly want to complete a math function and assign the resulting value back
to some named object, called a variable. ActionScript makes this easier by letting you
combine arithmetic and assignment operators together. Take a look at an assignment
operator example:
Create a new ActionScript 3.0 project and enter in the following code for the project:
// Assignment Operators
var myValue:Number = 2;
myValue = myValue + 2;
trace(myValue);
var myOtherValue:Number = 2;
myOtherValue += 2;
trace(myOtherValue);
Run the project. You'll get the following in the Output panel:
4
4
Let's walk through the code and explain how you get this result and what role variables
and combined assignment operators play.
Variables
You haven't really seen much about the var statement yet, so let's reveal a little bit more
about it. You have used it in the past to create named object containers that you have
then assigned MovieClip symbols to using the new statement. You can also use var to
create variables; in fact, variables is what var stands for. Variables are named objects
that can contain variable values.
Take a look at the second line of the assignment operators example:
var myValue:Number = 2;
The var statement is creating a variable called myValue. See that :Number after the
variable name? You have to tell ActionScript what type of data your variable can hold,
similar to how you did when using the function statement. In this case, you are saying
that myValue will contain a number. When you create the variable, it is empty, but when
you assign the numeric value 2 to it, you can refer to that value using the name
myValue.
myValue = myValue + 2;
trace(myValue);
On the second line above, you are accessing the myValue object and are assigning a
new value to it. Notice that you are not usi
- Understand the difference between = and == -CORRECT ANSWER The Difference
Between "is" and "==" in Python. Python has two operators for equality comparisons,
"is" and "==" (equals). ... There's a difference in meaning between equal and identical.