Skip to content

Instantly share code, notes, and snippets.

@matthewshawnkehoe
Created October 1, 2015 02:43
Show Gist options
  • Save matthewshawnkehoe/c438c6c58feb6dafccf4 to your computer and use it in GitHub Desktop.
Save matthewshawnkehoe/c438c6c58feb6dafccf4 to your computer and use it in GitHub Desktop.
Battle Ship!
/*
Matthew Kehoe
*/
public class GenericShip
{
private int crew;
private int cost;
private String shipName;
public GenericShip()
{
crew = 10;
cost = 5000;
shipName = "S.S. Default";
}
public GenericShip(int crew, int cost, String shipName)
{
this.crew = crew;
this.cost = cost;
this.shipName = shipName;
}
public void setCrew(int crew)
{
this.crew = crew;
}
public void setCost(int cost)
{
this.cost = cost;
}
public void setName(String shipName)
{
this.shipName = shipName;
}
public int getCrew()
{
return crew;
}
public int getCost()
{
return cost;
}
public String getShipName()
{
return shipName;
}
}
import java.text.DecimalFormat;
/*
Matthew Kehoe
*/
public class MainShip
{
public static void main(String[] args)
{
GenericShip fluffy = new GenericShip();
Warship guerilla = new Warship();
ScienceVessel magneto = new ScienceVessel();
Transport scaredKitten = new Transport();
DecimalFormat f = new DecimalFormat("##0,000.00");
System.out.println("The generic ship has the following...");
System.out.println();
System.out.println("Default of " + fluffy.getCrew() + " crew members.");
System.out.println("Default cost of $" + f.format(fluffy.getCost()) + ".");
System.out.println("Default name of "
+ fluffy.getShipName() + ".");
System.out.println();
System.out.println("Now we can test a transport ship...");
System.out.println();
System.out.println("The default name for this ship is " + scaredKitten.getShipName());
fluffy.setName("Kittens Of Doom.");
System.out.println("The new name of this ship is " + fluffy.getShipName());
fluffy.setCost(10000000);
System.out.println("Because the name is spiffy, we have increased the "
+ "price of the ship to $" + f.format(fluffy.getCost()) + ".");
scaredKitten.Help();
System.out.println("Who likes transports ships anyway?");
System.out.println();
System.out.println("Lets go ahead and switch over to a warship...");
System.out.println();
System.out.println("It appears that the default cost of this ship is $"
+ f.format(guerilla.getCost()));
guerilla.setCost(5000000);
System.out.println("That doesn't sound right, lets increase the cost to $"
+ f.format(guerilla.getCost()) );
guerilla.setTorpedos(10);
guerilla.setPhasers(100);
System.out.println("The warship has " + guerilla.getTorpedos() + " torpedos "
+ "and " + guerilla.getPhasers() + " phasers.\nYou probably shouldn't "
+ "try to attack with your transport ship.");
System.out.println("If you do, the warship may do " + guerilla.FireAway() +
" damage to your ship. So, don't attack the warship with "
+ "your transport ship.");
System.out.println();
System.out.println("Enough fighting, lets look at stars with the science "
+ "vessel ship...");
System.out.println();
magneto.setProbes(5);
System.out.println("This new ship has " + magneto.getScientist() + " scientist. "
+ "We have increased the number of probes to " + magneto.getProbes() +
".");
magneto.setName("MAGENTO");
System.out.println("The name is this ship is " + magneto.getShipName() + ".");
System.out.println("We are starting at " + magneto.getProbes() + " probes."
+ " Lets go ahead and launch some them into space!");
System.out.println("Starting with some turbulence...");
for (int i = magneto.getProbes(); i >= 0; i--)
{
magneto.launchProbe();
if (i != 0)
System.out.println("We now have " + magneto.getProbes() + " probes!");
}
}
}
/*
Matthew Kehoe
*/
public class ScienceVessel extends GenericShip
{
private int probes;
private int scientist;
private String chiefResearcher;
public ScienceVessel()
{
probes = 10;
scientist = 25;
chiefResearcher = "Cochrane";
}
public ScienceVessel(int probes, int scientist, String chiefResearcher)
{
this.probes = probes;
this.scientist = scientist;
this.chiefResearcher = chiefResearcher;
}
public void setProbes(int probes)
{
this.probes = probes;
}
public void setScientist(int scientist)
{
this.scientist = scientist;
}
public void setChiefResearcher(String chiefResearcher)
{
this.chiefResearcher = chiefResearcher;
}
public int getProbes()
{
return probes;
}
public int getScientist()
{
return scientist;
}
public String getChiefResearcher()
{
return chiefResearcher;
}
public void launchProbe()
{
if (probes > 0)
{
System.out.println("We�re launching the probe, sir.");
probes--;
}
else
{
System.out.println("Sorry sir there are no probes left.");
}
}
}
/*
Matthew Kehoe
*/
public class Transport extends GenericShip
{
private int materials;
public Transport()
{
materials = 100;
}
public Transport(int materials)
{
this.materials = materials;
}
public void setTransport(int materials)
{
this.materials = materials;
}
public int getTransport()
{
return materials;
}
public void Help()
{
System.out.println("Help! We're under attack!");
}
}
/*
Matthew Kehoe
*/
public class Warship extends GenericShip
{
private int torpedos;
private int phaserBanks;
private double shield;
private String captain;
public Warship()
{
torpedos = 2;
phaserBanks= 2;
shield = 100.0;
captain = "Snuggles";
}
public Warship(int torpedos, int phaserBanks, double shield, String captain)
{
this.torpedos = torpedos;
this.phaserBanks = phaserBanks;
this.shield = shield;
this.captain = captain;
}
public void setTorpedos(int torpedos)
{
this.torpedos = torpedos;
}
public void setPhasers(int phaserBanks)
{
this.phaserBanks = phaserBanks;
}
public void setShieldStrength(double shield)
{
this.shield = shield;
}
public void setCaptain(String captain)
{
this.captain = captain;
}
public int getTorpedos()
{
return torpedos;
}
public int getPhasers()
{
return phaserBanks;
}
public double getShieldStrength()
{
return shield;
}
public String getCaptain()
{
return captain;
}
public int FireAway()
{
int damage;
damage = (int) (Math.random() * 100 + 1);
return damage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment