Ohjelmointikielten periaatteet keväällä 2009: 1. harjoitukset

  1. [Muokattu Scott 1.1] Errors in a computer program can be classified according to when they are detected and, if they are detected at compile time, what part of the compiler detects them. Using Scala programming language, give an example of each of the following.
    1. A lexical error, detected by the scanner
    2. A syntax error, detected by the parser
    3. A static semantic error, detected by semantic analysis
    4. A dynamic semantic error, detected by code generated by the compiler
    5. [*]An error that the compiler can neither catch nor easily generate code to catch (this should be a violation of the language definition, not just a program bug)

  2. [Muokattu Scott 1.3] The gcd program
    var i=readInt
    var j=readInt
    while (i != j)
      if (i > j) i = i - j
      else       j = j - i
    println(i)
    
    might also be written
    var i=readInt
    var j=readInt
    while (i != j)
      if (i > j) i = i % j
      else       j = j % i
    println(i)
    
    Does this program compute the same result? If not, can you fix it? Under what circumstances would you expect one or the other to be faster?

  3. [Scott 1.4] In your local implementation of C, what is the limit on the size of integers? What happens in the event of arithmetic overflow? What are the implications of size limits on the portability of programs from one machine/compiler to another? How do the answers to these questions differ for Java? For Ada? For Pascal? For Scheme? (You may need to find a manual.)
    What about Scala?

  4. [Scott 1.6] Why is it difficult to tell whether a program is correct? How do you go about finding bugs in your code? What kinds of bugs are revealed by testing? What kinds of bugs are not? (For more formal notions of program correctness, see the bibliographic notes at the end of Chapter 4.)

  5. [*] Let us become familiar to basic Scala! This task is actually an exercise in the course Introduction to Programming! But now we'll use Scala instead of Java. Below all the syntax is still in Java - for ex. constructor syntax is different in Scala - it is your task to find out the corresponding Scala expressions.

    1. A ship, a vessel, has a direction of 0...359 degrees and speed of 0.0...40.0 knots. Implement the class Vessel:
      • public Vessel() constructs a ship with direction 0 and speed 0.0
      • public Vessel(double speed) constructs a ship with direction 0 and the given speed
      • public Vessel(int direction) constructs a ship with the given direction and speed 0.0
      • public Vessel(double speed, int direction) constructs a ship with the given direction and the given speed
      • public int getDirection() returns the direction in degrees
      • public int getSpeed() returns the speed in knots
      • public void toStarboard() turns the vessel to the right (the starboard of the ship) one degree (notice that if the direction is 359 and it gets turned right by one degree, the new direction is 0!)
      • public void toPort() turns the vessel to the left (port of the ship) one degree
      • public void setSpeed(double speed) sets a new speed according to the parameter

      According to good programming style the internal data of the ship is hidden from the user, so that the user can only manipulate the ship using its accessors. What should you do with erroneous parameters? Equip the vessel with a toString() method to make it easy to print out the state of the ship.

    2. Design an application Skipper that uses the Vessel class from previous exercise and enables you to "steer the ship". You can steer the ship by using the keyboard in the following way:
      • character "l": turn to left, the program asks the number of degrees
      • character "r": turn to right, the program asks the number of degrees
      • character "s": adjust the speed, the program asks the new speed
      • all other characters or strings will stop the the sailing

      The program will print out the state of the vessel after each operation. In this exercise you need not be prepared to non-numerical input.


Takaisin harjoitusten pääsivulle.