Re: [Skunkworks] Advanced Programming Cs012 Assignment 2

Skunks, Before this question was given as assignment. It was in Diploma exam for last Semester. I think the Lecturer had an answer to it.

In Java there is no operator overloading allowed other than the built-in exception of + and += that allows you to concatenate strings. What Java allows is Function Overloading which is something different. Steve ----- Original Message ----- | From: "Gate Keeper" <mohasq@gmail.com> | To: "Skunkworks Mailing List" <skunkworks@lists.my.co.ke> | Sent: Tuesday, April 3, 2012 11:23:27 AM | Subject: Re: [Skunkworks] Advanced Programming Cs012 Assignment 2 | Skunks, | Before this question was given as assignment. It was in Diploma exam | for last Semester. I think the Lecturer had an answer to it. | _______________________________________________ | Skunkworks mailing list | Skunkworks@lists.my.co.ke | ------------ | List info, subscribe/unsubscribe | http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks | ------------ | Skunkworks Rules | http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 | ------------ | Other services @ http://my.co.ke

Skunks I will ask the student to get the Lecturers answer then we see what he/she had as the answer.

Hi everyone, I agree with Steve, operator overloading is strictly speaking not native to JAVA (I know of it only in C++ and perhaps SmallTalk ). You can however mimic it in JAVA in the following way, lets say you have two Employee objects emp1 and emp2; This is done with the assumption that you have an Employee class with a definition similar to the one below: Disclaimer: I have not run the program below through a compiler to check for syntax and errors in logic: public class Employee { // Properties of the class String employeeID, name; Double salary, allowance; // Constructor for the class public Employee ( String employeeId, String employeeName) { setEmployeeID( employeeId ); setEmployeeName( employeeName ); } // End class Employee // Setter methods public void setEmployeeID( String employeeId ) { this.employeeID = employeeId; } // End method setEmployeeID public void setEmployeeName( String employeeName ) { this.employeeName = employeeName; } // End method setEmployeeID public void setSalary( double basicPay ) { this.salary = basicPay; } // End method setSalary public void setAllowance( double allowance ) { this.allowance = allowance; } // End method setAllowance // Getter methods public double computeGrossSalary ( ) { return this.salary + this.allowance; } // End method computeGrossSalary // Continue with the rest for names, id, salary and allowances // A method for comparing two employees' earnings public boolean equalsTo ( Employee compared ) { return this.computeGrossSalary ( ) == compared.computeGrossSalary ( ); } // Convert the employee object to string public String toString ( ) { return "My name is "+getEmployeeName( )+". My gross salary is "+computeGrossSalary ( ) ; } // End metho toString } //End class Employee @Gatekeeper, I hope that helps and the student was able to get a correct solution on time? Martin. On Tue, Apr 3, 2012 at 6:48 PM, Gate Keeper <mohasq@gmail.com> wrote:
Skunks I will ask the student to get the Lecturers answer then we see what he/she had as the answer. _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

Sorry, I forgot to mention that you may now be able to do comparisons like // Compare the salaries of two employees: boolean result = true; // Or initialize it to false if you prefer it that way result = emp1.comapreTo( emp2 ); // Continue with processing. Martin On 4/3/12, Martin Chiteri <martin.chiteri@gmail.com> wrote:
Hi everyone,
I agree with Steve, operator overloading is strictly speaking not native to JAVA (I know of it only in C++ and perhaps SmallTalk ).
You can however mimic it in JAVA in the following way, lets say you have two Employee objects emp1 and emp2;
This is done with the assumption that you have an Employee class with a definition similar to the one below:
Disclaimer: I have not run the program below through a compiler to check for syntax and errors in logic:
public class Employee { // Properties of the class String employeeID, name; Double salary, allowance; // Constructor for the class public Employee ( String employeeId, String employeeName) { setEmployeeID( employeeId ); setEmployeeName( employeeName ); } // End class Employee // Setter methods public void setEmployeeID( String employeeId ) { this.employeeID = employeeId; } // End method setEmployeeID public void setEmployeeName( String employeeName ) { this.employeeName = employeeName; } // End method setEmployeeID public void setSalary( double basicPay ) { this.salary = basicPay; } // End method setSalary public void setAllowance( double allowance ) { this.allowance = allowance; } // End method setAllowance // Getter methods public double computeGrossSalary ( ) { return this.salary + this.allowance; } // End method computeGrossSalary // Continue with the rest for names, id, salary and allowances // A method for comparing two employees' earnings public boolean equalsTo ( Employee compared ) { return this.computeGrossSalary ( ) == compared.computeGrossSalary ( ); } // Convert the employee object to string public String toString ( ) { return "My name is "+getEmployeeName( )+". My gross salary is "+computeGrossSalary ( ) ; } // End metho toString
} //End class Employee
@Gatekeeper, I hope that helps and the student was able to get a correct solution on time?
Martin.
On Tue, Apr 3, 2012 at 6:48 PM, Gate Keeper <mohasq@gmail.com> wrote:
Skunks I will ask the student to get the Lecturers answer then we see what he/she had as the answer. _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

// Wololo! I need a serious JAVA compiler result = emp1.equalsTo( emp2 ); // :-)

Oooh, and whoever told you that this is advanced Programming concepts was lying through his / her teeth. ;-) Martin.

hehehe more CODE!!!! On Tue, Apr 3, 2012 at 7:19 PM, Martin Chiteri <martin.chiteri@gmail.com>wrote:
Oooh, and whoever told you that this is advanced Programming concepts was lying through his / her teeth. ;-)
Martin. _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- GG

Thats why its a trap. If you do it in C++ and overload correctly, you fail. If you do it in java and mimic overloading (and not overload operator), you fail for not overloading. Let the lecturer come out clean! On Tue, Apr 3, 2012 at 8:53 PM, geoffrey gitagia <ggitagia@gmail.com> wrote:
hehehe more CODE!!!!
On Tue, Apr 3, 2012 at 7:19 PM, Martin Chiteri <martin.chiteri@gmail.com>wrote:
Oooh, and whoever told you that this is advanced Programming concepts was lying through his / her teeth. ;-)
Martin. _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- GG
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- www.golavish.com - The travel and leisure www.raccuddasys.com - code Development issues
participants (5)
-
Frankline Chitwa
-
Gate Keeper
-
geoffrey gitagia
-
Martin Chiteri
-
Steve Obbayi