Last active
December 29, 2015 11:29
-
-
Save jmoon90/7663887 to your computer and use it in GitHub Desktop.
OOP 1 Challenges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FinalGrade | |
| attr_reader :student, :avg | |
| def initialize(argument) | |
| @avg = argument[:avg] | |
| @student = argument[:student] | |
| end | |
| def letter_grade | |
| return "A" if @avg >= 90 | |
| return "B" if @avg >= 80 && @avg < 90 | |
| return "C" if @avg >= 70 && @avg < 80 | |
| return "D" if @avg >= 60 && @avg < 70 | |
| return "F" if @avg < 60 | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Student | Final Grade | Letter Grade | |
|---|---|---|---|
| Smith Johnny | 78.0 | C | |
| Strong Sally | 94.0 | A | |
| Fallon Jimmy | 80.0 | B | |
| Botsworth Chris | 86.0 | B | |
| Boyd Brian | 64.0 | D |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "csv" | |
| class GradeReader | |
| attr_reader :students | |
| def initialize(file) | |
| @file = file | |
| @students = [] | |
| end | |
| def parse_file | |
| CSV.foreach(@file, headers: true) do |row| | |
| @students << row.to_hash | |
| end | |
| end | |
| def individual_student | |
| student_info = [] | |
| @students.each do |student| | |
| grades = student.values | |
| name = grades.shift | |
| student_info << each_student(name, grades) | |
| end | |
| student_info | |
| end | |
| def each_student(name, grades) | |
| student_grades = { student: name, grades: grades } | |
| Student.new(student_grades) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class GradeSummary | |
| def initialize(students) | |
| @all_students = students | |
| @students_avg = 0 | |
| end | |
| def student_final_grade | |
| @all_students.each do |student| | |
| student.calc_final_grade | |
| student.grade_output | |
| end | |
| class_avg | |
| end | |
| def class_avg | |
| @all_students.each do |student| | |
| @students_avg += student.calc_avg | |
| end | |
| puts "\nThe class average: #{@students_avg /= @all_students.count}" | |
| end | |
| def class_min | |
| min_grade = [] | |
| @all_students.each do |student| | |
| grades = student.grades.map do |grade| | |
| grade.to_i | |
| end | |
| min_grade << grades.min | |
| end | |
| puts "The class min: #{min_grade.min}" | |
| min_grade.min | |
| end | |
| def class_max | |
| max_grade = [] | |
| @all_students.each do |student| | |
| grades = student.grades.map do |grade| | |
| grade.to_i | |
| end | |
| max_grade << grades.max | |
| end | |
| puts "The class max: #{max_grade.max}" | |
| end | |
| def class_standard_deviation | |
| difference = [] | |
| square_sum = 0 | |
| @all_students.each do |avg| | |
| difference << (avg.calc_avg - @students_avg) | |
| end | |
| difference.each do |dif| | |
| square_sum += (dif**2) | |
| end | |
| data_values = @all_students.count | |
| puts "The class standard deviation: | |
| #{ Math.sqrt((square_sum / data_values))}" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative "grade_reader" | |
| require_relative "final_grade" | |
| require_relative "grade_summary" | |
| require_relative "student" | |
| puts "What class would you like to load?" | |
| file = gets.chomp | |
| if File.exists?(file) | |
| grade_reader = GradeReader.new(file) | |
| grade_reader.parse_file | |
| individual_student_info = grade_reader.individual_student | |
| all_grade = GradeSummary.new(individual_student_info) | |
| all_grade.student_final_grade | |
| all_grade.class_min | |
| all_grade.class_max | |
| all_grade.class_standard_deviation | |
| else | |
| puts "File #{file} does not exist." | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student | |
| attr_reader :student, :grades, :final_grade | |
| def initialize(arguments) | |
| @student = arguments[:student] | |
| @grades = arguments[:grades] | |
| student_info = { avg: calc_avg, student: @student } | |
| @final_grade = FinalGrade.new(student_info) | |
| end | |
| def calc_avg | |
| grades = @grades.map do |grade| | |
| grade.to_i | |
| end | |
| avg = (grades.inject(:+) / grades.count) | |
| return avg | |
| end | |
| def calc_final_grade | |
| puts "#{@student} average grade was #{calc_avg}." | |
| puts "Final grade: #{@final_grade.letter_grade}" | |
| end | |
| def grade_output | |
| CSV.open("grade_output.csv", "a+") do |csv| | |
| csv << ["#{@final_grade.student.split.reverse.join(" ")}","#{@final_grade.avg.to_f}","#{@final_grade.letter_grade}"] | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| student_name | grade1 | grade2 | grade3 | grade4 | grade5 | |
|---|---|---|---|---|---|---|
| Johnny Smith | 100 | 80 | 75 | 78 | 60 | |
| Sally Strong | 100 | 100 | 90 | 95 | 85 | |
| Jimmy Fallon | 95 | 97 | 85 | 40 | 85 | |
| Chris Botsworth | 98 | 86 | 85 | 82 | 80 | |
| Brian Boyd | 50 | 60 | 65 | 70 | 75 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment