| CSCI A201/A597Lecture Notes 20 Spring 2000 |
We start with the following program.
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
System.out.println(greeting);
System.out.println(intro);
}
}
What is it doing? We then make a modification.
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
System.out.println(intro);
}
}
class Library {
public static void printGreeting(String g) {
System.out.println(g);
}
}
What is its output? How does it work? We've talked about this in lecture on Tuesday.
Let's make some more changes.
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
Library.printIntro(intro);
}
}
class Library {
public static void printGreeting(String g) {
System.out.println(g);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
What's the program doing now? How does it work?
We will keep changing. Here's a new version:
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
Library.printGreeting(intro);
}
}
class Library {
public static void printGreeting(String g) {
System.out.println(g);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
Do you see what changed? Do you have anything to say about it?
How's the program working?
What's its output?
Let's get back to the one we had before:
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
Library.printIntro(intro);
}
}
class Library {
public static void printGreeting(String g) {
System.out.println(g);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
OK, now let's change it.
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String g) {
System.out.println(g);
Library.printIntro(intro);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
What does the program do? Really?
Fine, let's compile and run it to verify that.
OK, can you explain why that happens?
Now let's change it again.
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String g) {
System.out.println(g);
Library.printIntro("I'm Adrian.");
}
public static void printIntro(String i) {
System.out.println(i);
}
}
How does it work? What does it print? Let's make some more changes.
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String g) {
String intro = "I'm Adrian.";
System.out.println(g);
Library.printIntro("I'm Adrian");
}
public static void printIntro(String i) {
System.out.println(i);
}
}
Please describe the change(s) and their effect on the program. One more change:
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String g) {
String intro = "I'm Adrian.";
System.out.println(g);
Library.printIntro(intro);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
OK, this was an easy one... Are you ready for the next change?
Fine, here it is:
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String g) {
String intro = "I'm Adrian.";
System.out.println(g);
Library.printGreeting(intro);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
Do you have anything to say about it? Is the program compiling? Why or why not?
If the program is compiling, what does it print (if anything)?
Why?
We'll talk about this some more in a little bit...
Let's backup a little, for a few minor comments (and changes).
We start from this:
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String g) {
String intro = "I'm Adrian.";
System.out.println(g);
Library.printIntro(intro);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
And make this change:
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String x) {
String intro = "I'm Adrian.";
System.out.println(g);
Library.printIntro(intro);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
What can you say about it? OK, then let's make one more change.
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String x) {
String intro = "I'm Adrian.";
System.out.println(x);
Library.printIntro(intro);
}
public static void printIntro(String i) {
System.out.println(i);
}
}
What do you think about it now? Now get ready for several changes performed at all at once:
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String greeting) {
String intro = "I'm Adrian.";
System.out.println(greeting);
Library.printIntro(intro);
}
public static void printIntro(String intro) {
System.out.println(intro);
}
}
What do you think? Indeed, so let's do it again.
public class Mar23 {
public static void main(String[] args) {
String greeting, intro;
greeting = "Hello!";
intro = "I'm Adrian.";
Library.printGreeting(greeting);
}
}
class Library {
public static void printGreeting(String string) {
String intro = "I'm Adrian.";
System.out.println(string);
Library.printIntro(intro);
}
public static void printIntro(String string) {
System.out.println(string);
}
}
Can you see what changed? Can you explain the effect of the changes, if any?
Can you describe the changes briefly?
Take a break now, when we come back we'll start a new set of examples.
public class Mar23 {
public static void main(String[] args) {
}
}
class Library {
}
What can you say about it? Compile and run it do double-check your answer.
Now let's start modifying it.
public class Mar23 {
public static void main(String[] args) {
int result;
result = 3 + 5;
System.out.println(result);
}
}
class Library {
}
What is it doing? What's Library used for? What about it now?
public class Mar23 {
public static void main(String[] args) {
int result;
result = 3 + 5;
System.out.println(result);
}
}
class Library {
public static void add(int a, int b) {
System.out.println(a + b);
}
}
Can we use that?
public class Mar23 {
public static void main(String[] args) {
int result;
result = 3 + 5;
System.out.println(result);
add(3, 5);
}
}
class Library {
public static void add(int a, int b) {
System.out.println(a + b);
}
}
What can you say about the program? What about it now:
public class Mar23 {
public static void main(String[] args) {
int result;
result = 3 + 5;
System.out.println(result);
Library.add(3, 5);
}
}
class Library {
public static void add(int a, int b) {
System.out.println(a + b);
}
}
OK, let's get rid of the extra line in main,
public class Mar23 {
public static void main(String[] args) {
int result;
result = 3 + 5;
Library.add(3, 5);
}
}
class Library {
public static void add(int a, int b) {
System.out.println(a + b);
}
}
and now let's make two changes to the resulting program
public class Mar23 {
public static void main(String[] args) {
int result;
result = 3 + 5;
Library.add(3, 5);
}
}
class Library {
public static int add(int a, int b) {
return (a + b);
}
}
One of the changes is marked in blue
so you can quickly identify it. Can you see what the other change is?
How does the program work?
The method evaluates the sum of the values and sends that to the code that called it. However, it is not used there at all, so once the value is returned it is lost if we don't store it in a variable (or storage location).Exactly. Can we then make the following change?
public class Mar23 {
public static void main(String[] args) {
int result;
result = 3 + 5;
result = Library.add(3, 5);
}
}
class Library {
public static int add(int a, int b) {
return (a + b);
}
}
Can you describe how this works?
Yes, the result is stored in the variable namedVery good. So let's change things a bit.result. And since the method is called with3and5this assignment is in a certain sense redundant.
public class Mar23 {
public static void main(String[] args) {
int result;
result = Library.add(25, 75);
System.out.println(result);
}
}
class Library {
public static int add(int a, int b) {
return (a + b);
}
}
What does the program output? How does it work?
OK, how about this one, then:
public class Mar23 {
public static void main(String[] args) {
int result;
result = 23 + Library.add(25, 75);
System.out.println(result);
}
}
class Library {
public static int add(int a, int b) {
return (a + b);
}
}
And how does this one work?
public class Mar23 {
public static void main(String[] args) {
int result;
result = Library.add(10 + 15, 75);
System.out.println(result);
}
}
class Library {
public static int add(int a, int b) {
return (a + b);
}
}
Or this one:
public class Mar23 {
public static void main(String[] args) {
int result;
result = Library.add(Library.add(10, 15), 75);
System.out.println(result);
}
}
class Library {
public static int add(int a, int b) {
return (a + b);
}
}
This I think would be enough for today. See you in lab.
Appendix:
In class we will be using the following diagrams:
1. Class.
class A {
}
2. Class with method.
class A {
public static void fun(int x) {
}
}
3. A method, with no parameters.
public static void fun() {
}
4. A method with one parameter.
public static void fun(int x) {
}