Monday, October 10, 2011

ITC 110 Assignment 2 in Java

Here is the Java equivalent of Assignment 2


package Income;
import java.util.Scanner;

public class NetIncomeCalculator {

 /**
  * This program calculates gross pay
  * and net pay given deductions in 
  * Social Security, medicare and a bus
  * pass. This is the equivalent of
  * Assignment two in C# in ITC 110
  * Steve Conger 10/10/2011
  */
 
 //declaring constants
 final double SOCIALSECURITY = .09;
 final double MEDICARE=.03;
 final int BUSPASS=25;
   
 public static void main(String[] args) {
  //declare and instantiate the class
  //to load it (main being static is
  //already loaded into memory
  NetIncomeCalculator netcalc = new NetIncomeCalculator();
  //call the starting method
  netcalc.Display();
 }
 
 private void Display()
 {
  //get input
  //the scanner is an object to read from the console
  Scanner reader = new Scanner(System.in);
  System.out.println("Enter the number of hours worked");
  double hours = reader.nextDouble();
  System.out.println("Enter your rate per hour");
  double rate=reader.nextDouble();
  
  //call the methods to do the calculations
  double grossPay=CalcIncome(hours, rate);
  double socialSec = CalcSocialSecurity(grossPay);
  double medicare = CalcMedicare(grossPay);
  double netPay=CalcNetPay(grossPay, socialSec, medicare);
  
  //outputs
  System.out.println("Your gross income is " + grossPay);
  System.out.println("Your Social Security deduction is " + socialSec);
  System.out.println("Your Medicare deduction is " + medicare);
  System.out.println("Your Bus pass deduction is " + BUSPASS);
  System.out.println("Your net pay is " + netPay);
 }
 
 private double CalcIncome(double hrs, double rt)
 {
  double gross=0;
  //check to see if the hours are more than forty
  //if they are pay time and a half for overtime
  //if not just pay the rate of pay
  if (hrs > 40)
  {
   gross=rt * (40 + ((hrs-40)*1.5));
  }
  else
  {
   gross = rt * hrs;
  }
  return gross;
 }
 
 private double CalcSocialSecurity(double gPay)
 {
  return gPay * SOCIALSECURITY;
 }
 
 private double CalcMedicare(double gPay)
 {
  return gPay * MEDICARE;
 }
 
 private double CalcNetPay(double gPay, double ss, double med)
 {
  return gPay - (ss + med + BUSPASS);
 }

}

Here is the output from the program


Enter the number of hours worked
45
Enter your rate per hour
25
Your gross income is 1187.5
Your Social Security deduction is 106.875
Your Medicare deduction is 35.625
Your Bus pass deduction is 25
Your net pay is 1020.0

ITC 110 Assignment 1 in Java

This is the Java equivalent of the first ITC 110 assignment. I will do the other assignments in ITC 110 and also post them here


package Mileage;
import java.util.Scanner;
import java.io.*;
public class CalcMileage {

 /**
  * This is a simple program to Calculate
  * miles per gallon
  * it is the equivalent to ITC110
  * assignment 1
  * steve conger 9-26-2011
  */
 public static void main(String[] args) {
  //declare Scanner
  Scanner reader = new Scanner(System.in);
  //declare variables
  double beginMileage;
  double endMileage;
  double gallons;
  double pricePerGallon;
  double milesPerGallon;
  double costPerMile;
  //get input
  System.out.println("Enter the beginning mileage");
  beginMileage=reader.nextDouble();
  System.out.println("Enter the ending mileage");
  endMileage=reader.nextDouble();
  System.out.println("Enter the number of gallons");
  gallons=reader.nextDouble();
  System.out.println("Enter the price per gallon");
  pricePerGallon=reader.nextDouble();
  
  //do processing of input
  milesPerGallon=(endMileage-beginMileage)/gallons;
  costPerMile=pricePerGallon /(endMileage-beginMileage);
  
  //output results
  System.out.print("Your miles per gallon is ");
  System.out.format("%.2f",milesPerGallon);
  System.out.println();
  System.out.print("the cost per mile is ");
  System.out.format("%.2f",costPerMile);
 }

}

Thursday, October 6, 2011

Books and Websites

Here is a list of books and websites you might fine useful to help you with C#

Books

  • Head First C#, O'Reilly, ISBN 978-0596514822
  • Programming C#, Jeff Liberty, O'Relly, ISBM 978-059600699
  • Sam's Teach Yourself, the C# Language in 21 days, Bradley Jones, Sams, ISBN 978-0672325465
  • Murach's C# 2010, Murach and Associates, ISBN 978-1890774592

Web Sites

http://www.msdn.microsoft.com MSDN Microsoft's development Network

http://www.Devx.com DevX development center

Wednesday, October 5, 2011

Start for Assignment 2

Here is the starting code for Assignment 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment2
{
    class Program
    {

        const double SOCSEC = .09;

        static void Main(string[] args)
        {
            Program p = new Program();
            p.Display();
            p.PauseIt();
        }//end main

        private void Display()
        {
            Console.WriteLine("Enter the hours worked this week");
            double hours = double.Parse(Console.ReadLine());
            Console.WriteLine("Enter the rate per hour");
            double rate = double.Parse(Console.ReadLine());

            double weeklyGross = GrossPay(hours, rate);
            Console.WriteLine("Your gross pay is {0:C}", weeklyGross);

            double socialsec = SocialSecurity(weeklyGross);
            Console.WriteLine("Your SS deduction is {0:C}", socialsec);


        }//end Display

        double GrossPay(double weeklyHours, double hourlyRate)
        {
            double gross =0;
            if (weeklyHours > 40)
            {
                gross = hourlyRate * (40 + ((weeklyHours - 40) * 1.5));
            }
            else
            {
                gross = hourlyRate * weeklyHours;
            }
            return gross;
        }//end GrossPay

        private void PauseIt()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }//end PauseIt

        private double SocialSecurity(double grossWeeklyPay)
        {
            return grossWeeklyPay * SOCSEC;
        }
    }//end Program
}//end namespace

Notes for Assignment 2

Remember to break it into methods. Each method should do one thing. For example, one method can calculate the gross pay, another method can deduct social security, etc.

To calculate Overtime we need to us an if statement. We need to do one of two things depending on if the hours are greater than 40 or 40 or less. This can be set up as follows:

if(hours > 40)
{
     pay=rate * (40 + ((hours - 40) * 1.5);
}
else
{
    pay=rate * hours;
}

If the hours are greater than forty, we multiply the rate times forty (because we know we have over forty hours and forty of them are paid at the regular rate) and then add the hours over forty times rate time 1.5 to get time and a half. The parenthesis are necessary to control the order of operations. We want the subtraction to occur first, then the multiplication, then the addition. If, on the other hand, the hours are 40 or less, we just multiply hours times the rate of pay.

For the assignment, you don't have to display the overtime pay separate from the regular pay. You can just display the Gross (total) pay, the deductions and the Net pay (what's left over). Though, if you want to add a challenge you can separate the two in the display.

Tuesday, October 4, 2011

Methods Morning Class

Here is the Morning class's sample code for methods. It is a little different from the evening class. I did not comment it nearly as thoroughly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Methods
{
    class Program
    {
        static void Main(string[] args)
        {
            Program prog = new Program();
            prog.Display();
            prog.PauseIt();
        }//end of Main

       private void PauseIt()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }//end of PauseIt

       private void Display()
       {
           Console.WriteLine("Enter the beginning mileage");
           double beginningMileage = double.Parse(Console.ReadLine());

           Console.WriteLine("Enter the ending mileage");
           double endingMileage = double.Parse(Console.ReadLine());

           double miles = TotalMiles(beginningMileage, endingMileage);

           Console.WriteLine("\nYour total miles is {0}",miles);

           Console.WriteLine("Enter the number of Gallons");
           double gallons=double.Parse(Console.ReadLine());

           double milesPerGal = MilesPerGallon(miles, gallons);
           Console.WriteLine("Your miles per gallon is {0}", milesPerGal);
           
       }//end Display

       private double TotalMiles(double startMiles, double endMiles )
       {
           return endMiles - startMiles;
       }//Total Miles

       private double MilesPerGallon(double totalMiles, double gals)
       {
           double mpg = totalMiles / gals;
           return mpg;
       }//end MilesPerGallon

    }//end of program class

}//end of namespace

Monday, October 3, 2011

Methods Example

Here is our example from class with extensive comments

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace methodsExample
{
    class Program
    {
        /*********************
         * the point of this excercise is to show
         * how to break a program into methods
         * we have a display method and seperate
         * methods to calcualte the gas mileage
         * and the cost per mile.
         * the point of breaking a program into methods
         * is to divide the work and make it easier
         * to maintain, debug and understand.
         * Ideally each method should do one thing
         * that way it is obvious where to look if
         * something goes wrong
         * Steve Conger 10/4/2011 */

        static void Main(string[] args)
        {
            //a static method is loaded automatically
            //into RAM, but the rest of the class is not
            //so we need to "instantiate" the class
            //to load it into memory where it can be used
            Program p = new Program();
            p.Display(); //call the display method
            p.PauseIt();// call the PauseIT class
            
        }

        //void methods don't return a value
        //they just do their thing
        private void Display()
        {
            //inputs
            //just like what we did in the first assignment
            Console.WriteLine("Enter the Mileage");
             double mileage = double.Parse(Console.ReadLine());
             Console.WriteLine("Enter The Gallons");
             double gals = double.Parse(Console.ReadLine());
             Console.WriteLine("Enter The price per gallon");
             double price = double.Parse(Console.ReadLine());
            //outputs
             Console.WriteLine("Your miles per gallon is {0:F2}", 
                 CalculateGasMileage(mileage, gals));//calls the CalculateGasMileage 
            //as part of the write line. You can only do this if the
            //method returns a value

            //this is another way to do it
            //here we assign the returned result of the method
            //to a new variable
            double mpg=CalculateGasMileage(mileage, gals);

            //we can use that variable in the writeline instead of calling
            //the method again
            Console.WriteLine("Your cost per mile is {0:C}",
                CalculateCostPerMile(mpg,price));

           
        }

        //this method returns a double
        //it takes two parameters that are give values
        //when the method is called in the Display() method above
        private double CalculateGasMileage(double miles, double gallons)
        {
            double milesPerGallon;
            milesPerGallon = miles / gallons; 
            return milesPerGallon;
        }

        private double CalculateCostPerMile(double milesPerGal, double pricePerGallon)
        {
            //double costPerGallon=0;
            //this is a different way to return a value
            //you can return the equation directly
            return pricePerGallon / milesPerGal;
        }

        private void PauseIt()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}