Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c# a.Notice how the all code is in the Main() method and there is a lot of repeated code. Your task is to modularize this

c#

a.Notice how the all code is in the Main() method and there is a lot of repeated code. Your task is to modularize this program by utilizing methods. Read the comments in the code to better understand what methods to create and then create the following 3 methods.

b. Create a method called getSize() and move the appropriate code to that method.

c. Create a method called computeNumberOfCans() and move the appropriate code to that method.

d.Create a final method called computeCost() and move the appropriate code. Make sure you call all your new methods inside Main().

the code given to do this is below.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace paintCan { class Program { static void Main(string[] args) { int height = 0, width = 0, length = 0; int numCans; /* Prompt the user for the height, width, and length of the room * Input must be validated. We need a try/catch block to catch if * the user does not input an integer. * We need to make sure that the size is greater than 0 also. * * There is a lot of repeated code here. Get rid of all the repeated code * by creating a method called getSize() and move the appropriate code to that method. * This method should accept one parameter, a string named caption. The value * will be either "height", "width", or "length". * The method should return an integer with the size that user the inputs. * * You will call this method 3 times, once for the height, once for the length, * and once for the width in order to get the 3 dimensions of the room. */ bool invalidValue = true; while (invalidValue == true) { Console.Write("Enter the height of the room: "); try { height = Convert.ToInt32(Console.ReadLine()); if (height > 0) invalidValue = false; else Console.WriteLine("Invalid height. Try again."); } catch { Console.WriteLine("Invalid height. Try again."); } } invalidValue = true; while (invalidValue == true) { Console.Write("Enter the width of the room: "); try { width = Convert.ToInt32(Console.ReadLine()); if (width > 0) invalidValue = false; else Console.WriteLine("Invalid width. Try again."); } catch { Console.WriteLine("Invalid width. Try again."); } } invalidValue = true; while (invalidValue == true) { Console.Write("Enter the length of the room: "); try { length = Convert.ToInt32(Console.ReadLine()); if (length > 0) invalidValue = false; else Console.WriteLine("Invalid length. Try again."); } catch { Console.WriteLine("Invalid length. Try again."); } } /* Compute how many paint cans are needed. Assume one can of paint can cover 365 square feet of wall. * Two of the walls will have a square footage of height * length, the other two height * width, and * the ceiling will be length * width. * If you just divide the squareFootage / 365 this will truncate the result. You need to cast * your integers as doubles. * Next, you need to round up. If 3.2 paint cans are required you really want 4. We must use * Math.Ceiling for this. * * This turns out to be a more involved calculation, so create a method that will modularize * this task outside of main. * Create a method called computeNumberOfCans(). It must take in height, width, length as parameters * and return an integer numCans. Move the appropriate code to that method and call it from Main */ int twoWalls = height * length; int otherTwoWalls = height * width; int ceiling = length * width; double exactCans = (double)(2 * twoWalls + 2 * otherTwoWalls + ceiling) / 365.0; numCans = (int)Math.Ceiling(exactCans); /* Compute the total cost of paint. Assume one paint can costs $4.25. * If you are buying more than 10 paint cans you get a 10% discount. * If you are buying more than 5 paint cans (but less than or equal to 10) you get a 6% discount * Move this logic into its own method called computeCost(). * Figure out what parameters this method needs to work, create it, and call it from Main. */ const decimal COST_OF_PAINT_CAN = 4.25M; decimal totalCost = COST_OF_PAINT_CAN * numCans; if (numCans > 10) { totalCost *= 0.9M; // 10% Discount 1 - 0.10 = 0.90 } else if (numCans > 5) { totalCost *= 0.94M; // 6% Discount } /* Finally print a summary of the results * You can leave this code in Main */ Console.WriteLine(); Console.WriteLine("Height of room: {0}", height); Console.WriteLine("Width of room: {0}", width); Console.WriteLine("Length of room: {0}", length); Console.WriteLine("Number of paint cans needed: {0}", numCans); Console.WriteLine("Total Cost: {0}", totalCost); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

3. Are our bosses always right? If not, what should we do?

Answered: 1 week ago