// Street is an abstract class // OneWayStreet and TwoWayStreet derive from Stre
ID: 3546534 • Letter: #
Question
// Street is an abstract class
// OneWayStreet and TwoWayStreet derive from Street
// On a OneWayStreet, it is illegal to make a U turn
// On a TwoWayStreet, a U Turn reverses the travelling direction
// Main program creates two Street child objects - one OneWay and one TwoWay
// and demonstrates what happens when you make a U Turn
// on a OneWayStreet and a TwoWayStreet
using System;
class DebugTen02
{
public static void Main()
{
OneWayStreet oak = new OneWayStreet("Oak Avenue", "east");
TwoWayStreet elm = new TwoWayStreet("Elm Street", "south");
Console.WriteLine("On " + oak.Name + " " + oak.MakeUTurn());
Console.WriteLine("On " + oak.Name + " " + oak.MakeUTurn());
Console.WriteLine("On " + elm.Name + " " + elm.MakeUTurn());
Console.WriteLine("On " + elm.Name + " " + elm.MakeUTurn());
}
}
abstract class Street
{
protected string name;
public string direction;
public Street(string name, string travelDirection)
{
name = name;
direction = travelDirection;
}
public string Name
{
get
{
return Name;
}
}
public abstract string MakeUTurn();
}
class OneWayStreet : Street
{
public OneWayStreet(string name, string direction) : base(name, direction)
{
}
public override string MakeUTurn()
{
string temp = "U Turn is illegal! Was going and still going " + direction;
return temp;
}
}
class TwoWayStreet : Street
{
public TwoWayStreet(string name, string direction) : base(name, direction)
{
}
public override string MakeUTurn
{
string wasGoing = travelDirection;
string[] directions = {"north", "south", "east", "west"};
string[] oppDirections = {"south", "north", "west", "east"};
for(int x = 0; x < directions.Length; ++x)
{
if(direction.Equals(directions[x]))
{
direction = oppDirections[x];
x = directions.Length;
}
}
string temp = "U Turn successful. Was going " + wasGoing +
". Now going " + direction;
return string;
}
}
Explanation / Answer
Your code re-edited
Try this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication2
{
class Class13
{
public static void Main()
{
OneWayStreet oak = new OneWayStreet("Oak Avenue", "east");
TwoWayStreet elm = new TwoWayStreet("Elm Street", "south");
Console.WriteLine("On " + oak.Name + " " + oak.MakeUTurn());
Console.WriteLine("On " + oak.Name + " " + oak.MakeUTurn());
Console.WriteLine("On " + elm.Name + " " + elm.MakeUTurn());
Console.WriteLine("On " + elm.Name + " " + elm.MakeUTurn());
Console.ReadKey();
}
}
abstract class Street
{
protected string name;
public string direction;
public Street(string name, string Direction)
{
name = Name;
direction = Direction;
}
public string Name
{
get
{
return name;
}
}
public string Direction
{
get
{
return direction;
}
}
public abstract string MakeUTurn();
}
class OneWayStreet : Street
{
public OneWayStreet(string name, string direction)
: base(name, direction)
{
}
public override string MakeUTurn()
{
string temp = "U Turn is illegal! Was going and still going " + direction;
return temp;
}
}
class TwoWayStreet : Street
{
public TwoWayStreet(string name, string direction)
: base(name, direction)
{
}
public override string MakeUTurn()
{
string wasGoing = Direction;
string[] directions = { "north", "south", "east", "west" };
string[] oppDirections = { "south", "north", "west", "east" };
for (int x = 0; x < directions.Length; ++x)
{
if (direction.Equals(directions[x]))
{
direction = oppDirections[x];
x = directions.Length;
}
}
string temp = "U Turn successful. Was going " + wasGoing +
". Now going " + direction;
return temp;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.