Create a class named Tape that includes fields for length andwidth in inches and
ID: 3619088 • Letter: C
Question
Create a class named Tape that includes fields for length andwidth in inches and properties for each field. Also include aToString() method that returns a string constructed from the returnvalue of the object's GetType() method and the values of the lengthand width fields. Derive two subclasses - VideoCassetteTapeand AdhesiveTape. The VideoCassetteTape class includes aninteger field to hold playing time in minutes - a value from 1 to180 - and a property for the field. The AdhesiveTape classincludes an integer field that holds a stickiness factor - a valuefrom 1 to 10 - and a property for the field. Write a programthat instantiates one object of each of the three classes, anddemonstrate that all of each class's methods work correctly. Be sure to use valid and invalid values when testing the numbersyou can use to set the properties of each class. Save thefile as TapeDemo.cs.
Explanation / Answer
using System;namespace SampleCramster { public class TapeDemo {
public static voidMain(String[] args) { Tape objTape =new Tape(); objTape.length= 10; objTape.width =20; Console.WriteLine( objTape.ToString());
VideoCassetteTape objVideoTape = newVideoCassetteTape(); objVideoTape.length = 10; objVideoTape.width = 10; objVideoTape.setPlayingTime(12); Console.WriteLine( objVideoTape.ToString()); objVideoTape.setPlayingTime(190);//Should throw an errormessage
AdhesiveTapeobjAdTape = new AdhesiveTape(); objAdTape.length = 10; objAdTape.width= 10; objAdTape.setStickiness(2); Console.WriteLine( objAdTape.ToString()); objAdTape.setStickiness(19);//Should throw an errormessage
Console.Read(); } }
class Tape { public double length =0.0; public double width =0.0;
public override stringToString() { StringstrOutput = this.GetType() + " length : " + this.length + "width : " + width; returnstrOutput; } }
class VideoCassetteTape : Tape { private int playingTime =1;
public voidsetPlayingTime(int num) { if (num >= 1&& num <= 180) { playingTime = num; } else { Console.WriteLine("Invalid Playing Time"); } }
public intgetPlayingTime() { returnplayingTime; }
public override stringToString() { StringstrOutput = this.GetType() + " length : " + this.length + "width : " + width + " Playing Time : " + playingTime; returnstrOutput; } }
class AdhesiveTape : Tape { private int stickiness =1;
public void setStickiness(intnum) { if (num >= 1&& num <= 10) { stickiness = num; } else { Console.WriteLine("Invalid stickiness"); } }
public intgetStickiness() { returnstickiness; }
public override stringToString() { StringstrOutput = this.GetType() + " length : " + this.length + "width : " + width + " Playing Time : " + stickiness; returnstrOutput; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.