GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 412 of 436    Print  
class Design
. Below is a set of birds and movements they can perform.
Birds:
Penguin:
• hopping: moves 2 ft
• flying: can't fly
Hawk:
• hopping: can't hop
• flying: moves 100 ft; won't fly if World.windSpeed > 40
Robin:
• hopping: moves 1 ft; won't hop if World.temperature < 0
• flying: moves 20 ft; won't fly if World.windSpeed > 20
Crow:
• hopping: moves 1 ft; won't hop if World.temperature < 0
• flying: moves 30 ft; won't fly if World.windSpeed > 25
Requirements:
• Create a class or set of classes in C# to represent the birds.
• Create an instance of each type of bird and add them all into a single collection. Have each bird hop and then fly in turn, each time after the World changes. After all birds have moved, print out the total distance that each one has moved. (See the comments in Main below marked // TODO).
• Fill in main() to generate the desired output (which is shown under main() below).
• Use best design practices. The priority is to create maintainable code.


using System;
using System.Collections.Generic;

namespace PCD
{

public class World
{
public static int temperature = 0; // celcius
public static int windSpeed = 0; // miles per hour
}

public class MainClass
{
static void Main(string[] args)
{
// TODO: create the birds here

World. = 20;
World. temperature windSpeed = 12;

// TODO: have each bird fly & hop

// a storm arrives!
World.temperature = -10;
World.windSpeed = 30;

// TODO: have each bird fly & hop

// TODO: print total distances

}

}
}


Desired output:
The penguin moved 4 ft.
The hawk moved 200 ft.
The robin moved 21 ft.
The crow moved 31 ft.




  
Total Answers and Comments: 1 Last Update: April 07, 2008     Asked by: musclebai 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: vshylaja
 
using System;
using System.Collections.Generic;

namespace PCD
{
    public class World
    {
        public static int temperature = 0; // celcius
        public static int windSpeed = 0; // miles per hour
    }

    public abstract class Bird
    {
        private string _TypeName = "Bird";

        private int _MovedDistance = 0; // feet  
   
        private int _HOP_DISTANCE = 0;
        private int _FLY_DISTANCE = 0;
        private int _MIN_TEMP = 0;
        private int _MAX_WIND_SPEED = 0;

        protected int HOP_DISTANCE
        {
            get { return _HOP_DISTANCE; }
            set { _HOP_DISTANCE = value; }
        }
        protected int FLY_DISTANCE
        {
            get { return _FLY_DISTANCE; }
            set { _FLY_DISTANCE = value; }
        }
        protected int MIN_TEMP
        {
            get { return _MIN_TEMP; }
            set { _MIN_TEMP = value; }
        }
        protected int MAX_WIND_SPEED
        {
            get { return _MAX_WIND_SPEED; }
            set { _MAX_WIND_SPEED = value; }
        }

        public string TypeName
        {
            get { return _TypeName; }
            set { _TypeName = value; }
        }

        public int MovedDistance
        {
            get
            {
                return _MovedDistance;
            }
        }

        virtual public void Hop()
        {
            if (World.temperature >= _MIN_TEMP)
                _MovedDistance += HOP_DISTANCE;
        }
        virtual public void Fly()
        {
            if (World.windSpeed <= _MAX_WIND_SPEED)
                _MovedDistance += FLY_DISTANCE;
        }
    }

    public class Penguin : Bird
    {
        public Penguin()
        {
            TypeName = "Penguin";
            HOP_DISTANCE = 2;
            FLY_DISTANCE = 0;
            MIN_TEMP = -60;
        }

        public override void Fly()
        {
            return;
        }
    }

    public class Hawk : Bird
    {
        public Hawk()
        {
            TypeName = "Hawk";
            HOP_DISTANCE = 0;
            FLY_DISTANCE = 100;

            MAX_WIND_SPEED = 40;
        }
    }

    public class Robin : Bird
    {
        public Robin()
        {
            TypeName = "Robin";

            HOP_DISTANCE = 1;
            FLY_DISTANCE = 20;

            MIN_TEMP = 0;
            MAX_WIND_SPEED = 20;
        }
    }

    public class Crow : Bird
    {
        public Crow()
        {
            TypeName = "Crow";

            HOP_DISTANCE = 1;
            FLY_DISTANCE = 30;

            MIN_TEMP = 0;
            MAX_WIND_SPEED = 25;
        }
    }

    public class MainClass
    {
        static void Main(string[] args)
        {
        // TODO: create the birds here

            Penguin peng = new Penguin();
            Hawk hawk = new Hawk();
            Robin robin = new Robin();
            Crow crow = new Crow();

            List<Bird> birds = new List<Bird>();

            birds.Add(peng);
            birds.Add(hawk);
            birds.Add(robin);
            birds.Add(crow);

            World.temperature = 20;
            World.windSpeed = 12;

            // TODO: have each bird fly & hop
            foreach (Bird bird in birds)
            {
                bird.Hop();
                bird.Fly();
            }

            // a storm arrives!
            World.temperature = -10;
            World.windSpeed = 30;

            // TODO: have each bird fly & hop
            foreach (Bird bird in birds)
            {
                bird.Hop();
                bird.Fly();
            }

            // TODO: print total distances
            foreach (Bird bird in birds)
            {
                Console.WriteLine("The {0} moved {1} ft.", bird.TypeName, bird.MovedDistance);
            }
        }
    }
}

Above answer was rated as good by the following members:
yongyici
April 05, 2008 22:54:00   #1  
vshylaja Member Since: April 2008   Contribution: 10    

RE: class Design
using System;
using System.Collections.Generic;

namespace PCD
{
public class World
{
public static int temperature 0; // celcius
public static int windSpeed 0; // miles per hour
}

public abstract class Bird
{
private string _TypeName "Bird";

private int _MovedDistance 0; // feet

private int _HOP_DISTANCE 0;
private int _FLY_DISTANCE 0;
private int _MIN_TEMP 0;
private int _MAX_WIND_SPEED 0;

protected int HOP_DISTANCE
{
get { return _HOP_DISTANCE; }
set { _HOP_DISTANCE value; }
}
protected int FLY_DISTANCE
{
get { return _FLY_DISTANCE; }
set { _FLY_DISTANCE value; }
}
protected int MIN_TEMP
{
get { return _MIN_TEMP; }
set { _MIN_TEMP value; }
}
protected int MAX_WIND_SPEED
{
get { return _MAX_WIND_SPEED; }
set { _MAX_WIND_SPEED value; }
}

public string TypeName
{
get { return _TypeName; }
set { _TypeName value; }
}

public int MovedDistance
{
get
{
return _MovedDistance;
}
}

virtual public void Hop()
{
if (World.temperature > _MIN_TEMP)
_MovedDistance + HOP_DISTANCE;
}
virtual public void Fly()
{
if (World.windSpeed < _MAX_WIND_SPEED)
_MovedDistance + FLY_DISTANCE;
}
}

public class Penguin : Bird
{
public Penguin()
{
TypeName "Penguin";
HOP_DISTANCE 2;
FLY_DISTANCE 0;
MIN_TEMP -60;
}

public override void Fly()
{
return;
}
}

public class Hawk : Bird
{
public Hawk()
{
TypeName "Hawk";
HOP_DISTANCE 0;
FLY_DISTANCE 100;

MAX_WIND_SPEED 40;
}
}

public class Robin : Bird
{
public Robin()
{
TypeName "Robin";

HOP_DISTANCE 1;
FLY_DISTANCE 20;

MIN_TEMP 0;
MAX_WIND_SPEED 20;
}
}

public class Crow : Bird
{
public Crow()
{
TypeName "Crow";

HOP_DISTANCE 1;
FLY_DISTANCE 30;

MIN_TEMP 0;
MAX_WIND_SPEED 25;
}
}

public class MainClass
{
static void Main(string[] args)
{
// TODO: create the birds here

Penguin peng new Penguin();
Hawk hawk new Hawk();
Robin robin new Robin();
Crow crow new Crow();

List<Bird> birds new List<Bird>();

birds.Add(peng);
birds.Add(hawk);
birds.Add(robin);
birds.Add(crow);

World.temperature 20;
World.windSpeed 12;

// TODO: have each bird fly & hop
foreach (Bird bird in birds)
{
bird.Hop();
bird.Fly();
}

// a storm arrives!
World.temperature -10;
World.windSpeed 30;

// TODO: have each bird fly & hop
foreach (Bird bird in birds)
{
bird.Hop();
bird.Fly();
}

// TODO: print total distances
foreach (Bird bird in birds)
{
Console.WriteLine("The {0} moved {1} ft." bird.TypeName bird.MovedDistance);
}
}
}
}

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    

 Related Questions

C# does not support an explicit fall through for case blocks. The following code is not legal and will not compile in C#: switch(x){case 0:// do somethingcase 1:// do something in common with 0default:// 
Latest Answer : c# switch allows fall through if and only if in empty case. as long as there is a statement in the case it must  use break or go to to  jump out of the case. ...

The difference is that static read-only can be modified by the containing class, but const can never be modified and must be initialized to a compile time constant. To expand on the static read-only case 
Latest Answer : As static read-only variables must be initialized in the static constructor (static constructor cannot have parameters and it cannot be called manually), it is efficient to used const variables over static read-only variables if you know the values ...

Use a conditional attribute on the method, as shown below: class Debug{[conditional("TRACE")]public void Trace(string s){Console.WriteLine(s);}}class MyClass{public static void Main(){Debug.Trace("hello");}}In 
Latest Answer : Well, you cant add the namespace System.Diagnostics.ConditionalAttribute. Actually you need to add the namespace System.Diagnostics and capitalise "Conditional" like so:using System;using System.Diagnostics;namespace Debug{    class ...

What is the syntax for calling an overloaded constructor within a constructor (this() and constructorname() does not compile)?
The syntax for calling another constructor is as follows: class B{B(int i){ }}class C : B{C() : base(5) // call base constructor B(5){ }C(int i) : this() // call C(){ }public static void Main() {}}  

C# requires only a single parameter for delegates: the method address. Unlike other languages, where the programmer must specify an object reference and the method to invoke, C# can infer both pieces of 
Latest Answer : This article is good.IntroductionIn this article I am going to share my knowledge on Delegates in C#.This would explain the Delegate using simple examples so that the beginner can understand the same.What is Delegate?Definition:Delegate is type which ...

Here's a quick example of the DllImport attribute in action: using System.Runtime.InteropServices;class C{[DllImport("user32.dll")]public static extern int MessageBoxA(int h, string m, string 
Latest Answer : ans:by using1st:using System.Runtime.InteropServicessecond step[DllImport("user32.dll")]use top of the class ...

C# has finalizers (similar to destructors except that the runtime doesn't guarantee they'll be called), and they are specified as follows: class C{~C(){// your code}public static void Main() 
Latest Answer : If you are not declare any Main() method in your program then compiler will give you this errors. Basically Main() is the entry point for compiler for execution. One more thing C# ia a case sensitive language so be carefull about the spelling of Main() ...

The most common problem is that you used a lowercase 'm' when defining the Main method. The correct way to implement the entry point is as follows: class test {static void Main(string[] args) 
Latest Answer : class test   {  public test() { } public static void Main(string[] args) {}  }   modifier for Main is public and inside the clas default constructor is necessary ...

Original Visual J++ code: public synchronized void Run() {// function body}Ported C# code: class C{public void Run(){lock(this){// function body }}public static void Main() {}}  

Yes. Here's a simple example: using System;class Class1 {private string[] MyField;public string[] MyProperty {get { return MyField; }set { MyField = value; }}}class MainClass{public static int Main(string[] 
Latest Answer : yes u can use indexer.its working is same as properties. ...


 Sponsored Links

 
Related Articles

Linux Thin Client Networks Design and Deployment Review

Linux Thin Client Networks Design and Deployment Review Introduction This book is written by David Richards a veteran Linux thin client network designer Designed for System Administrators Linux Thin Client Networks Design and Deployment goes over the concepts which are related to thin client network
 

Service Oriented Design and Development

Service Oriented Design and Development SOAD Service Oriented Analysis and Design The term Service Oriented Analysis and Design&nbsp; was first used in the publication Elements of Service Oriented Analysis and Design Service Oriented Analysis and Design is also covered in the publication Service
 

C++ Pure Virtual Function and Base Class

C Pure Virtual Function and Virtual Base Class In this C tutorial you will learn about pure virtual function declaration of pure virtual function and virtual base class virtual base class and how to implement a virtual base class explained with examples mosgoogle center What is Pure Virtual Function
 

How to Access C++ Class Members

How to Access C Class Members In this C tutorial you will learn how to access Class members dot operator or class member access operator difference between struct and class and&nbsp; scope resolution operator mosgoogle center It is possible to access the class members after a class is defined an
 

Systems Design

In previous chapter, we discussed system analysis and requirements stage in SDLC where everything was laid out on papers.&nbsp; SDLC makes sure that there is an actual need for the software being developed.&nbsp; Developers need to think of this first, especially in a business setting.&n
 

The Interview Snafu

How to turn someone else&rsquo;s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

Winning a Job Interview with a Winning Resume

Does your resume unlock your potential, take your skills to the highest level and win you the interview and the job you want now? The job market today is highly competitive and even if you think you have what it takes to get an interview you won&rsquo;t get over the line without a polished, prof
 

WinRunner Script Design Tips

Scripts play an important role in the function of WinRunner. Those who wish to successful with this program will need to learn how to properly create scripts. In this article, I will provide you with a number of useful tips for script creation. The first tip of script development is to always place
 

UML Elements : Class Diagram

UML Elements UML 2 0 is comprised of a total of 13 diagrams If you wish to understand these diagrams they should be organized based on a hierarchy For UML a diagram is an element which must define which things should be modeled in a system mosgoogle center Introduction to Class Diagram In this arti
 

Importance of Proper English during Job Interview

Importance of Proper English during Job Interview Your job interview is crucially important and it will determine whether or not you will get the job Depending on the type of job you re going for it is very important for you to use proper English In most cases jobs which offer higher salaries will h
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape