private static void SaveGrades(GradeBook book) { using (StreamWriter outputFile = File.CreateText("grades.txt")) { book.WriteGrades(outputFile); } }
private static void exampleDelegate() { Console.WriteLine("Starting Delegate example"); GradeBook book = new GradeBook(); book.AddGrade(92); book.AddGrade(34); book.AddGrade(75); GradeStatistics stats = book.ComputeStatistics(); //using delegate - this will later be triggered any time the name changes later. // book.NameChanged = new NameChangedDelegate(OnNameChanged); //alternate method : multi cast delegate //multiple subscribers to execute when the event occurs book.NameChanged += OnNameChanged; book.NameChanged += OnNameChanged2; //can unsubscribe from this event // book.NameChanged -= OnNameChanged2; book.Name = "changed name"; WriteNames(book.Name); Console.WriteLine("Avg:" + stats.AverageGrade); Console.WriteLine("Lowest" + stats.LowestGrade); Console.WriteLine("Highest" + stats.HighestGrade); }
public void LetterGradeTest() { var gradeBook1 = new GradeBook(); gradeBook1.AddGrade(90); gradeBook1.AddGrade(100); var stats1 = gradeBook1.ComputeStatistics(); Assert.AreEqual("A", stats1.LetterGrade); var gradeBook2 = new GradeBook(); gradeBook2.AddGrade(80); gradeBook2.AddGrade(87); var stats2 = gradeBook2.ComputeStatistics(); Assert.AreEqual("B", stats2.LetterGrade); var gradeBook3 = new GradeBook(); gradeBook3.AddGrade(77); gradeBook3.AddGrade(79); var stats3 = gradeBook3.ComputeStatistics(); Assert.AreEqual("C", stats3.LetterGrade); var gradeBook4 = new GradeBook(); gradeBook4.AddGrade(67); gradeBook4.AddGrade(61); var stats4 = gradeBook4.ComputeStatistics(); Assert.AreEqual("D", stats4.LetterGrade); var gradeBook5 = new GradeBook(); gradeBook5.AddGrade(58); gradeBook5.AddGrade(12); var stats5 = gradeBook5.ComputeStatistics(); Assert.AreEqual("F", stats5.LetterGrade); }
static void Main(string[] args) { Arrays(); //Immutable(); GradeBook book = new GradeBook("Scott's book"); book.AddGrade(91f); book.AddGrade(89.1f); book.AddGrade(75f); GradeStatistics stats = book.ComputeStatistics(); book.NameChanged = new NamedChangedDelegate(OnNameChanged); book.NameChanged += OnNameChanged; book.NameChanged += OnNameChanged2; book.Name = "Allen's book"; WriteNames(book.Name); Console.WriteLine(stats.AverageGrade); Console.WriteLine(stats.LowestGrade); Console.WriteLine(stats.HighestGrade); Console.ReadLine(); }
public void PassByValue() { GradeBook book = new GradeBook(); book.Name = "Not set"; SetName(book); Assert.AreEqual("Not set", book.Name); }
public void PassByValue() { GradeBook book = new GradeBook(); book.Name = "Hello World"; SetName(book); Assert.AreEqual(book.Name, "Hello World"); }
public void ComputeLowestGrade() { GradeBook book = new GradeBook(); book.AddGrade(90.5f); book.AddGrade(82); GradeStatistics result = book.ComputeStatistics(); Assert.AreEqual(82, result.LowestGrade); }
public void ComputeAverageGrade() { GradeBook book = new GradeBook(); book.AddGrade(90.5f); book.AddGrade(82); GradeStatistics result = book.ComputeStatistics(); Assert.AreEqual(86.25, result.AverageGrade); }
public void ReferenceTypesPassByValue() { var book1 = new GradeBook(); var book2 = book1; GiveBookAName(book2); Assert.AreNotEqual("A Grade Book", book1.Name); }
public void ComputesLetterGrade() { GradeBook book = new GradeBook(); book.AddGrade(88); GradeStatistics result = book.ComputeStatistics(); Assert.AreEqual("B", result.LetterGrade); }
private static void WriteResults(GradeBook book) { GradeStatistics stats = book.ComputeStatistics(); WriteResult("Average", stats.AverageGrade); WriteResult("Highest", stats.HighestGrade); WriteResult("Lowest", stats.LowestGrade); WriteResult(stats.Description, stats.LetterGrade); }
static void Main(string[] args) { GradeBook book = new GradeBook(); GetBookName(book); AddGrades(book); SaveGrades(book); WriteResults(book); }
public void PassByValue() { GradeBook book = new GradeBook(); book.Name = "Gradebook"; setName(book); Assert.AreEqual("Not what you expected", book.Name); }
public void ComputesHighestGrade() { GradeBook book = new GradeBook(); book.AddGrade(10); book.AddGrade(90); GradeStatistics result = book.ComputeStatistics(); Assert.AreEqual(90, result.HighestGrade); }
public void CalculateHighestGrade() { GradeBook book = new GradeBook(); book.AddGrade(90f); book.AddGrade(50f); GradeStats stats = book.ComputeStats(); Assert.AreEqual(90f, stats.HighestGrade); }
public void ComputesLowestGrade() { Grades.GradeBook book = new Grades.GradeBook(); book.AddGrade(10); book.AddGrade(90); Grades.GradeStatistics result = book.ComputeStatistics(); Assert.Equal(10, result.LowestGrade); }
public void IsGradeValid() { GradeBook book = new GradeBook(); book.AddGrade(90f); book.AddGrade(70f); Assert.AreEqual(2, book._grades.Count); }
private static void FillGradeBookWithRandomGrades(GradeBook GradeBook) { Random Random = new Random(); GradeBook.Name = "John Doe"; for (int i = 0; i < 6; i++) { GradeBook.AddGrade(Random.Next(75, 100)); } }
public void LowestGradeTest() { var gradeBook = new GradeBook(); gradeBook.AddGrade(90); gradeBook.AddGrade(10); var stats = gradeBook.ComputeStatistics(); Assert.AreEqual(10, stats.LowestGrade); }
public void ComputesAverageGrade() { GradeBook book = new GradeBook(); book.AddGrade(91); book.AddGrade(89.5f); book.AddGrade(75); GradeStatistics result = book.ComputeStatistics(); Assert.AreEqual(85.16, result.AverageGrade, 0.01); }
public void ComputesAverageGrade() { Grades.GradeBook book = new Grades.GradeBook(); book.AddGrade(91); book.AddGrade(89.5f); book.AddGrade(75); Grades.GradeStatistics result = book.ComputeStatistics(); Assert.Equal(85.17, result.AverageGrade, 2); }
private static void PassByValueAndRef() { GradeBook g1 = new GradeBook(); GradeBook g2 = g1; GiveBookAName(ref g2); Console.WriteLine(g2.Name); int x1 = 10; IncrementNumber(ref x1); Console.WriteLine(x1); }
public void AverageGradeTest() { var gradeBook = new GradeBook(); gradeBook.AddGrade(90); gradeBook.AddGrade(10); gradeBook.AddGrade(0); var stats = gradeBook.ComputeStatistics(); Assert.AreEqual(33.33f, stats.Average, 0.01); }
public void CalculateHighestGradeCorrectly() { GradeBook book = new GradeBook(); book.AddGrade(90f); book.AddGrade(80f); GradeStatistics stats = book.ComputeStatistics(); Assert.AreEqual(90f, stats.HighesetGrade); }
public void AverageGradeTest() { GradeBook book = new GradeBook(); book.AddGrade(90f); book.AddGrade(50f); GradeStatistics stats = book.ComputeStatistics(); Assert.AreEqual(70, stats.AverageGrade); }
public void LowestGradeTest() { GradeBook book = new GradeBook(); book.AddGrade(90); book.AddGrade(50); GradeStatistics stats = book.ComputeStatistics(); Assert.AreEqual(50, stats.LowestGrade); }
static void Main(string[] args) { GradeBook GradeBook = new GradeBook(); bool Done = false; int MaxNumberOfAttempts = 5; int NumberOfAttempts = 0; while (!Done) { Console.WriteLine("Please enter name of file containing student grades."); try { using (FileStream stream = File.Open(Console.ReadLine(), FileMode.Open)) using (StreamReader reader = new StreamReader(stream)) { string line = reader.ReadLine(); while (line != null) { float grade = float.Parse(line); GradeBook.AddGrade(grade); line = reader.ReadLine(); } Console.WriteLine("Student's grades:"); foreach (float grade in GradeBook.GetGrades()) { Console.WriteLine(grade); } Console.WriteLine(GradeBook.Name); Console.WriteLine("Average grade:" + GradeBook.LetterGrade + " (" + GradeBook.AverageGrade() +")"); Console.WriteLine("Highest grade:" + GradeBook.HighestGrade()); Console.WriteLine("Lowest grade:" + GradeBook.LowestGrade()); Done = true; } } catch (DirectoryNotFoundException ex) { Console.WriteLine("Invalid entry, please try again."); } catch (ArgumentException ex) { Console.WriteLine("Invalid entry, please try again."); } catch (FileNotFoundException ex) { Console.WriteLine("Could not find the file grades.txt"); } catch (UnauthorizedAccessException ex) { Console.WriteLine("You do not have access to grades.txt"); } NumberOfAttempts++; if (NumberOfAttempts > MaxNumberOfAttempts) { Console.WriteLine("Max number of attempts exceeded, exiting."); Done = true; } } Console.Read(); }
static void Main(string[] args) { GradeBook book = new GradeBook(); book.AddGrade(91); book.AddGrade(89.5f); book.AddGrade(75); GradeStatistics stats = book.ComputeStatistics(); Console.WriteLine(stats.AverageGrade); Console.WriteLine(stats.HighestGrade); Console.WriteLine(stats.LowestGrade); }
static void Main(string[] args) { GradeBook book = new GradeBook(); book.AddGrade(91); book.AddGrade(98.6f); GradeStatistocs stats = book.ComputeStatistocs(); GradeBook book2 = book; book2.AddGrade(75); }
public void HighestGradeTest() { GradeBook book = new GradeBook(); book.AddGrade(90f); book.AddGrade(50f); GradeStatistics stats = book.ComputeStatistics(); Assert.AreEqual(90f, stats.HighestGrade); }
private static void GetBookName(GradeBook book) { try { Console.WriteLine("Enter a name"); book.Name = Console.ReadLine(); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } }
public void CalculatesHighestGrade() { bool value = true; GradeBook book = new GradeBook(); book.AddGrade(90f); book.AddGrade(33f); GradeStatistics stats = book.ComputeStatistics(); Assert.AreEqual(90f, stats.HighestGrade); }
static void Main(string[] args) { GradeBook book = new GradeBook("Daniel's book"); GradebookThatThrowsAwayLowestGrade book2 = new GradebookThatThrowsAwayLowestGrade("Toms's book"); try { using (FileStream stream = File.Open("Grades.txt", FileMode.Open)) using (StreamReader reader = new StreamReader(stream)) { // when you exit "using" it calls "Dispose" method that close anything that was opened/created in "using" string line = reader.ReadLine(); while (line != null) // if == null --> this means end of file has been achieved { float grade = float.Parse(line); book.AddGrade(grade); book2.AddGrade(grade); line = reader.ReadLine(); // reads NEXT line of file into memory } } } catch (FileNotFoundException) { Console.WriteLine("Coldn't locate file Grades.txt"); return; } catch (UnauthorizedAccessException) { Console.WriteLine("No access"); return; } //book.AddGrade(91f); //book.AddGrade(89.1f); //book.AddGrade(75f); book.WriteGrades(Console.Out); book2.WriteGrades(Console.Out); //do // try // { // { // Console.WriteLine("Please enter a name for the book"); // book.Name = Console.ReadLine(); // Console.WriteLine("Please enter a name for the book that throws away lowest grade"); // book2.Name = Console.ReadLine(); // } // } // catch (ArgumentException ex) // { // Console.WriteLine("Invalid name"); // Console.WriteLine(ex.Message); // Console.WriteLine(); // } //while (book.Name == "Daniel's book"); GradeStatistics stats = book.ComputeStatistic(); GradeStatistics stats2 = book2.ComputeStatistic(); Console.WriteLine(book.Name); WriteStatistics(stats); Console.WriteLine(book2.Name); WriteStatistics(stats2); //book.NameChanged += OnNameChanged; //book.Name = "Scott's book"; //WriteNames(book.Name); //int number = 20; //WriteBytes(number); //WriteBytes(stats.AverageGrade); //Immutable(); //ParamsAsValueOrAsReference(); }
static void Main(string[] args) { #region A1 //GradeBook g1 = new GradeBook(); //GradeBook g2 = g1; //g1.Name = "Harirak"; //Console.WriteLine($" g2 = {g2.Name}"); //g2.Name = "toon"; //g1 = new GradeBook();//g1 have a new allocate. it's not point to previous box. //Console.WriteLine($" g1 = {g1.Name}"); #endregion #region previous code GradeBook book = new GradeBook(); book.NameChanged += OnNameChanged; book.NameChanged += OnToon; book.Name = "Harirak"; book.AddGrade(91); book.AddGrade(89.5f); book.AddGrade(35); book.AddGrade(41); Console.WriteLine("hi"); book.Name = "Somtavil"; GradeStatistics stats = book.ComputeStatistics(); Console.WriteLine($"Show Highest Grade {stats.HighestGrade}"); Console.WriteLine($"Show Lowest Grade {stats.LowestGrade}"); Console.WriteLine($"Show Average Grade {stats.AverageGrade}"); Console.ReadLine(); book.Name = "hi Gradebook"; GradeBook book2 = new GradeBook(); Logger aa = new Logger(); Writer oo = new Writer(aa.M); Writer write = new Writer(new Logger().WriteMessage); // new Logger().WriteMessage = Logger logger = new Logger(); // Writer write = new Writer(logger.WriteMessage); write("hi"); Writer write1 = new Writer(new Logger().M); write1("hi"); write = new Writer(new Logger2().WriteMessageOfLogger2); write("call from logger2"); book.Name = "Toon"; #endregion }
static void GiveBookAName(ref GradeBook book) { book.Name = "The Gradebook"; }
static void Main(string[] args) { GradeBook book = new GradeBook(); book.AddGrade(78); book.AddGrade(99.444f); book.AddGrade(80); book.NameChange += OnNameChanged; /*book.Name = "Hello, this is the Gradebook program!"; * book.Name = "The GradeBook Welcomes thee!";*/ bool validName = false; string bookName; while (validName == false) { try{ Console.WriteLine("Please Enter a Name for your gradebook: "); bookName = Console.ReadLine(); if (bookName != null && bookName != "" && bookName != " ") { book.Name = bookName; validName = true; } else { throw new ArgumentException("Name cannot be null or empty"); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Thank you choosing a valid name!"); } } /*book.WriteGradesForLoop(Console.Out); * book.WriteGradesForEachLoop(Console.Out); * book.WriteGradesWhileLoop(Console.Out);*/ using (StreamWriter outputFile = File.CreateText("Grades.txt")) { book.WriteGradesToFile(outputFile); outputFile.Close(); } Statistics stats = book.ComputeStatistics(); if (stats.GetActiveStats() == true) { writeResult("Highest Grade", stats.GetHighestGrade()); writeResult("Lowest Grade", stats.GetLowestGrade()); writeResult("Average Grade", stats.GetAverGrade()); writeResult(stats.Description, stats.LetterGrade); } else { Console.WriteLine("There are no grades to compute!"); } }
public GradeStatistics(GradeBook book) { _book = book; }
static void GiveBookAName(GradeBook book) { book = new GradeBook(); book.Name = "Nowy dziennik"; }
static void Main(string[] args) { GradeBook book = new GradeBook(); book.AddGrade(91); }
static void GiveBookAName(ref GradeBook book) { //book = new GradeBook(); //book.Name = "The Gradebook"; }
static void Main(string[] args) { // need to add system.speech reference to project for this to work. // SpeechSynthesizer synth = new SpeechSynthesizer(); // synth.Speak("Hello! This is the gradebook program"); GradeBook book = new GradeBook(); /////////////////////////////////////////////////////////////// // need to initialize the delegate // why not just do this in constructor? // because you need the name of the method to be called. book.NameChanged += new NameChangedDelegate(OnNameChanged); /* * // this will overwrite the original, wont trigger both methods * book.NameChanged = new NameChangedDelegate(OnNameChanged2); * * // must do the += for it to do both * book.NameChanged += new NameChangedDelegate(OnNameChanged2); * * // as an event, you can add the same method twice * book.NameChanged += new NameChangedDelegate(OnNameChanged2); * * // as an event, you can also reduce some of the code * // The C# compiler is smart enough to know what this means * book.NameChanged += OnNameChanged2; * * // you can also do this to remove a reference: * // this only removes one of them, * // you would have to remove all 3 in this example to stop it * book.NameChanged -= new NameChangedDelegate(OnNameChanged2); */ /////////////////////////////////////////////////////////////// book.Name = "Katy's Grade Book"; book.Name = "Grade Book"; book.AddGrade(91); book.AddGrade(89.5f); book.AddGrade(75); GradeStatistics stats = book.ComputeStatistics(); //cw <tab><tab> creates a writeline // Console.WriteLine(stats.AverageGrade); // Console.WriteLine(stats.HighestGrade); // Console.WriteLine(stats.LowestGrade); // this shows how method overloading is used Console.WriteLine(book.Name); WriteResult("Average", stats.AverageGrade); WriteResult("Highest", (int)stats.HighestGrade); WriteResult("Lowest", stats.LowestGrade); // WriteResult("example of multi-parms", 1, 2, 3, 4); /////////////////////////////////////////////////////////////// /* * // this logic shows how pointers/references work for classes * GradeBook g1 = new GradeBook(); * GradeBook g2 = g1; * * g1 = new GradeBook(); // if this was not here, g2 would show g1's name * g1.Name = "Katy's grade book"; * Console.WriteLine(g2.Name); */ Console.ReadLine(); }
private static void AddGrades(GradeBook grades) { grades.Add(50); grades.Add(60); grades.Add(70); }
private static void AddGrades(GradeBook book) { book.AddGrade(91); book.AddGrade(89.5f); book.AddGrade(75); }
private static void GetGradeBookName(GradeBook grades) { Console.Write("Enter Gradebook name : "); grades.Name = Console.ReadLine(); }