Professional Documents
Culture Documents
Detyrat Programim Kapitulli 8
Detyrat Programim Kapitulli 8
EXERCISES 8.1
Samuel,Alito,George W. Bush,NJ,2006,0
Henry,Baldwin,Andrew Jackson,PA,1830,1844
In Exercises 1 through 6, determine the first two lines of the new file
created by the code.
1. Dim query = From line In IO.File.ReadAllLines("Justices.txt")
Let data = line.Split(","c)
Let firstName = data(0)
Let lastName = data(1)
Let fullName = firstName & " " & lastName
Let state = data (3)
Select fullName &","& state
IO.File.WriteAllLines("NewFile.txt", query)
Samuel Alito,NJ
Henry Baldwin,PA
IO.File.WriteAllLines("NewFile.txt", query)
Alito was appointed by Bush
Baldwin was appointed by Jackson
In Exercises 11 through 14, describe the new file created by the code.
Assume the file NYTimes.txt contains the names of subscribers to the New
York Times and the file WSJ.txt contains the names of the subscribers to
the Wall Street Journal.
The new file contains the names of the people who subscribe to either
the New York Times or the Wall Street Journal or both.
12. Dim times() As String = IO.File.ReadAllLines("NYTimes.txt")
Dim wsj() As String = IO.File.ReadAllLines("WSJ.txt")
IO.File.WriteAllLines("NewFile.txt", times.Intersect(wsj))
The new file contains the names of the people who subscribe to either
the New York Times or the Wall Street Journal or both.
The new file contains the names of the people who subscribe to the
New York Times but not the Wall Street Journal.
The new file contains the names of the people who subscribe (New
York Times or the Wall Street Journal) and arent (New York times and
Wall Street Journal)
In Exercises 15 through 18, use the file USPres.txt that contains the
names of all the presidents of the United States and the file VPres.txt that
contains the names of all the vice-presidents.
15. U.S. Presidents Write a program that creates a file containing the names
of every president who also served as vice-president. The program also should
display the number of those presidents in a message box.
16. U.S. Presidents Write a program that creates a file containing the names of
every person who served as either vice-president or president. The program also
should display the number of those names in a message box.
Dim president() As String = IO.File.ReadAllLines("US pres.txt")
Dim vice() As String = IO.File.ReadAllLines("VPres.txt")
IO.File.WriteAllLines("Newfile.txt", president.union(visepresident))
Dim query = from line in IO.File.ReadAllLines(Newfile.txt)
Let data =lines.splite(,c)
Let name = data(0)
Let number = name.count
messageBox.show(Cstr( the number of president & Cstr(number))
17. U.S. Presidents Write a program that creates a file containing the names of
every person who served as either vice-president or president, but not both. The
program also should display the number of those names in a message box.
Dim president() As String = IO.File.ReadAllLines("US pres.txt")
Dim vice() As String = IO.File.ReadAllLines("VPres.txt")
Dim unionArray() as string = president.Union(visepresident).ToArray
Dim intersectArray() as string =
president.intersect(visepresident).ToArray
IO.File.ReadAllLines(Newfile.txt,unionArray.Except(intersectArray))
Dim query = from line in IO.File.ReadAllLines(Newfile.txt)
Let data =lines.splite(,c)
Let name = data(0)
Let number = name.count
messageBox.Show( the number of president &number)
18. U.S. Presidents Write a program that creates a file containing the names
of every president who did not also serve as vice-president. The program also
should display the number of those presidents in a message box.
Dim president() As String = IO.File.ReadAllLines("US pres.txt")
Dim vice() As String = IO.File.ReadAllLines("VPres.txt")
IO.File.WriteAllLines("Newfile.txt", president.Except(visepresident))
Dim query = from line in IO.File.ReadAllLines(Newfile.txt)
Let data =lines.splite(,c)
Let name = data(0)
Let number = name.count
messageBox.show ( the number of president & number)
27. Color at the beginning of 1900, a complete box o Crayola crayon had 72 colors
(in the file Pre 1900.txt).During the 1900s ,8 colors were retried (in the files
Retired.txt) and 56 new colors were added(in the file Added.txt).Write a program
that creates a text file listing the post 1990s set of 120 in the alphabetical order.
Dim Crayola () As String = IO. File . ReadAllLines ( "Pre1900 . txt")
Dim Retired() As String = IO.File.ReadAllLines(Retired.txt")
Dim Added() As String= IO.File.ReadAllLines(Added.txt)
Dim exceptCrayola() as String = Crayola.Except(Retired).ToArray
IO.File.WriteAllLines("NewFile.txt", exceptCaryola.Union(Added))
30. Supreme Court Write a program that display the entire contents of the file
Justices.txt in a DataGridView control. See fig 8.5
32.Suprem Court The first Supreme Court justice was appointed in 1789.Write a
program to create a file that lists the years from 1789 through 2012 in which no
Supreme Court justices were appointed.The first four lines of the file will be
1792,1794,1795 and 1797.
Dim query = From line In IO.File.ReadAllLines("Justices.txt")
Let data = line.Split(","c)
LettName = data(0)
Let abbrevitation = data(1)
Let Area = data(2)
Let population = CInt(data(3))
Let yrAppointed=date(4)
Where (yrAppointed >= 1789 And (yrAppointed < 2012)
For I as integer =4 to 49
IO.File.ReadAllLines(NewFile.text,States(i))
33.Supreme Court Write a program to create a file that lists the states that have
produced Supreme Court justices along with the numbe rof justices produced.The
states should be in alphabetical order by thei abbreviations.The first four lines of the
file will be AL,3;AZ,2; and return the number of justices from the state.
Dim query = From line In IO.File.ReadAllLines("Justices.txt")
Let data = line.Split(","c)
LettName = data(0)
Let abbrevitation = data(1)
Let Area = data(2)
Let population = CInt(data(3))
Let yrAppointed=date(4)
Where (yrAppointed >= 1789 And (yrAppointed < 2012)
Order by abrrevation
Select name,abvreavtion ,area, population
IO.File.ReadAllLines(NewFile.text,States(i))
EXERCISE 8.2
4.Assume that the contents of the file Greetings,txt are as shown in the figure 8.8
Hello
Aloha
Bon Jour
10.Redo Exercise 9 with the assumption that the file Ages.txt is not located in the
bin\Debug subfolder of the folder bin
The output is :File ages.txt not found
11.Assume that the contents of the file Greetings.txt are shown in the figure
8.8(page 39)
What is the effect of the following program?
Hello
Aloha
Bon Jour
12.Assume that the file Name.txt contains a list of names in alphabetical order.
What is the effect of the Mystery function?
In Exercises 13 through 18, identify any errors. Assume that the contents
of the file Greetings are as show in Fig.8.8.
13. Private Sub btnDisplay_Click(. . . ) Handles btnDisplay .Click
Dim sw As IO.StreamWriter = IO.File.Appendix(Greetings.txt)
sw.WritenLine(Gutem Tag)
sw.Close()
End Sub
The filespec Greetings . txt should be delimited with quotation marks.
20. Cowboys Suppose the price of saddles is reduced by 20%. Use the file
Cowboys.txt to create a file, Cowboys2.txt, containing the new price list.
21. Cowboys Write a program to add the data Winchester Rifle, 20.50 to the end
of the file Cowboys.txt
23. Cowboys Write a program to request an additional item and price from the
user and then create a file called Cowboy2.txt containing all the information in the
file Cowboy.txt with the additional item (and price) inserted in its proper
alphabetical sequence. See Fig.8.10. Run the program for both following data item:
Boots, 20.00 and Horse, 35.00.
24. Cowboys Write a program to allow additional items and price to be input by
the user and added to the end of the file Cowboy.txt. Include a method to terminate
the process. See Fig.8.11.
25. Cowboys Write a program using a StreamReader that displays the contents of
the file Cowboys.txt In DataGridView control. See Fig.8.12.
In Exercises 27 through 32, write a program to carry out the task without
using arrays or LINQ. Assume that the file Numbers.txt contains a list of
integers.
Or
End Sub
Or
30. Numbers Display the sum of the numbers in the file Number.txt.
1. 7up
No
2. vice president
yes
3. _77
No
4. Fred
Yes
5. xmlTitle
No
6. ?mark
No
9.<city>New York<city>
No
10.<first name>John</first name>
No
11. U.S. Presidents The first two lines of the file AgeAtlnaug.txt.are
George Washington ,57
John Adam, 61
Where each second has two fieldsname of president and his age when
inaafurated. Create a XML file containing these two records.
<?xml version=1.0>
<age_inaguration>
<president>
<name>George Washington</name>
<age>57</age>
</president>
<president>
<name>John Adam</name>
<age>61</age>
</president>
</age_inaguration>
12.Supreme Court The first two lines of the file Justice.txt are
Samuel,alitp,George M. Bush, NJ, 2006,0
Henry,Baldwin,Andrew Jackson,Pa, 1830,1844
Where each record has six fieldsfirst name, last name, appointing president, the
state from which the justice was appointed, year appointed, and year they left the
court. (For current justices, the last field is set to 0.).Create an XML file containing
these two records.
<?xml version=1.0>
<supremeCourt>
<Justice>
<firstname>Samuel</firstname>
<lastname>Alito</lastname>
<AppPres>George M. Bush</AppPres>
<state>NJ</state>
<yrappointed>2006</yrappointed>
</justice>
<Justice>
<firstname>Henry</firstname>
<lastname>Baldwin</lastname>
<AppPres> Adrew Jackson</AppPres>
<state>Pam</state>
<yrappointed>1830</yrappointed>
</justice>
</supremeCourt>
13. U.S. States The total population of the \united States in the year 2000.
Private Sub btnField_Click() Handles btnFind.Click
Dim stateData as XElement = XElement.Load(USStates.xml)
Dim query = From st in StateDate.Descendants(state)
Let name = st.<name>.Value
Let pop = st.CInt(st.<population>.Value)
Let year = CInt(st.<year>.Value)
Where year = 2000
Select pop
End Sub
16. U.S States The state(or states) with the longest name.
Private Sub btnField_Click() Handles btnFind.Click
Dim stateData as XElement = XElement.Load(USStates.xml)
Dim query = From st in StateDate.Descendants(state)
Dim m as integer
Let name = st.<name>.Value
For I as integer = 0 to 49
If (m<name.length))
Select m.max
End Sub
17. U.S States The states with area greater than 100,000 square miles.
Display both the name and area of each states, with the state in decreasing order
by area. See Fig. 8.13.
18. U.S States The states with population less than one million.
Display both the name and population of each state, with the states in
increasing order by population. See Fig. 8.14.
Private Sub btnField_Click() Handles btnFind.Click
Dim stateData as XElement = XElement.Load(USStates.xml)
Dim query = From st in StateDate.Descendants(state)
Let name = st.<name>.Value
Let pop =CInt(st.<area>.Value)
Order by pop ascending
Where pop<1000000
Select namearea
dgvStates.DataSource = query.Tolist
dgv.States.CurrentCell = Nothing
dgv.Columns(name).HeaderText = State
dgv.Columns(pop).HeaderText= pop
End Sub
19. U.S States The states whose name contain the most different
vowels. See Fig. 8.15.
20. U.S States The states whose abbreviations are different than the
first two letters of their names. Display both the name and abbreviation of
each state in alphabetical order by the abbreviation. See Fig. 8.16.
23. Colleges Write a program that fills a list box with the years
before 1800 in which colleges were founded. When the user selects a year,
the colleges founded that year should be displayed in another list box. See
Fig. 8.20.
25. U.S. States The CSV file Senate113.txt contains a record for each
member of the 113 U.S. Senate. (the 113th U.S. Senate was installed in
th
2013.) Each record contains three fields name, state, and party affiliation.
Some records in the files are
John McCain, Arizona, R
Bernie Sanders, Vermont, I
Kirsten Gillibrand, New York, D
(a) Write a program that uses the file Senate113.txt and creates an
XML file containing the same information
<?xml version=1.0>
<Senate113>
<Senate>
<name>John McCain</firstname>
<state>Arizona</state>
<partyaffiliation>R</partyaffiliation >
</Senate>
<Senate>
<name>Bernie Sanders </firstname>
<state>Vermont</state>
<partyaffiliation>I</partyaffiliation >
</Senate>
<Senate>
<name>Kristen Gillibrand </firstname>
<state>New Yorl</state>
<partyaffiliation>D</partyaffiliation >
</Senate>
</Senate113>
(b) Write a program that uses the XML file from part (a) to display
the names, states, and party affiliation of all the senators in a
DataGridView orderd by their state. See Fig. 8.22. The two senators
from each state should be ordered by their first names.
(c) Dim college as XElement = XElement.Load (senate)
Private Sub btnFind_Click() Handles btnFind.Click
Dim query = from cg in senate.Descendants(senate)
Let name = cg.<name>.Value
Let state = cg.<state>.Value
Let partyaffilation = cg.<partyaliffation>.values
Select name,state, yearaliffation
dgvStates.DataSource =query.Tolist
dgv.States.CurrentCell = Nothing
dgv.Columns(name).HeaderText = College
dgv.Columns(state).HeaderText= State
dgv.Columns(partyaliffation).HeaderText= Partyaliffation
End sub
26. Baseball The file Top23HR.xml contains statistics for the top
25 home-run hitters of all time in major league baseball. The first nine lines
of the file are shown in Fig. 8.23.
(a) Write a program that displays the contents of this files in a DataGridView
contron in descending order by the number of home runs hit. See Fig.8.24.
(b) Write a program that uses the file Top25HR.xnl and creates a CSV file
containing the same information.