Tuesday June 27, 2000
Let's return to the idea of class and instance variables to clear up some possible confusion.
Recall that a class variable is a variable related to a class but not dependent upon any particular instance of the class. It is
easier to understand the difference between class and instance variables if we think of space in memory.
When we define a class, any class variable gets allocated a certain space in memory. When we create a new instance of our class,
the instance has access to all the instance variables which are given new spaces in memory, and also has access to class variables
which occupy the same space in memory.
In the following diagram, a and b are class variables, and c and d are instance variables. Note that when we create a new instance
of the class, the same variables a and b are used, but new instances of c and d are created.
Class and Instance methods share a similar distinction. Instance methods are those methods that a new instance of our class would
invoke. So when a particular instance of a class calls an instance methods, it is known as the "actor". Any reference to an
instance variable inside the method would refer to the instance variable belonging to the actor.
Class methods are methods that are not viewed as being invoked by an instance of our class, but rather by the class itself.
To see examples of the differences between the two let's look at the following example.
public class Myclass {
int a;
public int value() {
return(a);
}
public static int classvalue() {
return(0);
}
...
}
The method value would be invoked by a particular instance of the class, for example, myclass. When myclass invokes the method
value, the method will return the value of the instance variable a that belongs to myclass. However, to invoke classvalue, we would
make the call Myclass.classvalue(). This would return the value 0.
Another very important part of the class definition is the constructor.
The job of a constructor is to create a new instance of our class, i.e. give values to each instance variable.
The signature of a constructor is access control keyword classname formal parameter list. For example, suppose we have the
following class definition.
public class Myclass {
int a;
public Myclass(int b) {
a = b;
}
...
}
This method is the constructor. It accepts as input an integer variable and initializes the instance variable a to that value.
Note one possible problem. Recall that if we define a variable in a formal parameter list and refer to that variable within the
body of the method, then the variable that we refer to is the one declared in the formal parameter list, even if there is another
variable by that name in another part of the program.
So suppose we have the following header on our constructor.
public Myclass(int a)
Then if we refer to a in the body of the constructor, the variable that is referred to is the one we declared in the formal
parameter list, not the instance variable a. So how do we tell the compiler that we want to refer to the instance variable a, and
not the variable a we declared in the formal paramter list?
The answer is the Java keyword this. this refers to the particular instance of a class which is currently invoking the
constructor. So to tell the compiler to set the instance variable a equal to value of a in the formal parameter list we would use
the following code.
public class Myclass {
int a;
public Myclass(int a) {
this.a = a;
}
...
}
This would tell the compiler that when this constructor is invoked by a particular instance of a class, then the instance variable
a associated with that instance should be assigned the value of the variable a declared in the formal parameter list.
Let's look at a problem that was mentioned last Thursday.
1. Associated with each number is certain properties: whole
number/fraction, prime/not prime, perfect square/not perfect square,
even/odd, etc. Write a class called Number which will have these
properties as variables. Write methods called is_integer, is_prime,
is_square, and is_even which return boolean variables based on whether or
the a particular instance of this class possesses these properties. Use a
constructor which will accept four boolean values.
In your current programming assignment you will need to make use of the String class. There are several methods available to you in
the String class that need to be mentioned.
public char charAt(int index)
public int length()
public boolean endsWith(String suffix)
public boolean equals(Object anObject)
public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)
s.substring(1) would return the string "ello".
s.substring(2) would return the string "llo".
s.substring(0,2) would return the string "He".
s.substring(1,3) would return the string "el".
public String concat(String str)
Programming Assignment II - Due: Wednesday July 5, 2000
Object Oriented Programming
This short assignment is designed to give you experience with the concepts
of class and instance variables, class and instance methods, and
constructors, and how they work together.
Evariste decides that in order to gain more understanding of Object Oriented Programming he is going to write a small
class called Binary. He will have five instance variables in his class.
| original |
of type String |
| quadra |
of type String |
| octal |
of type String |
| hexadecimal |
of type String |
| decimal |
of type int |
He will have four instance methods in his class.
| toQuadra |
accepts no parameters and returns type String |
| toOctal |
accepts no parameters and returns type String |
| toDecimal |
accepts no parameters and returns type int |
| toHexadecimal |
accepts no parameters and returns type String |
| toString |
returns the String equivalent of this class (we will discuss this topic Tuesday and
Wednesday) |
He will have one class method called equiv that is overloaded.
The method will accept two, three or four parameters. It will return type String.
The constructor for Evariste's class will accept one parameter of type String.
The purpose of Evariste's class is to accept a binary number written as a string. Each of the instance methods will be
used to convert that binary number into the appropriate number system. When the constructor is called, its job will be to
give a value to original, and then call each of the four conversion instance methods to give values to the instance
variables, quadra, octal, decimal, and hexadecimal.
The method equiv will be used to accept two, three, or four bits (characters) and convert that group of bits to the
appropriate equivalent. For example, if the bits we send to one implementation of the method are ``1'',``1'', and ``1'',
the
method would recognize that since we are sending it three bits, we want the octal equivalent, so it would return the
String ``7''.
The method toString will be used to prepare a formatted result. The output this method will produce will be
| Binary |
QuadraDecimal |
Octal |
Decimal |
Hexadecimal |
| 1010 |
22 |
12 |
10 |
A |
Evariste has created a new number system called the quadradecimal number system which has only the digits 0, 1, 2, and 3.
He has decided that the easiest way in which to convert a binary number to other systems is to use the methods available
in the String class.
He decides the easiest way to proceed with converting a binary number to a quadradecimal is with the following algorithm.
Assume that there is a binary number stored in the String original.
1. Let l = the length of the string original.
2. Let n = the integer part of the quotient l/2.
(He decides on this because like the conversion
from binary to octal and hexadecimal he must
take groups of bits. Since the base of the
quadradecimal system, 4, is 22, he must take
groups of two binary digits and convert them to
their quadradecimal equivalent).
He knows now that there are n groups of 2 bits
in original. Now he must determine is there are
any bits left over.
3. Let r = the remainder in the division l/2.
If r is 0 he knows that there are exactly n
groups of 2 bits in original and no more. If r
is 1 he knows that there are n groups of 2 bits
in original and 1 left over. If there is one left
over he must accomodate for this.
4. if (r == 1)
get quadradecimal equivalent of ``0" and
the first character in original and store the
equivalent in an output string.
Now that he has accounted for the first character
if there is one left over, he can now go through
the rest of the string taking groups of two bits
and converting them to their quadradecimal equivalent.
If r was 1, then he will start breaking up the
string into groups of 2 beginning with position 1.
If r was 0, then he will start breaking up the
string into groups of 2 beginning with position 0.
5. for (initialize counter to 1; as long as counter
is less than or equal to n; increment counter by 2) {
get quadradecimal equivalent of two characters
of original beginning at position counter.
concatenate the equivalent into the output string.
}
6. Return the output string.
Evariste decides he can carry out the conversion from binary to octal and hexadecimal in the same way.
He decides the conversion from binary to decimal is even easier.
Your task is to write the class Binary and have it carry out the proposed tasks. The class you write should contain a
main method in which you instantiate the class, and make a call to its toString method to produce the formatted output.