Introduction:
In this exercise, you are going to exercise the concept of overloading, Please note that overloading and overriding are two different concepts.
Steps to follow:
1. Modify StudentRecord.java of Exercise 10.1 as shown in Code-10.2.a below. The code fragement that needs to be added is highlighted with bold and blue-colored font.
* Add two overloaded print(..) methods.
public class StudentRecord {
...
public void print(String name ){
System.out.println("Name:"+name);
}
public void print(String name, double averageGrade){
System.out.print("Name:"+name+" ");
System.out.println("Average Grade:"+averageGrade);
}
}
Code-10.2.a: StudentRecord.java
2.Write StudentRecordExample2.java as shown in Code-10.2.b below.
* cd \myjavaprograms (if you are not in this directory already)
* jedit StudentRecordExample2.java
public class StudentRecordExample2{
public static void main(String [] args) {
StudentRecord annaRecord =new StudentRecord();
annaRecord.setName("Anna");
annaRecord.setEnglishGrade(95.5);
annaRecord.setScienceGrade(100);
// Invoke overloaded methods
annaRecord.print(annaRecord.getName());
annaRecord.print(annaRecord.getName(), annaRecord.getAverage());
}
}
Code-10.2.b: StudentRecordExample2.java
3. Compile and run the code. If you experience compile errors, fix the compile errors.
* javac *.java (or javac StudentRecord.java StudentRecordExample2.java)
* java -classpath . StudentRecordExample2
4. Verify the result
* Name:Anna
Name:Anna Average Grade:65.16666666666667
Steps to follow if you are using NetBeans
It is assumed you are using the same NetBeans project you created in Exercise 10.1.
1. Modify the StudentRecord.java of Exercise 10.1 as shown in Code-10.2.a above
2. Write StudentRecordExample2.java
* Right studentrecordexample package node (Not StudentRecordExample project node) and select New->Java Class
* Under Name and Location pane,
o for Class Name field, type StudentRecordExamle2
o Click Finish
3. Modify the NetBeans generated StudentRecordExample2.java
* Replace the code of the NetBeans generated StudentRecordExample2.java with the one of Code-10.2.b above while leaving the package statement at the top
4. Right click studentrecordexample package node (Not StudentRecordExample project node) and select Compile Package (F9)
5. Right click StudentRecordExamle2 and select Run File
Homework:
1. Modify StudentRecord.java as following
* Add another overloaded print() method which takes the following three parameters
o name
o grade average
o student count
2. Modify StudentRecordExmaple2.java as following
* Invoke the newly added print() method
Popular Posts
-
Now I will show you how to retrieve the data posted from a HTML file in a JSP page. Consider an html page that prompts the user to enter his...
-
Accessing the Standard CGI Variables: To build the successful web application, you often need to know a lot about the environment in which ...
-
INTRODUCTION TO 'C': C is a programming language developed at AT & T's Bell laboratories of USA in 1972.it was designed by d...
-
SQL aliases can be used with database tables and with database table columns, depending on task you are performing. SQL column aliases are u...
-
Learning about Events (Continue) Lets program the Form_Load event. "MsgBox" is Visual Basic command that launch a message box. for...
-
Steps to follow: 1. Write Person.java as shown below in Code-11.3.a under polypackage directory. (You are welcome to do this work using eit...
-
The Command Button's KeyPress, KeyDown and KeyUp Events The events that will be mentioned in the following pages are commonly used, and ...
-
The SQL AVG aggregate function selects the average value for certain table column. Have a look at the SQL AVG syntax: SELECT AVG(Column1) FR...
-
package interfaceexercise; // Define an interface with three abstract methods. // Any class that implements this interface has to // impleme...
-
We will use the Customers table to illustrate the SQL LIKE clause usage: FirstName LastName Email DOB Phone John Smith John.Smith@yaho...
No comments:
Post a Comment