INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM

ICS102, SECTIONS 52, 54 & 55  (001 Semester)

INTRODUCTION TO COMPUTING

LAB  #10 Revision of Method definition & Parameter Passing

 

Instructor: Bashir M. Ghandi

 


Objectives:

To review the following concepts

Introduce reading input using standard classes (without TextIO class).

 

1.  Review of Method definition and Parameter passing

We shall do this by examining a number of examples.  Most of the examples have errors.  You are to correct the errors and take note (in writing) the lesson learnt from each example.

 

Example 1: Correct the following program.

public class Discount1 {

 

  public static void main(String[] args) {

                double price = 100.00;

                discount(price);

  }

 

  public static void discount(int price) {

                double newPrice = price * 0.10;

                System.out.println("Price after discount is " + newPrice);

  }

}

 

What lesson did you learn from it?:

 

 

 

Example 2: Correct the following program.

public class Discount2 {

 

  public static void main(String[] args) {

                double price = 100.00;

                discount(price);

  }

 

  public static void discount(double price, double rate) {

                double newPrice = price * rate;

                System.out.println("Price after discount is " + newPrice);

  }

}

 

What lesson did you learn from it?:

 

 

 

Example 3: Correct the following program.

public class Discount3 {

  public static void main(String[] args) {

                double price = 100.00;

                double rate = 0.2;

                discount(price,rate);

  }

 

  public static double discount(double price, double rate) {

                double newPrice = price * rate;

                System.out.println("Price after discount is " + newPrice);

  }

}

 

What lesson did you learn from it?:

 

 

 

Example 4: Correct the following program.

public class Discount4 {

 

  public static void main(String[] args) {

                double price = 100.00;

                double rate = 0.2;

                int newPrice;

                newPrice = discount(price,rate);

                System.out.println("Price after discount is " + newPrice);

  }

 

  public static double discount(double price, double rate) {

                double newPrice = price * rate;

                return newPrice;

  }

}

 

What lesson did you learn from it?:

 

 

 

Example 5: Correct the following program.

public class Discount5 {

 

  public static void main(String[] args) {

                double price = 100.00;

                double rate = 0.2;

                double newPrice;

                newPrice = discount(double price, double rate);

                System.out.println("Price after discount is " + newPrice);

  }

 

  public static double discount(price, rate) {

                double newPrice = price * rate;

                return newPrice;

  }

}

 

What lesson did you learn from it?:

 

 

 

Example 6: Correct the following program with out changing the declaration of n.

public class ClassAndObjects1 {

 

  int n;

  static int m;

 

  public static void main(String[] args) {

                n = 10;

                m = 20;

                System.out.println("n = "+n+" , m = "+m);

  }

}

 

What lesson did you learn from it?:

 

 

 

Example 7: Correct the following program without changing the declaration of n and sum.

public class ClassAndObjects2 {

 

  int n;

  static int m;

 

  public int sum() {

                return n+m;

  }

 

 

  public static void main(String[] args) {

                n = 10;

                m = 20;

 

                System.out.println("n = "+n+" , m = "+m);

                System.out.println("The sum is "+sum());

  }

}

 

What lesson did you learn from it?:

 

 

 

Example 8: Correct the following program without changing the declarations of n and m.

class MyInteger {

                private int n;

                public int m;

}

 

public class PrivateAndPublic {

 

  public static void main(String[] args) {

   

    MyInteger intObject = new MyInteger();

                intObject.n = 10;

                IntObject.m = 20;

                System.out.println("n = "+intObject.n+" , m = "+intObject.m);

  }

}

 

What lesson did you learn from it?:

Example 9: What is the output of the following program?

class MyInteger {

                int n;

                int m;

               

                public MyInteger(int num1, int num2) {

                                n=num1;

                                m=num2;

                }

}

 

public class CanChangePartOfObject{

 

  public static void main(String[] args) {

      MyInteger intObject = new MyInteger(10, 20);

      square(intObject);

      System.out.println("n = "+intObject.n+" , m = "+intObject.m);

  }

 

  static void square(MyInteger object) {

                object.n = object.n * object.n;

                object.m = object.m * object.m;

  }           

}

 

What lesson did you learn from it?:

 

 

 

Example 10: What is the output of the following program?

class MyInteger {

                int n;

                int m;

               

                public MyInteger(int num1, int num2) {

                                n=num1;

                                m=num2;

                }

}

 

public class CannotChangeAllOfObject{

 

  public static void main(String[] args) {

      MyInteger intObject = new MyInteger(10, 20);

      square(intObject);

      System.out.println("After calling square method, n = "+intObject.n+" , m = "+intObject.m);

  }

 

  static void square(MyInteger object) {

                object = new MyInteger(100, 400);

                System.out.println("Inside square method, n = "+object.n+" , m = "+object.m);

  }           

}

 

What lesson did you learn from it?:

 

 

 

2.  Reading Input using standard classes

We have been using the TextIO class to simplify reading of input from the console.  As explained before, this is not a standard class (it does not come with JDK).  This means, if you are to take your programs to where there is no TextIO class, they will not run.

 

Java provides a number of classes in the java.io package that can be combined together to read input.

 

The basic classes are:

 

InputStreamReader:  This can be used to create an object that can be used to read streams (single characters) from an input stream source (e.g System.in).

 

e.g.  InputStreamReader streamInput  = new  InputStreamReader(System.in);

 

The problem with the above object is that it has only one method read() that can be used to read only one character at a time. 

 

 BufferedReader:  This can be used to create an object that can be used to read a sequence of characters (string) from a stream reader object.

 

e.g:          InputStreamReader streamInput  = new InputStreamReader(System.in);

                BufferedReader stdin = new BufferedReader(streamInput);

 

Again the buffered reader object has only one method readLine() that can be used to read a string.

 

This however, is not a problem since we can convert strings to the appropriate numeric forms using the parse classes introduced earlier (Integer.parseInt(),  Double.parseDouble(), etc.).

 

Since we do not really need to access the streamInput object in the above definition, it is a common practice to combine the two definitions above as follows:

                        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

Exceptions:

The read() method of the InputSreamReader and the readLine() method of the BufferedReader generate exceptions.  Thus, like TextIO, each time we need to use them, we must be prepared to handle a possible IOException or throw the exception to the interpreter.

 

Example 11:  The following example shows how input may be read without using TextIO class.

import java.io.InputStreamReader;

import java.io.BufferedReader;

import java.io.IOException;

 

public class InputWithoutTextIO {

 

  static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

 

  public static void main(String[] args)throws IOException {

    int intValue;

    float floatValue;

    double doubleValue;

    String string;

   

    System.out.print("Enter a String: ");

    string = stdin.readLine();

   

    System.out.print("Enter integer value: ");

    intValue = Integer.parseInt(stdin.readLine());

   

    System.out.print("Enter float value: ");

    floatValue = Float.parseFloat(stdin.readLine());

   

    System.out.print("Enter double value: ");

    doubleValue = Double.parseDouble(stdin.readLine());

   

    System.out.println();

    System.out.println(string);

    System.out.println(intValue);

    System.out.println(floatValue);

    System.out.println(doubleValue);

  }

}

 

 

               

3.  Assignment

Modify the assignments of Lab #7 to so that they will work without using the TextIO class.

The assignments are reproduced here for your convenience.

 

1.    Write a program that reads three integer numbers and prints the maximum, the minimum and the average.  In addition to the main method which should only reads the input and prints the result, your program should have three other methods whose format is given below: 

                                                                  public static int maximum (int n1, int n2, int n3)

                  public static int minimum (int n1, int n2, int n3)

                  public static double average (int n1, int n2, int n3)

 

2.    Write a program that simulates a simple arithmetic calculator, which displays a menu and allows a user to perform addition, subtraction, multiplication and division, as shown by the following sample run.  Your program should contain the methods for the different operations:    

                                  public static void showMenu()

                           public static int getChoice() throws IOException

                           public static double add(double op1, double op2)

                             (similarly for subtract, multiply and divide).

 

3.        Write a program that reads the sides of a triangle and determine the type of the triangle.  The possibilities are:

               

Right-angle

square of one side is same as sum of the squares of the other two sides

Equilateral

all sides are equal

Isosceles

two of the sides are equal

Scalene

no two sides are equal

 

You should have a boolean method to test for each of the above types. Your program should repeat until 0 0 0 is entered.