Review problems 2

Review problems 2#

Activity 54 (Operator precedence)

You want to calculate the following formula in C#.

\[ y = ax^{3}+ (b-1) x+c \]

Which of the following expressions calculate the formula?

  1. y = a * x * x * x + b - 1 * x + c

  2. y = a * (x * x * x) + (b - 1) * (x + c)

  3. y = a * (x * x * x) + b - 1 * x + c

  4. y = a * x * x * x + (b - 1) * x + c

  5. y = (a * x) * x * x + b - 1 * (x + c)

  6. y = (a * x * x * x) + (b - 1) * x + c

  7. y = a * Math.Pow(x, 3) + b - 1 * x + c

You don’t have to write any code to answer this question

Hint

Activity 55 (Coding a mathematical formula)

Write a program that receives the required input from the user and calculates y from Operator precedence. Your program should be able to accept decimal numbers like 1.2, -0.9.

For example, for a=1, b=1, c=1, and x=0.1, you should get 1.001.

Assume that the user will always provide numbers and no letters.

Hint

Activity 56 (Processing invalid user input)

Upgrade your solution from Activity 55 so that your program does not choke 🤮 if the user provides wrong input like this:

Let me calculate y = ax^3 + (b-1)x + c for you 🙂
a? 😛
Unhandled exception. System.FormatException: The input string '😛' was not in a correct format.

Your program should react recover like a boss as follows:

Let me calculate y = ax^3 + (b-1)x + c for you 🙂
a? 😛
Your input is not a decimal number, please try again. ↺
a?

Steps:

  1. Design a control flow using a flowchart

  2. Implement your flowchart using C#.

If you copy paste blocks, this is fine for this problem. You don’t have to implement any functions. We will implement functions in another problem.

Hint

Activity 57 (Creating random balls)

We will use raylib in this activity. Raylib is a simple library to develop games.

Install it by searching for Raylib-cs in the package manager (NuGet nuget).

Then use the following template to test your installation:

using Raylib_cs;

Raylib.InitWindow(800, 480, "Hello World");

while (!Raylib.WindowShouldClose())
{
    Raylib.BeginDrawing();
    Raylib.ClearBackground(Color.White);

    Raylib.DrawText("Hello, world!", 12, 12, 20, Color.Black);

    Raylib.EndDrawing();
}

If the template above does not work, then try the template here

Milestones:

  1. Create 10 balls with random colors at random places

  2. Add velocities to each ball, so that their position changes at each frame, i.e., pos = pos + vel for x and y coordinates.

  3. If a ball goes outside the view, it should pop up again at the other side of the screen.

Activity 58 (AdultCorpulenceIndex)

Corpulence index is a measure of leanness:

\( \mathrm{CI} = \frac{\mathrm{mass}}{\mathrm{height}^3} \)

Write a program that calculates corpulence index (CI) for adults and output the corresponding category.

Requirements:

  • use constant variables for the CI category ranges, e.g., const int underWeightMax = 11.

  • you must define a function called ci() for calculating the CI.

  • CI must be rounded to two digits after the decimal dot when printed on the console

  • use first an if-else statement

    • if your programs works, then comment it out and solve the problem with a switch statement

Enter mass in kg
80
Enter height in m
1.70
Adults corpulence index of 16.28 means: overweight
Hint

Activity 59 (OOPBasedTodoApp)

Let’s implement a simple OOP-based To-do app. It must have the following classes and methods:

        classDiagram
    class TodoItem {
        +string action
        +bool done
    }

    class TodoList {
        +List~TodoItem~ Items
        +Add(TodoItem item)
        +getTodos() List~TodoItem~
        +MarkActionDone(string action)
    }

    TodoList o-- TodoItem
    

Use the following code to test your app:

var tl = new TodoList();
tl.Add(new TodoItem { action = "Grocery", done = false });
tl.Add(new TodoItem { action = "Gym", done = false });
tl.MarkActionDone("Gym");
foreach (var todo in tl.getTodos()) Console.WriteLine(todo.action);
// Should output "Grocery"

// optional:
Console.WriteLine(tl);
// Should output "Grocery"
Hint