Luet oppimateriaalin englanninkielistä versiota. Mainitsit kuitenkin taustakyselyssä osaavasi suomea. Siksi suosittelemme, että käytät suomenkielistä versiota, joka on testatumpi ja hieman laajempi ja muutenkin mukava.

Suomenkielinen materiaali kyllä esittelee englanninkielisetkin termit. Myös suomenkielisessä materiaalissa käytetään ohjelmaprojektien koodissa englanninkielisiä nimiä kurssin alkupään johdantoesimerkkejä lukuunottamatta.

Voit vaihtaa kieltä A+:n valikon yläreunassa olevasta painikkeesta. Tai tästä: Vaihda suomeksi.


Chapter 3.3: Experiences and Truths

About This Page

Questions Answered: What are GoodStuff’s classes like? How do I compare values with one another? How do I express whether a number is greater than another or similar “yes or no” information? How can I make my flappy bug smash against an obstacle?

Topics: Truth values, the Boolean data type, comparison operators.

What Will I Do? Read and try things out yourself.

Rough Estimate of Workload:? A couple of hours.

Points Available: A70.

Related Projects: GoodStuff, IntroOOP, Odds, FlappyBug. Miscellaneous (new) features in one optional assignment.

../_images/person05.png

Recap: GoodStuff

Chapter 2.1 showed how the classes of our running example, GoodStuff, work together. Before we discuss the example further, it might be a good idea to review that graphical presentation. Here it is again:

The Components of GoodStuff

In this chapter, we’ll begin exploring the Scala code that implements GoodStuff. It has the following components.

../_images/goodstuff-en.png

GoodStuff’s user interface. The grin-face marks the experience that the user has rated highest.

Package o1.goodstuff:

  • Class Experience represents entries in the diary, which describe the user’s experiences; each instance of the class corresponds to one such entry. We’ll inspect this class in this chapter and the next.
  • Class Category can be used to group multiple comparable experiences together. Each category has a name and a pricing unit (e.g., hotel experiences are priced per night). A category object also keeps track of the user’s favorite experience within that category. We’ll inspect this class in Chapters 4.2 and 4.3.

Package o1.goodstuff.gui:

  • CategoryDisplayWindow is a class for representing a particular sort of GUI windows. The GoodStuff application, as given, features just a single window for hotel experiences, but in principle there could be more windows of this type. The window has been implemented using the Swing GUI library (Chapter 12.3).
  • GoodStuff is an app object (Chapter 2.7). It’s defined in GoodStuff.scala, which is the file that you’ve used to run the program in Eclipse.

Now, let’s look at Experience. As we do so, it will soon become apparent that we need to learn about a new basic data type available in Scala and the more general programming concepts associated with that type.

Read the documentation!

Locate the Scaladoc documentation for class Experience either within the GoodStuff project in Eclipse or through the project link at the top of this page. Read the Scaladocs. Continue only after you’ve read them.

You don’t need to look at the documentation of the other classes in the project now.

Experimenting with Experiences

Let’s try working with Experience objects in the REPL. To instantiate class Experience, we need to pass in a name, a description, a price, and a rating. In the given version of the full app, those attributes were associated with hotel experiences, but that’s just one of many possible uses of the class. For a change, let’s create objects that correspond to wine experiences:

import o1.goodstuff._import o1.goodstuff._
val wine1 = new Experience("Il Barco 2001", "okay", 6.69, 5)wine1: o1.goodstuff.Experience = o1.goodstuff.Experience@a62427

The class can be used as described in its Scaladocs. Here are a couple of examples:

wine1.nameres0: String = Il Barco 2001
wine1.valueForMoneyres1: Double = 0.7473841554559043

In order for GoodStuff to figure out the favorite experience within a category, it needs to compare experiences by their ratings. Two of the methods in class Experience are relevant here: isBetterThan and chooseBetter. Understanding the former will help you understand the latter, so that’s where we’ll start. Let’s try calling isBetterThan now and get back to chooseBetter in the next chapter.

val wine2 = new Experience("Tollo Rosso", "not great", 6.19, 3)wine2: o1.goodstuff.Experience = o1.goodstuff.Experience@140b3c7
wine2.isBetterThan(wine1)res2: Boolean = false
We can invoke isBetterThan to compare the target object with another experience object. When we call the method, we pass in a reference to the other object.
The method compares the two experiences: the one the method was invoked on and the one indicated by the parameter. It returns a value that tells us whether the former object had a higher rating than the latter.
There’s something unfamiliar about this return value, though. What does false mean, exactly? And what is its data type, Boolean?

Truth Values and the Boolean Type

It’s extremely common for programs to deal with dualities: Is this number larger than that one? Can we find a particular person among the customers recorded in the program? Has the user ticked a particular box in the GUI? Did the player choose black or white pieces? Was the Shift key held down while the mouse was clicked? Has the ladybug crashed against an obstacle?

What we need is a way to represent the truth values of propositions. More specifically, we’ll use Boolean logic (Boolen logiikka), which employs precisely two truth values: true and false.

Boolean, the data type

The Scala literals true and false represent “truth” and “untruth”. These two literals are reserved words in Scala, which means you can’t use them as names for variables or other program components.

Scala’s data type for truth values is called Boolean. This type is defined in package scala and is therefore always available in all Scala programs just like Int and String are.

val popeIsCatholic = truepopeIsCatholic: Boolean = true
val earthIsFlat = falseearthIsFlat: Boolean = false

Apart from true and false, no other values of type Boolean exist. (Compare: there are countless different strings and numbers.)

A Boolean literal alone forms an expression, too. The value of the expression is the truth value indicated by the literal:

falseres3: Boolean = false

Is a separate Boolean type necessary?

Could we just make isBetterThan return the string "yes" or "no" instead of a Boolean value? Or a number: we could decide, for example, that a return value of zero means “untrue” and any other number means “true”?

In principle, yes. And in practice, too, the latter suggestion is used in some programming languages. However, Scala, like many other languages, provides a custom data type for truth values. Custom data types help us write code that’s easier to understand; it also helps our programming tools check for errors automatically and produce better error messages. For instance, we can count on a Boolean variable always storing true or false and nothing else.

Using Boolean values

You can use truth values just like you’ve used values of other types: you can store them in variables, pass them as parameters, and return them from methods:

println("The truth is " + popeIsCatholic)The truth is true
val secondPreferred = wine2.isBetterThan(wine1)secondPreferred: Boolean = false
val betterThanItself = wine2.isBetterThan(wine2)betterThanItself: Boolean = false

Numbers and strings are associated with operators such as +. One of the operators available on Booleans is !. You can read this exclamation-mark operator as “not”. What it does is “flip” the truth value written after it, turning false to true and the other way around. Here’s an example:

!secondPreferredres4: Boolean = true

As reported by the REPL, the value of the entire expression !secondPreferred is true. Note that evaluating the expression has no effect whatsoever on the variable, which continues to store false.

Writing truth values as literals isn’t the only way to use them:

Comparison Operators

Scala’s comparison operators (vertailuoperaattori) form expressions that compare the values of their subexpressions.

Operator Meaning
> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to
== equals
!= does not equal

Note that two of the operators are written <= and >=, not =< or => (the latter of which means something completely different in Scala; we’ll discuss that later). A mnemonic: write the symbols in the order you read them: less than (<) or equal to (=).

Comparing numbers

The comparison operators produce Booleans:

10 <= 10res5: Boolean = true
20 < (10 + 10)res6: Boolean = false
val age = 20age: Int = 20
val isAdult = age >= 18isAdult: Boolean = true
age == 30res7: Boolean = false
20 != ageres8: Boolean = false
You can use a comparison operator to connect various kinds of expressions. Those subexpressions will be evaluated and their values compared to each other.
Notice that to compare for equality, you must use two equals signs. This is easy to forget before you get used to it. As you’ll recall, a single equals sign is used in Scala for some completely different purposes (i.e., to assign values to variables and at the beginning of a method definition).

The animation below features Boolean values. Can you work out what the code will print out even without first watching the animation?

Comparisons of different kinds

Take a moment to experiment with Boolean values and comparison operators in the REPL.

Try applying the operators to values other than numbers. You can compare strings much like you can compare numbers, for instance. You can also compare Booleans with each other.

Once you’ve tried a few comparisons in the REPL, proceed to the assignments below.

Practice on comparisons

From this chapter onwards, we’ll be using truth values and comparison operators all the time. The sooner you become fluent in their use, the better, so that you don’t have to waste your mental resources thinking about these basic constructs as you work on increasingly complex programs.

To help you build that fluency, there’s a whole bunch of tiny practice tasks below. Try and work out the answers in your head and experiment in the REPL. Don’t just guess at random or just copy every answer from the REPL. Try to understand each answer and use the REPL to check your thinking.

Suppose we’ve initialized two variables as follows:

val large = 10000
val small = 10

Choose the correct value of each expression listed below.

large > small
small < large
!(large > small)
"P" == "NP"
"cat" + "fish" != "catfish"
"my dad" > "yours"
"aaaaaaaaaa" < "bb"
false == true
true == true
false == false
(10 < 20) != (20 > 10)
"llama" < 100
"100" < 100
"100" == 100

Let’s assume that the following commands have just been executed in order:

var number = 19
println(number / 20)
val isZero = number == 0

What value is now stored in isZero?

Suppose we then issue two additional commands:

number = 0
println(isZero)

What does the second command print out??

Two more commands:

number = 20
println(number + number * 10 < (number + number) * 5)

What does the second command print out??

Next, we issue this command:

println((19 >= number) == (0 >= number))

Which of the following are correct? Select all that apply.

Finally, we enter this command:

println(0 >= number != isZero)

What does the command print out?

Let’s say we have two Int variables named number1 and number2 as well as two variables exp1 and exp2 that refer to Experience objects. The exact values of these variables are unimportant for present purposes. Which of the following claims are correct?

Mini-assignment: portraits

A picture can be shaped like a portrait or a landscape. A portrait’s height is greater than its width, while a landscape’s height is less than its width.

Fetch project Miscellaneous. Ignore the rest of it for now; just locate the file misc.scala. In that file, write an effect-free function that:

  • has the name isPortrait;
  • takes a single parameter of type Pic; and
  • returns true if the given image is taller than it’s wide and false otherwise (i.e., if square or in landscape orientation).

Enter import o1.misc._ before trying your function in the REPL.

A+ presents the exercise submission form here.

On object equality

You can use the operators == and != to check whether two objects are equal. But what exactly does equality mean in the case of, say, experience objects?

Explore the question in the REPL as you answer the following question.

Suppose we’ve already entered these commands:

val wine1 = new Experience("Il Barco 2001", "okay", 6.69, 5)
val wine2 = new Experience("Tollo Rosso", "not great", 6.19, 3)
val wine3 = wine1
val wine4 = new Experience("Il Barco 2001", "okay", 6.69, 5)

Which of the following expressions are now true?

As you see: the fact that two experience objects have identical values for their attributes isn’t sufficient to make them “equal”.

In these examples, we used the == operator to compare the contents of two variables — that is, to compare the references stored in two variables. Those operations don’t actually compare what the objects are like at all. For example, wine1 == wine4 is false, since what we have there isn’t two references to the same object but two references to two identical but distinct objects. On the other hand, wine1 == wine3 is true, because both variables contain a reference to the one and same object; the references lead to the same place.

More generally, how equality works on objects depends on the objects’ types. When we write a class of our own, the default is identity comparison as shown for class Experience above. On the other hand, many of the types in the standard Scala API have their own type-specific definitions of equality. Buffers are a good example: two buffers are equal if they have identical contents. The following REPL session illustrates this:

import scala.collection.mutable.Bufferimport scala.collection.mutable.Buffer
val someBuffer = Buffer(2, 10, 5, 4)someBuffer: Buffer[Int] = ArrayBuffer(2, 10, 5, 4)
val anotherBuffer = Buffer(2, 10, 5)anotherBuffer: Buffer[Int] = ArrayBuffer(2, 10, 5)
someBuffer == anotherBufferres9: Boolean = false
anotherBuffer += 4res10: Buffer[Int] = ArrayBuffer(2, 10, 5, 4)
someBuffer == anotherBufferres11: Boolean = true

Implementing Class Experience

Armed with comparison operators, we’re now ready to implement class Experience. Let’s begin with a pseudocode sketch:

class Experience(as constructor parameters, require a name, a description, a price,
                 and a rating; store all four in fixed-valued instance variables) {

  def valueForMoney = return the ratio of your rating to your price

  def isBetterThan(another: Experience) = return a truth value that indicates whether
                                          your own rating is higher than that of the
                                          experience object you received as a parameter

}
Our comparison method receives a reference to an Experience object. The word another has no special significance beyond being the parameter variable’s programmer-chosen name. In this respect, this method greatly resembles the methods xDiff and yDiff from class Pos (Chapter 2.5). Those methods, too, operated on two instances of the same class: this and another instance indicated by the parameter.

Here is a Scala implementation:

class Experience(val name: String, val description: String, val price: Double, val rating: Int) {

  def valueForMoney = this.rating / this.price

  def isBetterThan(another: Experience) = this.rating > another.rating

}
There’s nothing new about this example besides the fact that the method’s return value comes from a comparison and is therefore a Boolean.

Mini-Assignment: A Boolean Instance Variable

Preparation

Recall the classes Customer and Order from earlier chapters:

val testCustomer = new Customer("T. Tester", 12345, "test@test.fi", "Testitie 1, 00100 Testaamo, Finland")testCustomer: o1.Customer = o1.Customer@a7de1d
val exampleOrder = new Order(10001, testCustomer)exampleOrder: o1.Order = o1.Order@18c6974

Task description

Some object attributes can be conveniently expressed as Booleans. For example, we might wish to record whether or not express delivery has been requested for an order object. We can represent that information as an instance variable isExpress:

exampleOrder.isExpressres12: Boolean = false
exampleOrder.isExpress = true

Add this instance variable to class Order within the IntroOOP project. The variable’s value should be initially false, but you should be able to adjust the value by assigning a new value to the variable as shown above.

Submission form

A+ presents the exercise submission form here.

Assignment: Odds (Part 6 of 9)

We can use the newly introduced Boolean data type to extend Odds, the project we last worked with in Chapter 2.7.

(If you didn’t do the Odds assignments then, and don’t want to do them now, you can build on the example solutions. See the submit forms for links.)

Task description

Write a method isLikely that determines if the event represented by an Odds object is likely or not. We’ll say that an event is likely if the Odds of it occurring are greater than those of it not occurring. For instance, rolling a six on a die isn’t likely, but rolling “anything but a six” is:

val rollingSix = new Odds(5, 1)rollingSix: Odds = o1.Odds@d4c0c4
val notRollingSix = new Odds(1, 5)notRollingSix: Odds = o1.Odds@a5e42e
rollingSix.isLikelyres13: Boolean = false
notRollingSix.isLikelyres14: Boolean = true

Also add a method isLikelierThan that determines if one event is more likely than another:

rollingSix.isLikelierThan(notRollingSix)res15: Boolean = false
notRollingSix.isLikelierThan(rollingSix)res16: Boolean = true
rollingSix.isLikelierThan(rollingSix)res17: Boolean = false

Instructions and hints

  • Add the methods to Odds.scala.
  • You can use the app object OddsTest2 to test your methods. This small program has been provided for you but its contents have been “commented out” so that you don’t get error messages about the missing methods isLikely and isLikelierThan before you’ve implemented them. Once you’ve written the two methods in class Odds, uncomment the code in OddsTest2, then run the program.
  • As with OddsTest, which we used in Chapter 2.7, the plan is for OddsTest2 to invoke the methods of class Odds. Don’t copy any methods or variables from Odds into the test program.

Submission form

A+ presents the exercise submission form here.

Assignment: FlappyBug (Part 11 of 17: Game Over)

While experimenting with FlappyBug, you are sure to have noticed that the obstacle utterly fails to obstruct the bug. Let’s make the game end as soon as an obstacle touches the bug.

Preparation: the isDone method on Views

This example program from Chapter 3.1 displays a circle whose size grows with each click by the user.

object ClickApp extends App {
  val clickCounter = new Counter(5)
  val background = rectangle(500, 500, Black)

  val gui = new View(clickCounter) {
    def makePic = background.place(circle(clickCounter.value, White), new Pos(100, 100))

    override def onClick(locationOfClick: Pos) = {
      clickCounter.advance()
      println("Click detected at " + locationOfClick + "; " + clickCounter)
    }
  }

  gui.start()
}

You can define an isDone method on a View object. This method determines when the view stops handling clicks, ticks, and other GUI events:

object ClickAppThatStops extends App {
  val clickCounter = new Counter(5)
  val background = rectangle(500, 500, Black)

  val gui = new View(clickCounter) {
    def makePic = background.place(circle(clickCounter.value, White), new Pos(100, 100))

    override def onClick(locationOfClick: Pos) = {
      clickCounter.advance()
      println("Click detected at " + locationOfClick + "; " + clickCounter)
    }

    override def isDone = clickCounter.value > 10
  }


  gui.start()
}
isDone returns a Boolean that indicates whether the view is “done”, that is, whether the GUI can cease handling future events. In this example, the GUI stops when the counter’s value exceeds ten.
Our implementation overrides the default method, which always returns false and therefore never stops the GUI. Hence the override modifier (Chapter 3.1).

Task description

Do three things:

  1. In class Obstacle, define a method that checks whether the obstacle is touching a bug.
  2. Use that method to define a method in class Game that checks whether the player has collided with an obstacle and therefore lost the game.
  3. In FlappyBugApp, add an isDone method that ensures the GUI stops when the game is over.

Here are the steps in more detail:

  1. In class Obstacle, write an effect-free method touches that checks whether or not the obstacle is currently located where a bug is.

    • It receives a reference to a Bug object as its only parameter.
    • It returns a Boolean value.
    • It determines the distance between the obstacle in question (this) and the given bug. The return value depends on this distance: the method returns true if and only if the distance is less than the sum of the radiuses of the bug and the obstacle; otherwise, it returns false.
    • It computes the distance along a straight line between the centers of the bug and the obstacle. You can use the distance method from class Pos (Chapter 2.5).
  2. In class Game, add an isLost method that checks whether the player has lost the game. The game is lost precisely when the obstacle touches the bug:

    def isLost = this.obstacle.touches(this.bug)
    
  3. Add an isDone method to the GUI view in FlappyBugApp. It should return true if the player has lost and false otherwise. In practice, the game should stop when the bug collides with an obstacle.

    • See the isDone method of ClickAppThatStops, above, for a template. A single isLost call will do for a method body.

Submission form

A+ presents the exercise submission form here.

Summary of Key Points

  • When a program needs to know “yes or no?”, it’s often convenient to use the truth values of Boolean logic: true and false. Many programming languages provide a data type for this purpose.
  • There are exactly two different values of type Boolean: true and false are available as literals in Scala.
  • Comparison operators yield Boolean values. You can use these operators to compare two values; you might check whether one number is smaller than another, for example.
  • What makes two objects count as equal depends on the data type. For some objects, equality is determined by comparing object identities, for others, by comparing one or more of the objects’ attributes.
  • Links to the glossary: truth value, Boolean, comparison operator.

Feedback

Please note that this section must be completed individually. Even if you worked on this chapter with a pair, each of you should submit the form separately.

Credits

Thousands of students have given feedback that has contributed to this ebook’s design. Thank you!

Weeks 1 to 13 of the ebook, including the assignments and weekly bulletins, have been written in Finnish and translated into English by Juha Sorva.

Weeks 14 to 20 are by Otto Seppälä. That part of the ebook isn’t available during the fall term, but we’ll publish it when it’s time.

The appendices (glossary, Scala reference, FAQ, etc.) are by Juha Sorva unless otherwise specified on the page.

The automatic assessment of the assignments has been developed by: (in alphabetical order) Riku Autio, Nikolas Drosdek, Joonatan Honkamaa, Jaakko Kantojärvi, Niklas Kröger, Teemu Lehtinen, Strasdosky Otewa, Timi Seppälä, Teemu Sirkiä, and Aleksi Vartiainen.

The illustrations at the top of each chapter, and the similar drawings elsewhere in the ebook, are the work of Christina Lassheikki.

The animations that detail the execution Scala programs have been designed by Juha Sorva and Teemu Sirkiä. Teemu Sirkiä and Riku Autio have done the technical implementation, relying on Teemu’s Jsvee and Kelmu toolkits.

The other diagrams and interactive presentations in the ebook are by Juha Sorva.

The O1Library software has been developed by Aleksi Lukkarinen and Juha Sorva. Several of its key components are built upon Aleksi’s SMCL library.

The pedagogy behind O1Library’s tools for simple graphical programming (such as Pic) is inspired by the textbooks How to Design Programs by Flatt, Felleisen, Findler, and Krishnamurthi and Picturing Programs by Stephen Bloch.

The course platform A+ has been created by Aalto’s LeTech research group and is largely developed by students. The current lead developer is Jaakko Kantojärvi; many other students of computer science and information networks are also active on the project.

For O1’s current teaching staff, please see Chapter 1.1.

Additional credits appear at the ends of some chapters.

a drop of ink
Posting submission...

Submission received.