Fahad/CodeSamples/Classes.scala
author Chengsong
Fri, 26 May 2023 08:09:30 +0100
changeset 646 56057198e4f5
parent 44 a751aa1ee4f7
permissions -rw-r--r--
intro
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     1
package Main
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     2
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     3
class Point(xc: Int, yc: Int) {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     4
  var x: Int = xc
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     5
  var y: Int = yc
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     6
  def move(dx: Int, dy: Int) {
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     7
    x = x + dx
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     8
    y = y + dy
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     9
  }
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    10
  override def toString(): String = "(" + x + ", " + y + ")";
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    11
}
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    12
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    13
object Classes{
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    14
  def main(args: Array[String]){
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    15
    val pt = new Point(1,2)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    16
    println(pt)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    17
    pt.move(10,10)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    18
    println(pt)
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    19
  }
a751aa1ee4f7 Code Samples
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    20
}