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.

Huomautus: Lopulliset demotehtävät julkaistaan aina tasan viikkoa ennen deadlinea. Demotehtäviin saattaa ennen sitä tulla muutoksia tai korjauksia. Tehtäviä voi tulla lisää tai tehtäviä voi poistua. Tehdessäsi tehtäviä ennen virallista julkaisua, ota tekemästäsi koodista aina varmuuskopio omalle tietokoneellesi.

Return demo tasks before 11:59 on Monday.

Learning goals

# Oppimistavoitteet

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.).

# d4tunnit

Huomautus custom points -pisteistä

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 :-)

# demo4teht1

Tehtävä 2

Lue ensin: 7.7 Operaattorit

Alla on C#-kielisiä lausekkeita ja lauseita. Vastaa kuhunkin kohtaan.

Huomaa! Kuhunkin kohtaan on vain yksi vastausyritys.

# demo4teht2_1
# demo4teht2_2
# demo4teht2_3
# demo4teht2_4

Seuraavissa kohdissa on useita toisistaan erillisiä lauseita. Eri kohdissa olevia lauseita ei suoriteta peräkkäin.

# demo4teht2_5
# demo4teht2_6
# demo4teht2_7
# demo4teht2_8

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 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.

Ohjeita ja vinkkejä tehtävän tekemiseen

Kun olet tehnyt tehtävän, kopioi funktio esittelyriveineen vastauslaatikkoon.

Arviointi: Koodin ajamisesta 0.4 p., testien läpäisemisestä 0.5 p. ja dokumentaation lukemisesta 0.1 p. Dokumentaation lukemisella tarkoitetaan ajamisen seurauksena syntyneen Document-linkin klikkaamista ja ohjelmadokumentaation tosiasiallista lukemista.

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.

# demo4teht3

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.

# demo4teht4

Task 5

M: 13. If statements.

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:

Hint! The functions are easy to expand from this idea:

int largest = a;
if ( b > largest ) largest = b;
return largest;

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.

Suurin

# demo4teht5

Pienin

# demo4teht5b

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.

VainEkaSana

# VainEkaSana

PoistaEkaSana

# PoistaEkaSana

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

Tehtävä 7 a, b, c ja d: (0,4p)

# d3teht1abcd

Use the following notation to answer questions e)- and f):

a) 11-22 = -33

Tehtävä 7 e: (0,1p)

# d3teht1e

Tehtävä 7 f: (0,1p)

# d3teht1f

Tehtävä 7 g: (0,1p)

# d3teht1g

Tehtävä 7 h: (0,1p)

# d3teht1h

Tehtävä 7 i: (0,1p)

# d3teht1i

Tehtävä 7 j: (0,1p)

# d3teht1j

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.

# demo4tdd

Jos ComTest ei toimi koneellasi, voit Copy/Pasteta ohjelmia alla olevaan laatikkoon ja testata siinä.

# ComTestPohjaCS

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.

# demo4ville1

B1

Tehtävä: Lue seuraavat dokumentaatiot ja aliohjelmien esittelyt. Jokaisessa niissä on jotain outoa. Parannettavaa voi olla (mutta ei välttämättä ole) paluuarvon tyypissä, funktion nimessä, parametrien määrässä ja niiden nimissä tai tyypeissä, tai itse dokumentaatiokommenteissa. Myös kokonaisia sanoja voi puuttua esittelyriviltä tai dokumentaatiosta.

Kirjoita vastauslaatikkoon mitä korjattavaa löysit ylläolevasta koodista. Huomaa, että aliohjelmien toteutusta ei tässä arvioida eikä edes näytetä. Tee tulkintasi dokumentaation ja esittelyrivin perusteella.

Arviointi: Täysiä pisteitä varten jokaisesta kohdasta pitäisi löytyä vähintään kaksi korjattavaa asiaa.

A. Itseisarvo

/// <summary> Funktio palauttaa annetun luvun itseisarvon. </summary>
/// <param name="luku"> Luku, josta itseisarvo halutaan ottaa. </param>
/// <returns> Luvun itseisarvo. </returns>
public static int palautaitseisarvo(double luku)
{
  ...
}
# demo4tehtb1a

B. Lyhyempi sana

/// <summary> Funktio palauttaa kahdesta sanasta lyhyemmän. 
/// Sanojen ollessa yhtä pitkiä palautetaan ensimmäinen. </summary>
/// <param name="sana1"> Ensimmäinen sana. </param>
/// <param name="sana2"> Toinen sana. </param>
/// <returns> Lyhyempi sana. </returns>
void string LyhyempiKahdestaSanasta(char sana1 char sana2)
{
  ...
}
# demo4tehtb1b

C. Palindromi

/// <summary> Funktio kertoo, onko sana palindromi. </summary>
/// <param name="sana"> Tutkittava sana. </param>
/// <returns> Onko palindromi. </returns>
public static string Palindromi(bool)
{
  ...
}
# demo4tehtb1c

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.

# demo4tehtb2

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.

# demo4tehtb3a

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.)

# demo4tehtb3b

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

# demo4g1

These are the current permissions for this document; please modify if needed. You can always modify these permissions from the manage page.