EX01 task2

Moderatoren: pmueller, SE - Design and Construction

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

EX01 task2

Beitrag von mesrbn »

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
var grade= myGrade
}
Best

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

Re: EX01 task2

Beitrag von labataschö »

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
Neuling
Beiträge: 9
Registriert: 15. Apr 2016 11:44

Re: EX01 task2

Beitrag von mesrbn »

Thanks for the reply.
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

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

Re: EX01 task2

Beitrag von AizazZaidee »

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
Windoof-User
Beiträge: 37
Registriert: 15. Apr 2015 16:43

Re: EX01 task2

Beitrag von labataschö »

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.

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

Re: EX01 task2

Beitrag von AizazZaidee »

but for the case class default is val so you don't need to rewrite val it would be redundant.

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

Re: EX01 task2

Beitrag von labataschö »

Great, didn't know that myself :D

Antworten

Zurück zu „Software Engineering - Design and Construction“