5 Replies - 508 Views - Last Post: 04 December 2017 - 02:24 PM Rate Topic: -----

#1 sniperwolfsguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 17

Class not abstract, does not override abstract method, etc

Posted 04 December 2017 - 01:43 PM

Hello there,

I have quite a bit of code, and I will make sure I post it all. I am using netbeans. My assignment is this:

In this exercise, we modify the accounts payable application to include the complete functionality of the payroll application. The application should still process two Invoice objects, but now should process one object of each of the four Employee subclasses. If the object currently being processed is a BasePlusCommissionEmployee, the application should increase the BasePlusCommissionEmployee's base salary by 10%. Finally, the application should output the payment amount for each object. Complete the following steps to create the new application:
a. Modify classes HourlyEmployee and CommissionEmployee to place them in the Payable hierarchy as subclasses of the version of Employee that implements Payable. [Hint: Change the name of method earnings to getPaymentAmount in each subclass so that the class satisfies its inherited contract with interface Payable.]
b. Modify class BasePlusCommissionEmployee such that it extends the version of class CommissionEmployee created in Part a.
c. Modify PayableInterfaceTest to polymorphically process two Invoices, one SalariedEmployee, one HourlyEmployee, one CommissionEmployee and one BasePlusCommissionEmployee. First output a string representation of each Payable object. Next, if an object is a BasePlusCommissionEmployee, increase its base salary by 10%. Finally, output the payment amount for each Payable object.

I have two classes that have a problem with my abstract class/method. I am new to abstract as a whole in java.
package pkg1016;

/**
 *
 * @author tkasper
 */
  
   public abstract class Employee implements Payable
   {
      private String firstName;
      private String lastName;
      private String socialSecurityNumber;

     // three-argument constructor

     public Employee( String first, String last, String ssn )
     {
        firstName = first;
        lastName = last;
        socialSecurityNumber = ssn;
     } // end three-argument Employee constructor

     // set first name
     public void setFirstName( String first )
     {
        firstName = first;
     } // end method setFirstName


     // return first name
     public String getFirstName()
     {
        return firstName;
     } // end method getFirstName

     // set last name

     public void setLastName( String last )
     {
        lastName = last;
     } // end method setLastName

     // return last name
     public String getLastName()
     {
        return lastName;
     } // end method getLastName


     // set social security number
     public void setSocialSecurityNumber( String ssn )
     {
        socialSecurityNumber = ssn; // should validate
     } // end method setSocialSecurityNumber

     // return social security number

     public String getSocialSecurityNumber()
     {
        return socialSecurityNumber;
     } // end method getSocialSecurityNumber

     // return String representation of Employee object
     public String toString()
     {
        return String.format( "%s %s\nsocial security number: %s",
           getFirstName(), getLastName(), getSocialSecurityNumber() );
     } // end method toString
       }  

package pkg1016;

   public class commissionEmployee extends Employee
   {
      private double grossSales; // gross weekly sales

      private double commissionRate; // commission percentage

      
     public commissionEmployee( String first, String last,
                     String ssn,
        double sales, double rate )
     {
        super( first, last, ssn );
        setGrossSales( sales );
        setCommissionRate( rate );
     }


     // set commission rate
     public void setCommissionRate( double rate )
     {
        commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
     } // end method setCommissionRate


     // return commission rate
     public double getCommissionRate()
     {
        return commissionRate;
     } // end method getCommissionRate

     // set gross sales amount

     public void setGrossSales( double sales )
     {
        grossSales = ( sales < 0.0 ) ? 0.0 : sales;
     } 

     
     public double getGrossSales()
     {
        return grossSales;
     } 


     // calculate earnings; override abstract method earnings
                 // in Employee
     public double earnings()
     {
        return getCommissionRate() * getGrossSales();
     } // end method earnings

     // return String representation of CommissionEmployee object

     public String toString()
     {
        return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f",
           "commission employee", super.toString(),
           "gross sales", getGrossSales(),
           "commission rate", getCommissionRate() );
     } // end method toString

    
  } // end class CommissionEmployee

package pkg1016;

/**
 *
 * @author trevor kasper
 */

   public class hourlyEmployee extends Employee
   {
      private double wage; // wage per hour

      private double hours; // hours worked for week

     public hourlyEmployee( String first, String last, String ssn,
        double hourlyWage, double hoursWorked )
     {
        super( first, last, ssn );
        setWage( hourlyWage ); // validate hourly wage

        setHours( hoursWorked ); // validate hours worked
     }

     // set wage
     public void setWage( double hourlyWage )
     {
        wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
     } // end method setWage


     // return wage
     public double getWage()
     {
        return wage;
     } // end method getWage

     // set hours worked

     public void setHours( double hoursWorked )
     {
        hours = ( ( hoursWorked >= 0.0 ) && (
                     hoursWorked <= 168.0 ) ) ?
           hoursWorked : 0.0;
     } // end method setHours

     // return hours worked

     public double getHours()
     {
        return hours;
     } // end method getHours

     // calculate earnings; override abstract method
                 // earnings in Employee
     public double earnings()
     {
        if ( getHours() <= 40 ) // no overtime

           return getWage() * getHours();
        else
           return 40 * getWage() + ( getHours() - 40 ) * getWage() *
                           1.5;
     }
     // return String representation of HourlyEmployee object

     public String toString()
     {
        return String.format(
                       "hourly employee: %s\n%s: $%,.2f; %s: %,.2f",
           super.toString(), "hourly wage", getWage(),
           "hours worked", getHours() );
     }
  } 


Both of these give me the error "... class is not abstract and does not override abstract method getPaymentAmount() in Payable.

One of the suggested options is "implement all abstract methods". I selected that and it got rid of the aforementioned error, but when I went to run it with my array of [6], the first four print out but the last three do not. The code is here:[code]package pkg1016;

/**
*
* @author tkasper
*/
public class payableInterfaceTest
{
public static void main( String args[] )
{


Payable payableObjects[] = new Payable[ 6 ];


payableObjects[ 0 ] = new Invoice( "01234", "seat", 2,
375.00 );
payableObjects[ 1 ] = new Invoice( "56789", "tire", 4, 79.95 );
payableObjects[ 2 ] = new SalariedEmployee( "John", "Smith",
"111-11-1111", 800.00 );
payableObjects[ 3 ] =
new hourlyEmployee( "Lisa", "Barnes", "888-88-8888",
15.50, 40 );
payableObjects[4] = new commissionEmployee("Trevor", "Kasper",
"123-45-6789", 10, 0.8);
payableObjects[5]= new BasePlusCommissionEmployee("Brian","Sullivan",
"098-76-5432", 500, 0.7, 45000);


System.out.println(
"Invoices and Employees processed polymorphically:\n" );



for ( Payable currentPayable : payableObjects )
{
//output currentPayable and its appropriate payment amount
System.out.printf( "%s \n%s: $%,.2f\n\n",
currentPayable.toString(),
"payment due", currentPayable.getPaymentAmount() );
}
}
}

that outputs:

Invoices and Employees processed polymorphically:

invoice:
part number: 01234 (seat)
quantity: 2
price per item: $375.00
payment due: $750.00

invoice:
part number: 56789 (tire)
quantity: 4
price per item: $79.95
payment due: $319.80

salaried employee: John Smith
social security number: 111-11-1111
weekly salary: $800.00
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
payment due: $800.00

at pkg1016.hourlyEmployee.getPaymentAmount(hourlyEmployee.java:76)
at pkg1016.payableInterfaceTest.main(payableInterfaceTest.java:46)
/Users/tkasper/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

~~~~~~~~

I'm stuck and I don't know where to go from here. Anything else needed to understand it? I have the other classes, just didn't think they were super necessary. I can upload them if needed.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Class not abstract, does not override abstract method, etc

#2 sniperwolfsguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 17

Re: Class not abstract, does not override abstract method, etc

Posted 04 December 2017 - 01:49 PM

the implement all abstract methods adds the below to hourlyEmployee and commissionEmployee:
@Override
public double getPaymentAmount() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

leading to that crash at the bottom of the main topic
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,786
  • Joined: 12-June 08

Re: Class not abstract, does not override abstract method, etc

Posted 04 December 2017 - 02:02 PM

You definitely need to implement them all. Then fill in their bodies after removing the 'threw new exception'.
Was This Post Helpful? 1
  • +
  • -

#4 sniperwolfsguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 17

Re: Class not abstract, does not override abstract method, etc

Posted 04 December 2017 - 02:10 PM

View Postmodi123_1, on 04 December 2017 - 02:02 PM, said:

You definitely need to implement them all. Then fill in their bodies after removing the 'threw new exception'.


Okay, so I implemented all abstract methods. I need to fill in:
@Override
public double getPaymentAmount() { <-with something here?
before here? -> throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,786
  • Joined: 12-June 08

Re: Class not abstract, does not override abstract method, etc

Posted 04 December 2017 - 02:13 PM

Well read the line. It's there to remind you that you have not put in anything there.. like the actual logic to the method. So you would REMOVE the line and add what ever logic you think you need.

Hence the "not supported yet" in the exception.

That's the joys of abstraction and overriding.. Just promises to the future that you'll implement it.
Was This Post Helpful? 1
  • +
  • -

#6 sniperwolfsguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 17

Re: Class not abstract, does not override abstract method, etc

Posted 04 December 2017 - 02:24 PM

Copy! I thought more about it, so I just renamed my earnings method in those classes to getPaymentAmount and that resolved it. I removed the @Overrides.

Thanks!

This post has been edited by andrewsw: 04 December 2017 - 02:25 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1