Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. /*
  2. Create a class. DONE
  3.  
  4. Add a private integer called largest. Add a property that can get largest. DONE
  5.  
  6. Add a private List<int> initializing it to new List<int>() at declaration.
  7.  
  8. Add a method called Add that takes an integer.
  9. - This method will add the number to the list, if the value is larger than the current largest, it will update the largest to be the larger number.
  10.  
  11. In main.
  12. Instantiate your class.
  13.  
  14. Add a loop that gets a number from the user, adds it the list, then prints out the largest.
  15.  
  16. Loop until they enter -1.
  17.  
  18. */
  19.  
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25.  
  26. namespace Largest_Homework
  27. {
  28. class MyClass // My Class
  29. {
  30. // (1) Add a private integer called largest. Add a property that can get largest:
  31. private int largest; // Declare private int largest
  32.  
  33. public int Largest // Create a public i
  34. {
  35. get { return largest; }
  36. set { largest = value; }
  37. }
  38.  
  39. // (2) Add a private List<int> initializing it to new List<int>() at declaration:
  40. private static List<int> myList = new List<int>();
  41.  
  42. // (3) Add a method called Add that takes an integer:
  43. public void Add(int takeInteger)
  44. {
  45. // (3a) This method will add the number to the list:
  46. myList.Add(takeInteger);
  47.  
  48. // (3b) If the value is larger than the current largest, it will update the largest to be the larger number
  49. if (takeInteger > Largest)
  50. {
  51. Largest = takeInteger;
  52. }
  53.  
  54. return;
  55. }
  56.  
  57. static void Main(string[] args)
  58. {
  59. // (4) Instantiate your class:
  60. MyClass free = new MyClass();
  61.  
  62. // (5) Add a loop:
  63. while (free.largest > -1) // While loop which checks if the object variable free is > -1
  64. {
  65. // (5a) that gets a number from the user:
  66. Console.Write("Enter a number: "); // Prompt user for input
  67.  
  68. // (5b) adds it the list:
  69. var N = int.Parse(Console.ReadLine());
  70. free.Add(N);
  71.  
  72. // (5c) then prints out the largest:
  73. Console.Write("Your largest number is: " + free.largest);
  74.  
  75. // (5d) Loop until they enter -1:
  76. free.largest--;
  77. }
  78.  
  79. // Prevent Exit:
  80. Console.ReadLine();
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement