Tuesday, October 11, 2011

Morning Class Selection statements

Here is the code we did in class with comments


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

namespace SelectionExamples
{
    class Program
    {
        /*******************
         * this program has examples of 
         * if statements including an embedded if
         * and an if else statement
         * steve Conger 10/11/2011 
         * ****************************/

        static void Main(string[] args)
        {
            Program prog = new Program();
            //prog.SimpleIf();
            prog.IfElseIfExample();
            prog.PauseIt();

        }

        private void SimpleIf()
        {
            //this is not so simple an if any more
            //declare an integer
            //this is assigned a value by the "out" parameter
            //two lines down
            int number = 0;
            Console.WriteLine("Enter an integer");

            //try parse returns true or false
            //true if the number can be converted to an integer
            //otherwise false. If it is true it passes the 
            //interger value "out" to the number variable
            //declared above
            bool isInteger = int.TryParse(Console.ReadLine(),out number);

            //below is an example of an imbedded if
            //if it is a good number test it 
            if (isInteger == true)
            {
                //never get to this if, if it is not a number
                if (number % 2 == 0)
                {
                    Console.WriteLine("the number is even");
                }
                else
                {
                    Console.WriteLine("the number is odd");
                }
            }
            else
            {
                Console.WriteLine("Not a valid number");
            }
        }

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

        private void IfElseIfExample()
        {
            Console.WriteLine("Enter a test score");
            int score = int.Parse(Console.ReadLine());

            string grade=null; //initializes the string though to nothing

            //below is an if, else if statement. It allows you to test 
            // a value against a sequence of ranges
            //it is important that the sequence be correct
            if (score >= 90)
            {
                grade = "A";
            }
            else if (score >= 80)
            {
                grade = "B";
            }
            else if (score >= 70)
            {
                grade = "C";
            }
            else if (score >= 60)
            {
                grade = "D";
            }
            else
            {
                grade = "F";
            }
            Console.WriteLine("your grade is {0}", grade);
        }//end if else if
        
    }//end Program

    
}

Monday, October 10, 2011

If statements

Here is the code from the evening class



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

namespace IfExamples
{
    class IfStatements
    {
        static void Main(string[] args)
        {
            IfStatements ifs = new IfStatements();
            //ifs.SimpleIfStatement();
            //ifs.IfElseExample();
            ifs.SwitchExample();
            ifs.PauseIt();
        }//end main

        private void SimpleIfStatement()
        {
            Console.WriteLine("Enter an age");
            int age = int.Parse(Console.ReadLine());
           //equality ==
            //!= not equal

            if (age > 75)
            {
                Console.WriteLine("You are old");
            }
            else
            {
                Console.WriteLine("You are a spring chicken");
            }
        }//end SimpleIf

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


        }//end PauseIt

        private void IfElseExample()
        {
            bool isInteger;
            int score;
            Console.WriteLine("Enter your test Score");
             isInteger= int.TryParse(Console.ReadLine(), out score);

             if (isInteger == false)
             {
                 Console.WriteLine("Next time enter a number");
                 return;
             }

            int grade = 0;

            if (score >= 90)
            {
                grade = 4;
            }
            else if (score >= 80)
            {
                grade = 3;
            }
            else if (score >= 70)
            {
                grade = 2;
            }
            else if (score >= 60)
            {
                grade = 1;
            }
            else
            {
                grade = 0;
            }

            Console.WriteLine("Your Grade is {0}", grade);
        }//end ifelse

        private void SwitchExample()
        {
            int choice;
            Console.WriteLine("Choose which method to run");
            Console.WriteLine("1: SimpleIfStatement");
            Console.WriteLine("2: IfElseExample");
            Console.WriteLine("3: Exit");

            choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    SimpleIfStatement();
                    break;
                case 2:
                    IfElseExample();
                    break;
                case 3:
                    PauseIt();
                    break;
                default:
                    Console.WriteLine("Not a valid choice");
                    break;
            }

        }//end switch

        //if (number > 0 && number < 100) and
        //if (number < 0 || number > 100) or

    }//end class
}

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.