Professional Documents
Culture Documents
(Chapter 1)
Java Programming
In this course, we will study different kinds of data structures
and some methods for designing algorithms. For practice
purpose, all the discussed algorithms are implemented in Java
language.
Algorithm readDVDFile()
read the title of the first DVD
while there are more DVDs
read the rest of the DVD data
store the DVD data in the vector
read the title of next DVD
close the input file
//factorial of 0 is 1
Class Modifiers
abstract: Indicate that the class has abstract methods. Abstract
methods are declared with the abstract keyword and do not
have the body of code.
final: Indicate that the class can have no subclasses.
public: Indicate that the class can be instantiated or extended
by any class.
Without the public class modifier, the class can be used and
instantiated by all classes in the same package.
Base Types
Classes are treated as data types. However, there are data types
that are not classes. They are called base types.
boolean
char
byte
short
int
long
float
double
Objects
A new object in Java is created by using the new operator.
<variable_name> = new <class_type>([param1, param2, ]);
Example:
myPoint = new Point(3, 6);
myPoint
object of Point
For simplicity, we
call the variable
myPoint an object
variable. But
understanding this
picture is very
important.
String Objects
In Java, string objects are easy to create and use. For example,
we dont need to use the new operator to create a String object.
Example:
String s = "kilo" + "meters";
char a = s[0];
char b = s.charAt[1];
String substring = s.substring(0, 10);
int index = s.indexOf('m');
Instance Variables
Data fields in a class declaration are called instance variables.
Example:
class Gnome {
protected String name;
protected int age;
private boolean magical;
public double height = 2.6;
public static final int MAX_HEIGHT = 3;
. . .
}
Variable Modifiers
public: Anyone can access the variables.
protected: Only methods of the same package or of
subclassess can access the variables.
private: Only methods of the same class can access private
instance variable.
If none of above modifiers are used, the variables can be
accessed by any class in the same package.
Methods
Methods are members in some class. They are the functions
that can be invoked to update and access data fields in the
class.
[<method_modifiers>] <return_type> <method_name>( parameter_list) {
// method body . . .
}
Example:
public void rename( String s ) {
name = s;
}
Method Modifiers
public: Anyone can call the method.
protected: Only methods of the same package or of subclass
can call the method.
private: Only methods of the same class can call the method.
Constructor Methods
A constructor is a special method that is executed when an
object of the class is being created. Its method name is exactly
the same as the class name, within which it is defined.
[constructor_modifiers] <class_name> (parameter_list) {
// constructor body
}
Example:
public Fish( int w, String n ) {
weight = w;
name = n;
}
Example:
public void findSumAndCount() {
int score;
int SENTINEL = -1;
// sentinel value
score = readInt("Enter next score or " + SENTINEL +
" to stop");
// initialize lcv
while (score != SENTINEL) {
// test lcv
sum += score;
// add current score to sum
count++;
// increase count by 1
System.out.println("score: " + score);
score = readInt("Enter next score or " + SENTINEL +
" to stop");
// update lcv
}
}
import javax.swing.JOptionPane;
public class a1q2 {
/**
Not forget these two statements.
* main method
*
* prompts input and calls the recursive method
*/
public static void main(String[] args) {
//input dialog
int m = (int)new Integer(JOptionPane.showInputDialog("Enter an integer"));
//calculate f(N) = 3*f(N-3) + 4*f(N-2) - 5*f(N-1) where f(0) = 1, f(1) = 1, f(2) = 3.
System.out.println("result of fn(" + m + ") is " + computeFN(m));
}
/**
* computeFN - the definition of the function
*
* input: int
* output: int
*/
public static int computeFN(int n)
{
if(n==0) return 1;
else if(n==1) return 1;
else if(n==2) return 3;
else
return 3 * computeFN(n-3) + 4 * computeFN(n-2) - 5 * computeFN(n-1);
}
}