Review problems 2#
Activity 54 (Operator precedence)
You want to calculate the following formula in C#.
Which of the following expressions calculate the formula?
y = a * x * x * x + b - 1 * x + cy = a * (x * x * x) + (b - 1) * (x + c)y = a * (x * x * x) + b - 1 * x + cy = a * x * x * x + (b - 1) * x + cy = (a * x) * x * x + b - 1 * (x + c)y = (a * x * x * x) + (b - 1) * x + cy = a * Math.Pow(x, 3) + b - 1 * x + c
You don’t have to write any code to answer this question
Hint
section Operator precedence
section Math expressions
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
section Operator precedence
section Conversion between datatypes
Convert.*orTryParse
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:
Design a control flow using a flowchart
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
section Flowcharts
section Conversion between datatypes
Convert.*vsTryParse
section Boolean logic
negation
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 ).
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:
Create 10 balls with random colors at random places
Add velocities to each ball, so that their position changes at each frame, i.e.,
pos = pos + velfor x and y coordinates.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-elsestatementif your programs works, then comment it out and solve the problem with a
switchstatement
Enter mass in kg
80
Enter height in m
1.70
Adults corpulence index of 16.28 means: overweight
Hint
Section Creating functions
For rounding, see section Math expressions
Section if-else Syntax
Section Switch
you can also use comparison operators in case statements, e.g.,
case < underweightMax
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
Section Creating functions
Section Class diagram
Section Printable classes