Create a class named Tape that includes fi elds for length and
width in inches and properties for each fi eld. 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 fi elds. Derive two subclasses—
VideoTape and AdhesiveTape. Th e VideoTape class
includes an integer fi eld to hold playing time in minutes and
a property for the fi eld. Th e AdhesiveTape class includes an
integer fi eld that holds a stickiness factor—a value restricted
to fall in the range of 1 to 10—and a property for the fi eld.
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 AdhesiveTape
class stickiness factor.
I think I didn't do it correctly, could you look at it? please
=============================MAIN METHOD========================
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(tape1.StickFactor+" "+" "+tape1.Length+" "+tape1.Width);
tape2.PlayingTime = 20;
tape2.Length = 2;
tape2.Width = 5;
Console.WriteLine(tape2.PlayingTime + " " + tape2.Length + " " + tape2.Width);
tape3.Length = 41;
tape3.Width = 51;
Console.WriteLine(tape3.Length +" "+ tape3.Width);
}
}
===========================PARENT CLASS================================
namespace TapeDemo
{
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);
}
}
}
==========================Child class 1=============================
namespace TapeDemo
{
class VideoTape : Tape
{
int playingTime;
public int PlayingTime
{
get { return playingTime; }
set { playingTime = value; }
}
}
=========================Child class 2===============================
namespace TapeDemo
{
class AdhesiveTape : Tape
{
int stickFactor;
public int StickFactor
{
get { return stickFactor; }
set
{
if ((stickFactor > 1) && (stickFactor < 10))
{
stickFactor = value;
}
else
{
Console.WriteLine("WRONG NUMBER");
}
}
}
}
}
Loading
VulpesPosted Feb 15, 2012, 4:52 AM
The problem lies with the StickFactor setter which should be coded like this:
I's also change the code for the 3 examples as I think you're intended to make use of the Tape class's ToString() method, which will be inherited by the derived classes:
Gurkirat SinghPosted Apr 17, 2013, 7:14 PM
VulpesPosted Apr 17, 2013, 7:12 PM
Gurkirat SinghPosted Apr 17, 2013, 7:10 PM
using System.Collections.Generic;
using System.Linq;
using System.Text;
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(tape1.StickFactor + " " + " " + tape1.Length + " " + tape1.Width);
tape2.PlayingTime = 20;
tape2.Length = 2;
tape2.Width = 5;
Console.WriteLine(tape2.PlayingTime + " " + tape2.Length + " " + tape2.Width);
tape3.Length = 41;
tape3.Width = 51;
Console.WriteLine(tape3.Length + " " + tape3.Width);
}
}
namespace TapeDemo // Parent Class
{
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);
}
}
}
namespace TapeDemo
{
class VideoTape : Tape
{
int playingTime; // Child class
public int PlayingTime
{
get { return playingTime; }
set { playingTime = value; }
}
}
namespace TapeDemo
{
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");
}
}
}
}
}
}
}
That what i skim down too but if i try to rebuild solution it is giving me 6 errors.
Gurkirat SinghPosted Apr 17, 2013, 7:01 PM
VulpesPosted Apr 17, 2013, 6:58 PM
Gurkirat SinghPosted Apr 17, 2013, 6:53 PM
Thank you.
Prime bPosted Feb 15, 2012, 12:47 PM
Suthish NairPosted Feb 15, 2012, 3:15 AM