- Problem #1 (10 points)
Provide the missing class so that the given program will output.
a: 1 / 2 = 0.5
a: 3 / 2 = 1.5
a: 6 / 2 = 3.0
a: 10 / 2 = 5.0
b: 0.3333333333333333
b: 1.0
b: 2.0
b: 3.3333333333333335
Your code should not include numbers.
class prg {
public static void main (String[] args){
double value;
int i;
SumDiv a = new SumDiv ( 2 );
for( i=1; i<5; i=i+1 ){
a.add ( i );
System.out.println( "a: " + a );
}
System.out.println();
SumDiv b = new SumDiv ( 3 );
for( i=1; i<5; i=i+1 ){
b.add ( i );
value = b.result();
System.out.println( "b: " + value );
}
} }
- Problem #2 (10 points)
Modify the given program so that it will produce the following output.
1: 12
2: 10
3: 21
4: true
5: true
The only changes you are allowed to do is to replace 0 values with other values, in the assignments to the variables
‘i’, ‘j’, and ‘k’. Moreover, none of these variables may be assigned the 0 value.
class prg {
public static void main ( String [] args ){
int i, j, k, ians;
boolean bans;
i = 0; j = 2; k = 3;
ians = i * j + j % k;
System.out.println( "1: " + ians );
i = 0; j = 7; k = 11;
ians = i / 2 + j / 2 + k / 2;
System.out.println( "2: " + ians );
i = 0; j = 15; k = 17;
ians = (int) ( i / 2.0 )
+ (int) ( j / 2.0 )
+ (int) ( k / 2.0 );
System.out.println( "3: " + ians );
i = 0; j = 4; k = 5;
bans = (i+j > k) && (j+k > i) && (k+i > j);
System.out.println( "4: " + bans );
i = 0; j = 9; k = 11;
bans = (i>j) && !( k<=i );
System.out.println( "5: " + bans );
} }
- Problem #3 (10 points)
Write a program that counts the number of times the substring ‘and’ appears in the input file ‘in.txt’, and writes
this value to a file named ‘out.txt’.
- 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();
}
}