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
-
Arguments passed by value and by reference. Until now, in all the functions we have seen, the arguments passed to the functions have been pa...
-
SQL Server Express is a free, easy to use, redistributable version of SQL Server 2005 designed for building simple data-driven applications....
-
The SQL ORDER BY clause comes in handy when you want to sort your SQL result sets by some column(s). For example if you want to select all t...
-
Java Server Pages JavaServer Pages (JSP) technology is the Java platform technology for delivering dynamic content to web clients in a por...
-
DIFFERENCE BETWEEN STRUCT AND UNION: In structures,each member has its own storage location,whereas all memebers of a union use the same loc...
-
SQL is short for Structured Query Language and is a widely used database language, providing means of data manipulation (store, retrieve, up...
-
Inheritance A key feature of OOP is reusability. It's always time saving and useful if we can reuse something that already exists rather...
-
Declaration of variables In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax...
-
Syntax of JSP Scriptles are: <% //java codes %> JSP Scriptlets begins with <% and ends %> .We can embed any a...
-
In this section we are going to implement insert data, delete data, and update data using with JDBC database and also using of JavaScript. ...
No comments:
Post a Comment