Results 1 to 7 of 7

Thread: Pizza comparison program for beginners

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    183

    Pizza comparison program for beginners

    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:
    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;
    			}
    		}
    	}
    }
    DISCLAIMER: I kind of stole the idea from my textbook, "Computer Concepts 2014" by Parsons & Oja.
    Do not read this sentence.
    You read that last one, didn't you?
    Darn. You now read the previous two.

    Check out my pins on Pinterest!

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Re: Pizza comparison program for beginners

    Do you have a question regarding this code or something?

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Pizza comparison program for beginners

    Actually... - and this is being pedantic, but it's important - it only accepts "rectangle" as a valid pizza shape ... treats it like square though... all other "shape" entries are treated as circle. From an efficiency standpoint, I don't care for the repeated calls to getArea... I'd call it once for each pizza, store the results, then use those in all of the calculations that follow. I also wouldn't use Convert.ToDecimal especially when reading directly from the user's console (now, I'm just nit-picking, since I know (or hoping) that this is just an example, but it'd important to know what's wrong in the event someone else comes along and tries to use it) ... but rather read in the user entry into a stirng var, then use TryPArse to coerse the value out... Otherwise an exception would occur if I entered "twelve" for a side.

    For an example to use as a base for objectfying objects ... ??? ... it's not a bad start. It could be worse.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    5

    Re: Pizza comparison program for beginners

    You could store all the pizzas in memory using LINQ in C# and then be able to query them because the program itself is being treated like a database. I.e., source code as database. I am not quite sure that is a path we developers would want to take. Need reexamine that concept LINQ, Lambda expressions etc that purportedly use code to query data within source code without needing to store data in external database There seem to beobvious advantages.
    can someone explain

    Blog:http://www.mohanarun.com/wordpress

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Pizza comparison program for beginners

    Yeah, that needs to be reexamined because that's not exactly right. LINQ can be used to query data in memory... it doesn't necessarily need to be in source code... it could have come from a text file, a database, or generated on the fly. How the data is created is irrelevant. Lambdas are a different animal all together, while employed by LINQ, their use is not exclusive to LINQ. I've used lambdas quite extensively before using just generics and a few extensions, with nary a LINQ command in sight. As for advantages - I find that I use them most when I need to work with a subset of the data I have. Instead of going back to the database and retrieve data I already have, I'll use LINQ to pull out just the records I need to work with. I suppose in some cases I could just filter the data... but that only works if the data container supports filtering - DataGridView allows for this. But I don't always use a DGV. And Lists don't support filtering... but they do support IQueryable which is what lets me apply a selection to it, giving me back just the records I want to work with.

    LINQ and lambdas can be powerful tools when applied correctly. But that also means they can be applied in very irresponsible and inappropriate ways. But that goes for just about anything in programming. And carpentry.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Registered User
    Join Date
    Sep 2015
    Posts
    5

    Re: Pizza comparison program for beginners

    I agree. There are indeed uses for LINQ and Lambda expressions but what I am expressly concerned about is that some childish employers find this keyword connected to C# and ask questions in interviews that tend to disqualify someone applying "with elementary c# skills and the will to learn programming as they go along" just because they say they have heard about LINQ and Lambda expressions in C# but have no clue as to what it does exactly. That cannot be grounds for rejecting a job application could it be? What if I do know Lambdas and LINQs and I don't have a computer science 4 year degree? Almost all jobs these days are asking for 4 year college degree and I am only high school with a graduate degree in a different track and I am aged 39 and I wanted to earn in the software field on my own. What gives?

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Pizza comparison program for beginners

    Quote Originally Posted by mohanarun View Post
    I agree. There are indeed uses for LINQ and Lambda expressions but what I am expressly concerned about is that some childish employers find this keyword connected to C# and ask questions in interviews that tend to disqualify someone applying "with elementary c# skills and the will to learn programming as they go along" just because they say they have heard about LINQ and Lambda expressions in C# but have no clue as to what it does exactly. That cannot be grounds for rejecting a job application could it be? What if I do know Lambdas and LINQs and I don't have a computer science 4 year degree? Almost all jobs these days are asking for 4 year college degree and I am only high school with a graduate degree in a different track and I am aged 39 and I wanted to earn in the software field on my own. What gives?
    Rather than ranting can you please stay on topic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width