Handout: various Java tricks and tips. Casting. Arithmetic. Reading from the console.
Java Data Tyes
From on-line notes of Rebecca Hasti,
Lessons -> Lesson 1 -> Java Types.
- Primitive types: boolean, char, byte, short, int, long, float, double.
- Reference types: arrays, classes. They are really pointers.
- Wrapper classes: Boolean, Integer etc. When an object is really needed. E.g., the Java Stack class is a stack of Objects.
To use it for integers, you must convert int to Integer (which is a type of Object). Read from Hasti -> Lesson 2
(Classes) -> Some useful built-in classes (Integer, etc.).
Inheritance: extending existing classes
- How complex a class definition may be? Let's look at the
java.awt package (what you import in the beginning of any Java program
that uses graphics).
How does it compare with the class myPoint defined by you during
last week's lab?
- What if we wanted to take advantage of all the methods
implemented in the java.awt.Point class, but also include
other desirable features (such as shape and color, as in
last week's lab)? The solution provided by Java is
public class myPoint extends Point
no longer appear explicitly as class variables of myPoint. But
they are present, and are inherited from the Point class.
of the new class myPoint and how to use it.
- Overriding methods. A class which extends another
class inherits the parent's methods. But it can also
override them. This means, defining a method with exactly
the same name as a method from the parent.
For example, the java.awt class Point has a method to test
when two points are equal. This is done by comparing the
corresponding x and y coordinates. We can override this method in
the class myPoint and have equals also test for
equality of the shape and color of a point.
boolean equals(myPoint p)
if (x == p.x && y == p.y && shape == p.shape && color == p.color)
else return false;
More on Inheritance and Extending Objects
- Use of super in constructor of a class which extends another class.
- When you are using paint, mousePressed, actionPerformed, etc., you are overriding some
method from the class that you are extending.
Overview of new Hwk 4
Discussion of new Homework 4.
- Insertion Sort. Applet, from Dominique Thiebaut's web page.
Input: an array A of integers, of size n
Output: in the same array A, the numbers in increasing
The algorithm works in n-1 iterations by scanning the
array from left to right. At iteration i,
the ith element is considered and inserted in the part of the array
that has already been sorted (from 1 to i-1).
For this, the
algorithm first searches the array from 1 through
i-1 to find where the ith element would fit, then
shifts to the right those elements larger than it to make room
for its insertion in the array.
- Developing the program by stepwise refinement.
We will develop the program in stages, by stepwise refinement,
first on paper, as pseudo-code, then implement it. The
pseudo-code development should roughly follow the same structure
as the one I gave you for Selection Sort, starting with a general
description in English, then refining it to include functions. All
parameters to the functions should be clearly identified as IN,
OUT or IN/OUT.
In the stepwise refinement of your program for insertion sort,
you should have
separate functions for searching and for shifting,
invoked when inserting the current element.
The main program (applet)
should be similar to the one for Selection
get input via mouse clicks, sort, then print and draw the result.