G+_J. Peter Haliburton Posted March 24, 2014 Share Posted March 24, 2014 After going through a few episodes of Coding 101, and watching a few of the Channel 9 videos, I thought I would try to write a simple program. Turns out it was not so simple, and I had to figure out how to do a lot of things which I had not yet seen covered. Got the program working, but I'd like someone to check it over to see if I could have done something differently/better. namespace HullSpeedConsole { class Program { static void Main(string[] args) { Console.WriteLine("What is the waterline length of your displacement vessel in feet?"); string lwlIn; double lwl, speed; lwlIn = Console.ReadLine(); lwl = double.Parse(lwlIn); speed = Math.Round(1.34 * Math.Sqrt(lwl),2); Console.WriteLine("Your boat has calculated maximum speed of " + speed + " knots."); Console.ReadLine(); } } } Link to comment Share on other sites More sharing options...
G+_Charles Kelly Posted March 24, 2014 Share Posted March 24, 2014 Your code looks good. Don't forget to include comments. Link to comment Share on other sites More sharing options...
G+_Volkan Paksoy Posted March 24, 2014 Share Posted March 24, 2014 Good job. Starting from scratch is daunting sometimes and you managed to overcome that. As an improvement I'd suggest to check the input first before converting to double so that if the user enters a wrong (i.e. 1 meter) value it doesn't crash. You can use double.TryParse for example. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 24, 2014 Share Posted March 24, 2014 I would also suggest naming your variables something easy to understand for someone who doesnt know about the subject of the code. For example lwlIn means nothing to me so if I was to pick up your code at a later time it makes it harder to follow the code. Its easy enough on a small snippit of code but as it gets larger it gets harder to follow to the easier you name things the better. Link to comment Share on other sites More sharing options...
G+_J. Peter Haliburton Posted March 24, 2014 Author Share Posted March 24, 2014 Thanks for the comments. Here is the code commented: static void Main(string[] args) { // Call to console for the waterline length of the boat in feet from the user. Console.WriteLine("What is the waterline length of your displacement vessel in feet?"); // Assign Length WaterLine Input (lwlIn) to String. string lwlIn; // Assign length waterline (lwl), and calculated maximum speed in knots to Double double lwl, speed; // Obtain the length subitted by user from the console. lwlIn = Console.ReadLine(); // Convert the waterline length string to a double for use in the sqrt function. lwl = double.Parse(lwlIn); // Calculate the maximum speed of the boat rounded to 2 decimal places. speed = Math.Round(1.34 * Math.Sqrt(lwl),2); // Display the output from the formula to the user in console. Console.WriteLine("Your boat has calculated maximum speed of " + speed + " knots."); // Keep window open until [Enter] is hit. Console.ReadLine(); } LWL is the standard abbreviation for waterline length of a vessel. "In" was added to get the string number provided from the console so that is could be converted to a double. Double was a new term to me. I ran across it when that was what the sqrt function required. I had tried to use float, but it didn't like it. Having started dabbling in Python before C101 introduced me to C#, I had used "Math." before, and was almost surprised that the same thing worked here. I guess there is a lot common between languages. The next thing I should probably work on is making sure that a valid number is provided. I could make a metric version too, if I convert all my formulas. I have a bunch of them in a spreadsheet for determining various things about sailboats. I eventually want to make a stand-alone app I can give people. Link to comment Share on other sites More sharing options...
G+_J. Peter Haliburton Posted March 24, 2014 Author Share Posted March 24, 2014 Here is the code checking that a number was entered, not text or a mix. I almost understand it all too. :-) namespace HullSpeedConsole { class Program { static void Main(string[] args) { // Call to console for the waterline length of the boat in feet from the user. Console.WriteLine("What is the waterline length of your vessel in decimal feet (eg. 27.36)?"); // Assign Length WaterLine Input (lwlIn) to String for input from console. string lwlIn; // Assign length waterline (lwl) and calculated maximum speed in knots to Double double lwl, speed; // Obtain the value submitted by the user from the console. lwlIn = Console.ReadLine(); // Test that a usable number was provided and // Convert the waterline length string to a double for use in the sqrt function. bool testIn = double.TryParse(lwlIn, out lwl); if (testIn == false) { // Print a warning to console. Console.WriteLine("That was not a valid number. Hit [Enter] to close window."); // Keep window open until [Enter] is hit. Console.ReadLine(); } else { // Calculate the maximum speed of the boat rounded to 2 decimal places. speed = Math.Round(1.34 * Math.Sqrt(lwl), 2); // Add a blank line. Console.WriteLine(); // Display the output from the formula to the user in console. Console.WriteLine("Your boat has a calculated maximum speed of " + speed + " knots."); // Add a blank line. Console.WriteLine(); Console.WriteLine("Hit [Enter] to close window."); // Keep window open until [Enter] is hit. Console.ReadLine(); } } } } Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 24, 2014 Share Posted March 24, 2014 Nice. Im always lazy and never validate the input for programs that only I am going to be using as Ive been creating quick applications here and there for stuff I need. But the Japanese verbs app I put up earlier today I did sanitise the input for others too use. Its good habit but since I have only been programming a few weeks its not my main concern until I start making public applications. Youre certainly going the right route though as you start to understand more the more you program and if there is anything you dont understand then post on here and youll always get help. I have notifications turned on so I can help when ever I can. When you start getting in to functions / methods you could create one method to run metric and the other non metric. Have fun! Link to comment Share on other sites More sharing options...
G+_Nevrin O Posted March 29, 2014 Share Posted March 29, 2014 You definitely could have done it different, but better? Well with this simple of a program its probably more of a matter of opinion what would be better. You could reduce the number of lines to something like: namespace HullSpeedConsole { class Program { static void Main(string[] args) { Console.WriteLine("What is the waterline length of your displacement vessel in feet?"); Console.WriteLine("Your boat has calculated maximum speed of " + Math.Round(1.34 * Math.Sqrt(double.Parse(Console.ReadLine())), 2) + " knots."); Console.ReadLine(); } } } But is it better to use less lines? not always. The code i have might technically use less memory (no variables being addressed into memory) and run a little faster (as i said with a program this simple its advantages are basically null) but personally I prefer something more like the way you wrote it cause it is easier to read and find bugs and nether have there inputs sanitized which is a very good habit to get into. Link to comment Share on other sites More sharing options...
G+_J. Peter Haliburton Posted April 6, 2014 Author Share Posted April 6, 2014 That is much more compact! Link to comment Share on other sites More sharing options...
Recommended Posts