Friday June 16, 2000
Quiz 9
1. What are the five primary components in a class definition.
2. What is the Java keyword used to indicate a call to the constructor of
a class?
3. What is the signature of the constructor of a class?
4. What is the syntax of a class header?
Classes
Let us go back and examine the things we talked about yesterday.
The header of a class has the following syntax
<access control keyword> class classname <extends A> <implements B,C, etc.>
The extends clause allows us to inherit properties of another class. By
default whenever you create a class you extend the Object class. The
Object class is a basic class defined in java.lang. It contains several
methods which are useful in classes.
Every object that we create has as part of it the Object class.
Whenever one class extends another we call the class a subclass of
the first. The class we extend is refered to as the superclass or
the parent class. So the Object class is a superclass to all
classes.
However, there is a stipulation. When we create a new class definition we
may only extend one other class (not including the Object class). This is
called single inheritance. The parent of a class may inherit
properties from another class so classes in Java form a hierarchical or
tree structure with the Object class at the root of the tree.
Recall that there are 5 primary components in a class definition.
- Instance Variables
- Instance Methods
- Class Variables
- Class Methods
- Constructors
Obtaining Input
Obtaining Input from the user is an essential activity. In order to do this in our program we must import the java.io package.
A stream refers to any input source or output destination for data. There are two classes that Java provides in the java.io package. In order to get
input we use the class BufferedReader. The constructor for this class asks for a parameter which is of type Reader, which is another class in java.io. For
reasons we will discuss later we can substitute a variable of type InputStreamReader. InputStreamReader is a class whose constructor has as a parameter a
variable of type InputStream. There is a standard variable of this type in the System class. It is called in. So to create a new instance of the an
InputStreamReader we could use the following code.
InputStreamReader stream = new InputStreamReader(System.in);
Now to get input from the user we can pass this variable to the constructor of the BufferedReader class.
BufferedReader keyboard = new BufferedReader(stream);
Now if we invoke the method readLine() from the BufferedReader class, during execution the program will pause and wait for input from the user.
However, there is a problem. The readLine() method will input a variable of type String. String is a class which is found in the java.lang package.
So to get input from the user we could use the following code.
String s = keyboard.readLine();
During execution the program will pause and wait for input from the user. The input will be placed into the String variable s. Question. What if I want to
input an integer from the user.
Answer. A Wrapper class.
In order to understand what a wrapper class is we need to talk about a fundamental aspect of Java. Recall that the syntax of a class header is
<access control keyword> class classname <extends A> <implements B,C, etc.>
extends is a keyword that allows us to inherit properties of another class. When we extend a class definition we inherit data and methods from that class.
There is a special class defined in the java.lang package called Object. All objects that we create inherit from this class. We do not need to use an extends
clause. All objects that we create will have the data and methods of the Object object.
Therefore all objects in Java can be treated in a similar way since they all inherit from Object.
However, the primitive data types are not objects and cannot be treated as objects. If we do want to treat them as objects, then we must wrap them into an
object which has a variable which will contain the value of the data type. These wrapper classes are found in the java.lang package.
In order to convert the string we get from input into an integer we use the wrapper class Integer.
int num = new Integer(s).intValue();
Note here that I don't just create a new instance of the Integer object and assign that to num because num is not an object. However, there is an instance
method in the Integer class called intValue which returns the integer value of the object.
Obtaining Output
In order to obtain output we normally use the following code.
System.out.println(s);
out is a variable found in the System class. It is an object of a class found in the java.io package. One of the methods in that class is println.
There is an operation related to string that will be very useful to us. It
is the concatenation operator. Concatenating two strings means to join
them. In order to join two string we may use the + symbol.
String s = "Hello " + "World";
When using the System.out.println method we can use the concatenation
operator with variables that are not necessarily strings. For example
System.out.println("The number " + num + " is not prime.");
There is also another method which is important in writing applications
called the main method. The signature of the main method is
public static void main(String[] args)
Whenever we want a Java program to be executed with the interpreter it
must contain a main method.
Let us now look at a few examples of classes with main methods.
Recall the problems that we considered last week.
1. Determine whether or not a given number is even.
2. Given a positive number find all of the positive divisors of the
number.
3. Determine whether or not a given positive number is prime.
4. Given a positive number, n, find all of the prime numbers less than or equal to n.
Let us look at the algorithms for each of these problems and see how we
can translate it into Java code.
1.
1. Get the number, n.
2. Divide n by 2 to get the quotient and the remainder r.
3. If (r equals 0)
report that n is even
else
report that n is not even
import java.io.*;
public class IsItEven {
public static void main(String[] args) throws IOException {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(stream);
System.out.println("Please enter your number ");
int num = new Integer(keyboard.readLine()).intValue();
int remainder = num % 2;
if (remainder==0)
System.out.println(num + " is even");
else
System.out.println(num + " is not even");
}
}
2.
1. Get the number, n.
2. Report that 1 is a divisor of n.
3. Report that n is a divisor of n.
4. for (initialize counter to 2;counter is less than n;increment counter by 1) {
Divide n by counter to get the quotient and remainder r
if (r equals 0)
report that counter is a divisor of n
}
import java.io.*;
public class GetDivisors {
public static void main(String[] args) throws IOException {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(stream);
System.out.println("Please enter your number");
int num = new Integer(keyboard.readLine()).intValue();
System.out.println("1 is a divisor of " + num);
for (int counter=2;counter<num;counter++) {
int remainder = num % counter;
if (remainder==0)
System.out.println(counter + " is a divisor of " + num);
}
System.out.println(num + " is a divisor of " + num);
}
}
3.
1. Get the number, n
2. Initialize prime to 1.
3. for (initialize counter to 2;counter is less than n;increment counter by 1) {
Divide n by counter to get the quotient and remainder r
if (r equals 0)
set prime to 0
4. if (prime equals 1)
report that n is a prime number
else
report that n is not a prime number
import java.io.*;
public class IsItPrime {
public static void main(String[] args) throws IOException {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(stream);
System.out.println("Please enter your number");
int num = new Integer(keyboard.readLine()).intValue();
int prime = 1;
for (int counter=2;counter<num;counter++) {
int remainder = num % counter;
if (remainder==0)
prime=0;
}
if (prime==1)
System.out.println(num + " is prime");
else
System.out.println(num + " is not prime");
}
}
The Assignment
Let us now discuss your programming assignment.