Veriz Create the following classes. Vessel, which is an abstract class and repre
ID: 3589582 • Letter: V
Question
Veriz Create the following classes. Vessel, which is an abstract class and represents any water-going craft. Ship, which is a Vessel and Cat, which is just a cat. All Vessels should have a float speed; and string name Ships should have an int fuel and an int maxFuel. Boats should have an int oars; Cats should have an int hunger and an maxHunger; int All Vessels should have the ability to Move If a Ship has fuel, it moves at the rate stored in the float speed Create a program with several Ships and several Boats. Give them varying appropriate values for names, speed, fuel, etc. Using a single foreach loop, make all the Ships and Boats move in the manner specific to each type of Vessel. Create an interface IRefillable with a method void Refill(int amt); and a property float FuelPercentage(get Ships and Cats should implement IRefillable If a user gets the FuelPercentage property of a Ship it should return a value representing the percentage of fuel that remains on the Ship. If a user gets the FuelPercentage of a Cat, it should return a percentage of how full the cat is. If Refill0 is called on a Ship, its fuel should be increased by the amount provided. If Refill0 is called on a cat, its hunger is reduced by the amount provided, to a minimum of 0 Demonstrate by using these in your program. Output the fuel percentage of a ship and a cat before and after calling Refill0.Explanation / Answer
namespace AbstractClasses
{
public interface ITransactions // interface definition
{
String name
{
get;
set;
}
float speed
{
get;
set;
}
void Refil(int amt); // interface members
float FuelPercent(get;);
}
abstract class Vessel // abstract class definition
{
public float speed;
public String name;
public abstract float speed
{
get;
set;
}
public abstract String name
{
get;
set;
}
public virtual int Move()
{
return speed;
}
}
class Ship : Vessel,ITransactions // child class where vessel and ITransactions are getting implemented
{
public int fuel;
public int maxFuel;
Ship()
{}
public override String name
{
get
{
return name;
}
set
{
name = value;
}
}
public override float speed
{
get
{
return speed;
}
set
{
speed = value;
}
}
public override int Move()
{
if (fuel>=0)
return speed;
else
return 0;
}
void Refil(int amt)
{
maxFuel=amt+fuel;
Console.WriteLine("The amount of refil amount is "+amt+ " and total amount is "+ maxFuel);
}
float FuelPercent(get;)
{
return speed;
float perc=maxFuel/100;
Console.WriteLine("The fuel percent is "+perc);
}
}
class Boats : Vessel // child class where vessel is getting implemented
{
public int oars;
Boats()
{}
public override String name
{
get
{
return name;
}
set
{
name = value;
}
}
public override float speed
{
get
{
return speed;
}
set
{
speed = value;
}
}
}
class Cat: ITransactions // child class where ITransactions is getting implemented
{
public int hunger;
public int maxHunger;
Cat()
{}
void Refil(int amt)
{
maxHunger=amt+hunger;
Console.WriteLine("The amount of hunger for cat is "+amt+ " and hunger is"+ hunger + " and max hunger is "+maxHunger);
}
float FuelPercent(get;)
{
}
}
class Program
{
static void Main(string[] args)
{
Ship ship1 = new Ship(); //ship class objects are called
ship1.name="Venus"; ship1.speed=10 ; ship1.fuel=5;
Console.WriteLine(ship1.Move());
ship1.Refil(9); ship1.FuelPercent(ship1.fuel);
Ship ship2 = new Ship();
ship2.name="Satrun"; ship2.speed=5 ; ship2.fuel=8;
Console.WriteLine(ship2.Move());
ship2.Refil(3); ship2.FuelPercent(ship2.fuel);
Ship ship3 = new Ship();
ship3.name="Mars"; ship3.speed=9 ; ship3.fuel=1;
Console.WriteLine(ship3.Move());
ship3.Refil(7); ship3.FuelPercent(ship3.fuel);
Ship ship4 = new Ship();
ship4.name="Sun"; ship4.speed=10 ; ship4.fuel=0;
Console.WriteLine(ship4.Move());
ship4.Refil(10); ship4.FuelPercent(ship1.fuel);
Boats boat1= new Boats(); //boat class objects are called
boat1.name="Rose"; boat1.speed=10 ; boat1.oars=5;
Console.WriteLine(boat1.Move());
Boats boat2= new Boats();
boat2.name="Lily"; boat2.speed=2 ; boat2.oars=2;
Console.WriteLine(boat2.Move());
Boats boat3= new Boats();
boat3.name="Rose"; boat3.speed=10 ; boat3.oars=5;
Console.WriteLine(boat3.Move());
Boats boat4= new Boats();
boat4.name="Jasmine"; boat4.speed=20 ; boat4.oars=4;
Console.WriteLine(boat4.Move());
Cat cat1=new Cat(); //cat class objects are called
cat1.name="R";cat1.hunger=5;
cat1.Refil(2);
Cat cat2=new Cat();
cat2.name="Z";cat2.hunger=7;
cat2.Refil(9);
foreach (Vessel element in Boats) // foreach is used to display object values for boats
{
System.Console.WriteLine("Vessel element for boats : "+ element.name+ " " + element.speed+ " " + element.oars );
}
foreach (Vessel element in Ship) // foreach is used to display object values for Ship
{
System.Console.WriteLine("Vessel element for boats : "+ element.name+ " " + element.speed+ " " + element.fuel );
}
Console.ReadKey();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.