-2

Write a function that returns true if the student has a higher average grade than another student given as a parameter.

here my code

class Student {  

var id: Int = 0
var firstName: String = ""
var lastName: String = "" 

init(id: Int,firstName: String,lastName: String){
    self.id = id
    self.firstName = firstName
    self.lastName = lastName

    }

func fullName() -> String  {

    return "\(firstName) \(lastName)"
    }

var grades: [Float] = []

func addGrade(_ input: Float)    {
    grades.append(input)


}
func numberOfGrades() -> Int {
    return grades.count
}
func average(numbers: Int...) -> Float {
    assert(grades.count > 0)
    return Float(grades.reduce(0, {$0 + Double($1)})/Double(grades.count))
}

func hasHigherAverageThan() -> Bool {
    if  lois.hasHigherAverageThan(peter){
    print("\(lois.firstName) beats \(peter.firstName)")
    }                        
} 
}
let peter = Student(id: 5, firstName: "Peter", lastName: "Parker")
peter.addGrade(2.95)
peter.addGrade(3.45) 
let lois = Student(id: 6, firstName: "Lois", lastName: "Lane")
lois.addGrade(4.0)
lois.addGrade(3.2)      

How to write code correctly

i want output:Prints "Lois beats Peter"

Lorlay
  • 1
  • 1

2 Answers2

0

If your only method of comparison is the student's grades, consider the following:

func hasHigherGradeThan(_ student: Student) -> Bool {
     //In your current construct, the parameter passed to "average" is not necessary, so I'm passing a 0 right now.
     return self.average(numbers: 0) > student.average(numbers: 0)
}

This will only return true if one average is greater than the other; if the average is equal or lesser, it will return false.

binaryPilot84
  • 994
  • 6
  • 20
  • I thought it would be better if the OP worked out the body of the method themselves. Giving students copy-and-paste-ready code means they don't learn how to solve problems themselves. – Duncan C Sep 08 '18 at 21:31
  • @Duncan-C, I get what you're saying. That being said, I'm a firm believer in answering questions where able. People learn in different ways; I believe the more exposure people have to different approaches of coding, the better they will become. – binaryPilot84 Sep 08 '18 at 22:37
-1

It sounds to me like you are supposed to write a an instance method of your Student class that takes another Student as a parameter and compares the two:

The function that compares 2 Student objects' average grades should not have any code dealing with specific students. It should be written as an instance method to compare the current student to another student.

class Student {
   //Your other code here...

   func hasHigherAverageThan(otherStudent: Student) -> Bool {
     //Your code to compare the current student to otherStudent goes here
     //comare `self` to `otherStudent` and return a Bool result
   }
}

Then, outside of the Student class, you would create and compare the 2 students:

let peter = Student(id: 5, firstName: "Peter", lastName: "Parker")
peter.addGrade(2.95)
peter.addGrade(3.45) 
let lois = Student(id: 6, firstName: "Lois", lastName: "Lane")
lois.addGrade(4.0)
lois.addGrade(3.2)      

if  lois.hasHigherAverageThan(peter) {
    print("\(lois.firstName) beats \(peter.firstName)")
} else {
    print("\(peter.firstName) beats \(lois.firstName)")
}

(Note that the code above does not allow for the 2 students having exactly the same average grade.)

See if you can work out the code for the hasHigherAverageThan(otherStudent:) method.

EDIT:

One other thing: You gave your Student object an array of grade values, which seems reasonable. Why does your average function take any parameters, much less a variadic parameter? It should probably be written with no parameters:

func average() -> Float 

It would use array grades to calculate the average grade.

EDIT #2:

I would rewrite your average function like this:

func average() -> Float {
    guard !grades.isEmpty else { return 0.0 } 
    return Float(grades.reduce(0.0, { $0 + Double($1) })/Double(grades.count))
}

Changes:

  1. Got rid of parameters
  2. Got rid of assert and instead return 0 if array is empty

(You could also make it return an Optional Float/Double, and return nil if the array is empty.)

I would also suggest settling on one floating point type and using it throughout. Double is the native floating-point type, so I would make the array of grades an array of Doubles, and make the average() function return a Double.

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272