Hi Guys
NP96 Error in the program
This program is not executing. There is an error in the program. Please correct it.
Thank you
using System;
using System.Collections;
using System.Collections.Specialized;
namespace CollectionTypes
{
class CollectionApp
{
public static void WashCar(Car c)
{
Console.WriteLine("Cleaning {0}", c.PetName);
}
static void
{
#region Fun with ArrayList
// Create ArrayList and fill with some initial values.
Console.WriteLine("***** Fun with ArrayList *****");
ArrayList carArList = new ArrayList();
carArList.AddRange(new Car[] { new Car("Fred", 90, 10),
new Car("Mary", 100, 50), new Car("MB", 190, 0)});
Console.WriteLine("Items in carArList: {0}", carArList.Count);
// Print out current values.
foreach (Car c in carArList)
{
Console.WriteLine("Car pet name: {0}", c.PetName);
}
// Insert a new item.
Console.WriteLine("-> Inserting new item");
carArList.Insert(2, new Car("TheNewCar", 0, 0));
Console.WriteLine("Items in carArList: {0}", carArList.Count);
// Get object array from ArrayList & print again.
object[] arrayOfCars = carArList.ToArray();
for (int i = 0; i < arrayOfCars.Length; i++)
{
Console.WriteLine("Car pet name: {0}",
((Car)arrayOfCars[i]).PetName);
}
#endregion
#region Fun with Queue
Console.WriteLine("\n***** Fun with Queue *****");
// Now make a Q with three items.
Queue carWashQ = new Queue();
carWashQ.Enqueue(new Car("FirstCar", 0, 0));
carWashQ.Enqueue(new Car("SecondCar", 0, 0));
carWashQ.Enqueue(new Car("ThirdCar", 0, 0));
// Peek at first car in Q.
Console.WriteLine("First in Q is {0}",
((Car)carWashQ.Peek()).PetName);
// Remove each item from Q.
WashCar((Car)carWashQ.Dequeue());
WashCar((Car)carWashQ.Dequeue());
WashCar((Car)carWashQ.Dequeue());
// Try to de-Q again?
try
{
WashCar((Car)carWashQ.Dequeue());
}
catch (Exception e)
{ Console.WriteLine("Error!! {0}", e.Message); }
#endregion
#region Fun with Stack
Console.WriteLine("\n***** Fun with Stack *****");
Stack stringStack = new Stack();
stringStack.Push("One");
stringStack.Push("Two");
stringStack.Push("Three");
// Now look at the top item.
Console.WriteLine("Top item is: {0}", stringStack.Peek());
Console.WriteLine("Popped off {0}", stringStack.Pop());
Console.WriteLine("Top item is: {0}", stringStack.Peek());
Console.WriteLine("Popped off {0}", stringStack.Pop());
Console.WriteLine("Top item is: {0}", stringStack.Peek());
Console.WriteLine("Popped off {0}", stringStack.Pop());
try
{
Console.WriteLine("Top item is: {0}", stringStack.Peek());
Console.WriteLine("Popped off {0}", stringStack.Pop());
}
catch (Exception e)
{ Console.WriteLine("Error!! {0}\n", e.Message); }
#endregion
}
}
}
Posted Apr 21, 2008, 9:53 AM
Thank you, Alan.
AlanPosted Apr 21, 2008, 9:41 AM
It doesn't compile because you haven't got a Car class in there!
I found one amongt your old threads which also needs a Radio class. When I inserted these into the latest code it compiled and ran fine.
public class Car
{
// Internal state data.
private int currSpeed;
private int maxSpeed;
private string petName;
// Is the car alive or dead?
bool carIsDead;
// A car has-a radio.
private Radio theMusicBox = new Radio();
public Car()
{
maxSpeed = 100;
}
public Car(string name, int max, int curr)
{
currSpeed = curr;
maxSpeed = max;
petName = name;
}
public void CrankTunes(bool state)
{
// Tell the radio play (or not).
// Delegate request to inner object.
theMusicBox.TurnOn(state);
}
public void SpeedUp(int delta)
{
// If the car is dead, just say so...
if (carIsDead)
{
Console.WriteLine("{0} is out of order....", petName);
}
else // Not dead, speed up.
{
currSpeed += delta;
if (currSpeed >= maxSpeed)
{
Console.WriteLine("{0} has overheated...", petName);
carIsDead = true;
}
else
Console.WriteLine("\tCurrSpeed = {0}", currSpeed);
}
}
// Properties.
public string PetName
{
get { return petName; }
set { petName = value; }
}
public int CurrSpeed
{
get { return currSpeed; }
set { currSpeed = value; }
}
public int MaxSpeed
{
get { return maxSpeed; }
set { maxSpeed = value; }
}
}
public class Radio
{
public void TurnOn(bool on)
{
if (on)
Console.WriteLine("Jamming...");
else
Console.WriteLine("Quiet time...");
}
}