1:33 PM 3/12/2015

Adrian German 
dgerman@indiana.edu

Teammate: Mardin Yadegar

Lab Nine 

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta();
  }
  Alpha(int i) {  }
}

class Beta extends Alpha { 
  Beta() {
    super();  
  }
}

Answer: 

(a) If it were to compile and I would run it the main would be executed. 
If it's executed the constructor with no arguments in Beta will be invoked.
The constructor exists so thus far apparently no problem. However since the
default no arg constructor is empty it has a super() as the first line. But
that means that Alpha's constructor with no arguments will be called first.
But we don't have such a constructor in Alpha. So this code does not compile.

(b) -bash-4.1$ cat One.java

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta();
  }
  Alpha(int i) {  }
}
class Beta extends Alpha { }
-bash-4.1$ javac One.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
One.java:8: error: constructor Alpha in class Alpha cannot be applied to given types;
class Beta extends Alpha { }
^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error
-bash-4.1$

(c) I was right. The error message is a bit hard to understand though. 

From the multiple choices I would choose 
      (d) The code does not compile because Alpha doesn't define a no-args constructor. 

I can prove that what's missing is indeed a no-arg constructor in Alpha. 
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta();
  }
}
class Beta extends Alpha { }

So this compiles and runs (and prints nothing). 

2. Compile and run the code:

-bash-4.1$ pwd
/u/dgerman/02132015/002
-bash-4.1$ ls -l
total 8
-rw-r--r-- 1 dgerman faculty 110 Mar 12 13:46 Alpha.java
-rw-r--r-- 1 dgerman faculty  49 Mar 12 13:46 Beta.java
-bash-4.1$ cat Alpha.java
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha (int i) {  }
}
-bash-4.1$ cat Beta.java
class Beta extends Alpha {
  Beta(int i) {
  }
}
-bash-4.1$

(a) For same reason it won't compile. 

(b) -bash-4.1$ javac *.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
Beta.java:2: error: constructor Alpha in class Alpha cannot be applied to given types;
  Beta(int i) {
              ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error
-bash-4.1$ pwd
/u/dgerman/02132015/002
-bash-4.1$ ls
Alpha.class  Alpha.java  Beta.java
-bash-4.1$

(c) I was right. What's missing is a no arg constructor in Alpha. 

-bash-4.1$ pico -w Alpha.java
-bash-4.1$ cat Alpha.java
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  // Alpha (int i) {  }
}
-bash-4.1$ javac *.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.1$ ls
Alpha.class  Alpha.java  Beta.class  Beta.java
-bash-4.1$

So providing a no-arg constructor gets rid of the error. 

3. Compile and run this code 

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha() { System.out.println(0); }
  Alpha(int i) {
    System.out.println(i);
  }
}
class Beta extends Alpha {
  Beta()      { }
  Beta(int i) { }
}

(a) It will compile, run and print a 0 (zero). 

(b) 

-bash-4.1$ pwd
/u/dgerman/02132015/003
-bash-4.1$ ls
Alpha.java  Beta.java
-bash-4.1$ cat Alpha.java
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha() { System.out.println(0); }
  Alpha(int i) {
    System.out.println(i);
  }
}
-bash-4.1$ cat Beta.java
class Beta extends Alpha {
  Beta()      { }
  Beta(int i) { }
}
-bash-4.1$ javac *.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.1$ java Alpha
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
0

(c) I  was right again. Here's a thought: 

class Horse {
  String name; 
  Horse(String name) {
    this.name = name; 
  }
  void talk() {
    System.out.println("Howdy, my name is: " + this.name); 
  }
}

class Unicorn extends Horse {
  
}

This has a problem. I think and I produce:


-bash-4.1$ more *.java
::::::::::::::
Example.java
::::::::::::::
class Example {
  public static void main(String[] args) {
    Unicorn a = new Unicorn("Alex");
    a.talk(); // Howdy, I am Alex.
    // how do I make it where the Unicorns say it in French
  }
}
::::::::::::::
Horse.java
::::::::::::::
class Horse {
  String name;
  Horse(String name) {
    this.name = name;
  }
  void talk() {
    System.out.println("Howdy, my name is: " + this.name);
  }
}

::::::::::::::
Unicorn.java
::::::::::::::
class Unicorn extends Horse {
  Unicorn(String name) {
    super(name);
  }
  void talk() {
    System.out.println("Je m'appelle " + this.name);
  }
}

-bash-4.1$

Now I move the next exercise.