Moderatoren: pmueller, SE - Design and Construction

mesrbn
Neuling
Beiträge: 9
Registriert: 15. Apr 2016 11:44

Hi,
Is it ok if I use constructor for the class TestResult? something like this:

Code: Alles auswählen

case class TestResult(myCourse: Course, myGrade: Grade) {
var course = myCourse
}

Best

labataschö
Windoof-User
Beiträge: 37
Registriert: 15. Apr 2015 16:43

Hi,

class definitions like

Code: Alles auswählen

class MyClass(param: type)
already create a default constructor. Of course you may specify further constructors but I'd advise to reflect if the default constructor is not already sufficient. Also I'd recommend to have a look at the effects of val and var before a parameter in a class definition, e.g. here: http://www.java2s.com/Tutorials/Java/Sc ... uctors.htm

Greetings,
labataschö

mesrbn
Neuling
Beiträge: 9
Registriert: 15. Apr 2016 11:44

I am just wondering the way I define the constructor can affect the grade I get for the exercise or not!
Basically, I am not sure how my code will be tested. When I come to an idea to implement the solution on my way, a little bit different from the sentences written in the exercise sheet, I get worried. One example is that the way I define the default constructor.
Regards,
M. Sarabchian

AizazZaidee
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Code: Alles auswählen

case class TestResult(myCourse: Course, myGrade: Grade) {

}
I think after this you can be able to access myCourse and myGrade, you don't have to do something like that

Code: Alles auswählen

var course = myCourse
var grade= myGrade
furthermore it could be var or val. Correct me if I am wrong, I am also new to Scala.

labataschö
Windoof-User
Beiträge: 37
Registriert: 15. Apr 2015 16:43

AizazZaidee hat geschrieben:
4. Mai 2018 12:46

Code: Alles auswählen

case class TestResult(myCourse: Course, myGrade: Grade) {

}
I think after this you can be able to access myCourse and myGrade
Yes, that's correct. But you cannot access myCourse and myGrade from anywhere - only from within the class definition. This is similar to a private field in Java. For access from outside you need the val/var modifiers in front of the parameter names.

AizazZaidee
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49