View Single Post
  #1 (permalink)  
Old 11-06-2007, 10:31 PM
assamhammad assamhammad is offline
Member
 
Join Date: Nov 2007
Posts: 7
assamhammad is on a distinguished road
for Assignment plz help
i m given an assignment in which i have to make a windows based application in which there are the buttons of some workers and employees and aslo the button of boss.now if we click any button, it tells the salary of an employee.also it can be exited by clicking the exit button.
i m giving the code of programme in which modifications are needed like there should be a dialog box when we write boss salary in dialog box it should display the salary of boss, same is the case with employees.
plz help me how to do this assignment.
i m very much new to java.
plz help plz

the code is


// Boss class derived from Employee

public final class Boss extends Employee {
private double weeklySalary;

// Constructor for class Boss
public Boss( String first, String last, double s)
{
super( first, last ); // call superclass constructor
setWeeklySalary( s );
}

// Set the Boss's salary
public void setWeeklySalary( double s )
{ weeklySalary = ( s > 0 ? s : 0 ); }

// Get the Boss's pay
public double earnings() { return weeklySalary; }

// Print the Boss's name
public String toString()
{
return "Boss: " + super.toString();
}
}


// CommissionWorker class derived from Employee

public final class CommissionWorker extends Employee {
private double salary; // base salary per week
private double commission; // amount per item sold
private int quantity; // total items sold for week

// Constructor for class CommissionWorker
public CommissionWorker( String first, String last,
double s, double c, int q)
{
super( first, last ); // call superclass constructor
setSalary( s );
setCommission( c );
setQuantity( q );
}

// Set CommissionWorker's weekly base salary
public void setSalary( double s )
{ salary = ( s > 0 ? s : 0 ); }

// Set CommissionWorker's commission
public void setCommission( double c )
{ commission = ( c > 0 ? c : 0 ); }

// Set CommissionWorker's quantity sold
public void setQuantity( int q )
{ quantity = ( q > 0 ? q : 0 ); }

// Determine CommissionWorker's earnings
public double earnings()
{ return salary + commission * quantity; }

// Print the CommissionWorker's name
public String toString()
{
return "Commission worker: " + super.toString();
}
}

// Abstract base class Employee

public abstract class Employee {
private String firstName;
private String lastName;

// Constructor
public Employee( String first, String last )
{
firstName = first;
lastName = last;
}

// Return the first name
public String getFirstName() { return firstName; }

// Return the last name
public String getLastName() { return lastName; }

public String toString()
{ return firstName + ' ' + lastName; }

// Abstract method that must be implemented for each
// derived class of Employee from which objects
// are instantiated.
public abstract double earnings();
}


// Definition of class HourlyWorker

public final class HourlyWorker extends Employee {
private double wage; // wage per hour
private double hours; // hours worked for week

// Constructor for class HourlyWorker
public HourlyWorker( String first, String last,
double w, double h )
{
super( first, last ); // call superclass constructor
setWage( w );
setHours( h );
}

// Set the wage
public void setWage( double w )
{ wage = ( w > 0 ? w : 0 ); }

// Set the hours worked
public void setHours( double h )
{ hours = ( h >= 0 && h < 168 ? h : 0 ); }

// Get the HourlyWorker's pay
public double earnings() { return wage * hours; }

public String toString()
{
return "Hourly worker: " + super.toString();
}
}


// PieceWorker class derived from Employee

public final class PieceWorker extends Employee {
private double wagePerPiece; // wage per piece output
private int quantity; // output for week

// Constructor for class PieceWorker
public PieceWorker( String first, String last,
double w, int q )
{
super( first, last ); // call superclass constructor
setWage( w );
setQuantity( q );
}

// Set the wage
public void setWage( double w )
{ wagePerPiece = ( w > 0 ? w : 0 ); }

// Set the number of items output
public void setQuantity( int q )
{ quantity = ( q > 0 ? q : 0 ); }

// Determine the PieceWorker's earnings
public double earnings()
{ return quantity * wagePerPiece; }

public String toString()
{
return "Piece worker: " + super.toString();
}
}



// Driver for Employee hierarchy
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Test {
public static void main( String args[] )
{
Employee ref; // superclass reference
String output = "";

Boss b = new Boss( "John", "Smith", 800.00 );
CommissionWorker c =
new CommissionWorker( "Sue", "Jones",
400.0, 3.0, 150);
PieceWorker p =
new PieceWorker( "Bob", "Lewis", 2.5, 200 );
HourlyWorker h =
new HourlyWorker( "Karen", "Price", 13.75, 40 );

DecimalFormat precision2 = new DecimalFormat( "0.00" );

ref = b; // Employee reference to a Boss
output += ref.toString() + " earned $" +
precision2.format( ref.earnings() ) + "\n" +
b.toString() + " earned $" +
precision2.format( b.earnings() ) + "\n";

ref = c; // Employee reference to a CommissionWorker
output += ref.toString() + " earned $" +
precision2.format( ref.earnings() ) + "\n" +
c.toString() + " earned $" +
precision2.format( c.earnings() ) + "\n";

ref = p; // Employee reference to a PieceWorker
output += ref.toString() + " earned $" +
precision2.format( ref.earnings() ) + "\n" +
p.toString() + " earned $" +
precision2.format( p.earnings() ) + "\n";

ref = h; // Employee reference to an HourlyWorker
output += ref.toString() + " earned $" +
precision2.format( ref.earnings() ) + "\n" +
h.toString() + " earned $" +
precision2.format( h.earnings() ) + "\n";

JOptionPane.showMessageDialog( null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
Reply With Quote
Sponsored Links