Chapter 13
Code Execution

   13.1 Control Flow
   13.2 Inside Constructors
   13.3 Inside Instance Methods
   13.4 Inside Class Methods
   13.5 Objects and Fields
   13.6 Life Span of Variables and Objects
   13.7 Data Flow
   13.8 String Values
   13.9 Assignment #14: Objects: Issues of Syntax
   13.10 Assignment #15: The Working of Constructors and Methods
   13.11 Assignment #16: Objects: Issues of Semantics
   13.12 Assignment #17: Objects

13.1 Control Flow

class prg { 
  public static void main ( String [] args ){ 
    int iVar; 
    MyClass cVar; 
    cVar = new MyClass(); 
    MyClass.cMethod(); 
    iVar = cVar.iMethod(); 
    System.exit(0); 
} } 
class MyClass { 
  MyClass (){ 
    System.out.println( "a constructor" ); 
  } 
  static void cMethod(){ 
    System.out.println( "a class method" ); 
  } 
  int iMethod(){ 
    System.out.println( "an instance method" ); 
    return 0; 
  } 
} 

13.2 Inside Constructors

class prg { 
  public static void main ( String[] args ){ 
    MyClass a = new MyClass(12); 
} } 
class MyClass { 
  int f1; 
  double f2 = 0.0001; 
  MyClass (int i){ } 
} 
When invoked, a constructor

13.3 Inside Instance Methods

class prg { 
  public static void main ( String[] args ){ 
    MyClass a = new MyClass(); 
    int i = a.myMethod(25); 
} } 
class MyClass { 
  int f1; 
  double f2 = 0.0001; 
  int myMethod (int x){ 
    return 7; } 
} 
When invoked, an instance method

13.4 Inside Class Methods

13.5 Objects and Fields

13.6 Life Span of Variables and Objects

class prg { 
  public static void main ( String[] args ){ 
    MyClass n1 = new MyClass(); 
            n1 = new MyClass(); 
} } 
class MyClass { 
  class int f1; 
  int f2; 
  MyClass (){ } 
} 

13.7 Data Flow

13.8 String Values

Exercises

13.9 Assignment #14: Objects: Issues of Syntax

Due: Mo, May 12, midnight

Consider the following program.

class lab14 { 
  public static void main ( String[] args ){ 
    Person me, you, other; 
    me = new Person( "my name", "my address", "me@here", 1234567 ); 
    you = new Person( "your name", "your address", "you@there", 7654321 ); 
    other = new Person( "other name", "", "", -1 ); 
    other.changeAddress( "other address" ); 
    other.changeEmail( "other@where" ); 
    other.changePhone( 1357642 ); 
    System.out.println( me ); 
    System.out.println( you ); 
    System.out.println( other ); 
} } 
Add to the program the missing class, so that the program will compile under ‘javac lab14.java’ and execute under ‘java lab14’. Don’t put any instructions within the bodies of the constructors and methods.

The execution of the program should provide output similar to:

Person@720eeb 
Person@3179c3 
Person@310d42 
Assume ‘lab14’ for the submit program. The program should be stored in a file named ‘lab14.java’.

Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade of 0 points.

Q&A

13.10 Assignment #15: The Working of Constructors and Methods

Due: We, May 14, midnight

Consider the following program.

class lab15 { 
  public static void main ( String[] args ){ 
    Person me, you, other; 
    me = new Person( "my name", "my address", "me@here", 1234567 ); 
    you = new Person( "your name", "your address", "you@there", 7654321 ); 
    other = new Person( "other name", "", "", -1 ); 
    other.changeAddress( "other address" ); 
    other.changeEmail( "other@where" ); 
    other.changePhone( 1357642 ); 
    System.out.println( me ); 
    System.out.println( you ); 
    System.out.println( other ); 
} } 
Add to the program the missing class, so that its output will equal:
New Object: 
----------- 
this = Person@256a7c 
name = my name 
address = my address 
email = me@here 
number = 1234567 
 
New Object: 
----------- 
this = Person@720eeb 
name = your name 
address = your address 
email = you@there 
number = 7654321 
 
New Object: 
----------- 
this = Person@3179c3 
name = other name 
address = 
email = 
number = -1 
 
Change Address: 
--------------- 
this = Person@3179c3 
new address = other address 
 
Change Email: 
------------- 
this = Person@3179c3 
new email = other@where 
 
Change Number: 
-------------- 
this = Person@3179c3 
new number = 1357642 
 
Person@256a7c 
Person@720eeb 
Person@3179c3 
Assume ‘lab15’ for the submit program. The program should be stored in a file named ‘lab15.java’.

Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade of 0 points.

Q&A

13.11 Assignment #16: Objects: Issues of Semantics

Due: We, May 21, midnight

Consider the following program.

class lab16 { 
  public static void main ( String[] args ){ 
    Person me, you, other; 
    me = new Person( "my name", "my address", "me@here", 1234567 ); 
    you = new Person( "your name", "your address", "you@there", 7654321 ); 
    other = new Person( "other name", "", "", -1 ); 
    other.changeAddress( "other address" ); 
    other.changeEmail( "other@where" ); 
    other.changePhone( 1357642 ); 
    System.out.println( me ); 
    System.out.println( you ); 
    System.out.println( other ); 
} } 
Add to the program the missing class, so that the execution of the program would provide the following output.
my name, my address, me@here, 1234567 
your name, your address, you@there, 7654321 
other name, other address, other@where, 1357642 
The only methods allowed to be invoked in the class are ‘concat’ from java.lang.String and ‘toString’ from java.lang.Integer.

Assume ‘lab16’ for the submit program. The program should be stored in a file named ‘lab16.java’.

[practice example]

Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade of 0 points.

13.12 Assignment #17: Objects

Due: Fr, May 23, midnight

Consider the following program.

class lab17 { 
  public static void main(String[] args){ 
    Student a, b, c, d, e; 
    a = new Student( "Dan" ); 
    b = new Student( "Evy" ); 
    c = new Student( "Eli" ); 
    d = new Student( "Ron" ); 
    a.friend(d); 
    b.friend(c); 
    c.friend(a); 
    d.friend(b); 
    Student.showStudents(); 
    e = a; 
    System.out.println( e ); 
    e = e.friendOf(); 
    System.out.println( e ); 
    e = e.friendOf(); 
    System.out.println( e ); 
    e = e.friendOf(); 
    System.out.println( e ); 
  } 
} 
Add to the program the missing class, so that the execution of the program would provide the following output.
Dan Evy Eli Ron 
Dan 
Ron 
Evy 
Eli 
The only methods allowed to be invoked in the class are ‘concat’ from java.lang.String and ‘toString’ from java.lang.Integer.

Assume ‘lab17’ for the submit program. The program should be stored in a file named ‘lab17.java’.

Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade of 0 points.