Created
December 2, 2013 18:48
-
-
Save justuseapen/7754787 to your computer and use it in GitHub Desktop.
Object Oriented Programming Challenge
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 AssignmentGrade | |
def initialize (score) | |
@score = score.to_i | |
end | |
def score | |
@score | |
end | |
def all | |
all_scores = [] | |
ObjectSpace.each_object(AssignmentGrade) do |score| | |
all_scores << score | |
end | |
all_scores | |
end | |
def average | |
sum = 0 | |
all.each { |score| sum += score } | |
sum / all.length | |
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 'csv' | |
require 'pry' | |
require_relative 'assignment_grade.rb' | |
require_relative 'student.rb' | |
#GradeReader - an object that is responsible for reading in grade data from a CSV | |
class GradeReader | |
def initialize(file_path) | |
raise "#{file_path} does not exist" unless File.exists? file_path | |
@file_path = file_path | |
end | |
def construct_students | |
students = [] | |
CSV.foreach(@file_path, :headers => true) do |row| | |
scores = [] | |
row["scores"].split.each do |score| | |
score = AssignmentGrade.new(score) | |
scores << score | |
end | |
student = Student.new(row["student_name"],scores) | |
students << student | |
end | |
students | |
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.rb" | |
require_relative "final_grade.rb" | |
require_relative "assignment_grade.rb" | |
require 'pry' | |
def prompt(query) | |
puts query | |
gets.chomp | |
end | |
filename = prompt "Where are your grades located (insert file name)?" | |
data = GradeReader.new(filename) | |
students = data.construct_students | |
name = prompt "Whose average would you like to check?" | |
unless name == "n" | |
student = students.find do |student| | |
student.name == name | |
end | |
puts student.display_status | |
end | |
log_response = prompt "Would you like to log the class grades to a file?" | |
if log_response == "Yes" || log_response.upcase == "Y" | |
students.each do |s| | |
CSV.open("final_grades.csv", 'a') do |row| | |
row << [s.display_status] | |
end | |
end | |
end | |
view_analytics = prompt "Would you like to see the class analytics?" | |
all_scores = [] | |
ObjectSpace.each_object(AssignmentGrade) do |grade| | |
all_scores << grade.score | |
end | |
sum = 0 | |
all_scores.each { |score| sum += score } | |
average = sum / all_scores.length | |
min = all_scores.minmax.first | |
max = all_scores.minmax.last | |
deviations = [] | |
all_scores.each do |score| | |
deviation = score-average | |
deviations << deviation | |
end | |
sum = 0 | |
deviations.each do |d| | |
sum += d**2 | |
end | |
variance = sum / deviations.length | |
standard_deviation = Math.sqrt(variance) | |
puts "The class average is #{average}" | |
puts "The class minimum is #{min}" | |
puts "The class maximum is #{max}" | |
puts "The class standard deviation is #{standard_deviation}" |
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 | scores | |
---|---|---|
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 |
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 | |
def initialize(name,grades) | |
@name = name | |
@grades = grades | |
end | |
def name | |
@name | |
end | |
def grades | |
@grades | |
end | |
def average | |
sum = 0 | |
grades.each { |grade| sum += grade.score } | |
sum / grades.length | |
end | |
def final_grade | |
if average >= 90 | |
grade = 'A' | |
elsif average >= 80 | |
grade = 'B' | |
elsif average >= 70 | |
grade = 'C' | |
elsif average >= 60 | |
grade = 'D' | |
else | |
grade = 'E' | |
end | |
end | |
def display_status | |
"#{name} has a #{final_grade} with an average of #{average}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try to make sure that everything is indented consistently, with 2 spaces. Make sure that you've got these settings in your Sublime Text settings https://gist.github.com/HeroicEric/77c860d2fe8d7ae0a98f