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
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
(Use C++ language) Create a program, named threeTypesLoops.cpp, that does the following; 1. Uses a For...
(Use C++ language) Create a program, named threeTypesLoops.cpp, that does the following; 1. Uses a For Loop that asks for a number between 1 and 10; Will double the number each time through the loop and display the answer. Will run five times, so the number will have been doubled five times, and the answer displayed five times. After the loop, display a message saying 'It's done!'. 2. Uses a While Loop; Ask the user to input a friend's name....
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
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...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT