• 13
• 15
• 19
• 27
• 9

# Program Help Needed

This topic is 3827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I dont know if anyone is familiar with Visual C# 2005: How to Program by Deitel/Deitel, I'm up to chapter 5 and doing the exercises in the back, specifically 5.20 is the one that is stumping me the most. It asks to make a simple program that shows 3 employees, and their hours worked, plus amount they make per hour. If they work 40 hours or less pay is regular, but above 40 hours it is time and a half. I am in need of desperate help here heh. I learn visually, and absorb it that way, so if anyone has done this exercise, or could show me the code, it'd be much appreciated, that way I can move on to the next and tackle another problem. C# doesn't come as easy to me, so it'll take time for me to absorb it :). Thanks in advance.

##### Share on other sites
Rather than just asking for the answers, why don't you post what you've already tried (surrounded by [source] and [/source] tags) and why it doesn't work. You're much more likely to get a constructive response because

1) It shows you tried.
2) It looks less like homework.
3) It's less work for us.

Also, you'll learn more by having others show you what's going wrong, why this is better than that, and new ways of thinking, rather than "this is how this problem is solved."

So, what've you got for us? [smile]
-jouley

##### Share on other sites
I've got the title heh.
I've heard before that it is never the code is the problem, but rather the logic, which is what I'm finding now. No, it's not homework, it is a side hobby, and I learn by doing the exercises, and asking/looking online for answers to what I have no clue about. The thing is I have NO clue where to start at all, my mind is a blank at this point. The book went through Object Oriented, and I sort of understood that, to certain degrees, now it's introducing the "get-set" accessors, if statements, nested if-else statements, and that type of thing. I fully understand HOW to use these, I just do not understand WHEN to use them, if you understand that?

Thanks again, glad someone responded, I'd sure like to learn this, so I can move on to using more loops than just the For loop (Which is part of chapter 6, but I read ahead a bit :) ).

Original post by jouley
Rather than just asking for the answers, why don't you post what you've already tried (surrounded by
 and
tags) and why it doesn't work. You're much more likely to get a constructive response because

1) It shows you tried.
2) It looks less like homework.
3) It's less work for us.

Also, you'll learn more by having others show you what's going wrong, why this is better than that, and new ways of thinking, rather than "this is how this problem is solved."

So, what've you got for us? [smile]
-jouley[/quote]

##### Share on other sites
Hello,

Well I am going to explain how to do it, I can't provide the code source because I am actually learning C# and I am only on data type stuff, but I know C++. I am pretty sure they have not cover classes so that is not a choice. You probably would want to create an array and initialize it or create the data for each. For example, if you use an array it would be along the lines of string employeeName[2](plus you would initialize it then), or you can do this string employeeName1, string employeeName2, and string employeeName3 and do it for each data type. (Note: they are probably getting paid the same per hour, so you should use a constant for it) Then you would make an if statement to check to see if the employee worked more than 40 hours and if so you would do a calculation to figure it out. If they did not it would execute a different statement. I hope this helps, but I will also post the if statement in C++ code and hopefully you understand from it too.

#include <iostream>int main(){	int employeeHours = 42;	const double PAYRATE = 7.50;	const double OVERTIMERATE = 1.5;	double payCheck = 0;	if (employeeHours > 40)	{		int overPayHours = employeeHours - 40;		payCheck = ( (PAYRATE * OVERTIMERATE) * overPayHours) + ( PAYRATE * 40 );	}	else	{		payCheck = ( PAYRATE * 40);	}	return 0;}

In the example source code I provide how I would do the if statements. Please note, that I did it really quick so there probably is a neater better way to do it. Remember that this code is for C++ and not C#. I hope that you understand what I tried to explain or even understand the code. I believe you should be able to understand, but if not I will explain more if you want me to.

~Carl J. Loucius

*Edit: I see you said they did cover some class stuff. I would recommend to make a class that has data members for the employee and have it included functions or methods(I am not sure what they are called in C#, haven't got on that topic yet) that sets or gets info from it.*

##### Share on other sites
(hours>40 ? ((( hours - 40)* 1.5) +40) * pph : hours * pph)

Messy on the brackets, suck at the mathematic ordering.

##### Share on other sites
Hmm thanks a bunch, I can make sense of the code, looks easy to put into terms for me for C#. Yeah I flipped ahead a bit in the book, chapter 8 is actually when they decide to get into Arrays, looks pretty straightforward to me.

I'll try this out tonight and post if I got it working or not. Thanks for the help so far.

Oh also, I did start off with the private strings, and then naming the employee's, such as firstEmployee, and so on, so we can agree on something there too :).

##### Share on other sites
If just finished reading about object oriented....I would think they would want you to use a class. Are you familar with what a class is?

##### Share on other sites
I just did this the class way. I created three different employees but I only did the code for one of them.

It could be coded much better, but it gives you the idea atleast.

using System;using System.Collections.Generic;using System.Text;namespace Employees{    class Employee    {        public double Hours;  // hours worked        public double Pay;    // obvious?    }    class Program    {        static void Main(string[] args)        {            Employee Tom = new Employee();            Employee Bill = new Employee();            Employee Jeff = new Employee();            double FinalPay = 0;  // Final Result            double OverTime = 0;  // Over Time Hours            Tom.Hours = 42;  // 38 hours            Tom.Pay = 10;  // \$10 an hour            Bill.Hours = 45;            Bill.Pay = 12;            Jeff.Hours = 19;            Jeff.Pay = 9;            if (Tom.Hours > 40) // Test to see if theres over time            {                // Figure how much over time                OverTime = Tom.Hours - 40;                // Figure the regular pay                FinalPay = (Tom.Hours - OverTime) * Tom.Pay;                  //Figure the overtime pay                FinalPay += (OverTime * (Tom.Pay * 1.5));            }            else            {                OverTime = 0;                FinalPay = Tom.Hours * Tom.Pay;            }            // Display the full Check            Console.Write("Tom's Paycheck: \t" + FinalPay + "\n");            // Display regular hours worked            Console.Write("Tom's Regular hours: \t" +                 (Tom.Hours - OverTime) + "\n");            // Display Over time hours            Console.Write("Tom's Overtime: \t" + OverTime + "\n");                                           }    }}

[Edited by - dillscon on September 25, 2007 11:17:43 PM]

##### Share on other sites
Ok, I'll write what I can. Pseudo code, though. (kind of, maybe)

class Employee{  int hours;  string name;  float hourWage;public Employee(string name, float hourWage){  this.name = name;  this.hourWage = hourWage;   //assign name & hourWage of this object to what's sent in}public void setHours(int hours){  this.hours = hours;  //set number of hours.  If you had employees, you would need to set this weekly, that's why it's not up above (in Constructor)}public float getPay() //most important thing.{  float temp;  //to store the calculated value.  if(this.hours > 40){    temp = (this.hours - 40) * this.hourWage * 1.5; //overtime hours    temp += 40 * hourWage;  //for the first 40, non-overtime hours}else temp = this.hours * this.hourWage ;  //add to temp either the leftover hours or all ( less than 40) hours.return temp;}}

That's probably about what the Employee class would look like. In your main you would make 3, possibly using an array ( Employee[]; ) if you know how. Send in the name & wage when they're made, and set their hours, or you could overload the constructor and allow it so you can do all three (or just modify what I did).

The getPay method returns what they should be paid. The way it works, is it checks if they should receive any overtime in the "if" statement. If they don't get overtime, it just multiples hours & hourWage and returns. If they DO have overtime, it calculates the overtime hours (hours - 40) * wage * 1.5 . Then calculates the 40 at normal value. There's a better way to do all that than that, but it gives you an idea.

And for your logic ideas, you use an IF statement, pretty much if you would say if. If this thing is green turns into
 if (color ==green)

Hope that's coherent and good luck!