Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CPSC 411-02 Homework Assignment #1 [Optional Type and Higher Order Function] You will implement a higher order function to compare two objects (of Int and

image text in transcribed

CPSC 411-02 Homework Assignment #1 [Optional Type and Higher Order Function] You will implement a higher order function to compare two objects (of Int and String type) by applying a user-defined condition (via a lambda function). The function will return true if the condition is met; otherwise, false will be returned. Here is the specification of the function. The function named compare Function will take three input parameters: obj1: Int? obj2: String? cond: (val1: Int, val2: Int) -> Boolean The function has Boolean as its return type. The parameter cond is a lambda function defining the condition (specified by users) used to compare two Int objects. You function needs to do the following. 1. Convert the obj2 object from String into an Int type. Please consider use the String.tolnt() method for this. Let's assume that obj2 only contain numeric characters so the String.tolnt() always works. 2. If the input object (obj1 or obj2) is null, initialize it as 0 before passing to the lambda function. Please only use the operators ?., ?:, and !! to accomplish this. 3. Invoke the lambda function with the objects obtained from step 1 and 2. 4. Return the value obtained from step 3. To test your function, use the code snippet like below. val result = compareFunction(null, "4") {x, y-> x >= y} In addition to testing the scenarios where obj1 or obj2 might be null, you also need to test all possible conditions to compare two Int objects such as 1. The first Int is greater than the second one 2. The first Int is less than the second one 3. The first one equals to the second one CPSC 411-02 Homework Assignment #1 [Optional Type and Higher Order Function] You will implement a higher order function to compare two objects (of Int and String type) by applying a user-defined condition (via a lambda function). The function will return true if the condition is met; otherwise, false will be returned. Here is the specification of the function. The function named compare Function will take three input parameters: obj1: Int? obj2: String? cond: (val1: Int, val2: Int) -> Boolean The function has Boolean as its return type. The parameter cond is a lambda function defining the condition (specified by users) used to compare two Int objects. You function needs to do the following. 1. Convert the obj2 object from String into an Int type. Please consider use the String.tolnt() method for this. Let's assume that obj2 only contain numeric characters so the String.tolnt() always works. 2. If the input object (obj1 or obj2) is null, initialize it as 0 before passing to the lambda function. Please only use the operators ?., ?:, and !! to accomplish this. 3. Invoke the lambda function with the objects obtained from step 1 and 2. 4. Return the value obtained from step 3. To test your function, use the code snippet like below. val result = compareFunction(null, "4") {x, y-> x >= y} In addition to testing the scenarios where obj1 or obj2 might be null, you also need to test all possible conditions to compare two Int objects such as 1. The first Int is greater than the second one 2. The first Int is less than the second one 3. The first one equals to the second one

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

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions