Popular Posts

Monday, February 23, 2009

Exercise 10.1: Create Your Own Class


Introduction:

So far, you have dealt with a single class that contains main(..) method. In this exercise, you are going to create mulitple classes among which one class invokes another class - more precisely, a method in a class invokes a method of another class.

* If you are new to Object-Oriented Programming concept, please read "Object Oriented Programming Concept" section of the Java Programming Tutorial.
* If you are new on how to create a class, please see "Creating Class" section of Java Programmng Tutorial.


Steps to follow:

1. Write StudentRecord.java as shown in Code-10.1.a below. Please note that each *.java file contains a single Java class. (You are welcome to do this work using either command line tools or NetBeans. The instruction for using NetBeans is given below.)

* cd \myjavaprograms
* jedit StudentRecord.java


public class StudentRecord {

// Declare instance variables.
private String name;
private double mathGrade;
private double englishGrade;
private double scienceGrade;
private double average;

// Declare static variables.
private static int studentCount = 0;

/**
* Returns the name of the student
*/
public String getName(){
return name;
}

/**
* Changes the name of the student
*/
public void setName(String temp ){
name =temp;
}

/**
* Computes the average of the english,math and science
* grades
*/
public double getAverage(){
double result =0;
result =(mathGrade+englishGrade+scienceGrade )/3;
return result;
}

/**
* Returns the number of instances of StudentRecords
*/
public static int getStudentCount(){
return studentCount;
}

/**
* Returns the number of instances of StudentRecords
*/
public static void increaseStudentCount(){
studentCount++;
}

}
Code-10.1.a: StudentRecord.java

2. Write StudentRecordExample.java as shown in Code-10.1.b below.

* cd \myjavaprograms (if you are not in this directory already)
* jedit StudentRecordExample.java

public class StudentRecordExample{
public static void main(String [] args ){

// Create an object instance of StudentRecord class.
StudentRecord annaRecord =new StudentRecord();

// Increament the studentCount by invoking a static method.
StudentRecord.increaseStudentCount();

// Create another object instance of StudentRecord class.
StudentRecord beahRecord =new StudentRecord();

// Increament the studentCount by invoking a static method.
StudentRecord.increaseStudentCount();

// Create the 3rd object instance of StudentRecord class.
StudentRecord crisRecord =new StudentRecord();

// Increament the studentCount by invoking a static method.
StudentRecord.increaseStudentCount();

// Set the names of the students.
annaRecord.setName("Anna");
beahRecord.setName("Beah");
crisRecord.setName("Cris");

// Print anna's name.
System.out.println(annaRecord.getName());

// Print number of students.
System.out.println("Student Count = "+StudentRecord.getStudentCount());
}
}
Code-10.1.b: StudentRecordExample.java

3. Compile and run the code

* javac StudentRecord.java StudentRecordExample.java (or javac Student*.java or javac *.java)
* java -classpath . StudentRecordExample

4. Verify the result

* C:\myjavaprograms1>java -classpath . StudentRecordExample
Anna
Student Count = 3



Steps to follow if you are using NetBeans

1. Start the NetBeans IDE (if you have not done so yet)

* Windows: Start > All Programs > NetBeans 5.0 > NetBeans IDE or click NetBeans IDE 5.0 desktop icon.
* Solaris/Linux: /bin/netbeans or click NetBeans IDE desktop icon.

2. Create a new NetBeans project and StudentRecordExample main class

* Select File from the menu bar and select New Project.
* Under Choose Project, select General and Java Application
* Click Next.
* Under Name and Location pane, (Figure-10.1.d below)
o For Project Name field, fill it with StudentRecordExample
o For Create Main Class field, change it to studentrecordexample.StudentRecordExample (from studentrecordexample.Main) - You are creating StudentRecordExample class under studentrecordexample package.
o Click Finish
3. Modify the NetBeans generated code

* Replace the NetBeans generated StudentRecordExample.java code in the source editor with the one of Code-10.1.b above (not Code-10.1.a) while leaving the package studentrecordexample; statement on the top.
o You will see NetBeans generates some compile errors indicating it cannot find StudentRecord class. This is what is expected since you have not written StudentRecord class yet,

4. Write StudentRecord.java

* Right click StudentRecordExample project node and select New->Java Class. The New Java Class window appears.
* Under Name and Location pane,
o for Class Name field, type StudentRecord
o for Package field, choose studentrecordexample from the drop-down menu (or you can type studentrecordexample) - You are creating StudentRecord class under studentrecordexample package.
o Click Finish

5. Modify the NetBeans generated code

* Replace the NetBeans generated StudentRecord.java code in the source editor with the one of Code-10.1.a above while leaving the package studentrecordexample; statement on the top.

6. Run StudentRecordExample application

* Right click StudentRecordExample project node and select Run Project.
* Note that the Output window displays the result

No comments:

Post a Comment