Advertisement
A4L

Chapter - 18 - Designing-Building Classes (program)

A4L
Dec 8th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. /*******************
  7. **** Try It Out! ***
  8. ********************
  9. Designing and Building Classes. Try creating the two classes below, and make a simple program to
  10. work with them, as described below.
  11.  
  12. Write some code in your Main method to create a few balls, throw them around a few times, pop a
  13. few, and try to throw them again, and print out the number of times that the balls have been thrown.
  14. (Popped balls shouldn't have changed.)
  15.  
  16. --== Create a Color class ==--
  17.     On a computer, colors are typically represented with a red, green, blue, and alpha
  18. (transparency) value, usually in the range of 0 to 255. Add these as instance variables.
  19.     A constructor that takes a red, green, blue, and alpha value.
  20.     A constructor that takes just red, green, and blue, while alpha defaults to 255 (opaque).
  21.     Methods to get and set the red, green, blue, and alpha values from a Color instance.
  22.     A method to get the grayscale value for the color, which is the average of the red, green and
  23. blue values.
  24.  
  25. --== Create a Ball class ==--
  26.     The Ball class should have instance variables for size and color (the Color class you just
  27. created). Let's also add an instance variable that keeps track of the number of times it has
  28. been thrown.
  29.     Create any constructors you feel would be useful.
  30.     Create a Pop method, which changes the ball's size to 0.
  31.     Create a Throw method that adds 1 to the throw count, but only if the ball hasn't been
  32. popped (has a size of 0).
  33.     A method that returns the number of times the ball has been thrown*/
  34.  
  35. namespace Chapter18DesigningBuildingClasses
  36. {
  37.     class Program
  38.     {
  39.         static void Main(string[] args)
  40.         {
  41.             Ran Ran = new Ran();
  42.             Console.WriteLine("Lets create some balls!");
  43.  
  44.             List<Ball> balls = new List<Ball>();
  45.             for (int i=0; i < Ran.Int(5,50); i++)
  46.             {
  47.                 balls.Add(new Ball(Ran.Int(1, 25)));
  48.             }
  49.             Console.WriteLine($"We have just created : {balls.Count} : balls!");
  50.  
  51.             Console.WriteLine($"Lets throw those balls around, up to a dozen times each.");
  52.             for (int iList=0; iList<balls.Count;iList++)
  53.             {
  54.                 for (int iTrow=0; iTrow< Ran.Int(1, 12); iTrow++)
  55.                 {
  56.                     balls[iList].ThrowBall();
  57.                 }
  58.                 Console.WriteLine($"   Ball {iList+1} has been thrown {balls[iList].GetThrown()} times.");
  59.             }
  60.  
  61.             Console.WriteLine($"Now lets Pop at least 5 balls but no more then 10!");
  62.             bool loopPop = true;
  63.             for (int i = 0; i<Ran.Int(5,10); i++)
  64.             {
  65.                 while(loopPop)
  66.                 {
  67.                     int iList = Ran.Int(balls.Count);
  68.                     if (balls[iList].size > 0)
  69.                     {
  70.                       balls[iList].Pop();
  71.                       loopPop = false;
  72.                     }
  73.                 }
  74.                 loopPop = true;
  75.             }
  76.             for (int iList = 0; iList < balls.Count; iList++)
  77.             {
  78.                 if (balls[iList].size == 0)
  79.                 {
  80.                     Console.WriteLine($"   Ball {iList + 1} has been POPPED!!");
  81.                 }
  82.             }
  83.  
  84.             Console.WriteLine($"Now Lets throw the remaining balls around at most a three dozen more times");
  85.             for (int iList = 0; iList < balls.Count; iList++)
  86.             {
  87.                 for (int iTrow = 0; iTrow < Ran.Int(1, 36); iTrow++)
  88.                 {
  89.                     balls[iList].ThrowBall();
  90.                 }
  91.                 if (balls[iList].size > 0)
  92.                 {
  93.                     Console.WriteLine($"   Now Ball {iList + 1} has been thrown {balls[iList].GetThrown()} times.");
  94.                 }
  95.             }
  96.  
  97.             int finalPopCount = 0;
  98.             int finalThrownCountPopped = 0;
  99.             int finalThrownCountWhole = 0;
  100.             for (int iList = 0; iList < balls.Count; iList++)
  101.             {
  102.                 if (balls[iList].size == 0)
  103.                 { finalPopCount++;
  104.                     finalThrownCountPopped = finalThrownCountPopped + balls[iList].GetThrown();
  105.                 }
  106.                 else { finalThrownCountWhole = finalThrownCountWhole + balls[iList].GetThrown(); }
  107.             }
  108.  
  109.             Console.WriteLine($"\n*******************\n   Final Results\n*******************\n");
  110.             Console.WriteLine($"We had {balls.Count} balls to play with.");
  111.             Console.WriteLine($"The {finalPopCount} Popped balls were Thrown a total of : {finalThrownCountPopped} times");
  112.             Console.WriteLine($"The {balls.Count - finalPopCount} unPopped balls were Thrown a total of : {finalThrownCountWhole} times");
  113.             Console.WriteLine($"In total all the balls were Thrown a total of : {finalThrownCountPopped + finalThrownCountWhole} times");
  114.  
  115.             Console.WriteLine("\n: Thrown Values for each POPPED ball : ");
  116.             for (int iList = 0; iList < balls.Count; iList++)
  117.             {
  118.                 if (balls[iList].size == 0)
  119.                 {
  120.                     Console.WriteLine($"   Ball {iList + 1} has been thrown {balls[iList].GetThrown()} times.");
  121.                 }
  122.             }
  123.  
  124.             Console.WriteLine("\n: Thrown Values for each UNPOPPED ball : ");
  125.             for (int iList = 0; iList < balls.Count; iList++)
  126.             {
  127.                 if (balls[iList].size > 0)
  128.                 {
  129.                     Console.WriteLine($"   Ball {iList + 1} has been thrown {balls[iList].GetThrown()} times.");
  130.                 }
  131.             }
  132.  
  133.             ExitConsole.AK();
  134.         }
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement