Skip to content

Instantly share code, notes, and snippets.

@colinrymer
Last active August 29, 2015 14:00
Show Gist options
  • Save colinrymer/11225468 to your computer and use it in GitHub Desktop.
Save colinrymer/11225468 to your computer and use it in GitHub Desktop.
class GradeCalc
def initialize
@@total_grade ||= 0.0
@@total_percent_given ||= 0
@assingment_num = nil
@percent_of_total = nil
@sum = 0
@avg = 0
end
def add_assignments(percent_of_total, assignment_num)
@percent_of_total = percent_of_total
@assignment_num = assignment_num
@@total_percent_given += @percent_of_total
end
def calc_grade_section(grades_input)
@sum += grades_input.reduce(:+)
@avg = @sum / @assignment_num
@@total_grade += (@avg * @percent_of_total)
end
def calc_needed_grade
grades_return = []
if @@total_percent_given < 100
{"A+" => 100, "A" => 90, "B" => 80, "C" => 70}.each do |k,v|
needed = needed_grade(v)
grades_return << k << (needed <= 0 ? "You don't need to finish this last section to make an #{k}" : needed.to_s)
end
else
grades_return << "You have completed 100% of the class"
end
grades_return
end
def get_final_grade
(@@total_grade * 100.0).round / 100.0
end
def get_avg
@avg
end
def get_percent_of_total
@percent_of_total
end
def get_sum
@sum
end
def get_total_percent_given
@@total_percent_given
end
private
def needed_grade(grade_minimum)
(((grade_minimum - @@total_grade) / grade_percent_needed) * 100).round / 100.0
end
def grade_percent_needed
1 - @@total_percent_given
end
end
require './grade_calc_ported_from_java'
hw = GradeCalc.new
hw.add_assignments(0.2, 10)
puts hw.calc_grade_section([100.0, 100.0, 80.0, 90.0, 100.0, 85.0, 100.0, 95.0, 70.0, 93.0])
labs = GradeCalc.new
labs.add_assignments(0.4, 8)
puts labs.calc_grade_section([100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 94.0])
tests = GradeCalc.new
tests.add_assignments(0.2, 3)
puts tests.calc_grade_section([107.0, 108.5, 106.0])
puts tests.get_final_grade
tests.calc_needed_grade.each do |grade|
puts grade
end
class GradeCalc
def initialize
@total_grade = 0.0
@total_percent_given = 0
end
def add_assignments(percent_of_total, grades)
avg = grades.reduce(:+) / grades.length
@total_grade += (avg * percent_of_total)
@total_percent_given += percent_of_total
end
def calc_needed_grade
grades_return = []
if @total_percent_given < 100
{"A+" => 100, "A" => 90, "B" => 80, "C" => 70}.each do |k,v|
needed = needed_grade(v)
grades_return << k << (needed <= 0 ? "You don't need to finish this last section to make an #{k}" : needed.to_s)
end
else
grades_return << "You have completed 100% of the class"
end
grades_return
end
def get_final_grade
(@total_grade * 100.0).round / 100.0
end
private
def needed_grade(grade_minimum)
(((grade_minimum - @total_grade) / grade_percent_needed) * 100).round / 100.0
end
def grade_percent_needed
1 - @total_percent_given
end
end
gc = GradeCalc.new
gc.add_assignments(0.2, [100.0, 100.0, 80.0, 90.0, 100.0, 85.0, 100.0, 95.0, 70.0, 93.0])
gc.add_assignments(0.4, [100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 94.0])
gc.add_assignments(0.2, [107.0, 108.5, 106.0])
puts gc.get_final_grade
gc.calc_needed_grade.each do |g|
puts g
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment