Question

Language is C# 1. Create the following classes: Vessel, which is an abstract class and represents...

Language is C#

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().

Homework Answers

Answer #1

Working code implemented in C# and appropriate comments provided for better understanding.

Here I am attaching code for these files:

  • Cat.cs
  • Ship.cs
  • Boat.cs
  • Vessel.cs
  • Program.cs
  • IRefillable.cs

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Determine how all of your classes are related, and create a complete UML class diagram representing...
Determine how all of your classes are related, and create a complete UML class diagram representing your class structure. Don't forget to include the appropriate relationships between the classes. GameDriver import java.util.Scanner; public class GameDriver { Scanner in = new Scanner(System.in); public static void main(String[] args) { Move move1 = new Move("Vine Whip", "Grass", 45, 1.0f); Move move2 = new Move("Tackle", "Normal", 50, 1.0f); Move move3 = new Move("Take Down", "Normal", 90, 0.85f); Move move4 = new Move("Razor Leaf", "Grass",...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...