I just built this program using Xamarin Studio. It calculates the areas of two different pizzas and tells you which is bigger. It asks you for the shape of the pizza - it only takes "circle" or "square" as shapes, and the side length/radius - called the "length" - of each pizza.
Here's the code:
DISCLAIMER: I kind of stole the idea from my textbook, "Computer Concepts 2014" by Parsons & Oja.Code:using System; // This tells .Net/Mono we want to use the "System" namespace. namespace MyFirstCApp // This is a namespace, a place wherein (as you can see) we put classes. { class MainClass { public static void Main (string[] args) { Pizza pizza1 = new Pizza(); Console.WriteLine ("Pizza 1 shape?"); pizza1.shape = Console.ReadLine (); Console.WriteLine ("Pizza 1 length?"); pizza1.length = Convert.ToDouble (Console.ReadLine ()); Pizza pizza2 = new Pizza (); Console.WriteLine ("Pizza 2 shape?"); pizza2.shape = Console.ReadLine (); Console.WriteLine ("Pizza 2 length?"); pizza2.length = Convert.ToDouble (Console.ReadLine ()); Pizza bestDeal = Pizza.bestDeal (pizza1,pizza2); String output; if (bestDeal == pizza1) { output = "Pizza 1 is the best deal."; } else if (bestDeal == pizza2) { output = "Pizza 2 is the best deal."; } else { output = "Both pizzas are the same deal."; } Console.WriteLine (output); } } class Pizza { public String shape; public double length; public double getArea() { double a = Math.Pow (length, (double)2); if (shape == "rectangle") { return a; } else { return a * Math.PI; } } // The "static" keyword allows us to call bestDeal without creating a new Pizza. public static Pizza bestDeal(Pizza pizza1,Pizza pizza2) { Console.WriteLine (pizza1.getArea ()); Console.WriteLine (pizza2.getArea ()); if (pizza1.getArea () > pizza2.getArea ()) { return pizza1; } else if (pizza1.getArea () == pizza2.getArea ()) { return null; } else { return pizza2; } } } }




Reply With Quote