![]() ![]() cs Class file will generally have a constructor, Variables (for object state), and Methods (for object behavior). (for example about half of MapLocation should be in a new Map class) Yes a. The data model being a little lacking is causing you to have to resort to a procedural thinking for some of the concepts. This is especially important when you are working with a team and other people will be developing on the same project. Like i said it is ok here on this small app but when you get to more complex applications you could have a ton of custom exceptions and if they were all on one file and you could have to scroll through hundreds of lines of code to find what you are looking for. This would be better in two separate files (BattleshipException.cs and OutOfBoundsException.cs) with those files inside a folder called 'Exceptions'. But the only file you have with multiple classes is the Exception.cs. It should have stuff like Players (also another object id suggest), a Map (same thing here), etc. Have your Game.cs/class as the engine of the game. Look at what is in your Main() method as a large group of properties and methods that need to be sorted into categories. Create a Program.cs that contains only the Program class and Main() method. I would start with dealing with the structure fist. use names like 'isTouching' only for boolean values (this is actually used as a bool later in the code just with 1 and 2 instead of true and false).That way it will read a little better when dealing with the Ship Object. when creating a ship instead of copying the values from the MapLocation object, just store the whole MapLocation as a custom field.It could use a couple more classes to encapsulate most of whats is going on int the Main() method.While they are small classes and it would be fine in this instance, but its best practice for larger projects Grouping the custom exceptions together should be done with a folder. one class per file *Note this is a general rule so it does have its exceptions.The Objects you have are modeled well and encapsulate the functionality pretty well. I am sorry to hear that and wish you a speedy recovery! Life > code hahaĪnyway as for the code, this is definitely heading in the right direction. Console.ReadLine() should wait for input anyway so they may not be needed. Lastly, 'while(true)' is valid but bad form, you will be able to find better ways to control the logic flow (ones that don't have inherent risks of an infinite loop : ) ). Try creating classes that model objects using the properties as the dimensions and the methods to do all the computations. Try to think about the objects involved, namely triangles, circles, and rectangles (being squares are rectangles you can use some inheritance magic here). Secondly, this is a very procedural approach, the classes you have are static helper classes that handle the computations and the main method handles the interaction with the user. if there is more than trivial logic it should be in its own method at the very least (better if its in another class I dont like having other methods on the Main.cs to keep the whole file as minimal as possible). Geometry calculations are common, examine if there is open source libraries or libraries within the JDK that perform these calculations.Now I am not sure what the instructions for the calculator are specifically but i have a few pointers just based on what you have.įirst, try to keep you Main() method to a minimum.Breaking it down Geometry class public static class Geometry Output Geometry Calculator Next write a program to test the class, which displays the following menu and responds to the user’s selection:ĭisplay an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. The methods should display an error message if negative values are used for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height. The method should return the area of the triangle. A static method that accepts the length of a triangle’s base and the triangle’s height.A static method that accepts the length and width of a rectangle and returns the area of the rectangle.Use Math.PI for π and r for the radius of the circle.A static method that accepts the radius of a circle and returns the area of the circle.Design a Geometry class with the following methods: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |