a. Write a program named DEMOJOBS for Harold’s Home Services. The program should
ID: 3676354 • Letter: A
Question
a. Write a program named DEMOJOBS for Harold’s Home Services. The program should instantiate several Job objects and demonstrate their methods. The Job class contains four data fields—description (for example, “wash windows”), time in hours to complete (for example, 3.5), per-hour rate charged (for example, $25.00), and total fee (hourly rate times hours). Include properties to get and set each field except the total fee—that field will be read-only, and its value is calculated each time either the hourly fee or the number of hours is set. Overload the + operator so that two Jobs can be added. The sum of two Jobs is a new Job containing the descriptions of both original Jobs (joined by “and”), the sum of the time in hours for the original Jobs, and the average of the hourly rate for the original Jobs.
Explanation / Answer
/**Job class that constains methods to set description, time and rate.
The method calculateFee updates the fee whenever change of time and rate*/
//Job.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DEMOJOBS
{
class Job
{
private string description;
private double time;
private double rate;
private double totalFee;
//Construcytor to set the description, time and rate
public Job(string descrition, double time, double rate)
{
this.description = descrition;
this.time = time;
this.rate = rate;
//calculate fee
calculateFee(time, rate);
}
public Job()
{
}
public void setDescription(string description)
{
this.description = description;
}
public string getDescription()
{
return description;
}
public void setTime(double time)
{
this.time = time;
//update totalfee
calculateFee(time , getRate());
}
public double getTime()
{
return time;
}
public void setRate(double rate)
{
this.rate = rate;
//update totalfee
calculateFee(getTime(), rate);
}
public double getRate()
{
return rate;
}
public double getFee()
{
return totalFee ;
}
private void calculateFee(double time, double rate)
{
totalFee = time * rate;
}
// Overload + operator to add two Box objects.
public static Job operator +(Job a, Job b)
{
//create temp job object
Job temp = new Job();
//set description from a and b objects of Job
temp.setDescription(a.getDescription() + " and " + b.getDescription());
//set time
temp.setTime(a.getTime() + b.getTime());
//set rate as average of two rates
temp.setRate((a.getRate() + b.getRate()) / 2.0);
//return temp
return temp;
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
/**Tester c sharp program that instantiates two objects
of Job and prints the attribute details and test the
overloaidng + operator and print results to concolse*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DEMOJOBS
{
class Program
{
static void Main(string[] args)
{
//Create two Job objects
Job joba = new Job("wash windows", 3.5, 25);
Job jobb = new Job("clean floor", 5.5, 10);
Console.WriteLine("joba");
//print joba
Console.WriteLine("Description : " + joba.getDescription());
Console.WriteLine("Hours : " + joba.getTime());
Console.WriteLine("Rate : " + joba.getRate());
Console.WriteLine("Fee : " + joba.getFee());
Console.WriteLine("jobb");
//print jobb
Console.WriteLine("Description : " + jobb.getDescription());
Console.WriteLine("Hours : " + jobb.getTime());
Console.WriteLine("Rate : " + jobb.getRate());
Console.WriteLine("Fee : " + joba.getFee());
//Adding two new jobs using + operator overloading
Job newjob = joba + jobb;
//print newjob attribute values
Console.WriteLine("New Job");
Console.WriteLine("Description : " + newjob.getDescription());
Console.WriteLine("Hours : " + newjob.getTime());
Console.WriteLine("Rate : " + newjob.getRate());
Console.WriteLine("Fee : " + newjob.getFee());
Console.ReadKey();
}
}
}
----------------------------------------------------------------------------------------------------------------------------------
Sample output:
joba
Description : wash windows
Hours : 3.5
Rate : 25
Fee : 87.5
jobb
Description : clean floor
Hours : 5.5
Rate : 10
Fee : 87.5
New Job
Description : wash windows and clean floor
Hours : 9
Rate : 17.5
Fee : 157.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.