Write a C# console application named Tape that includes fields for length and wi
ID: 2247661 • Letter: W
Question
Write a C# console application named Tape that includes fields for length and width in inches and properties for each field. Also include a ToString() method that returns a string constructed from the return value of the object's GetType() method and the values of the length and width fields. Derive two subclasses - VideoCassetteTape and AdhesiveTape. The VideoCassetteTape class includes an integer field to hold playing time in minutes - a value from 1 to 180 - and a property for the field. The AdhesiveTape class includes an integer field that holds a stickiness factor - a value from 1 to 10 - and a property for the field. Write a program that instantiates one object of each of the three classes, and demonstrate that all of each class's methods work correctly. Be sure to use valid and invalid values when testing the numbers you can use to set the properties of each class. Save the file as TapeDemo.cs.
Explanation / Answer
CODE
THANK YOU
namespace TapeDemo
{
class Program
{
static void Main(string[] args)
{
AdhesiveTape tape1 = new AdhesiveTape();
VideoTape tape2 = new VideoTape();
Tape tape3 = new Tape();
tape1.StickFactor = 2;
tape1.Length = 20;
tape1.Width = 10;
Console.WriteLine("{0} {1}", tape1, tape1.StickFactor);
tape2.PlayingTime = 20;
tape2.Length = 2;
tape2.Width = 5;
Console.WriteLine("{0} {1}", tape2, tape2.PlayingTime);
tape3.Length = 41;
tape3.Width = 51;
Console.WriteLine(tape3);
// now an invalid one
AdhesiveTape tape4 = new AdhesiveTape();
tape4.StickFactor = 11; // too big
tape4.Length = 30;
tape4.Width = 15;
Console.WriteLine("{0} {1}", tape4, tape4.StickFactor);
}
}
class Tape
{
private int length;
private int width;
public int Length
{
get { return length; }
set { length = value; }
}
public int Width
{
get { return width; }
set { width = value; }
}
public override string ToString()
{
return (GetType() + " " + length + " " + width);
}
}
class AdhesiveTape : Tape
{
int stickFactor = 1; // use a default of 1 as 0 would be invalid
public int StickFactor
{
get { return stickFactor; }
set
{
if ((value >= 1) && (value <= 10)) // test value here, not stickFactor
{
stickFactor = value;
}
else
{
Console.WriteLine("WRONG NUMBER FOR STICKINESS FACTOR");
}
}
}
}
class VideoTape : Tape
{
int playingTime;
public int PlayingTime
{
get { return playingTime; }
set { playingTime = value; }
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.