Learn Scala 18th advanced Type Exercise Solutions

Source: Internet
Author: User

1. Implement a bug class to model insects crawling along the horizontal line. Move method moves to the current direction, the Turn method lets the worm turn around, the show method prints out the current position. Allow these methods to be called by string. For example:

Bugsy.move (4). Show (). Move (6). Show (). Turn (). Move (5). Show ()

The above code should display 4 10 5.

package ex18_01class bug {  var x = 0  var y =  0  var curr_direction = 0  def move (LEN: INT)  = {     curr_direction match {      case 0 = > x += len      case 1 => y += len       case 2 => x -= len       case 3 => y -= len    }     This  }  def turn ()  = {    curr_direction =  ( curr_direction + 1)  % 4    this  }  def show ( )  = {    curr_direction match {       Case 0 =>&nBsp;print (x +  " ")       case 1 => print (y  +  " ")       case 2 => print (x +  " ")       case 3 => print (y +  " ")      }    this  }}object Main extends App {   val bugsy = new bug  bugsy.move (4). Show (). Move (6). Show (). Turn (). Move (5). Show ()}/*output:4 10 5*/

2. Provide a fluent interface for the bug class in the previous exercise to achieve the following code:

Bugsy move 4 and show and then move 6 and show turn around move 5 and show

Answer://todo

3. Complete the Fluent interface in section 18.1 so that we can make the following call:

Book set Title to ' Scala for the impatient ' set Author to ' Cay Horstmann '

Package ex18_03object main extends app {  val book = new  Document  book set Title to  "Scala for the impatient"   set author to  "Cay horstmann"   println (book)}//  Knowledge points:  Singleton type object  title // this object is used as an argument for a  fluent interfaceobject authorclass document {  private var title  =  ""   private var author =  ""   private var usenextargas :  any = null  def set (Obj: title.type): this.type = {  Usenextargas = obj; this }  def set (obj: author.type):  this.type  = { usenextargas = obj; this }  def to (arg: String):  this.type =&nBsp {    if  (Usenextargas == title)  title = arg     if  (Usenextargas == author)  author = arg    this   }  override def toString = getClass.getName +  "[title="  + title +  ",  author="  + author +  "]"}/*output:eg18_01_b.document [title=scala for the impatient] */

4. Implement the Equals method of the member class that is nested in the network class in section 18.2. Two members must belong to the same network if they want to equal one another.

Answer://todo

5. Consider the following types of aliases

Type Networkmember = N.member forsome {val N:network}

and functions

def process (M1:networkmember, m2:networkmember) = (M1, m2)

What is the difference between this and the process function in section 18.8?

Answer: The function accepts members of the same or different networks, while the process function in section 18.8 rejects members from different networks.

package ex18_05//  Knowledge Point:  existence type Import javax.swing._import scala.collection.mutable._ object main extends app {  type networkmember = n.member  Forsome { val n: network }  def process (m1: NetworkMember,  M2: networkmember)  =  (m1, m2)   val chatter = new network   val myface = new network  val fred = chatter.join (" Fred ")   val wilma = chatter.join (" Wilma ")   val barney =  Myface.join ("Barney")   process (Fred, wilma)  // OK   accept members of the same network    Process (Fred, barney)  // OK  accept members from different networks   fred.contacts += wilma //  OK  //fred.contacts += barney //ERROR}class Network {   Class member (val name:  String)  {    val contacts = new ArrayBuffer[Member]     override def toString = getClass.getName +  "[Name="  + name  +       ", contacts="  + contacts.map (_.name) mkstring ("[",  ", ", ")  +  "]"   }  private val members = new  Arraybuffer[member]  def join (name: string): member = {     val m = new member (name)     members += m     m  }  override def tostring = getclass.getname +   "[members="  + members +  "]"}/*output:*/


6.

7.

8.

9.

10.


Learn Scala 18th advanced Type Exercise Solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.