Wednesday, November 2, 2011

Class Examples (Evening)

Here is our Box class


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

namespace ClassExamples
{
    public class Box
    {
        //fields
        private double height;
        private double width;
        private double depth;

        //default constructor
       //a constructor has the same name
       //as the class and no return type
        public Box()
        {
            Height = 0;
            Width = 0;
            Depth = 0;
        }

       //overloaded constructor
        public Box(double h, double w, double d)
        {
            Height = h;
            Width = w;
            Depth = d;
        }
        
        #region properties

        public double Width
        {
            get { return width; }
            set { width = value; }
        }
        
       
        public double Depth
        {
            get { return depth; }
            set { depth = value; }
        }

        public double Height
        {
            get 
            {
                return height;
            }
            set
            {
                height = value;
            }
        
        }
        #endregion

        //class method 
        public double CalculateVolume()
        {
            return height * width * depth;
        }
    }
}

Here is the main class where we called the Box class and assigned properties


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

namespace ClassExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.BackgroundColor = ConsoleColor.DarkMagenta;
            //Console.WriteLine("Hello");

            //initialize the new box class 
            //using the default constructor
            Box b = new Box();
            //set the property values
            b.Height = 5.1; //all use the set of the property
            b.Depth = 6;
            b.Width = 4;

            //call the method
            double volume = b.CalculateVolume();

            //these are using the get part of the properties
            Console.WriteLine("The volume of a box with a height of {0} and a width of {1} and a depth of {2} has a volume of {3} cubic feet", b.Height, b.Width, b.Depth, volume);

            //initialize a second Box instance
            //useing the overloaded constructor
            Box b2 = new Box(4, 5, 2);

            Console.WriteLine("The volume of box 2 is {0} cubic feet", b2.CalculateVolume());
           
            Console.ReadKey();


        }
    }
}

Monday, October 31, 2011

Java Equivalent to ITC110 Assignment 5

Here is the java code


import java.util.Scanner;
public class GuessingGame {

 /**
  * This program presents a guessing game
  * where the computer randomly selects
  * a number between 0 and 1000
  * and the player gets 10 chances to guess
  * the player can play as many times
  * as he or she wants and the program
  * will list total wins and average score
  */
 int gameCounter=0;
 int[] scoreArray=new int[50];
 public static void main(String[] args) {
  //instantiate the class
  GuessingGame gg = new GuessingGame();
  //get the scanner
  Scanner reader = new Scanner(System.in);
  //initialize the variable for exiting
  int choice=1;
  //loop so the player can play until they choose
  //to quit
  do
  {
   //call the start of the game
  gg.Start();
  //increment the number of games
  gg.gameCounter++;
  System.out.println("Do you want to play again? 0 to quit");
  choice=reader.nextInt();
  
  }while (choice != 0);

 }
 
 private void Start()
 {
  //Call the method to get the random number
  double num=GetRandom();
  //the line below just for testing 
  //System.out.println((int)num);
  
  //cast the random number to an integer
  //and pass to the GetGuess method
  GetGuess((int)num);
 }
 
 private double GetRandom()
 {
  //Get the random number
  double number = Math.random()*1000;
  return number;
 }
 
 private void GetGuess(int number)
 {
  int score; //declare score counter
  Scanner reader = new Scanner(System.in);
  //loop backwards to keep score
  for(score=10; score > 0; score--)
  {
   System.out.println("Enter a quess between 0 and 1000");
   double guess=reader.nextDouble();
   
   //check the guesses
   if(guess > number)
    System.out.println("Your guess is too high");
   else if(guess < number)
    System.out.println("Your guess is too low");
   else
   {
    System.out.println("congratulations");
    break; //leave the loop if correct
   }
  }
  GetScores(score);//call the get scores method
 }
 
 private void GetScores(int score)
 {
  //output the current score
  System.out.println("Your current Score is " + score);
  //assign it to the scoreArray
  scoreArray[gameCounter]=score;
  //initialize the variable to get the totals
  int scoreTotal=0;
  //loop through the array to get the total

  for (int i=0;i<50;i++)
  {
   scoreTotal+=scoreArray[i];
  }
  //get the average casting it to a double to get decimal
  //pars
  double scoreAverage=(double)scoreTotal/(double)(gameCounter + 1);
  //pring out the results
  System.out.println("Your have played " + (gameCounter + 1) + "games");
  System.out.println("Your average score is " + scoreAverage);
 }

}

Here is a transcript of the console when the program is running


Enter a quess between 0 and 1000
500
Your guess is too low
Enter a quess between 0 and 1000
750
Your guess is too low
Enter a quess between 0 and 1000
875
Your guess is too high
Enter a quess between 0 and 1000
800
Your guess is too low
Enter a quess between 0 and 1000
850
Your guess is too high
Enter a quess between 0 and 1000
825
Your guess is too high
Enter a quess between 0 and 1000
812
Your guess is too low
Enter a quess between 0 and 1000
819
Your guess is too low
Enter a quess between 0 and 1000
822
Your guess is too low
Enter a quess between 0 and 1000

823
congratulations
Your current Score is 1
Your have played 1games
Your average score is 1.0
Do you want to play again? 0 to quit
1
Enter a quess between 0 and 1000
500
Your guess is too high
Enter a quess between 0 and 1000
250
Your guess is too low
Enter a quess between 0 and 1000
350
Your guess is too low
Enter a quess between 0 and 1000
425
Your guess is too low
Enter a quess between 0 and 1000
475
Your guess is too high
Enter a quess between 0 and 1000
450
Your guess is too high
Enter a quess between 0 and 1000
433
Your guess is too low
Enter a quess between 0 and 1000
444
Your guess is too high
Enter a quess between 0 and 1000
439
Your guess is too low
Enter a quess between 0 and 1000
441
Your guess is too high
Your current Score is 0
Your have played 2games
Your average score is 0.5
Do you want to play again? 0 to quit

0

Thursday, October 27, 2011

MultiDimensional Arrays

The simplest multidimensional array is a two dimensional array. You can think of a two dimensional array as a simple sort of table with columns and rows. The first dimension is the number of rows and the second dimension is the number of columns.

Here is an example of a two dimensional string array that keeps track of book titles and authors. It has 3 rows and 2 columns


 string[,] books = new string[3, 2];
           
            books[0, 0] = "War and Peace";
            books[0, 1] = "Tolstoy";
            books[1, 0] = "Lord of the Rings";
            books[1, 1] = "Tolkein";
            books[2, 0] = "Huckleberry Finn";
            books[2, 1] = "Twain";

Here is an example of a two dimensional array with 3 rows and 3 columns. Think of it as containing the height, width and length of a set of boxes

int[,] boxes = new int[3, 3];
            boxes[0, 0] = 4;
            boxes[0, 1] = 2;
            boxes[0, 2] = 3;
            boxes[1, 0] = 2;
            boxes[1, 1] = 2;
            boxes[1, 2] = 1;
            boxes[2, 0] = 3;
            boxes[2, 1] = 2;
            boxes[2, 2] = 5;

It is also possible to have arrays with more than two dimensions. Below is a three dimensional array.

int[, ,] space = new int[2, 2, 2];

            space[0, 0, 0] = 5;
            space[0, 0, 1] = 4;
            space[0, 1, 0] = 3;
            space[0, 1, 1] = 4;
            space[1, 0, 0] = 3;
            space[1, 0, 1] = 6;
            space[1, 1, 0] = 2;
            space[1, 1, 1] = 5;
            space[2, 0, 0] = 3;
            space[2, 0, 1] = 6;
            space[2, 1, 0] = 2;
            space[2, 1, 1] = 5;

Arrays can have more than 3 dimensions, but they rapidly get difficult to manage.

To display a multidimensional array, you access its indexes just as in an one dimensional array. Below is the full program that creates these arrays and displays all but the last one. If you would like a little chalange, you can add code to the display method to display the contents of the 3 dimensional array.


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

namespace MultidimensionalArrays
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Display();
            Console.ReadKey();
        }

        private string[,] TwoDimensionalArray()
        {
            //define two dimensional Array
            //first number is number of rows
            //the second is number of columns
            string[,] books = new string[3, 2];
           
            books[0, 0] = "War and Peace";
            books[0, 1] = "Tolstoy";
            books[1, 0] = "Lord of the Rings";
            books[1, 1] = "Tolkein";
            books[2, 0] = "Huckleberry Finb";
            books[2, 1] = "Twain";

            return books;
        }

        private int[,] Another2DimensionalArray()
        {
            //think of an array that holds
            //the height, width, and length
            //of boxes
            //although it has 3 columns
            //it is still a two dimensional 
            //array
            int[,] boxes = new int[3, 3];
            boxes[0, 0] = 4;
            boxes[0, 1] = 2;
            boxes[0, 2] = 3;
            boxes[1, 0] = 2;
            boxes[1, 1] = 2;
            boxes[1, 2] = 1;
            boxes[2, 0] = 3;
            boxes[2, 1] = 2;
            boxes[2, 2] = 5;

            return boxes;

        }

        private int[, ,] ThreeDimensionalArray()
        {
            //think of 3 dimensionals as a cube
            //you can do more dimensions but it
            //gets beyond absurd
            int[, ,] space = new int[2, 2, 2];

            space[0, 0, 0] = 5;
            space[0, 0, 1] = 4;
            space[0, 1, 0] = 3;
            space[0, 1, 1] = 4;
            space[1, 0, 0] = 3;
            space[1, 0, 1] = 6;
            space[1, 1, 0] = 2;
            space[1, 1, 1] = 5;
            space[2, 0, 0] = 3;
            space[2, 0, 1] = 6;
            space[2, 1, 0] = 2;
            space[2, 1, 1] = 5;

            return space;
        }

        private void Display()
        {
            //you access the values in multidimensional arrays
            //just like regular ones, through their indexes

            string[,] mybooks = TwoDimensionalArray();
            Console.WriteLine("The second book is {0}, by {1}", 
                mybooks[1, 0], mybooks[1, 1]);
            Console.WriteLine("*********************************");
            // the loop below multiplies the contents
            //of the three columns together
            int[,] cubes = Another2DimensionalArray();
            int cubicInches = 0;
            for (int i = 0; i < 3; i++)
            {
                cubicInches = cubes[i, 0] * cubes[i, 1] * cubes[i, 2];
                Console.WriteLine("Box {0}, is {1} cubic inches", i+1, cubicInches);
            }
            Console.WriteLine("*********************************");
           
        }
    }
}

Wednesday, October 26, 2011

Midterm code

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

namespace ExamExample
{
    class Program
    {
        const int SIZE = 50;
        static void Main(string[] args)
        {
            Program p = new Program();
            p.CreateArrays();
            p.PauseIt();
        }

       

        private void CreateArrays()
        { 
            int[] numbers= new int[SIZE];
            FillArray(numbers);

        }

        private void FillArray(int[] numbers)
        {
            Random rand = new Random();
            for (int i = 0; i < SIZE; i++)
            {
                numbers[i] = rand.Next(1, 100);
            }
            GetEvenNumbers(numbers);
        }

        private void GetEvenNumbers(int[] numbers)
        {
            int evenCount = 0;
            int oddCount = 0;

            for (int i = 0; i < SIZE; i++)
            {
                if (numbers[i] % 2 == 0)
                {
                    evenCount++;
                }
                else
                {
                    oddCount++;
                }

            }
            Console.WriteLine("There were {0} even numbers and {1} odd numbers", evenCount, oddCount);
           
        }

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

       
    }
}


Tuesday, October 25, 2011

Random

Here is the random Function

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

namespace RandomExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();
            int number=rand.Next(1, 1000);

            Console.WriteLine(number);

            Console.ReadKey();
        }
    }
}

Assignment4 Morning Class


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

namespace Assignment4Morning
{
    class Program
    {
        /// 
        /// This program calculates GPAs
        /// Based on grades and credits
        /// entered
        /// Steve Conger 10/25/2011
        /// 
      
        static void Main(string[] args)
        {
            Program p = new Program();

            string quit = "y";
            while (quit != "n")
            {
                p.CreateGPAArrays();
                Console.WriteLine("Continue y/n?");
                quit = Console.ReadLine();
               quit= quit.ToLower();
            }
            //Console.ReadKey();
        }//end main

        private void CreateGPAArrays()
        {
            Console.WriteLine("Enter how many grades you want to enter");
            int number = int.Parse(Console.ReadLine());

            double[] grades = new double[number];
            int[] credits = new int[number];

            FillGPAArrays(grades, credits);
        }//end Create

        private void FillGPAArrays(double[] grades, int[] credits)
        {
            for (int index = 0; index < grades.Length; index++)
            {
                double grade=0;
                
                do
                {
                    Console.WriteLine("enter grade");
                    grade = double.Parse(Console.ReadLine());
                    if (grade < 0 || grade > 4)
                    {
                        Console.WriteLine("Grades must be between 0 and 4");
                        
                    }
                } while (grade < 0 || grade > 4);
                grades[index] = grade;

                Console.WriteLine("enter Credits");
                credits[index] = int.Parse(Console.ReadLine());

            
            }//end for
            CalculateGPA(grades, credits);
        }//end Fill

        private void CalculateGPA(double[] grades, int[] credits)
        {
            double weight=0;
            int totalCredits = 0;

            for (int index = 0; index < grades.Length; index++)
            {
                weight += grades[index] * credits[index];
                totalCredits += credits[index];
            }

            //alternate way to sum credits
            //c# 4.0 and above only
            int total = credits.Sum();

            double gpa = weight / totalCredits;

            Console.WriteLine("Your GPA is {0}", Math.Round(gpa,1));
            
        }


    }//end class
}

Monday, October 24, 2011

Assignment 4


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

namespace Assignment4
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            string quit = "y";
            while (quit != "n")
            {
                p.CreateArrays();
                Console.WriteLine("Do you want start over? n to quit");
               quit=Console.ReadLine();
                quit.ToLower();
            }
            Console.ReadKey();
        }

        private void CreateArrays()
        {
            Console.WriteLine("How many Grades do you want to enter");
            int number = int.Parse(Console.ReadLine());

            double[] grades = new double[number];
            double[] credits = new double[number];

            FillArrays(grades, credits);
            

        }

        private void FillArrays(double[] grades, double[] credits)
        {
            for (int counter = 0; counter < grades.Length; counter++)
            {
                double grade = 0;
                do
                {
                    Console.WriteLine("Enter a Grade");
                    grade = double.Parse(Console.ReadLine());
                    if (grade <= 0 || grade > 4)
                    {
                        Console.WriteLine("Grades must be between 0 and 4");
                    }

                } while (grade <= 0 || grade > 4);
                grades[counter]=grade;

                Console.WriteLine("Enter the Credits");
                credits[counter] = double.Parse(Console.ReadLine());
            }//end for
            CalculateGPA(grades, credits);
        }//end fill arrays

        private void CalculateGPA(double[] grades, double[] credits)
        {
            double totalCredits = 0;
            double weight = 0;

            for (int counter = 0; counter < grades.Length; counter++)
            {
                weight+=grades[counter] * credits[counter];
                totalCredits += credits[counter];
            }//end for

            
            
            double gpa = weight / totalCredits;

            Console.WriteLine("Your GPA is {0:F1}", gpa);
        }

    }//end class
}

and here is the code for doing random numbers


 static void Main(string[] args)
  {
            
            Random rand = new Random();
            int number = rand.Next(1, 10);
            Console.WriteLine(number);
            Console.ReadKey();

   }