// Interface IReversable defines behavior Reverse() // Reverse is implemented di
ID: 3546538 • Letter: #
Question
// Interface IReversable defines behavior Reverse()
// Reverse is implemented differently for a Soldier and a PhoneCall
// Main program demonstrates an object of each type
using System;
class DebugTen03
{
static void Main()
{
Soldier giJoe = new Soldier(266143);
PhoneCall aCall = new PhoneCall("212", "344-4188");
Console.WriteLine(giJoe.ToString());
giJoe.Reverse();
Console.WriteLine(giJoe.ToString());
giJoe.Reverse();
Console.WriteLine(giJoe.ToString());
Console.WriteLine(aCall.ToString());
aCall.Reverse();
Console.WriteLine(aCall.ToString());
}
}
public interface IReversable
{
void Reversable();
}
public class Soldier : IReversable
{
private int idNum;
private string facing;
public Soldier(int id)
{
idNum = id;
facing = "front";
}
// When a Soldier reverses if the Soldier was facing front, change to back
// If the Soldier was facing back, change to front
public void Reverse()
{
if(facing == "front")
facing = "front";
else
facing = "back";
}
public override void toString()
{
string temp = "Soldier #" + idNum + " is facing " + facing;
}
}
public class PhoneCall : IReversable
{
private string areaCode;
private string number;
private double charge;
private string mode;
public PhoneCall(string area, string num)
{
areaCode = area;
number = num;
charge = 0.25;
mode = "regular";
}
// When a phone call reverses, mode becomes collect and charge becomes 0
public void Reverse()
{
mode = "collect";
charge = 0;
}
public override string ToString()
{
string temp = "(" + areaCode + ") " + number + " Charge " + charge.ToString("C") +
" Call is " + mode;
return temp;
}
}
Explanation / Answer
TRY THIS CODE //
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Class14
{
static void Main()
{
Soldier giJoe = new Soldier(266143);
PhoneCall aCall = new PhoneCall("212", "344-4188");
Console.WriteLine(giJoe.ToString());
giJoe.Reversable();
Console.WriteLine(giJoe.ToString());
giJoe.Reversable();
Console.WriteLine(giJoe.ToString());
Console.WriteLine(aCall.ToString());
aCall.Reversable();
Console.WriteLine(aCall.ToString());
Console.ReadKey();
}
}
public interface IReversable
{
void Reversable();
}
public class Soldier : IReversable
{
private int idNum;
private string facing;
public Soldier(int id)
{
idNum = id;
facing = "front";
}
// When a Soldier reverses if the Soldier was facing front, change to back
// If the Soldier was facing back, change to front
public void Reversable()
{
if (facing == "front")
facing = "front";
else
facing = "back";
}
public override string ToString()
{
string temp = "Soldier #" + idNum + " is facing " + facing ;
return temp ;
}
}
public class PhoneCall : IReversable
{
private string areaCode;
private string number;
private double charge;
private string mode;
public PhoneCall(string area, string num)
{
areaCode = area;
number = num;
charge = 0.25;
mode = "regular";
}
// When a phone call reverses, mode becomes collect and charge becomes 0
public void Reversable()
{
mode = "collect";
charge = 0;
}
public override string ToString()
{
string temp = "(" + areaCode + ") " + number + " Charge " + charge.ToString("C") +
" Call is " + mode;
return temp;
}
}
}
//U HAVENT EMPLEMENTED INTERFACE METHOD REVESABLE .. WHICH IS NOT ACCEPTABE .. THAT MAY BE THE ISSUE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.