Aufgaben zum Kapitel „Datentypen in Kotlin“

Aufgabe Nr. 1

Beantworte folgende Fragen:

Hat Kotlin eine dynamische oder statische Typisierung?
Kotlin ist eine statisch typisierte Sprache.
Was bedeutet statische Typisierung und welche Vorteile sie bietet?
Statische Typisierung bedeutet, dass jeder Variablen bereits beim Kompilieren ein bestimmter Datentyp zugewiesen wird und dieser während der Laufzeit nicht geändert werden kann.
Welche zwei Parameter werden durch den Datentyp in Kotlin bestimmt?
Durch den Datentyp wird festgelegt, wie viel Speicherplatz für ein Objekt (z. B. Variable) reserviert werden muss und welche Operationen auf dieses Objekt angewendet werden können.

Aufgabe Nr. 2 – Ganzzahlige Datentypen

a). Initialisiere für jeden Datentypen Byte, Short, Int, Long eine Variable und gib die Werte mit Hilfe der Funktion println() in Konsole aus.

Lösung für Aufgabe Nr. 2 a).

fun main() {
   val byteVariable: Byte = -1
   val shortVariable: Short = 99
   val intVariable: Int = 15000
   val longVariable: Long = 800000
   println(byteVariable)
   println(shortVariable)
   println(intVariable)
   println(longVariable)
}

b). Der nachstehende Code enthält drei Fehler. Finde diese und korrigiere sie.

Lösung für Aufgabe Nr. 2 b).

fun main() {
    val byteVariable: Byte = 130        // The value 130 is outside the permitted range of -128 to 127
    val shortVariable: Short = -33000  // The value -33000 is outside the permitted range -32,768 to 32,767
    val intVariable: Int = 25120
    val longVariable: Long = "40500"   // It is not possible to assign a string to the Long data type
    println(byteVariable)
    println(shortVariable)
    println(intVariable)
    println(longVariable)
}

c). Initialisiere für jeden Datentypen UByte, UShort, UInt, ULong eine Variable und gib die Werte in Konsole aus.

Lösung für Aufgabe Nr. 2 c).

fun main() {
    val uByteVariable: UByte = 20U
    val uShortVariable: UShort = 176U
    val uIntVariable: UInt = 99000U
    val uLongVariable: ULong = 4520004880U
    println(uByteVariable)
    println(uShortVariable)
    println(uIntVariable)
    println(uLongVariable)
}



Aufgabe Nr. 3 – Datentypen für Gleitkommazahlen

Der folgende Code hat zwei Fehler. Finde diese und korrigiere.

Lösung für Aufgabe Nr. 3

fun main() {
    val floatVariable1: Float = 5.18f // A suffix "f" or "F" must be appended to a float number to tell the compiler that it is a float number and not a double number.
    val floatVariable2: Double = -4.123 // The suffix "F" is unnecessary at this point. Value assignment for the Double data type occurs without a suffix.
    println(floatVariable1)
    println(floatVariable2)
}



Aufgabe Nr. 4 – Logischer Datentyp Boolean

Erstelle zwei Variablen mit logischen Werten true und false, gib die Werte in Konsole aus.

Lösung für Aufgabe Nr. 4

fun main() {
    val isEarthCube: Boolean = false
    val isSunHot: Boolean = true
    println(isEarthCube)    
    println(isSunHot) 
}



Aufgabe Nr. 5 – Typinferenz (Typableitung)

Erstelle sechs Variablen mit unterschiedlichen Werten ohne Angabe des Datentyps.  Gib die Werte in Konsole aus. Erkläre welche Datentypen wurden von Kotlin in deinen Variablen ermittelt.

Lösung für Aufgabe Nr. 5

fun main() {
    val age = 100          // Integers are recognized as Int
    val name = "Arnold"    // In this case the data type String is identified
    val route = 1.2        // Any number with a decimal point is interpreted as the data type Double
    val sum = 160L         // To explicitly specify the Long data type, you should use the suffix “L”.
    val height = 5.40F     // To initialize a float type variable, one must specify the suffix "f" or "F" after the number
    val width = 50U        // Unsigned integer data types are specified with "u" or "U"
    println(age)
    println(name)
    println(route)
    println(height)
    println(width)
}


Aufgabe Nr. 6 – Datentyp Any

  • Deklariere eine Variable mit dem Datentyp Any. Weise der Variable zuerst einen String-Wert zu. Gib den Wert aus.
  • Anschließend weise einen Int-Wert zu und gib diesen in Konsole aus.
  • Zum Schluss wiederhole das Gleiche mit einem Double-Wert.
Lösung für Aufgabe Nr. 6

fun main() {
    var anyVariable: Any
    anyVariable = "London is the capital city of England and the United Kingdom"
    println(anyVariable)
    anyVariable = 100
    println(anyVariable)
    anyVariable = 1.9
    println(anyVariable)
}



Aufgabe Nr. 7 – String Templates

Schreibe eine kurze Geschichte über einen Jungen, der besonders gerne Basketball spielt. Deklariere dabei folgende vorgegebenen Parameter als einzelne Variablen und baue diese in deinen Text (String Templates) ein:

  • Vorname: Tim
  • Nachname: Johnson
  • Alter: 19 Jahre
  • Größe: 182 cm
  • Hobby: Basketball

Gib das Ergebnis in Konsole aus.

Lösung für Aufgabe Nr. 7

fun main() {
// I have prepared the following text in English as a solution:
   val name: String = "Tim"
   val surname: String = "Johnson"
   val age: Int = 19
   val height: Double = 1.82
   val hobby: String = "basketball"
   val finalText: String
   finalText = "Meet $name $surname, a $age-year-old boy who is passionate\nabout his hobby $hobby. Standing tall at $height meters,\nhe spends most of his free time practicing his skills on the court."
   println(finalText)
}


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert