Assignment 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Advanced Functional Thinking

Assignment
Date of Submission: 30/11/2020
1. Create a Scala program to reverse, and then format to upper case, the given String: "Hello
World".
2. Create a Scala program to output the following basic JSON notation.
Output:
{
"donut_name":"Vanilla Donut",
"quantity_purchased":"10",
"price":2.5
}

3. Create a Scala program to prompt customers for their name and age. The format for the
name and age labels should be in bold. And, the name literal should be underlined.
Output:
Name: Akash Saini
Age: 20

4. Create a Scala program to find the 8th character in the String: "Hello World"

5. Create a Scala program to calculate the total cost for a customer who is buying 10
Glazed donuts. You can assume that the price of each Glazed donut item is at $2.50. Notice
the format of the $25.00 total cost literal, which is essentially at 2 decimal places.
Output:
Total cost of 10 Glazed Donuts = $25.00

6. Write a Scala program and use a Sequence data structure to store a combination of names
to ages as follows:
Seq(
"James", 7,
"Andy", 8,
"Tommy", 10,
"Bob", 13,
"Sam", 10
)
Note that the above elements are not in a key value pair format. They are instead in a
comma separated format within the Sequence. From the above Sequence, extract the age
number literal, and use this to find the sum of ages.
Output:
Combined sequence of names to ages = James,7,Andy,8,Tommy,10,Bob,13,Sam,10
Sum of ages = 48
7. Write a Scala program that will model a basic Vehicle class system, or type hierarchy, as
per the previous Scala exercise. More precisely, you will have a base trait for a Vehicle type
and with an abstract printName() method. The Vehicle type has two sub-classes, namely, a
Car, and a Bike. The Car type itself has two further subsequent sub-types, namely, a
BmwCar and a MazdaCar. Likewise, the Bike type has two sub-classes, namely, a
HondaBike, and a BmwBike. Modify the VehicleMaker class from the previous Scala
exercise, and make its constructor argument optional, and do remember that the
VehicleMaker had a type parameter with a constraint to the Vehicle type. In the
VehicleMaker class, create a method named makeSimilarCars that will have as input
parameters two vehicles whose types are of course derived from the Vehicle type
constraint. In addition, this particular makeSimilarCars() method, as its name implies,
should only accept objects that are of the same types - for instance, you could pass-through
two BmwCar objects, and not a BmwCar along with a HondaBike. In addition, create
another method named makeBikes() that also has two input parameters for two Vehicle
types. As its name implies, though, this particular method will accept objects, say, a
HondaBike with a BmwBike. As a hint to creating the makeSimilarCars() and the
makeBikes() methods, you will need to lift some types into scope and generalize type
constraints for the arguments using Scala's built-in implicit evidence features.

Output:
Making two CAR vehicles:
vehicleA = bmw car 3 series
vehicleB = bmw car 5 series
Making two BIKE vehicles:
vehicleA = honda bike firestorm
vehicleB = bmw bike r 2000

You might also like