Hi guys,
I've been given 3 questions to do
1. Write a console program that calculates the area of a circle based on the radius supplied by the user.(circle area= PI*r2)
2. Write a console program that generates random numbers until the user chooses to stop.
3.Write a program which lets the user enter at least three and up to ten names, then sort these names alphabetically and print them to the console.
PLEASE CAN SOMEONE HELP ME
AlanPosted Oct 17, 2007, 5:29 PM
OK, Darren, I'll do them this one more time but, as Jan said, it's best for you to have a go youself and then we'll correct them if necessary. They'll have a much greater impact on the mind if you work that way.
// Question 1
using System;
class Program1
{
static void Main()
{
MyClass mc1 = new MyClass(1, 2.0, "Full");
Console.WriteLine("{0}, {1}, {2}", mc1.MyInt, mc1.MyDouble, mc1.MyString);
MyClass mc2 = new MyClass(1, 2.0);
Console.WriteLine("{0}, {1}, {2}", mc2.MyInt, mc2.MyDouble, mc2.MyString);
MyClass mc3 = new MyClass(1);
Console.WriteLine("{0}, {1}, {2}", mc3.MyInt, mc3.MyDouble, mc3.MyString);
Console.ReadLine();
}
}
class MyClass
{
int myInt;
double myDouble;
string myString;
public int MyInt
{
get { return myInt;}
}
public double MyDouble
{
get { return myDouble; }
}
public string MyString
{
get { return myString; }
}
public MyClass(int i, double d, string s)
{
myInt = i;
myDouble = d;
myString = s;
}
public MyClass(int i, double d): this(i, d, "Empty")
{
}
public MyClass(int i): this(i, 0.0, "Empty")
{
}
}
// Question 2
using System;
class Program2
{
static void Main()
{
DoubleArrayWrapper daw = new DoubleArrayWrapper();
daw[0] = 1.0;
daw[1] = 3.4;
daw[2] = 6.7;
daw[3] = 8.6;
daw[4] = 9.8;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Number {0} : {1}", i+1, daw[i]);
}
Console.ReadLine();
}
}
class DoubleArrayWrapper
{
double[] values = new double[10]; // all elements 0.0 by default
public double this[int index]
{
get
{
if (index >= 0 && index <= 9)
{
return values[index];
}
else
{
Console.WriteLine("Index out of bounds in getter, returning zero");
return 0.0;
}
}
set
{
if (index >= 0 && index <= 9)
{
values[index] = value;
}
else
{
Console.WriteLine("Index out of bounds in setter");
}
}
}
}
darren WrightPosted Oct 17, 2007, 4:10 PM
Thanx ALAN for your reply. It helped alot, i was stuck on that circle question for a while.
Anyway hope you don't mind but can you help me with this question?
1) Write a class with three data fields. Then create three constructors, each with a different number of parameters. The constructors with fewer than three parameters should call another constructor, which contains the actual implementation to set all the fields' values.
2) Write a class to store an array of ten numbers at most. Make the array accessible by implementing an indexer. The indexer should include code to check that the requested index is not out of bounds. Use this class to add five numbers to the list, and then write them to the console.
PLEASE CAN YOU CODE THIS FOR ME. ITS HARD TRYING TO TEACH MYSELF WITHOUT HELP
DARREN
Jan MontanoPosted Oct 8, 2007, 10:37 PM
AlanPosted Oct 8, 2007, 4:04 PM
To give you a start, I'll do the first question for you but, if you're going to learn programming, you've got to try and do the others yourself and make plenty of mistakes - believe me, it's the only way.
using System;
class Program
{
static void Main()
{
Console.Write("Please enter the radius : ");
string input = Console.ReadLine();
double radius = double.Parse(input);
double area = Math.PI * radius * radius;
Console.WriteLine("The area of the circle is {0}", area);
Console.ReadLine(); // press enter to end program
}
}