Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To get a good introduction to using objects, make an instance of the Random class. Now use that instance to generate random numbers instead of

To get a good introduction to using objects, make an instance of the Random class. Now use that instance to generate random numbers instead of using our Utility class. The method that you want to call from your Random instance is called Next(); it will return to you a random number. Why don't you generate 5 random numbers now using this method?

2

Write a program to sum all the numbers between 1 and 10000, inclusive. Do this two different ways using the following algorithms.

1. Loop from 1 to 10000 and sum all the values

2. Use Gauss Sum (http://mathcentral.uregina.ca/QQ/database/QQ.02.06/jo1.html)

Finally, use the System.Diagnostics.Stopwatch class to perform timings on the two solutions. Use the class by first making an instance of it. You can use the methods: Start(), Stop(), and Reset() to control when the Stopwatch object is running or not running. You should start the Stopwatch at the start of an algorithm, and then stop it as soon as the algorithm is finished. Then use ElapsedTicks after each solution to see how many processor ticks it took to compute that algorithm.

The point of this lab is to prove to you that some algorithms are a lot more efficient than other algorithms. It also forces you to use an object.

3

Make a class called MyFirstClass with no member variables and a single public default constructor, which only writes the line Constructor! to the Console. Now in your Main initialize several instances of your class. Use the debugger to step through your program line by line. You should see in the Console window, the output Constructor! whenever each objects default constructor is called.

Now, modify your program to define an array of MyFirstClass objects. By making an array of the objects, youd expect each objects default constructor to be called, right? But the question is, did you actually create each object in the array, or just the array itself? What happens when you run your program; is there a Constructor! output for each object in the array? If so, how? If not, why? How could you change the program to ensure that each object in the array has its constructor called?

4

Make a MilitaryClock class that stores and displays military time.

Your class should contain the following private fields (member variables):

short hours;

short minutes;

Your class should support the following constructors and methods (member functions):

MilitaryClock();

MilitaryClock(short h, short m);

void SetTime(short h, short m);

void Display();

Here is an example of using your new class:

MilitaryClock mc = new MilitaryClock();

mc.Display();

mc = new MilitaryClock(7, 0);

mc.Display();

mc.SetTime(12, 15);

mc.Display();

Dont forget to write the preceding 0 before the hours and/or minutes if less than 10. You can use a simple if statements to do this, or a more complex format specification when printing.

5

Earlier you completed a lab that computed statistics on the daily sales figures. This time, wrap all of that in a class. You should be able to pass an array of sales figures (floats) into an object of the class, and then call methods to retrieve various statistics on it, such as the total sales, average sale, highest sale, etc.

Here is an example of using your new class:

SalesSummary ss = new SalesSummary();

ss.SetSales(arrayOfSales);

ss.PrintSales();

Console.WriteLine(Total: + ss.GetTotalValue());

Console.WriteLine(Average: + ss.GetAverageSale());

Console.WriteLine(Highest: + ss.GetHighestSale());

Console.WriteLine(Lowest: + ss.GetLowestSale());

6

Make a Circle class. It should hold a 2D coordinate and a radius, and have a function for drawing the circle on the screen.

Your class should contain the following private fields (member variables):

float mX;

float mY;

float mRadius;

Your class should support the following constructors and methods (member functions):

Circle(float x, float y, float radius);

Draw();

Here is an example of using your new class:

Circle c1 = new Circle(Console.WindowWidth/2, Console.WindowHeight/2, 10);

c1.Draw();

To draw a circle on the screen, think about a unit circle. A point (x, y) on the edge of a circle can be computed by taking the COS(angle) and the SIN(angle) respectively. So all you have to do is loop through all angles that you want to compute an (x, y) coordinate for. For example, if we made a for loop where i went from 0 to 360, then i would represent the angle. Now all we have to do is take the COS and SIN of the angle to get the (x, y) at each angle, and draw a point on the screen at that location.

Three things to keep in mind:

Math.Cos() and Math.Sin() take arguments in RADIANS, not ANGLES, so you must first convert your angle to radians. You can do so with the equation: angle * Math.PI / 180.0

After you compute (x, y) for each angle, note that the (x, y) you just computed is with respect to a unit circle at the origin of a Cartesian graph. You can think of that (x, y) as an offset from the center of the circle. So you must finally draw something at that (x, y) PLUS the center point of the circle. Those two coordinates added together will represent the final location of where to draw that point on the circle's surface.

The circle may not look very circular because the default font in the Console window does not have a square aspect ratio. If you want it to look truly circular, change the Font in the Properties of the Console window to a square aspect ratio font, such as 8x8.

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

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books