UW Homework: Complex Kotlin

Related tags

App complexkotlin
Overview

UW Homework: Complex Kotlin

This homework is designed to force you to exercise your knowledge of the Kotlin programming language. It does not involve Android in any way yet. Like the previous assignment, it will be done entirely from the command-line using Gradle and a good text editor.

Goal

Your task is simple: Make the code compile, and make all the unit tests pass.

Again, all your work should be in the Library.kt file, and you may not modify the LibraryTest.kt file in any substantive way.

To obtain this code...

... clone the repository using Git:

git clone https://github.com/tedneward/uw-complex-kotlin

Build and run the tests by using gradlew or gradlew.bat, depending on your operating system.

./gradlew test

Make sure to create a GitHub repository for your clone, and re-home the remote origin to it:

git remote set-url origin https://github.com/[your-ID]/complexkotlin

If you find it hard to remember what to do, look back at the previous assignment for details. Reach out to the TA if you are still stuck.

Now what?

If you do not see the Library.kt and LibraryTest.kt files at first, you can find them here:

  • src/main/kotlin/edu/uw/complexkotlin/Library.kt
  • src/test/kotlin/edu/uw/complexkotlin/LibraryTest.kt

Note that you will do all of your work in the Library.kt file, and you should not need to modify anything in the LibraryTest.kt file; in fact, modifying that file could jeopardize your grade! If you feel you need to make changes to it, contact the TA before doing so--chances are very good that the problem lies elsewhere.

The assignment

The code is broken into a sequence of sections. Your job is to implement the body of each section, so that the tests will pass.

FIZZBUZZ lambda

In the first section, you will be using the collection methods map and fold to perform the "FIZZBUZZ test". This is a simple algorithm, usually used as part of an interview process as a way of verifying developer skill (and rather poorly at that).

FIZZBUZZ goes like this: For any sequence of numbers (such as 1 to 15), any number that is divisible by 3 returns "FIZZ", and any number divisible by 5 returns "BUZZ". Numbers which are divisible by both 3 and 5 return "FIZZBUZZ".

Use the map function to transform the range of numbers into an list of strings (either "FIZZ", "BUZZ" or the empty string ""), and then use fold to combine them all down into a single String. The end result should be "FIZZBUZZFIZZFIZZBUZZFIZZFIZZBUZZ".

You need to define all this in a lambda value, called fizzbuzz, that I can call from the tests. Be prepared to support any range of numbers from 0 to 15 (0 to 1, 0 to 0, 4 to 12, and so on). You need not worry about negative numbers, or numbers greater than 15 (but it shouldn't be too hard to figure out how to do FIZZBUZZ for any number).

In addition to teaching you FizzBuzz (which really is used a lot in programmer interviews, I'm not making that up), this is an exercise in understanding how map and fold work, as they are commonly used in Kotlin code. Both come in a triad along with filter, and these three functions are the cornerstone library set of "functional programming". You are free to use filter as well, if you so choose, but you do not need to in order to pass the tests. This exercise also reinforces your comfort level with lambda and block syntax.

Use process

In the second section, your job is to use the process function defined in the code to generate strings. The process function takes a parameter and a block of code. Your job is to write the lambdas r1 and r2 to call process such that the tests for r1 and r2 pass.

This is an exercise in getting the "block syntax" correct, as well as more practice around lambdas. If it's not becoming clear, the use of anonymous functions, lambdas, and block syntax is really important in Kotlin and other languages, and it's incredibly useful to be able to understand and use. Lambdas open up a whole new world of capability in code design, and you need to know it.

Ho, Socrates!

In the third section, you are to create an enum class called Philosopher. This is going to be a peculiar use of enum, however, as we are going to model a very simple state machine: that of the classic Greek philosopher.

As everybody knows, philosophers split their time between THINKING and TALKING, and only shift from one state to the other when told to do so via the method call signal (which you will need to define). Additionally, each state should override the toString function to indicate the Philosopher's current state. When THINKING, a Philosopher will return "Deep thoughts..." and when TALKING, a Philosopher will return "Allow me to suggest an idea...". If you are not sure of the syntax here, check out the Kotlin reference page on Enum classes.

Modeling state machines in a mobile application is a very common occurrence, so this forces you to think about how to do that using this Kotlin feature. And giving you the link to the Kotlin reference page really makes this a "gimme", honestly.

By the way, I will give a bonus extra credit point to anyone who can find out who Seneca the Younger was, which school of philosophy he is commonly associated with, and another bonus extra credit point if you can summarize that school of philosophy in a single sentence. Put your answers in comments in the code, and let the TA know so they can make sure to look for them. Direct copy-and-paste from Wikipedia does not count.

On another note, when you have some time to kill, play "The Wikipedia Game"--pick any random subject on Wikipedia, and click the first link on that page. On the page that comes up, click the first link on that page. Continue doing this until you have reached the Wikipedia page on Philosophy, and marvel at how everything in human existence essentially traces its roots back to philosophy and a bunch of dead Greeks in togas. STEM is not the only thing you should be learning while you are in college--it may not feel like it now, but stepping into the world of the liberal arts and philosophy in particular will make you a much better programmer--and human--down the road.

Command (object)!

In the fourth section, create a class called Command that can be used as a function. This means you will need to provide an invoke method on the class. The primary constructor should take a String parameter (prompt), and the invoke operator should also take a String parameter (message), and return a concatenation of the prompt and the message. The tests will invoke Command objects in several different ways, to verify that the concatenation works correctly.

Rubric

As with the previous assignment, 5 points if all the tests pass, and 0 points if any of them fail.

Where this README appears to disagree with the code, follow the code, but do not change the tests.

You might also like...
Android-basics-kotlin-tip-time-app - Tip Time app from Android Basics in Kotlin
Android-basics-kotlin-tip-time-app - Tip Time app from Android Basics in Kotlin

Tip Time Tip Time app from Android Basics in Kotlin at developers.google.com. It

Coinbase-pro-feed-kotlin - Kotlin Coinbase Pro Level 2 Order Book Feed
Coinbase-pro-feed-kotlin - Kotlin Coinbase Pro Level 2 Order Book Feed

Kotlin Coinbase Pro Level 2 Order Book Feed Quick start Depending on your OS run

Kotlin-basic-calculator - Basic calculator to understand syntax and the methods of Kotlin

KotlinBasicCalculator I always love to create a calculater for understand the sy

To Do List App is built in Kotlin using Material 3, Data Binding, Navigation Component Graphs, Room persistence library, Kotlin coroutines, LiveData, Dagger Hilt, and Notifications following MVVM Architecture.
To Do List App is built in Kotlin using Material 3, Data Binding, Navigation Component Graphs, Room persistence library, Kotlin coroutines, LiveData, Dagger Hilt, and Notifications following MVVM Architecture.

ToDoListApp ToDoList App demonstrates modern Android development with Hilt, Coroutines, LiveData, Jetpack (Room, ViewModel), and Material 3 Design bas

A showcase music app for Android entirely written using Kotlin language

Bandhook Kotlin This project is a small replica of the app I developed some time ago. Bandhook can still be found on Play Store At the moment it will

Utility Android app for generating color palettes of images using the Palette library. Written in Kotlin.
Utility Android app for generating color palettes of images using the Palette library. Written in Kotlin.

Palette Helper is a simple utility app made to generate color palettes of images using Google's fantastic Palette library. It's mostly a for-fun pet p

Utility Android app for generating color palettes of images using the Palette library. Written in Kotlin.
Utility Android app for generating color palettes of images using the Palette library. Written in Kotlin.

Palette Helper is a simple utility app made to generate color palettes of images using Google's fantastic Palette library. It's mostly a for-fun pet p

Episodie is a TV show time tracker app with unusual design written in kotlin and clean architecture approach. Get to know how much time you spent watching tv shows.
Episodie is a TV show time tracker app with unusual design written in kotlin and clean architecture approach. Get to know how much time you spent watching tv shows.

Episodie Episodie is a TV show time tracker app with unusual design. Get to know how much time you spent watching tv shows. Track easily overall progr

An library to help android developers working easly with activities and fragments (Kotlin version)
An library to help android developers working easly with activities and fragments (Kotlin version)

AFM An library to help android developer working easly with activities and fragments (Kotlin) Motivation Accelerate the process and abstract the logic

Owner
Wendell Li
Wendell Li
Appcent Nasa Rover Photos API Apptern Homework

AppcentNasaApi Appcent Nasa Rover Photos API Apptern Homework NASA Api : https://api.nasa.gov/ See Photos that taken by Nasa's 3 Rovers (Curiosity, Op

wh1tec3t 3 Jun 13, 2022
My CS 4th semester homework: simple Android app for looking up cocktails using an open API

CocktailApp My CS 4th semester homework: simple Android app for looking up cocktails using an open API A simple Android app that uses the free api of

null 2 May 18, 2022
A full-stack application showing the power šŸ’Ŗ of KOTLIN. Entire android app + backend Apis written in Kotlin šŸ”„

Gamebaaz ?? A full-stack application showing the power ?? of KOTLIN. Entire android app + backend Apis written in Kotlin ?? Android Backend Jetpack Co

Sarnava Konar 85 Nov 17, 2022
An simple image gallery app utilizing Unsplash API to showcase modern Android development architecture (MVVM + Kotlin + Retrofit2 + Hilt + Coroutines + Kotlin Flow + mockK + Espresso + Junit)

Imagine App An simple image gallery app utilizing Unsplash API. Built with ā¤ļøŽ by Wajahat Karim and contributors Features Popular photos with paginatio

Wajahat Karim 313 Jan 4, 2023
A collection of custom Android/Kotlin lint checks we use in our Android and Kotlin code bases at Slack.

slack-lints This repository contains a collection of custom Android/Kotlin lint checks we use in our Android and Kotlin code bases at Slack. This repo

Slack 119 Dec 20, 2022
Primeiro aplicativo desenvolvido na linguagem kotlin, durante o Hackathon no curso de Android Kotlin da Cel.Lep em parceria com EstaĆ§Ć£o Hack from Facebook

Tela Splash: Nome do aplicativo e uma imagem: Tela Main, principal tela do aplicativo: Um campo para o usuƔrio informar a medida; Um spinner com os ti

Maria Fortunato 0 Nov 3, 2021
MVVM + Kotlin + Jetpack Compose +Navigation Compose + Hilt + Retrofit + Unit Testing + Compose Testing + Coroutines + Kotlin Flow + Io mockK

MvvmKotlinJetpackCompose Why do we need an architecture even when you can make an app without it? let's say you created a project without any architec

Sayyed Rizwan 46 Nov 29, 2022
A general purpose kotlin library that use kotlin coroutines, flows and channels to provide timer features with the most easy and efficient way

Timer Timer is a general purpose kotlin library that use kotlin coroutines, flows and channels to provide timer features with the most easy and effici

Amr Saraya 3 Jul 11, 2022
Solution to the 2021 Advent of code challenge in Kotlin. aoc-2021-in-kotlin

advent-of-code-2021 Welcome to the Advent of Code1 Kotlin project created by aniobistanley using the Advent of Code Kotlin Template delivered by JetBr

null 0 Dec 24, 2021