Basic List
Practice
Complete the following exercises related to List
objects.
Exercise 1: Bugs
Visit each of the following Repls. Start by trying to run the programs, and make a note of any error messages received. Then, fork the Repl and try to fix the code!
Exercise 2: A Playlist of Your Favorite Songs
Think of some of your favorite songs. Create a program that adds those songs to a list, and then displays them!
- Create a new project in Repl
- At the top of the file, add
using System.Collections.Generic;
- In the body of the
Main
method, print out a message that says "My Favorite Songs" - Under that, initialize a new
List<string>
object namedsongs
List<string> songs = new List<string>();
- Under that, add a few of your favorite songs to the list
songs.Add("9 to 5 by Dolly Parton"); // etc
- Under that, create a
foreach
loop to loop through eachstring
in thesongs
listforeach (string song in songs) { }
- In the body of the
foreach
loop, print out thesong
Console.WriteLine(song);
Run the program, and make sure that all the songs in the list appear!
Exercise 3: Spaced Out Text
Sometimes, people on the internet like to display messages with extra spaces between each text character. For example, instead of writing THE VOID
, they might write T H E V O I D
. Create a program that allows the user to input some text, and gives them the "spaced" version of that text!
Background - string
Objects
For this to work, it is important to know that a string
object can actually be treated as a List
of char
objects. A char
is just like a string
, only it contains one single character instead of multiple characters. This means that, given a string
variable named myString
it is possible to do something like this:
foreach (char c in myString)
{
Console.WriteLine(c);
}
The code above would print out each individual character in the string
!
Implementation
Follow the steps below to implement the "Spaced Out Text" program.
- Create a new project in Repl
- In the body of the
Main
method, print out a message that says "Enter some text to space out" - Under that, create a new
string
variable namedtext
- Set the
text
variable to the result ofConsole.ReadLine()
- Under that, create another
string
variable namedspacedText
- This will hold the "spaced out" text
- Set the
spacedText
variable to be an empty string (""
) - Under that, create a
foreach
loop that will loop through eachchar c
in thetext
stringforeach (char c in text) { }
- In the body of the
foreach
loop, addc
and an extra space to thespacedText
variablespacedText += c + " ";
- Under the
foreach
loop, print out the finalspacedText
value
Run the program. Make sure it is possible to enter some text, and see the spaced out version of it!
Exercise 4: Course Ranking
Think about all of the courses you take in school. Order them by how much you like them, with number 1
being the top course. Create a program that will allow the user to check which course corresponds with each ranking.
- Create a new project in Repl
- At the top of the file, add
using System.Collections.Generic;
- In the body of the
Main
method, print out a message that says "Course Ranking" - Under that, initialize a new
List<string>
object namedcourses
List<string> courses = new List<string>();
- Under that, add each of your courses in order, with the top course coming first
courses.Add("Geometry"); // etc
- Under that, use
Console.WriteLine
to ask the user which ranking they would like to see - Under that, create a new
int
variable namedrank
- Set the
rank
variable toConvert.ToInt32(Console.ReadLine())
- Under that, create a new
int
variable namedrankIndex
, and set it torank - 1
- Note that this is necessary because
List
objects start index at0
- Note that this is necessary because
- On the next line, create a new
string
variable namedcourseAtRank
- Set the
courseAtRank
variable to the element of the list at therankIndex
indexstring courseAtRank = courses[rankIndex];
- On the next line, print out the value of the
courseAtRank
variable
Run the program and verify that the proper courses appear based on what the user enters!
Bonus - Limited Ranking
Currently, if the user enters something outside of the range of the course ranking list, the program will fail. Update it so that it checks whether the rank the user enters is within range. If it is not, display a message saying "Course Ranking Not In Range."