do
/while
LoopHomework 6 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
I have posted homework 6 here.
It is due this coming Sunday at 11:59 PM.
The mid-term exam will be given on Monday, March 23rd.
It will consist of questions like those on the quizzes along with questions asking you to write short segments of Java code.
60% of the points on this exam will consist of questions from the Ungraded Class Quizzes.
The other 40% will come from four questions that ask you to write a short segment of code.
The last class before the exam, Friday, March 13th, will be a review session.
You will only be responsible for the material in the Class Notes for that class on the exam.
The Mid-term is a closed book exam.
The due date for homework assignments 2 - 6 is Friday, March 20th at 11:59 PM.
That is the Friday of the Spring Break.
Any of these assignments submitted after that date will receive a score of 0.
&&
(and) operator returns true if both its
operands are true
5 > 4 && 4 > 2 : true
&&
returns false
5 > 4 && 4 < 2 : false
5 < 4 && 4 < 2 : false
||
(or) operator returns true if either of its operands are true
5 > 4 || 4 < 2 : true
5 < 4 || 4 < 2: false
!
(not) operator takes only one operand and reverses its value
! 5 > 4: false
! 5 < 4: true
&&
(and)
p | q | p && q |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
||
(or)
p | q | p || q |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
!
(not)
p | ! p |
---|---|
true | false |
false | true |
a + b > c and c * d < athere are three different kind of operators
! | Logical NOT |
* / % | Multiplication, division and remainder |
+ - | Addition and subtraction |
> < >= <= | Relational Operators |
== != | Equality Operators |
&& | Logical AND |
|| | Logical OR |
while
loop has the following format
while (BOOLEAN_EXPRESSION){
STATEMENT;
STATEMENT;
...
}
java
interpreter comes to a while
loop it evaluates the BOOLEAN_EXPRESSION
do
/while
Loopdo
/while
loop
do {
STATEMENT;
STATEMENT;
...
}
while (BOOLEAN_EXPRESSION);
do
/while
and the while
loop is when the boolean expression is
evaluated
while
loop the code block is only executed
if the boolean expression is true
do
/while
loop the loop
body is always executed at least once
x = x + 1; y = y - 1; z = z + 2;
x += 1; y -= 1; z += 2;
public class CountByThree { public static void main (String[] args){ int num = 1; for (int i = 1; i <= 10; i++){ System.out.println(num); num += 3; } } } $ java CountByThree 1 4 7 10 13 16 19 22 25 28
public class CountDownByThree { public static void main (String[] args){ int num = 28; while (num >= 1) { System.out.println(num); num -= 3; } } } $ java CountDownByThree 28 25 22 19 16 13 10 7 4 1
x = 5; x *= 2; # x is now 10
x = 10; x /= 2; # x is now 5
x = 11; x %= 3; # x is now 3
Expression | Read As |
---|---|
x += 2 | add 2 to x |
x -= 2 | subtract 2 from x |
x *= 2 | mulitply x by 2 |
x /= 2 | divide x by 2 |
x %= 2 | take the remainder when x is divided by 2 |
Description | Operators |
---|---|
Unary operators | ! ++ -- |
Multiplicative operators | * / % |
Additive operators | + - |
Relational operators | < > <= >= |
Equality operators | == != |
Logical AND | && |
Logical OR | || |
Assignment operators | = += -= *= |
public class OneToTen { public static void main (String[] args){ int n1 = 1; int n2 = 2; int n3 = 3; int n4 = 4; int n5 = 5; int n6 = 6; int n7 = 7; int n8 = 8; int n9 = 9; int n10 = 10; int total = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10; System.out.println("The total of 1 to 10 is " + total); } } $ java OneToTen The total of 1 to 10 is 55
public class Print3Args { public static void main (String[] args){ System.out.println(args[0]); System.out.println(args[1]); System.out.println(args[2]); } } $ java Print3Args foo bar bletch foo bar bletch
for
loop to get each value in an array
but we have to know the length of the array to do it
ARRAY_VARIABLE.length
args.length
public class PrintArgs {
public static void main (String[] args){
for (int i = 0; i < args.length; i++){
System.out.println(args[i]);
}
}
}
$ java PrintArgs 1 2 3 4 5
1
2
3
4
5
for
loop to compute the average
of numbers entered on the command line
public class Average { public static void main (String[] args){ double = 0; for (int i = 0; i < args.length; i++){ total += Integer.parseInt(args[i]); } System.out.println("The average is " + total/args.length); } } $ java Average 1 2 3 4 5 6 7 8 9 10 The average is 5.5
double
int
we would lose the decimal part of the result
DATA_TYPE int VARIABLE_NAME = new DATA_TYPE[LENGTH];
public class IntArray { public static void main (String[] args){ int [] int_array = new int [5]; for (int i = 0; i < 5; i++){ int_array[i] = i + 1; } for (int i = 0; i < 5; i++){ System.out.println(int_array[i]); } } } $ java IntArray 1 2 3 4 5
public class DoubleArray { public static void main (String[] args){ double [] double_array = new double [5]; for (int i = 0; i < 5; i++){ double_array[i] = i + 1.0 ; } for (int i = 0; i < 5; i++){ System.out.println(double_array[i]); } } } $ java DoubleArray 1.0 2.0 3.0 4.0 5.0
public class IntNotInitialized {
public static void main (String[] args){
int num;
System.out.println(num);
}
}
$ javac IntNotInitialized.java
IntNotInitialized.java:4: error: variable num might not have been initialized
System.out.println(num);
^
1 error
int num = 0;
public class InitIntArray { public static void main (String[] args){ int [] int_array = new int[3]; for (int i = 0; i < 3; i++){ System.out.println("int_array["+ i +"] = " + int_array[i]); } } } $ java InitIntArray int_array[0] = 0 int_array[1] = 0 int_array[2] = 0
Type | Value |
---|---|
int | 0 |
double | 0.0 |
char | '\0' |
boolean | false |
public class AutoInit { public static void main (String[] args){ System.out.println("Integer array"); int [] int_array = new int[3]; for (int i = 0; i < 3; i++){ System.out.println(int_array[i]); } System.out.println("Double array"); double [] double_array = new double[3]; for (int i = 0; i < 3; i++){ System.out.println(double_array[i]); } System.out.println("Boolean array"); boolean [] boolean_array = new boolean[3]; for (int i = 0; i < 3; i++){ System.out.println(boolean_array[i]); } } } $ java AutoInit Integer array 0 0 0 Double array 0.0 0.0 0.0 Boolean array false false false
{VALUE, VALUE, ...}
public class ArrayLiteral { public static void main (String[] args){ int [] odds = {1, 3, 5, 7, 9}; for (int i = 0; i < odds.length; i++){ System.out.println(odds[i]); } } } $ java ArrayLiteral 1 3 5 7 9
VARIABLE_NAME = NEW_VALUE
VARIABLE_NAME = NEW_VALUE[INDEX]
public class ChangeElements { public static void main (String[] args){ int [] numbers = new int[5]; numbers[0] = 1; numbers[1] = 2; numbers[2] = 3; numbers[3] = 4; numbers[4] = 5; for (int i = 0; i < 5; i++){ System.out.println(numbers[i]); } } } $ java ChangeElements 1 2 3 4 5
public class ChangeElements2 { public static void main (String[] args){ int [] odds = new int[5]; int n = 1; for (int i = 0; i < odds.length; i++){ odds[i] = n; n += 2; } for (int i = 0; i < odds.length; i++){ System.out.println(odds[i]); } } } $ java ChangeElements2 1 3 5 7 9
for
loop
for
loop
for
loops above also use the < operator
in the loop test
public class BoundsTest {
public static void main (String[] args){
int [] numbers = new int[5];
for (int i = 1; i <= 5; i++){
numbers[i] = 1;
}
}
}
$ java BoundsTest
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at BoundsTest.main(BoundsTest.java:5)
int [] nums = new int[5]
new
tells the Java interpreter to create an array object
in RAM
java
create the object it returns it location
in memory