Question
1 Earlier you wrote a Circle class, which was used to make circle objects and draw them on the screen. Now add the option to
1
Earlier you wrote a Circle class, which was used to make circle objects and draw them on the screen. Now add the option to draw them as filled-in circles, instead of just empty circles. This may sound challenging, but the algorithm for drawing a filled-in circle is actually easier than drawing an empty circle.
Add the following private field (member variable) to your Circle class:
bool mShaded;
Then add the ability to be able to specify whether the circle should be filled-in or not:
Circle(float x, float y, float radius, bool shaded);
SetShaded(bool shaded);
Here is an example of using your new class:
Circle c = new Circle(Console.WindowWidth/2, Console.WindowHeight/2, 10, true);
c.Draw();
Finally, have the Circle's Draw() method check the value of the mShaded boolean, to determine if it should draw the filled-in portion of the circle as well. If not, just use your previous algorithm to draw the outside of the circle. If so, then draw both the outside and the inside, using the algorithm below.
To draw a filled-in circle, picture a square that is snuggly surrounding the circle. Thus, each side of the square has a length of 2 * the radius of the circle (i.e. the diameter of the circle). Below is a picture of the square area you should be thinking about. Now, iterate through each coordinate of that square and simply check if the distance from that coordinate to the center of the circle is less than the radius of the circle. If the distance from a coordinate to the center of the circle is less than the circle radius, it means that the coordinate is within the circle; otherwise it means that it is outside the circle. So for each coordinate in that square, if it is within the circle, draw something at that coordinate, otherwise don't draw anything.
2
Earlier you made a class called MyFirstClass which had nothing more than a single default constructor which contained the printout Constructor! Modify this class to also have a destructor with the print out Destructor!
When you make instances of your class, you should see the print out Constructor! on the screen. However, you probably wont see Destructor! until the very last second of execution because destructors are only called when objects are deallocated, and because C# is a managed language, the garbage collector determines when unused objects will be deallocated (which may not be until the end of the life of the program). If you're lucky, you might see "Destructor!" flash on the screen real quick right before the Console window shuts down.
As an experiment, after you create an instance of MyFirstClass, set it to null, and then invoke the garbage collector by calling GC.Collect(). If you use the debugger to step past the garbage collector call, you should see the Destructor! printout written to the screen, indicating that the garbage collector deallocated your object. You may have to step the debugger a few statements before this happens. This at least should prove to you that a destructor is called when an instance is deallocated.
NOTE: In general you should refrain from invoking the garbage collector manually.
3
Create an ATM-ish program that allows a user to access their bank account (assume theyve already verified their identity). First, create a BankAccount class with the information below, then in Main, allocate a default BankAccount object and then in an indefinite loop, display a menu of choices, which correspond to methods to be called on the BankAccount object. Break out of the loop when the user indicates theyd like to exit.
Your BankAccount class should contain the following private fields (member variables):
string mCustomer;
float mBalance;
Your BankAccount class should support the following constructors and methods (member functions):
BankAccount();
BankAccount(string customerName, float initialBalance);
void SetCustomer(string customerName);
string GetCustomer();
float GetBalance();
bool Credit(float amount);
bool Debit(float amount);
The only way to change the balance after the BankAccount object has been created is to call the Credit and Debit methods. Both of those methods shouldnt allow negative transactions (e.g. debiting more than the account has in it). Both methods should return a bool to indicate if they were able to perform their action successfully or not.
4
You already created a Circle class in a previous lab. Now add an IsColliding() method to check if a Circle object is colliding with another Circle object, the one passed into the method.
You should add the following method (member function) to your Circle class:
public bool IsColliding(Circle other);
To help you solve this collision problem, on a piece of paper, draw out 2 circles that are colliding, and draw 2 circles that are disjoint. Now try and see if you can figure out how to determine if they are colliding or not. Think about the distance between the center points of the two circles relative to the radiuses of the two circles. The problem is surprisingly simple if you use the Pythagorean theorem and the radiuses of the two circles. If you cannot figure this out on your own, search on the Internet for an existing algorithm.
Here is an example of using your Circle class and the new method you just added:
Circle c1 = new Circle( ... );
Circle c2 = new Circle( ... );
c1.Draw();
c2.Draw();
bool collision = c1.IsColliding(c2);
// print out whether there is a collision or not
5
Create a TextCodec class to handle encrypting and decrypting messages.
Your TextCodec class should contain the following private fields (member variables):
sbyte offset;
Your TextCodec class should support the following constructors and methods (member functions):
TextCodec(); // calls SetOffset(0)
TextCodec(sbyte offset); // calls SetOffset(offset)
void SetOffset(sbyte offset); // sets offset to the offset passed in
string Encode(string message); // encodes the message and returns it
string Decode(string message); // decodes the message and returns it
Because characters are actually numeric values themselves in memory, you can actually increment or decrement the character's values in order to reference other characters. In this program, when encoding, you will iterate through the message, character by character, and add the offset to each character in the message. When decoding, you will iterate through the message, character by character, and subtract the offset from each character in the message.
Use the System.Text.StringBuilder class to help you. The StringBuilder class is almost identical to the String class. The only real difference is that it is mutable, which means the characters of the StringBuilder can be changed (whereas they cannot be changed in a String class, because String is immutable). Use the StringBuilder class like so:
StringBuilder sb = new StringBuilder(message);
for (int i=0; i sb[i] = (char)(sb[i] + offset); // add offset to each letter to encode, subtract to decode string encoded = sb.ToString(); NOTE: Your TextCodec class has a member variable called offset, and several of the methods take as input a parameter called offset as well. Thus, you will have to manually resolve this name collision using the this reference. NOTE: If you use a large offset, it is possible that some of the characters will be encrypted as characters that cannot be printed out properly in the Console window; instead they will appear as ? symbols. When you copy your encrypted text and enter it back in, you are copying the ? characters themselves, not the character that should have been printed out but couldn't because of limitations in the Console font. When those characters are decrypted in your program, your program will think they are actually ? characters, which are value 63 in the ASCII table, instead of the character they should really be but you can't see in Console, and thus compute an offset from there, which in turn will not give you your original unencrypted message. This is a known issue due to lack of Unicode character support in Console. 6 Create a database of alien life forms. Make an Alien class with at least the following information, then in Main allocate an array of Aliens with various information, and then in an indefinite loop, display a menu of choices, which allow the user to search the array of Aliens in various ways. Break out of the loop when the user indicates theyd like to exit. Your Alien class should contain the following private fields (member variables): string mSpecies; string mPlanet; float mPopulation; // billions bool mHumanoid; Your Alien class should support the following constructors and methods (member functions): Alien(); void Define(string species, string planet, float pop, bool humanoid); string GetSpecies(); string GetPlanet(); float GetPopulation(); bool IsHumanoid(); void DisplayInfo(); In Main, you are welcome to initialize the database however you wish, but an easy way is to: Initialize an array of 5 strings of names of alien species (whatever you want). Initialize an array of 9 strings of planet names (whatever you want). Define an array of 5 Alien objects. Loop through the Alien object array and for each one call its Define method with: o the name of the species from the species name array o a random planet from the planet name array o a random population between 0.1 and 10.0 o a random bool to indicate if the Alien is humanoid or not Then define a few functions in your Program, which can be called from Main, all of which search the list of Aliens by looking for a specific piece of information, and call the Display method of any Alien that contains the information searched for: void SearchBySpecies(Alien[] aliens, string species); void SearchByPlanet(Alien[] aliens, string planet); void SearchByHumanoid(Alien[] aliens, bool humanoid);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started