Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

abstract class IntSet { def incl(x: Int): IntSet def contains(x: Int): Boolean def union(other: IntSet): IntSet } object Empty extends IntSet { def incl(x: Int):

abstract class IntSet {

def incl(x: Int): IntSet

def contains(x: Int): Boolean

def union(other: IntSet): IntSet

}

object Empty extends IntSet {

def incl(x: Int): IntSet = new NonEmpty(x, Empty, Empty)

def contains(x: Int): Boolean = false

def union(other: IntSet): IntSet = other

override def toString(): String = "."

}

class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet {

def incl(x: Int): IntSet = {

println(x)

if (x < elem) new NonEmpty(elem, left incl x, right)

else if (x > elem) new NonEmpty(elem, left, right incl x)

else this

}

def contains(x: Int): Boolean = {

if (x < elem) left contains x

else if (x > elem) right contains x

else true

}

def union(other: IntSet) = {

right union (left union (other incl elem))

}

override def toString(): String = {

elem + " " + left.toString() + " " + right.toString

}

}

object Hello {

def main(args: Array[String]) = {

println("hello world!")

val B = Empty incl 4 incl 1 incl 5 incl 6

println(B)

val C = Empty incl -1 incl 2 incl 10 incl 100

println(C)

val D = B union C

println (D)

}

}

augment the previous scala code with the following methods:

1) def filter(p: Int => Boolean): IntSet Hint: start by defining the helper method filterAcc which takes an accumulator set as a second argument. This accumulator contains the ongoing result of the filtering. def filterAcc(p: Int => Boolean, acc: IntSet): IntSet 2) Using the method remove with the following implementations:

def remove(i: Int): IntSet = this // for Empty IntSet

def remove(i: Int): IntSet = // for NonEmpty IntSet

if (i < elem) new NonEmpty(elem, left.remove(i), right)

else if (elem < i) new NonEmpty(elem, left, right.remove(i))

else left.union(right)

Write a method descending() which prints the IntSet elements in descending order. Hint: start by implementing the method max() which returns the max element in the set. 3) Write a Scala main method to test your implemented methods

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Iv Special Issue On Database Systems For Biomedical Applications Lncs 6990

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Christian Bohm ,Johann Eder ,Claudia Plant

2011th Edition

3642237398, 978-3642237393

More Books

Students also viewed these Databases questions

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago