- Problem #1 (10 points)
Show the output of the following program
class prg {
public static void main (String[] args){
Cat c1, c2;
c1 = new Cat ( "miau" );
c2 = c1.puppy( "mia" );
c2 = c1.puppy( "miu" );
c2 = c2.puppy( "miua" );
System.out.println( c2 );
c2 = c2.puppyOf();
System.out.println( c2 );
c2 = c2.puppyOf();
System.out.println( c2 );
} }
class Cat{
String name;
int count = 0;;
Cat parent;
Cat( String nm ){ name = nm; }
Cat puppy( String nm ){
Cat c = new Cat( nm );
c.parent = this;
count = count + 1;
return c;
}
Cat puppyOf( ){
Cat c = this.parent;
return c;
}
public String toString(){
String s;
s = "I'm a cat named "
+ name + "; I have "
+ count + " offsprings.";
return s;
} }
- Problem #2 (10 points)
Provide the missing class for the given program, so that the program will produce the following output.
Calculator #1 shows the value 0
Calculator #2 shows the value 0
Calculator #1 shows the value 5
Calculator #2 shows the value 24
Calculator #1 shows the value 29
Calculator #2 shows the value 24
Suggestion. Program the class in two stages: (1) First consider a class that will allow to compile and execute the
program, without being concerned about the output being produced; (2) Then fix the class to imply the desirable
output.
class prg {
public static void main (String[] args){
Calculator c1, c2;
int v;
c1 = new Calculator ();
c2 = new Calculator ();
System.out.println( c1 );
System.out.println( c2 );
c1.plus ( 5 );
c2.plus( 12 );
c2.plus( c2 );
System.out.println( c1 );
System.out.println( c2 );
v = Calculator.plus (c1,c2);
c1.plus( c2 );
System.out.println( c1 );
System.out.println( c2 );
} }
- Problem #3 (10 points)
Modify the given program so that it will produce the following output.
1: 27
2: 21
3: 16
4: false
5: true
The only changes you are allowed to do is to replace 0 values with other values, in the assignments to the variable
‘j’. Moreover, none of the assigned values may be 0.
class prg {
public static void main ( String [] args ){
int i, j, k, ians;
boolean bans;
i = 5; j = 0; k = 3;
ians = i * j + j % k;
System.out.println( "1: " + ians );
i = 7; j = 0; k = 21;
ians = i / 2 + j / 2 + k / 2;
System.out.println( "2: " + ians );
i = 3; j = 0; k = 15;
ians = (int) ( i / 2.0 )
+ (int) ( j / 2.0 )
+ (int) ( k / 2.0 );
System.out.println( "3: " + ians );
i = 11; j = 0; k = 4;
bans = (i+j > k) && (j+k > i) && (k+i > j);
System.out.println( "4: " + bans );
i = 7; j = 0; k = 9;
bans = (i>j) && !( k<=i );
System.out.println( "5: " + bans );
} }
- Problem #4 (10 points)
Provide the missing code so that the program will produce the listed output.
---------------------
--*------------------
--*------------------
-*******************-
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
--*------------------
---------------------
class prg {
public static void main (String[] args){
char [][] s = new char [21][21];
init( s );
stars( s );
show( s );
}
static void stars(char [][] s){
/************ missing code ********/
}
static void init(char [][] s){
int i, j;
for( i=0; i<21; i=i+1 ){
for( j=0; j<21; j=j+1 ){
s[i][j] = '-';
} } }
static void show(char [][] s){
int i, j;
for( i=0; i<21; i=i+1 ){
for( j=0; j<21; j=j+1 ){
System.out.print( s[i][j] );
}
System.out.println();
}
System.out.println();
}
}