Demo 4
NOTE! You must get a minimum of 2 points from star(*) exercises! Those who complete less than 5 points per demo are more likely to fall behind by the end of the course.
Demo exercises need to be returned by 11 am on Monday. You can also return a part or all exerices beforehand and tweak them before the deadline. The model answers mentioned below will be visible only after the demo return sessions have been held.
Return demo tasks before 11:59 on Monday.
Learning goals
Your time usage this week (0.5 p.)
Submitting time usage grants 0.5 points. Use the Set Custom Points feature to set your points.
After completing the exercises, give an estimate of how much time you spent on the course this week. Include all course-related work during the week (lectures, reading the course material, completing the exercises etc.).
A note about given points
Every student has a right as well as a responsibility to evaluate the correctness of their own points. If your program doesn't work, give yourslf Custom points depending on how you think you did. For example, if you think that you've achieved 50% of the task's goals, give yourself at most 0.5 points. Likewise, if you think that your answer is 90% correct but the automatic scoring system gives you less points for some reason, give yourself 0.9 points from the task.
Jypeli-related tasks
Task 1*
Do the Pong tutorial chapters 1-7. (You can skip chapters 1-3 if you already did them previously.) Write a short learning diary (at least 100 words), on what you learned and what was new to you? What was easy and what you found difficult? Was this task useful to you? You can also give feedback regarding the content of this tutorial. You don't need to hand in the code files you created during the Pong tutorial. Note! You cannot do this task in TIM!
Do not worry too much about the odd way the physics work in some collisions :-)
Text-based tasks
The following tasks are text-based. Use either the ConsoleMain template included in Jypeli or the default Visual Studio Console Application template.
Task 2
Read the following documentations and the subroutine declarations. Each one of them has something wrong. There could be something to improve (but not necessarily is) in the type of the return value, the name of the function, the number of parameters or their names and types, or the documentation comments. Entire words can also be missing from the declaration lines or documentation.
Write an answer which tells what is wrong in each of the subroutine declarations. Note that the actual implementation of the subroutines isn't evaluated here, nor is it even shown. Use only the documentation and function declaration lines to figure out what's wrong.
For full points, you should find at least 2 things to fix in each subroutine or its documentation.
A. Absolute value
/// <summary> The function returns the absolute value of the given number. </summary>
/// <param name="number"> The number that the absolute value is taken from. </param>
/// <returns> The absolute value of the number. </returns>
public static int returnabsolutevalue(double number)
{
...
}
B. Shorter word
/// <summary> The function returns the shorter one of the two given words.
/// In case the words are equal in length, the first word is returned. </summary>
/// <param name="word1"> The first word. </param>
/// <param name="word2"> The second word. </param>
/// <returns> The shorter word. </returns>
void String ShorterOfTwoWords(char word1 char word2)
{
...
}
C. Palindrome
/// <summary> The function examines whether the given word is a palindrome. </summary>
/// <param name="word"> The word to examine. </param>
/// <returns> Whether the word is a palindrome. </returns>
public static String Palindrome(bool)
{
...
}
Task 3*
M: 12. Strings, 13. If statements.
You've been given a main function. Insert a class named PidempiSana around it.
Write a required function PidempiMerkkijono (LongerString) that is used by Main (this is a ConsoleMain project). The function should return the longer one of two given strings. In case the strings are equal in length, the first one is returned.
Note! You need to do this task in Visual Studio.
public static void Main(string[] args)
{
Console.Write("Anna 1. jono >");
string jono1 = Console.ReadLine();
Console.Write("Anna 2. jono >");
string jono2 = Console.ReadLine();
string pidempi = PidempiMerkkijono(jono1, jono2);
Console.WriteLine("\"" + pidempi + "\" on pidempi merkkijono");
}
Remember that first you write the functions introduction line (the one with the: public static etc...).Then write documentation for the function (the /// and fill the summary and meaning of the parameters). You can find more info on writing subroutines on the Wiki page. At first the content of the function can just be:
return "test";
Then test that the program compiles, runs and asks the questions and prints the output with just the above piece of code in the function. When it works you can start altering the content of the function so that it will work as intended.
If you do proper TDD (Test Driven Development), you will write proper tests for the function and see that they fail before you start altering the content. Only after this you will make the function work and you run the tests again if the tests still fail you will fix the function etc... Tests for this function could be for example:
/// Note! tests are always written
/// ClassName.FunctionName(Parameters) === "Expected output";
/// or ~~~ in stead of === if you are comparing doubles or floats etc
/// In English they would be LongerWord.LongerString("...
/// But because the class name and the function name in TIM are
/// expected to be in Finnish you need to write:
/// PidempiSana.PidempiMerkkijono("First word", "second") === "First word";
/// PidempiSana.PidempiMerkkijono("First", "Second") === "Second";
/// PidempiSana.PidempiMerkkijono("Fir", "Sec") === "Fir";
/// PidempiSana.PidempiMerkkijono("", "Second") === "Second";
/// PidempiSana.PidempiMerkkijono("", "") === "";
After you have done this task copy the function with its introduction line into the box below.
In this task you get 0.4 points for running the code, 0.5 points for passing the tests and 0.1 points for viewing the documentation.
Task 4
M: 12. Strings.
Create a Console program that works like in the field below.
I repeat what you say but I cannot say the letter r!
Give input >race car drives super fast.
So you said : lace cal dlives supel fast.
Hint: read String class’s documentation and search for a method that you can use to swap letters. For the reading of input see the previous task’s main (in this task you don’t necessarily need to write any subroutines, but considering upcoming tasks writing a helper-function wouldn’t hurt).
You get 0.4 points for running a working program, 0.5 points for passing the tests and 0.1 points for viewing the documentation.
Note! In TIM reading from console doesn’t work like it would in a real program. In TIM the user input needs to be writen into the input field before running.
Task 5
Create a program that asks for three numbers and prints out the largest and smallest of them. Main is already given; you need create functions Suurin (largest) and Pienin (smallest), both of which take three parameters. In other words, both of these functions can be called so that they're given three integer-type arguments in the function call.
You can assume that the user gives proper integers as input so no exceptions (other than integer given) need to be considered. You can read a number from the console like this:
Console.Write("Give 1. integer >");
int a = int.Parse(Console.ReadLine());
Also think which values it would be good to test these function with, and add your ideas inside the function as a normal comment given with two forward slashes.
Hint! The functions are easy to expand from this idea:
int largest = a;
if ( b > largest ) largest = b;
return largest;
Huom: Alla olevaan vastauslaatikkoon laitetaan vain valmiit funktiot esittelyriveineen.
In the task you get 0.4 points for running the code, 0.5 points for passing the tests (with the Test button) and 0.1 points for viewing the documentation.
Task 6
M: 12. Strings, Jonoja.cs.
Create necessary subroutines so that the following Main function will work as intended:
public static void Main()
{
Console.Write("Give your name in last name first name format: ");
string wholeName = Console.ReadLine();
string lastName = AnnaEkaSana(wholeName); //GiveFirstWord()
string firstName = PoistaEkaSana(wholeName); //RemoveFirstWord()
Console.WriteLine("Your first name is " + firstName +
" and last name is " + lastName);
}
Hint: String class functions IndexOf, Substring and Split. Find the MSDN documentation and figure out how to split a string. The Split function requires a table which we haven’t covered yet.
In the task you get 0.4 points for running the code, 0.5 points for passing the tests (with the Test button) and 0.1 points for viewing the documentation.
Task 7
M: 26.4 Negative binary numbers
In most computers, negative integers are represented using so-called two's complement operation. Using two's complement, a positive integer is converted to a negative by flipping all bits (i.e. 1 \(\rightarrow\) 0 ja 0 \(\rightarrow\) 1) and by adding 1 to the resulting number.
Below is an example of how number -3 is represented in binary using two's complement (using 8-bit integers):
3 is 0000 0011 in binary, so
-3 is computed as follows:
1) flip all bits = 1111 1100
2) +1 = 1111 1101
When converting a two's complement binary number to base 10, inspect the most significant bit (i.e. the leftmost bit) first. If the most significant bit is 0, it is positive and can be directly converted to base 10.
Otherwise, if the bit is 1, it is negative two's complement and it must be converted to a positive number by repeating the two's complement conversion.
Below, binary numbers 0010 1101 and 1101 1111 are interpreted into base 10. Note that we assume that the integers are 8-bits in length and that two's complement representation is in use. You must always know if two's complement is in use! Otherwise you'll get wrong results.
0010 1101 is positive (leftmost bit is 0), so normal base 10 conversion
=> 45
1101 1111 is negative (leftmost bit is 1) and two's complement is in use
=> convert to positive (flip bits and add 1)
=> 0010 0000 + 1 = 0010 0001 and do normal base 10 conversion.
=> 0010 0001 = 33, so the answer is -33
Use the following notation to answer questions e)- and f):
a) 11-22 = -33
TDD 1
If you test at least two functions with an automatic test (ComTest), using Visual Studio or similar you can mark yourself one extra demo point. Tests cannot be the same as the ones you can copy from the TIM version. As an answer you will write which tasks and function/functions operations you have tested. You can also give feedback and improvement ideas for using ComTest.
Note! Programs that only have a Main are very hard to test with the knowledge of this course. For this reason it is advisable to test only functions that clearly return something. Like the ones in tasks 3, 4, 5 and 6 in this demo.
Jos ComTest ei toimi koneellasi, voit Copy/Pasteta ohjelmia alla olevaan laatikkoon ja testata siinä.
Ville 1
Do Ville-exercises 3.3, 3.5, 3.6. 4.1-4.4. Ville’s Substring exercises work the wrong way (the parameters are the ones you would give in Java language, meaning substring(startIndex, endIndex+1), but in C# they are Substring(startIndex, endIndex). The result is the same in both if the startIndex == 0, but in all the other cases they are different. So the line 4 in 3.5 exercise is wrong.). Answer like you should in C# and ignore the "wrong answer". In exercise 3.6 the parameters are according to Java, the index where to start and the index which will not be counted into the substring. For example s = "helpotus"; s.Substring(1,5) ==> "elpo";
As an answer, tell us shortly what you learned from the Ville exercises and whether you found them useful.
Remember the Ville user manual.
B1-2
Take the 2nd demo's example solution Suorakulmiot.cs (requires log-in using university credentials).
First run it as it is. Then alter it so that 30 balls is created around the game field into random places (preferably in random colors). The balls will have random radius between 5 and 30. Test that your changes work. Finally, add one more ball with radius 40 which is white and you can control by "hitting" it using the arrow keys (See how this was done in the previous demo where a snowman was hit around).
Note: TIM doesn't give points in this exercise, so you'll have to give them manually.
Use Custom Points and give yourself 2 points for an answer that fills the requirements and is well documented. For a good attempt which includes moving at least 1 ball or the creation of randomized balls, you can give yourself one point.
Hints for adding the controls you can look from the Pong-tutorial.
B3
You can do subtasks A and B into the same .cs file.
A. Explore the documentation of char type. What do you have to write in place of xxx for the program below to work as intended.
Note: pienena means as lower case and isona means as upper case.
B. Write a program that asks a string from a user and prints whether the string's first character is uppercase ("iso") or lowercase ("pieni").
(Take a look at the lecture material for additinal info.)
G1
Extend the answer to task B1-2 as follows: if you hit another ball with the ball you control in the upper half of the level, the other ball will explode and be removed from the game. Thus, the goal of this game is to blow up all the balls.
Hint: Jypeli wiki
These are the current permissions for this document; please modify if needed. You can always modify these permissions from the manage page.