Midterm Exam

CS 1 - I

FALL 2000


Problems :


1.

Add four more member methods to the class "Arithmetic" (attached below)  :
    (i)    public int mult_and_ret() that computes the product of the two data member operands (op1 and op2) and returns it,
    (ii)   public void mult() that computes the product of the two data member operands (op1 and op2) and stores it into the third data member of the class temp_sto,
    (iii)   public int div_and_ret() that first checks if the operand op2 is zero or not and if not, computes the quotient (op1/op2) of the two data member operands (op1 and op2) and returns it. If the operand op2 turned out to be zero, the 0 value is returned, after an error message "Division by zero attempted. Discard the returned output." got printed.
    (iv)   public void div() that that first checks if the operand op2 is zero or not and if not, computes the quotient (op1/op2) of the two data member operands (op1 and op2) and stores it into the third data member of the class temp_sto. If the operand op2 turned out to be zero, the 0 value is stored into the third data member of the class temp_sto and an error message "Division by zero attempted. Discard the value in temp_sto." got printed.

Then, create an application class "MT" that :
    (i)    creates an instance of the "Arithmetic" class,
    (ii)    reads two int values into its two data member operands (op1 and op2) by calling one of its member methods,
    (iii)   displays the values of all of its data members as they are initially by calling some of its member methods,
    (iv)   invokes the public int mult_and_ret() member method and then shows the value returned by it,
    (v)    invokes the public int div_and_ret() member method and then shows the value returned by it,
    (vi)   invokes the public void mult() member method and then shows the changes it produced in its accessing object, and
    (vii)  invokes the public void div() member method and then shows the changes it produced in its accessing object.
 

Attachment :

import SimpleIO.*;

public class Arithmetic extends SimpleGui{

         private     int     op1, op2;

         private     int     temp_sto;

         public void read_ops() {
              op1    =    getInt ("op1= ");
              op2    =    getInt ("op2= ");
         }

         public void display_ops() {
              displayResult ("op1=" + op1 + " op2=" + op2);
         }

         public void display_temp_sto() {
              displayResult ("temp_sto=" + temp_sto);
         }
 

         public int add_and_ret () {
              return (op1+op2);
         }

         public void add () {
              temp_sto = op1+op2;
         }

         public int sub_and_ret () {
              return (op1-op2);
         }

         public void sub () {
              temp_sto = op1-op2;
         }

        // (i)
         public int mult_and_ret() {
            //    Your code

                return op1 * op2;

         }

        // (ii)
         public void mult() {
            //    Your code

                temp_sto = op1 * op2;

         }

        // (iii)
         public int div_and_ret() {
            //    Your code

                if (op2 != 0)
                    return op1/op2;
                else {
                    displayResult("Division by zero attempted. Discard the returned output.");
                    return 0;
                }

         }

        // (iv)
         public void div() {
            //    Your code

                if (op2 != 0)
                    temp_sto = op1/op2;
                else {
                    displayResult("Division by zero attempted. Discard the returned output.");
                    temp_sto =  0;
                }
 

         }

 }

========================================================================

// MT.java

import SimpleIO.*;
class MT extends SimpleGui {
        public static void main (String[] args) {
                // (i)
                Arithmetic ar = new Arithmetic();

                // (ii)
                ar.read_ops();

                // (iii)
                ar.display_ops();
                ar.display_temp_sto();

                // (iv)
                displayResult ("Product of op1 and op2 is " + ar.mult_and_ret() + "." );
                    /*
                        int mu = ar.mult_and_ret();
                        displayResult ("Product of op1 and op2 is " + mu + "." );
                    */

                // (v)
                displayResult ("Qotient of op1 and op2 is " + ar.div_and_ret() + "." );
                    /*
                        int di = ar.div_and_ret();
                        displayResult ("Quotient of op1 and op2 is " + di + "." );
                    */

                // (vi)
                ar.mult();
                ar.disply_temp_sto();

                // (vii)
                ar.div();
                ar.disply_temp_sto();

        }
}
 
 
 
 

(61 (=10+10+15+16+10)  points)

2.
        Find all syntax errors in the following JAVA method and correct them. Then, explain what it does. Please be brief and up to the point.
public void int question ( float X, Y, Z );
            {;
               if  (X == Y)
                    if (X == Z)
                      return 0
               return 1

public  int question ( float X, float Y,  float Z )
            {
               if  (X == Y)
                    if (X == Z)
                      return 0;
               return 1;
           }
 
 
 
 
 

(15 points)

3.

(24 (=8+8+8)  points)