Question: Part 1 1. Overview In this lab exercise youll work with Visual Studio and C# to create a console application. This lab exercise is based

Part 1

1. Overview

In this lab exercise youll work with Visual Studio and C# to create a console application. This lab exercise is based on Lecture 2.

2. The Hullspeed Program in VS 2013

In the previous exercise you created a Sailboat class and a main program to test it. What were going to do is take this code and extend it.

Start by creating a new console application in Visual Studio; as in the previous exercise, Id recommend a project location on the local hard drive (e.g. C:\YourName\Demo02). Delete the default Program.cs file that VS creates (click on icon in Solution Explorer and press Delete). Next, add the two source code files from the previous exercise; to add existing files to a Visual Studio project, use the Project menus Add Existing Item command. Note that when you add an existing file to a project, Visual Studio will make a copy of the file and store it in the project folder. Its important to realize that by default Visual Studio does not share files between projects, but makes copies. [ If you want to share a file, click Add As Link instead of Add. ]

Okay, run and test your program, it should work exactly as before. Add code to keep the console window open until the user presses Enter.

3. Exploring VS 2013 Project Folders

Exit Visual Studio, and go find the project folder for the application you just created. Browse the folder and sub-folders. In particular, drill down into the bin\Debug folder, and double-click on the .exe file for your program (ignore the .vshost.exe file, which is a special Visual Studio program used when you run from within VS). Your program should run exactly as it did before.

While you are here in the bin\Debug sub-folder, right-click on the folders background and create a new Text Document. Name this file sailboats.txt. Double-click on the file to open in Notepad, and add the following 6 lines:

Archimedes II

36.25

Winddancer

68.0

Saudade

48.0

Note that by placing the name and length on separate lines, it will be easier to read this file from our program (i.e. a simple ReadLine( ) will suffice, we wont need to Split the line and deal with individual tokens as shown on page 2-13). Save and close this file.

Re-open your program in Visual Studio 2013 this is best done by double-clicking on the .sln file at the root of the project folder. [ If you cant see the file extensions, go to the explorers Tools menu, Folder Options, View tab, and uncheck the option for Hide extensions for known file types. ]

4. Reading Sailboat Data

Okay, modify the main method to open the sailboats.txt file, read a sailboats name and length, create a sailboat object, and store this object into an ArrayList. Use page 2-13 as a guide, except read each input value using reader.ReadLine( ) instead of the line.Split approach the later is only used when multiple values are stored on the same line. Also, note that the ArrayList class resides in the System.Collections namespace, so youll need to refer to it by its full name: System.Collections.ArrayList. Likewise, the StreamReader class resides in the System.IO namespace, so use System.IO.StreamReader.

After you have read all the data and created a collection of sailboat objects, close the file and use a for loop to iterate through the collection, outputting the name and hullspeed for each boat; use pages 2-14 and 2-15 as a reference. Run and test. Add another boat to the sailboats.txt file, and run again.

If you havent already, turn your file I/O code into a static method, much like the method shown on page 2-13. Modify the main program accordingly, run and test.

Part 2

1. Overview

Use the Demo2 solution as the starting point for this lab. You are being asked to add additional sailboats to the input file and added a Width value for each sailboat. Then enhance the Sailboat.cs Class definition file, to store the Width and use it to adjust the HullSpeed calculation to make it more accurate. See details below.

2. The Hullspeed Program in VS 2013

In the previous Demo 2 program you created a Sailboat class and a Main method to test it. You added read capability using StreamReader to read sailboats.txt data into the program, created Sailboat objects with the data, and stored those objects in an ArrayList for later reference, to display each sailboat object's data elements

Start by creating a new console application in Visual Studio; as in the previous exercise, Id recommend a project location on the local hard drive (e.g. C:\YourName\Lab1). Delete the default Program.cs file that VS creates (click on icon in Solution Explorer and press Delete). Next, add the two source code files (Sailboat.cs, Main.cs) from Demo2; to add existing files to a Visual Studio project, use the Project menus Add Existing Item command. Note that when you add an existing file to a project, Visual Studio will make a copy of the file and store it in the project folder. Its important to realize that by default Visual Studio does not share files between projects, but makes copies. [ If you want to share a file, click Add As Link instead of Add. ]

Be sure to copy the sailboats.txt file from /bin/debug of demo2 to /bin/debug of lab1.

Okay, run and test your program, it should work exactly as before.

I

3. Exploring VS 2013 Project Folders

Exit Visual Studio, and go find the project folder for the application you just created. Browse the folder and sub-folders. In particular, drill down into the bin\Debug folder, and double-click on the .exe file for your program (ignore the .vshost.exe file, which is a special Visual Studio program used when you run from within VS). Your program should run exactly as it did before.

While you are here in the bin\Debug sub-folder, right-click on the folders background and edit the sailboats.txt file. Double-click on the file to open in Notepad, and modify the contents to reflect the data below.

Archimedes II

36.25

14.0

Winddancer

68.0

17.0

Saudade

48.0

14.0

Endeavor

45.0

15.0

Catalina 315

31.0

15.0

1.) Add two more boats to this file as above (Endeavor and Catalina 315)

2.) Add a second number, (Width) after the Length number for each sailboat.

Example: (Name: Endeavor Length: 45 Width: 15)

Note that by placing the name and length on separate lines, it will be easier to read this file from our program (i.e. a simple ReadLine( ) will suffice, we wont need to Split the line and deal with individual tokens as shown on page 2-13). Save and close this file.

Re-open your program in Visual Studio 2013 this is best done by double-clicking on the .sln file at the root of the project folder. [ If you cant see the file extensions, go to the explorers Tools menu, Folder Options, View tab, and uncheck the option for Hide extensions for known file types. ]

4. What to do with this new piece Data (Width)?

Modify Sailboat.cs.

First capitalize the method name correctly. It should be HullSpeed not Hullspeed.

Create a class variable to store Width.

Adjust the class constructor to accept an incoming Width value and store it as a class variable. (Model what was done for the Length property.)

Modify the HullSpeed( ) calculation. Use the 1.34 coefficient by default. But override this value with 1.44 If (Length divided by Width is greater than 3). This will make the hull speed calculation more realistic. (A narrower boat relative to its overall length would cut through the water better than a wide one would.) It's really more complicated than this. There are displacement hulls vs. planing hulls, keels make a difference too.

Modify Main.cs.

Note that an additional call to reader.ReadLine( ) will be needed to read Width. Store this value and pass it to the Sailboat constructor after Length in the parameter list.

Also modify the code in the loop at the end that prints the Sailboat class data to print Width to the console in addition to the other fields.

5. Reading Sailboat Data

Okay, modify the main method to open the sailboats.txt file, read a sailboats name and length, create a sailboat object, and store this object into an ArrayList. Use page 2-13 as a guide, except read each input value using reader.ReadLine( ) instead of the line.Split approach the later is only used when multiple values are stored on the same line. Also, note that the ArrayList class resides in the System.Collections namespace, so youll need to refer to it by its full name: System.Collections.ArrayList. Likewise, the StreamReader class resides in the System.IO namespace, so use System.IO.StreamReader.

After you have read all the data and created a collection of sailboat objects, close the file and use a for loop to iterate through the collection, outputting the name length, width and hullspeed for each boat; use pages 2-14 and 2-15 as a reference. Run and test. Add another boat to the sailboats.txt file, and run again.

If you havent already, turn your file I/O code into a static method, much like the method shown on page 2-13. Modify the main program accordingly, run and test.

6. Output proof

Show the console output (screen shots pasted in a word document named Output.doc in /bin/debug folder) under two scenarios:

1. 1.34 coefficient in HullSpeed calculation is used all the time

2. 1.44 is used in lieu of 1.34 if the Length/Width ration is > 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!