0% found this document useful (0 votes)
14 views49 pages

Lecture 6(6)

The document outlines key concepts in programming fundamentals, focusing on expressions, data types, and operators in Java. It discusses integer and floating-point data types, boolean variables, and the use of flags in programming. Additionally, it covers arithmetic operators, variable casting, operator precedence, and string concatenation, along with a reminder about an upcoming mid-semester test covering these topics.

Uploaded by

John Xavier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views49 pages

Lecture 6(6)

The document outlines key concepts in programming fundamentals, focusing on expressions, data types, and operators in Java. It discusses integer and floating-point data types, boolean variables, and the use of flags in programming. Additionally, it covers arithmetic operators, variable casting, operator precedence, and string concatenation, along with a reminder about an upcoming mid-semester test covering these topics.

Uploaded by

John Xavier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Programming Fundamentals (300580)

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Lecture 6: Expressions

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Quote of the week

“Learning to program is exciting: it's


stimulating, fun, develops new ways of
thinking, and in a very short time you
can build programs that will impress
your friends and family! Discovering
the joy of programming was the spark
that started my professional life and
impressed the teenage beauty who, 40
years later, is still the love of my life!“

John Hennessy
President, Stanford University

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Clara’s Intelligence Level

• Knows conditional
statements
• Knows loops
• Has senses
• Has memory
(variables)
• Doesn’t have vision
yet

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Letterboxes in the US
• A flag on the side of US mailbox is meant
to be used by the owner and a mail delivery
person: It is meant to signal that something
is inside that needs picking up by either a
mail delivery person or the mailbox owner.
• Boolean variables are used in a much the
same way: as flags that must first be set and
can be further tested to make right
decisions

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Clara Postie

• Clara goes international and


gets a job as a postie in the
US
• She must learn using the
letterbox flags and based on
whether the flag is set or
not – decide whether to
pick up mail or deliver to
the street where she is at.

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Integer Data Types


• byte, short, int, and long are all integer data
types.
• They can hold whole numbers such as 5, 10, 23, 89,
etc.
• Integer data types cannot hold numbers that have a
decimal point in them.
• Integers embedded into Java source code are called
integer literals.

int i = 23; //23 is an integer literal

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Floating Point Data Types


• Data types that allow fractional values are called
floating-point numbers.
– 1.7 and -45.316 are floating-point numbers.
• In Java there are two data types that can represent
floating-point numbers.
– float - also called single precision (7
decimal points).
– double - also called double precision (15
decimal points).

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Floating Point Literals


• When floating point numbers are embedded into Java
source code they are called floating point literals.
• The default type for floating point literals is double.
– 29.75, 1.76, and 31.51 are double data types.
• Java is a strongly-typed language.

Suggestion: Only use double in this course!

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Floating Point Literals


• A double value is not compatible with a float
variable because of its size and precision.
float number;
number = 23.5; // Error!

• A double can be forced into a float by appending


the letter F or f to the literal.
– float number;
– number = 23.5F; // This will
work.

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Floating Point Literals


• Literals cannot contain embedded currency
symbols or commas.
– grossPay = $1,257.00; // ERROR!
– grossPay = 1257.00; // Correct.
• Floating-point literals can be represented in
scientific notation.
– 47,281.97 == 4.728197 x 104.
• Java uses E notation to represent values in
scientific notation.
– 4.728197X104 == 4.728197E4.

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

The char Data Type


• The Java char data type provides access to
individual characters (only one character).
• char literals are enclosed in single quote marks.
– 'a', 'Z', '1'
• Don’t confuse char literals with string literals.
– char literals are enclosed in single quotes.
– String literals are enclosed in double quotes.

char lowerCaseA = 'a';

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

The boolean Data Type

• The Java boolean data type can have two possible


values.
– true
– false
• The value of a boolean variable may only be copied
into a boolean variable.

boolean b = true;
boolean p = b;

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Flags
• A flag is a boolean variable that monitors some
condition in a program.
• When a condition is true, the flag is set to true.
• The flag can be tested to see if the condition has
changed.
if (average >= 85)
highScore = true;
• Later, this condition can be tested:
if (highScore)
System.out.println("That′s a high score!");

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Flags in your Practicals

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Clara Postie

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Console Output
• Use System.out.println() to print a String
value and change to new line.

public void run() {


System.out.println("String 1");
System.out.println("String 2");
}
Result:
String 1
String 2

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Console Output
• Use System.out.print() to print a String
value without changing to new line.

public void run() {


System.out.print("String 1");
System.out.print("String 2");
}
Result:
String 1String 2

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Variables and Literals


This line is called The following line is known
a variable declaration. as an assignment statement.
int value; value = 5;

0x000 The value 5


0x001 5 is stored in
0x002 memory.
0x003

This is a string literal. It will be printed as is.

System.out.print("The value is ");


System.out.println(value);
The integer 5 will
be printed out here.
Notice no quotation marks?
Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics
Programming Fundamentals (300580)

Variables and Expressions


• Expression example:
– int total = n1 + n2;
– total = total + 1;

• Expression is a bunch of terms and operators


• Terms can be:
– variables: like n1
– constant values: like 3
– method calls: like readInt()
Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics
Programming Fundamentals (300580)

Arithmetic Operators
Operator Meaning Type Example

+ Addition Binary total = cost + tax;

- Subtraction Binary cost = total – tax;

* Multiplication Binary tax = cost * rate;

/ Division Binary salePrice = original / 2;

% Remainder Binary remainder = value % 5;

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Arithmetic Operators
• The operators are called binary operators because
they must have two operands.
• Each operator must have a left and right operator.
• The arithmetic operators work as one would
expect in algebra (with small exceptions).
• It is an error to try to divide any number by zero.
• When working with two integer operands, the
division operator requires special attention.

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Subtraction
• n1 – n2
• Works similar to mathematics
• Can also represent unary minus
• -n2 + n1

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Remainder
• Only works with integer values
• Returns a remainder of division between the two
operands

2 R 1
7%2= 1
2|5
7 % 20 = 7

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Division
• All other operators work the way you would
expect
• Division works slightly differently depending on
the type of the arguments you divide
• Division of real numbers is simple:

5.0 / 2.0 == 2.5

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Integer Division
• Division can be tricky.
In a Java program, what is the value of 1/2?
• You might think the answer is 0.5…
• But, that’s wrong.
• The answer is simply 0.
• Integer division will truncate any decimal remainder.

1/2.0 == 0.5 – because the compiler knows 2.0 is


a double value

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Common Division Error


• Let’s imagine you have the following code

int x = 5;
double y = x / 2;

• What is the result?


• The result is 2.0
• Because Java first evaluates the right hand side and
the result is 2
• Then it casts it to type double and makes it 2.0
Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics
Programming Fundamentals (300580)

Variable casting
• To deal with this problem we must employ type
casting on one of the operands in the right hand side

int x = 5;
double y = (double) x / 2;
• You can also do the opposite:
The result is ?
double y = 2.5;
z = 2;
int z = (int) y;

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Example: Aussie Postie

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Operator Precedence
• Mathematical expressions can be very complex.
• There is a set order in which arithmetic operations will
be carried out.

Operator Associativity Example Result


-
(unary Right to left x = -4 + 3; -1
Higher
Priority negation)
x = -4 + 4 % 3 * 13 +
* / % Left to right 2; 11
Lower
Priority + - Left to right x = 6 + 3 – 4 + 6 * 3; 23

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Grouping with Parenthesis


• When parenthesis are used in an expression, the inner
most parenthesis are processed first.
• If two sets of parenthesis are at the same level, they
are processed left to right.
3

• x = ((4*5) / (5-2) ) – 25; // result = -19

1 2

4
Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics
Programming Fundamentals (300580)

The + Operator
• The + operator can be used in two ways.
– as a concatenation operator
– as an addition operator
• If either side of the + operator is a string,
the result will be a string.
System.out.println("Hello " + "World");
System.out.println("The value is: " + 5);
System.out.println("The value is: " + value);
System.out.println("The value is: " + ‘/n’ + 5);

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

String Concatenation
• Java commands that have string literals must be
treated with care.
• A string literal value cannot span lines in a Java
source code file.
System.out.println("This line is too long and now
it has spanned more than one line, which will
cause a syntax error to be generated by the
compiler. ");

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

String Concatenation
• The String concatenation operator can be used to fix
this problem.
System.out.println("These lines are " +
"are now ok and will not " +
"cause the error as before.");
• String concatenation can join various data types.
System.out.println("We can join a string to " +
"a number like this: " + 5);

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

String Concatenation
• The Concatenation operator can be used
to format complex String objects.
System.out.println("The following will be printed "
+
"in a tabbed format: " +
\n\tFirst = " + 5 * 6 + ", " +
"\n\tSecond = " (6 + 4) + "," +
"\n\tThird = " + 16.7 + ".");

• Notice that if an addition operation is also


needed, it must be put in parenthesis.
Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics
Programming Fundamentals (300580)

Example: Average

Compute an average of 2 integer numbers

num1+num2
average =
2

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Mid-Semester Test

• Week 7 (next week) during the lecture


• Don’t forget your student ID, or otherwise you will
not be allowed to take the test!
• 20 questions
• Multiple choice only
• 60 minutes duration
• Covers material from the first 6 lectures

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Mid-Semester Test

• Things you should know well:


– Variables
– Expressions
– Arithmetic Operators
– Loops
– Conditional Statements
– Text output with System.out.println() and
System.out.print();

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples
• What’s wrong with the program below?

public class MyClara extends Clara {

public void act()


}
int a,b,c \\ Three integers
a = 3
b = 4
c = a + b

System.out.println('The value of C is: ' + C);


{
}

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples
• What’s wrong with the program below?

public class MyClara extends Clara {

public void act()


}
int a,b,c; \\ Three integers
a = 3;
b = 4;
c = a + b;

System.out.println('The value of C is: ' + C);


{
}

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples

• The expression 17 + 5 / 2 – 2*3 yields:

a) 17
b) 13.5
c) 13
d) 22.5

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples

• The expression 17 + 5 / 2 – 2*3 yields:

a) 17
b) 13.5
c) 13
d) 22.5

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples

• What is the output of the code below

for (int i = 0; i < 10; i++)


{
System.out.print(i + " ");
}

a) 0 1 2 3 4 5 6 7 8 9
b) 1 2 3 4 5 6 7 8 9 10
c) Syntax error
d) 9

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples

• What is the output of the code below

for (int i = 0; i < 10; i++)


{
System.out.print(i + " ");
}

a) 0 1 2 3 4 5 6 7 8 9
b) 1 2 3 4 5 6 7 8 9 10
c) Syntax error
d) 9

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples

• What is the output of the code below

int i = 0;
{
for (;i < 10; i ++)
System.out.print(i + " ");
System.out.print( (i+1) + " ");
}
a) 0 1 2 3 4 5 6 7 8 9
b) 0 1 2 3 4 5 6 7 8 9 11
c) Syntax error
d) 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples

• What is the output of the code below

int i = 0;
{
for (;i < 10; i ++)
System.out.print(i + " ");
System.out.print( (i+1) + " ");
}
a) 0 1 2 3 4 5 6 7 8 9
b) 0 1 2 3 4 5 6 7 8 9 11
c) Syntax error
d) 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples

• If treeFront() is true the boolean expression:


( !treeFront() || onLeaf() )
is :
a) false
b) true if onLeaf() is true
c) true
d) true if onLeaf() is false

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Question Examples

• If treeFront() is true the boolean expression:


( !treeFront() || onLeaf() )
is :
a) false
b) true if onLeaf() is true
c) true
d) true if onLeaf() is false

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics


Programming Fundamentals (300580)

Textbook

This lecture is covered in


Chapter 6 of the textbook

Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics

You might also like