I am never taking a C# class again.
My question this time is I am trying to create an object named "newEmployee" in my main class that calls the employee class constructor method. My TL;DR constructor code is:
// Constructor method
public Employee(double hrsWorked, double rate) // These are defined elsewhere. See end of post for
// total code if need be.
{
hoursWorked = hrsWorked;
rateofPay = rate;
}
Isn't that the name of my object too? as in:
// Object
Employee newEmployee = new Employee(double hrsWorked, double rate);
I swear that is how it is in the book. But, I keep getting the errors (ALL on line 29)
Error 3 ; expected
Error 4 ; expected
Error 6 ; expected
Error 5 Invalid expression term ')'
Error 2 Invalid expression term ','
Error 1 Invalid expression term 'double'
Error 7 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Error 8 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Does VS just hate me and want me to stop trying to write code or what?
Here's the full code just incase I do not know what I'm talking about:
using System;
using SC = System.Console;
public class Lab18
{ // Main method
public static void Main()
{
string hoursWorkedStr, rateofPayStr;
double hrsWorked, rateofPay;
SC.WriteLine("Lab 18");
//Input
SC.Write("\nPlease input hours worked: ");
hoursWorkedStr = Console.ReadLine();
hrsWorked = Convert.ToDouble(hoursWorkedStr);
SC.Write("\nPlease input rate of pay: ");
rateofPayStr = Console.ReadLine();
rateofPay = Convert.ToDouble(rateofPayStr);
Employee newEmployee = new Employee(double hrsWorked, double rate);
newEmployee.CalcGrossPay();
newEmployee.CalcStateTax();
newEmployee.CalcFedTax();
newEmployee.CalcFicaTax();
newEmployee.CalcNetPay();
SC.WriteLine("\nEnd of Lab 18\n");
}
} // end of "Lab18" class
class Employee
{
private double hoursWorked, rateofPay, grossPay, stateTax, fedTax, ficaTax, netPay;
// Constructor method
public Employee(double hrsWorked, double rate)
{
hoursWorked = hrsWorked;
rateofPay = rate;
}
// Gross pay method
public void CalcGrossPay()
{
grossPay = hoursWorked * rateofPay;
SC.WriteLine("\nGross pay is:\t{0,10}", grossPay.ToString("C"));
}
// State tax method
public void CalcStateTax()
{
const double stateTaxRate = .04;
stateTax = grossPay * stateTaxRate;
SC.WriteLine("\nState tax is:\t{0,10}", stateTax.ToString("C"));
}
// Federal tax method
public void CalcFedTax()
{
const double fedTaxRate = .23;
fedTax = grossPay * fedTaxRate;
SC.WriteLine("\nFederal tax is:\t{0,10}", fedTax.ToString("C"));
}
// FICA tax method
public void CalcFicaTax()
{
const double ficaTaxRate = .14;
fedTax = grossPay * ficaTaxRate;
SC.WriteLine("\nFICA tax is:\t{0,10}", grossPay.ToString("C"));
}
//Net pay method
public void CalcNetPay()
{
netPay = grossPay - (stateTax + fedTax + ficaTax);
SC.WriteLine("\nNet pay is:\t{0,10}", netPay.ToString("C"));
}
}