Wednesday July 12, 2000


Quiz 20


1. What are the parameters listed in the signature of a method called?

2. What is the rule about sending parameters to a method?

3. What are the parameters listed in the call to a method called?

4. When we use more than one parameter in a method how do we seperate them?
































The Extra Credit Assignment

Problem

Write a class whose constructor will accept as input a binary string and whose toString() method will output the original string, the decimal equivalent, the ternary equivalent, and the septadecimal equivalent of the original binary number.

A Solution

public class Converter {
   String original;

   public Converter(String original) {
      this.original = original;
   }

   private int toDecimal() {

      int power = original.length() - 1;

      int sum = 0;

      for (int counter = power;counter >= 0;counter--)
         if (original.charAt(original.length()-1-counter) == '1')
            sum += Math.pow(2,counter);

      return(sum);
   }

   private String convert(int base) {
      int number = toDecimal();

      int power = (int)(Math.log(number)/Math.log(base));

      String outputString = "";

      for (int counter=power;counter >= 1;counter--) {
         int quotient = (int)(number/Math.pow(base,counter));
         outputString += quotient;
         number = number % (int)(Math.pow(base,counter));
      }

      outputString += number;

      return(outputString);
   }

   private String pad(int n) {
      String outputString = "";

      for (int counter=1;counter <= n;counter++)
         outputString += " ";

      return(outputString);
   }

   public String toString() {
      String line = "\n";
      line += "Original" + pad(12);
      line += "Decimal" + pad(13);
      line += "Ternary" + pad(13);
      line += "Septadecimal" + "\n";
      line += original + pad(20-original.length());
      line += toDecimal() + pad(20-(String.valueOf(toDecimal())).length());
      line += convert(3) + pad(20-(convert(3)).length());
      line += convert(7) + "\n";
      return(line);
   }

   public static void main(String[] args) {
      Converter myConverter = new Converter("10000000");

      System.out.println(myConverter);
   }
}
Original            Decimal             Ternary             Septadecimal
10000000            128                 11202               242


Original            Decimal             Ternary             Septadecimal
1010                10                  101                 13
Recall that we were discussing methods yesterday.

The parameters which are listed in the header of the method are called formal parameters.

The parameters which are listed in the call to the method are called actual parameters.

If there is more than one parameter in the formal parameter list, they are seperated by commas.

The actual parameters must match in type and position to the parameters listed in the formal parameter list.

For example, consider the following code.

public class Test {
   static int a = 3;

   public int return_a(int a) {
      return(a);
   }

   public static void main(String[] args) {
      int b = return_a(3.0);

      System.out.println(b);
   }
}
When this program is compiled, the following error is reported.

Test.java:9: Incompatible type for method. Explicit cast needed to convert
double to int. 
      int b = return_a(3.0); 
                       ^
1 error
The situation is a little more interesting when Objects are used as parameter types.

Recall that we mentioned the tree hierarchy in Java. Using the extends clause in the class header we can inherit from other classes. Whenever we extend another class, then we create an object which is of that type.

For example, if the class header in class B is

public class B extends A,
then wherever a parameter of type A is expected, we can use an object of type B.

Since every class extends the Object class, any object can be used when the parameter type is Object.

To further understand this concept, let's look at some of the classes you will use in your assignment.

In the io package, one constructor of the class BufferedReader has the following signature.

public BufferedReader(Reader in)
So this means that the parameter which is sent to this constructor of the BufferedReader class must be of type Reader.

However, we have been sending an object of type InputStreamReader to this constructor of the BufferedReader class when we get input from the keyboard. Why doesn't the compiler complain?

Let's look at the class header in the InputStreamReader class.

public class InputStreamReader extends Reader
Since InputStreamReader extends the class Reader, anywhere that an object of type Reader is expected, we can use an object of type InputStreamReader.

There is another class in the io package which is important to us.

The class FileReader has the following header.

public class FileReader extends InputStreamReader
So this means that anywhere an object of type InputStreamReader is expected, we can use an object of type FileReader. Since InputStreamReader extends the class Reader, anywhere an object of type Reader is expected, we can use an object of type FileReader.

Consider the following example.

import java.io.*;

public class Test {

   public static void main(String[] args) throws IOException {
      FileReader file = new FileReader("data.txt");
      BufferedReader inputFile = new BufferedReader(file);

      String s = inputFile.readLine();

      inputFile.close();

      System.out.println(s); 
   }
}
This is how we open external files for input.

Note that when we are through reading from a file we close it.

To open external files for output we use the class FileWrite. The counterpart of BufferedReader is PrintWriter. Consider the following example.

import java.io.*;

public class Test1 {

   public static void main(String[] args) throws IOException {
      FileWriter file = new FileWriter("data1.txt");
      PrintWriter outputFile = new PrintWriter(file);

      outputFile.println("Hello");

      outputFile.close();
   }

}
Note that when we are through writing to a file we close it.

Notice that the file names given were relative file names. That is, relative to where the program is located. If we wish to specify absolute file names we can do that with one stipulation.

Normally on a Windows system you would specify the absolute path of a file like the following.

c:\jbuilder2\myfiles\data.txt
However there is a problem trying to use this as the input parameter to the FileReader or FileWriter class.

The symbol \ is the escape symbol. This directs the compiler to do something special with the character following it. When we want the compiler to simply use the symbol \ we must escape the slash. So the input to the FileReader and FileWriter classes would look like the following.

c:\\jbuilder2\\myfiles\\data.txt
On your programming assignment it would help if you would make up a plan of attack before beginning to write the program.

Recall some of the methods available in the String class.

  • public static String valueOf(int i)

  • public static String valueOf(float f)

  • public static String valueOf(double d)

  • public static String valueOf(char c)

  • public static String valueOf(boolean b)

    Recall some of the methods that are available to us in the StringTokenizer class.
    public class StringTokenizer implements Enumeration {
       public StringTokenizer(String str,String delim,boolean returnTokens);
       public StringTokenizer(String str,String delim);
       public StringTokenizer(String str);
       public boolean hasMoreTokens();
       public String nextToken();
       public String nextToken(String delim);
       public boolean hasMoreElements();
       public Object nextElement();
       public int countTokens();
    }