Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

Earnings

Write a program that calculates an employee’s pay for a week based on the hourly wage and the number of hours worked. See the figure below. All computations should be performed by an instance of the class Wages.

Earnings

Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
 frmEarnings Earnings Form Holds Controls
 txtHoursWorked TextBox Captures hours worked
 txtHourlyWage TextBox Captures hourly wage
 btnCalculate  Calculate Pay for the Week Button Triggers event to calculate pay
 txtPay TextBox Displays week’s pay.  Read Only set to True

Write the Code:

' Project:  Earnings
' Description:  This program illustrates the use of objects and classes.  The user enters
'    a wage and number of hours worked.  The values are assigned to members of the object.
'    The total pay is calcuted by a function and displayed.

Public Class frmEarnings
    Dim worker As Employee ' worker is an object of class Employee

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        worker = New Employee()  ' Create an instance of Employee
        worker.HoursWorked = CDbl(txtHours.Text)
        worker.Wage = CDbl(txtWage.Text)
        txtPay.Text = worker.CalcPay().ToString("C")
    End Sub
End Class

Class Employee
    Private m_wage As Double
    Private m_hoursWorked As Double
    Private m_pay As Double

    Public Property Wage() As Double
        Get
            Return m_wage
        End Get
        Set(value As Double)
            m_wage = value
        End Set
    End Property

    Public Property HoursWorked() As Double
        Get
            Return m_hoursWorked
        End Get
        Set(value As Double)
            m_hoursWorked = value
        End Set
    End Property

    Function CalcPay() As Double
        ' Calculates the pay from the wage and hours worked
        m_pay = m_hoursWorked * m_wage
        Return m_pay
    End Function
End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2025 · Metro Pro Theme on Genesis Framework · WordPress · Log in