| CSCI A201/A597Lecture Notes 10 Spring 2000 |
First we start with a boolean
simplification that we didn't quite do last
time. We will prove that
can be simplified toa != true
Same thing can be said about!a
which is the same asa != false
for any value ofa
a.
Then we prove that
is the same as! (a && b)
which is incidentally also known as one of the DeMorgan's laws.!a || !b
We will solve a few more exercises like that:
!( a || b) same as !a && !b
eligible_female same as !eligible_male && eligible
i < 3 && i > 10 same as false
i < 3 && i < -4 same as i < -4
i < 10 || i > 3 same as true
We actually go over a few things regarding the
Math class
and String objects.
We look up the two classes on the web and learn how to read their descriptions.
Then we go over for loops again.
We present the following interpretation of the loops.
First we review a single for loop.
The syntax is like this:
Here's a diagram that not only shows the flow of control but also gives some specific examples for the initialization step, condition, re-initialization step and body of thefor (<init>; <condition>; <re-init>) <statement>
for loop.
This is a simple loop because we first want to make clear we understand the flow of control. The body is as simple as it can be. There is just one statement which is an invocation of a function. A star will be printed for each value of![]()
i from 0 to 9.
Once it's clear how it works we blow up the body of the for loop and place in it another
for loop. The new path is drawn in red but if we want to ignore it we can and we recover
the simple example from above.
This new example works the same except the task of printing one star has now become much more complex. Instead of printing a star for each value of![]()
i we now need to give values to j
from 0 to 10. Here's a simplified version of the entire mechanism in which we
provide the relevant information in the key points of the process.
In this new picture we also add a call to![]()
println after the inner loop (whose body consists
of the original print a star invocation. We will discuss these examples and also go over this one, as a test:
int k, p;
for (k = 0; k < 10; k = k + 1) {
for (p = 0; p < 10; p = p + 1) {
if (k == 0) {
System.out.print(" ");
} else {
System.out.print(k);
}
System.out.println(p);
}
}
// System.out.println(100);
What will the above code fragment print? If we have time we will also go over the following program:
frilled.cs.indiana.edu%cat Example.java
class Student {
int age;
boolean eligible;
String name;
char gender;
double gpa;
void introduction() {
System.out.println("Hello!!!");
System.out.println("My name is: " + name);
System.out.println("Gender: " + gender);
System.out.println("Eligible: " + eligible);
System.out.println("I am " + age + " years old.");
System.out.println("My GPA is: " + gpa);
}
}
class Example {
public static void main(String[] args) {
Student a = new Student();
a.name = "Larry Bird";
a.gpa = 3.9;
a.gender = 'M';
a.age = 21;
a.eligible = (1 == 1);
a.introduction();
}
}
frilled.cs.indiana.edu%javac Example.java
frilled.cs.indiana.edu%java Example
Hello!!!
My name is: Larry Bird
Gender: M
Eligible: true
I am 21 years old.
My GPA is: 3.9
frilled.cs.indiana.edu%
Except we probably will take it step by step, starting with the simpler:
frilled.cs.indiana.edu%cat Simpler.java
public class Simpler {
public static void main(String[] args) {
Student a = new Student();
a.name = "Michael Jordan";
a.age = 23;
System.out.println(
"Student " + a.name + " is " + a.age + " years old."
);
}
}
class Student {
int age;
String name;
}
frilled.cs.indiana.edu%javac Simpler.java
frilled.cs.indiana.edu%java Simpler
Student Michael Jordan is 23 years old.
frilled.cs.indiana.edu%