1. |
Create the following classes: Vessel, which is an abstract class and represents any water-going craft. Ship, which is a Vessel. Fun Fact: A Ship is any vessel that is large enough to carry smaller Boats. Boat, 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 int maxHunger; All Vessels should have the ability to Move. If a Ship has fuel, it moves at the rate stored in the float speed; A Boat moves by its speed multiplied by how many oars it has. |
|
2. |
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. |
|
|
|
|
3. |
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 Refill() is called on a Ship, its fuel should be increased by the amount provided. If Refill() 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 Refill(). |
Working code implemented in C# and appropriate comments provided for better understanding.
Here I am attaching code for these files:
Cat.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainProgram
{
class Cat : IRefillable
{
int hunger, maxHunger;
float fuelPercentage;
public float FuelPercentage
{
get
{
return (float)hunger / (float)maxHunger;
}
}
public Cat( int _hunger, int _maxHunger )
{
hunger = _hunger;
maxHunger = _maxHunger;
}
public void
PrintHungerPercentage() {
Console.WriteLine( "Cat is now {0:P} hungry.", FuelPercentage
);
}
public void Refill( int amount )
{
if ( amount <
0 )
amount = 0;
else if ( amount
> hunger )
amount = hunger;
hunger -= amount;
Console.WriteLine( "Cat ate {0} morsels of food and is now {1:P}
hungry.", amount, FuelPercentage );
}
}
}
Ship.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainProgram
{
class Ship : Vessel, IRefillable
{
int fuel, maxFuel;
float fuelPercentage;
public Boat lifeBoat;
public float FuelPercentage
{
get
{
return (float)fuel / (float)maxFuel;
}
}
public Ship( float _speed,
string _name, int _fuel, int _maxFuel )
: base( _speed,
_name )
{
fuel =
_fuel;
if ( fuel < 0
)
fuel = 0;
maxFuel =
_maxFuel;
if ( maxFuel
< 1 )
maxFuel = 1;
lifeBoat =
new Boat( 5, name + "'s lifeboat", 2 );
}
public override void
Move()
{
if ( fuel > 0
)
{
fuel--;
Console.WriteLine( "{0} has moved {1} knots. It
has {2:P} fuel remaining.", name, speed, FuelPercentage );
}
else
Console.WriteLine( "{0} is out of fuel!", name
);
}
public void
PrintFuelPercentage()
{
Console.WriteLine( "{0} has {1:P} fuel remaining.", name,
FuelPercentage );
}
public void Refill( int amount )
{
if ( amount <
0 )
amount = 0;
else if ( amount
> maxFuel - fuel )
amount = maxFuel - fuel;
fuel += amount;
Console.WriteLine( "{0} refilled {1} fuel and now has {2:P} fuel
remaining.", name, amount, FuelPercentage );
}
}
}
Boat.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainProgram
{
class Boat : Vessel
{
int numOfOars;
public Boat( float _speed, string _name, int _numOfOars )
: base( _speed, _name )
{
if ( _numOfOars < 1 )
numOfOars = 1;
else
numOfOars = _numOfOars;
}
public override void Move()
{
Console.WriteLine( "{0} has moved {1} feet.", name, speed *
numOfOars );
}
}
}
Vessel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainProgram
{
abstract class Vessel
{
protected float speed;
protected string name;
public Vessel( float _speed,
string _name )
{
if ( _speed <
1 )
_speed = 1;
speed =
_speed;
name =
_name;
}
/// <summary>
/// This function should be
overridden in descendant classes. Right now it simply prints that
the ship's name has moved the distance of the speed in feet
/// </summary>
public virtual void Move()
{
Console.WriteLine( "{0} has moved {1} knot.", name, speed );
}
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainProgram
{
class Program
{
static void Main( string[] args
)
{
Ship ship = new
Ship( 20, "HMS We Lost in 1776", 4, 5 );
Ship ship2 = new
Ship( 100, "Pharaon", 13, 17 );
Ship ship3 = new
Ship( 1, "Kim Jong-Il's Super Fast Yacht", 0, 1 );
Boat boat = new
Boat( 20, "Boaty McBoatFace", 4 );
Boat boat2 = new
Boat( 4, "Cuban Regufee Raft", 1 );
Cat cat = new
Cat( 3, 5 );
List<Vessel> vessels = new List<Vessel>();
vessels.Add(
ship );
vessels.Add(
ship.lifeBoat );
vessels.Add(
boat );
vessels.Add(
ship2 );
vessels.Add(
ship2.lifeBoat );
vessels.Add(
boat2 );
vessels.Add(
ship3 );
vessels.Add(
ship3.lifeBoat );
foreach (
Vessel ves in vessels )
{
ves.Move();
Console.WriteLine();
}
List<IRefillable> refills = new
List<IRefillable>();
refills.Add(
ship );
refills.Add(
ship2 );
refills.Add(
ship3 );
refills.Add( cat
);
ship.PrintFuelPercentage();
ship2.PrintFuelPercentage();
ship3.PrintFuelPercentage();
cat.PrintHungerPercentage();
Console.WriteLine();
foreach (
IRefillable refill in refills ) {
refill.Refill( 2 );
}
}
}
}
IRefillable.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainProgram
{
interface IRefillable
{
float FuelPercentage { get; }
void Refill( int amount );
}
}
Sample Output Screenshots:
Get Answers For Free
Most questions answered within 1 hours.