Here is the piece of Assignment Two
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
//declare a constant
const double MEDICARE = .03;
static void Main(string[] args)
{
Program p = new Program();
//call show pay
p.ShowNetPay();
}
private double GrossPay(double hours, double rate)
{
//we pass hours and rate into the method and then
//multiply them
//not worried about overtime at this point
return hours * rate;
}
private double CalculateMedicare(double gross)
{
//pass in gross and multiply it by constant
return gross * MEDICARE;
}
private void ShowNetPay()
{
//enter the hours and rate
Console.WriteLine("Enter hours worked");
double hours = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the rate of Pay");
double rate = double.Parse(Console.ReadLine());
//get the gross pay by calling the method
//and storing the value it returns
double grosspay = GrossPay(hours, rate);
//call the method for medicare
double med = CalculateMedicare(grosspay);
//do the calculation for net pay
double netpay = grosspay - (med );
//Display the result
Console.WriteLine("Your net pay is {0}", netpay);
}
}
}
Here is the sample with if statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SelectionExamples
{
class Program
{
//first we will make a method to determine
//if a number even
//we also introduce tryParse
static void Main(string[] args)
{
//initialize the rest of the class
Program p = new Program();
//call the TestNumber method
p.TestNumber();
//pause the program to wait for a key stroke
Console.ReadKey();
}
private void TestNumber()
{
//this gets an integer number from the user
//if it is not a valid integer it propmpts the user
//to start again with a valid integer number
//declare a variable with a default of zero
//the TryParse out parameter will assign
//a new value to this variable
int num = 0;
//asl for a number from the user
Console.WriteLine("Enter an Integer");
//the tryParse returns true or false (a boolean)
//if the string on the console can be parsed as an integer
//it returns true and assigns the value to num,
//if not it returns false
bool isInt = int.TryParse(Console.ReadLine(), out num);
//if it is false prompt them to enter a valid number
if (isInt == false)
{
Console.WriteLine("Make sure you enter an integer");
return; //end the method
}
//if it is true it will continue to execute the code
//we call the TestForEven number,
//and pass the num we got from the console
//then we store the
//result ("odd" or "even") in the variable numType
string numType = TestForEven(num);
//print out the value of numType
Console.WriteLine(numType);
}
private string TestForEven(int number)
{
//create a variable with a default value
//of odd
string result = "odd";
//if then the condition in parenthesis
//must resolve to true or false
if (number % 2 == 0) //== means equal
{
result = "even";
}
//return the result
return result;
}
}
}
No comments:
Post a Comment