Jump to content

OOp programing question

- - - - -

  • Please log in to reply
13 replies to this topic

#1
geno53

geno53

    Newbie

  • Members
  • PipPip
  • 11 posts
Write a program that inputs (from the user) the number of hours worked and hourly pay rate for employees and outputs their total pay. There should be processed an arbitrary number of employees; the user will terminate input by entering 0 for hours worked and rate of pay. If we use a class called Worker : How should this look like in any program language

Attributes: Hours, Rate, Total
Compute Total and access methods for each of the attributes

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
What have you gotten so far? Where are you having trouble writing it?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Given that some of us are studying esoteric languages, that is a dangerous request (any language).

Given that the nature of this problem is procedural, rather than OOP, why do you call it an OOP programming question?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
I have no idea what OOP language you have in mind. But since it's for any language, then I'll use UML. At least it is a language for modelling OO software project. :)

Below is an UML class diagram of how I think you should design these classes. Note that between Worker and Task there should be a collection of task. I did not put it there just to make it a bit clearer.

Attached File  oop-programing-question.png   7.83K   36 downloads

#5
geno53

geno53

    Newbie

  • Members
  • PipPip
  • 11 posts
Well I'm learning from a programming book, however I can only give an example for this problem which I found:
Class Tax

Income As Real

TaxDue As Real

Subprogram SetIncome(NewIncome)

Set Income = NewIncome

End Subprogram

Subprogram ComputeTax()

If Income <= 50000 Then

Set TaxDue = .05 * Income

Else

If Income > 50000 And Income < 100000 Then

Set TaxDue = 2500 + .07 (Income - 50000)

Else

Set TaxDue = 6000 + .11 * (Income - 100000)

End If

End If

End Subprogram

Function GetTax() As Real

Set GetTax = TaxDue

End Function

End Class

Main

Declare Tax1 As Tax

Declare Income As Real

Write “Enter an income; this program will compute the tax due.”

Write “To quit, enter 0.”

Input Income

While Income > 0

Call Tax1.SetIncome(Income)

Call Tax1.ComputeTax()

Write “Tax due = $”, GetTax()

Write “Enter an income; enter 0 to quit.”

Input Income

End While

End Program

#6
geno53

geno53

    Newbie

  • Members
  • PipPip
  • 11 posts
Ok Thanks so much, this looks like a start I'll try to put this into a program list

#7
geno53

geno53

    Newbie

  • Members
  • PipPip
  • 11 posts
I'm not sure but I believe I must start with this format can you tell me if I pointed in the right direction. Methods: computTotal and access methods (sethours, GetHours, setRate, GetRate, SetTotal and getTotal) for each of the attributes.

#8
geno53

geno53

    Newbie

  • Members
  • PipPip
  • 11 posts
If i can get the correct line format this will work: Methods: computTotal and access methods (sethours, GetHours, setRate, GetRate, SetTotal and getTotal) for each of the attributes.

#9
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
You can go a few routes with this, the two obvious ones I see are as follows.

Worker is a complete class containing both data and functions to modify.

class Worker
{
float Hours, Rate, Total;

get/set Hours, Rate, Total;
}

Or worker can be a pure data class exposing itself to be used and modified as it will.

class Worker
{
float Hours, Rate, Total;
}

class PayRoll
{
vector<Worker> Employees;

get/set Hours, Rate, Total;
}

The question is solved by looking at the overall design and deciding which is more transparent, secure, and in general works best.

#10
JewFro297

JewFro297

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 224 posts
This seems like Java to me... Is that the language you are using?
Yea Dat's right.
Apple sucks :thumbup:
[SIGPIC][/SIGPIC]

#11
geno53

geno53

    Newbie

  • Members
  • PipPip
  • 11 posts
Thanks, I'm doing a flow chart and this looks good I'm going to try a few different directions, to get the best results thanks for the input.

#12
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts

geno53 said:

If i can get the correct line format this will work: Methods: computTotal and access methods (sethours, GetHours, setRate, GetRate, SetTotal and getTotal) for each of the attributes.

Yes, sure. For each attribute you need getter and setter methods.

Quote

Thanks, I'm doing a flow chart and this looks good I'm going to try a few different directions, to get the best results thanks for the input.

If you are really into OOP, better start learning UML now. There is a diagram in UML that resembles flow chart, btw.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users