{"id":26561,"date":"2021-04-28T23:10:07","date_gmt":"2021-04-29T03:10:07","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/a-ship-class-illustrating-object-oriented-programming-concepts-programming-code-examples-java-j2ee-j2me-object-oriented-programming\/"},"modified":"2021-04-28T23:10:07","modified_gmt":"2021-04-29T03:10:07","slug":"a-ship-class-illustrating-object-oriented-programming-concepts-programming-code-examples-java-j2ee-j2me-object-oriented-programming","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26561","title":{"rendered":"A Ship class illustrating object-oriented programming concepts #Programming Code Examples #Java\/J2EE\/J2ME #Object Oriented Programming"},"content":{"rendered":"<pre>\n************************\nShip.java A Ship class illustrating object-oriented programming concepts. Incorporates Javadoc comments. See ShipTest.java for a test. \n************************\n\/** Ship example to demonstrate OOP in Java.\n *\n *  @author <a HREF=\"larry@corewebprogramming.com&quot;\">\n *          Larry Brown<\/a>\n *  @version 2.0\n *\/\n\npublic class Ship {\n  \/\/ Instance variables\n\n  private double x=0.0, y=0.0, speed=1.0, direction=0.0;\n  private String name;\n\n  \/\/ Constructors\n\n  \/** Build a ship with specified parameters. *\/\n\n  public Ship(double x, double y, double speed,\n              double direction, String name) {\n    setX(x);\n    setY(y);\n    setSpeed(speed);\n    setDirection(direction);\n    setName(name);\n  }\n\n  \/** Build a ship with default values\n   *  (x=0, y=0, speed=1.0, direction=0.0).\n   *\/\n\n  public Ship(String name) {\n    setName(name);\n  }\n\n  \/** Move ship one step at current speed\/direction. *\/\n\n  public void move() {\n    moveInternal(1);\n  }\n\n  \/** Move N steps. *\/\n\n  public void move(int steps) {\n    moveInternal(steps);\n  }\n\n  private void moveInternal(int steps) {\n    double angle = degreesToRadians(direction);\n    x = x + (double)steps * speed * Math.cos(angle);\n    y = y + (double)steps * speed * Math.sin(angle);\n  }\n\n  private double degreesToRadians(double degrees) {\n    return(degrees * Math.PI \/ 180.0);\n  }\n\n  \/** Report location to standard output. *\/\n\n  public void printLocation() {\n    System.out.println(getName() + &quot; is at (&quot; + getX() +\n                       &quot;,&quot; + getY() + &quot;).&quot;);\n  }\n\n  \/** Get current X location. *\/\n\n  public double getX() {\n    return(x);\n  }\n\n  \/** Set current X location. *\/\n\n  public void setX(double x) {\n    this.x = x;\n  }\n\n  \/** Get current Y location. *\/\n\n  public double getY() {\n    return(y);\n  }\n\n  \/** Set current Y location. *\/\n\n  public void setY(double y) {\n    this.y = y;\n  }\n\n  \/** Get current speed. *\/\n\n  public double getSpeed() {\n    return(speed);\n  }\n\n  \/** Set current speed. *\/\n\n  public void setSpeed(double speed) {\n    this.speed = speed;\n  }\n\n  \/** Get current heading (0=East, 90=North, 180=West,\n   *  270=South).  I.e., uses standard math angles, <b>not<\/b>\n   *  nautical system where 0=North, 90=East, etc.\n   *\/\n\n  public double getDirection() {\n    return(direction);\n  }\n\n  \/** Set current direction (0=East, 90=North, 180=West,\n   *  270=South). I.e., uses standard math angles,<b>not<\/b>\n   *  nautical system where 0=North,90=East, etc.\n   *\/\n\n  public void setDirection(double direction) {\n    this.direction = direction;\n  }\n\n  \/** Get Ship's name. Can't be modified by user. *\/\n\n  public String getName() {\n    return(name);\n  }\n\n  private void setName(String name) {\n    this.name = name;\n  }\n}\n*********************\nShipTest.java \n*********************\npublic class ShipTest {\n public static void main(String[] args) {\n    Ship s1 = new Ship(&quot;Ship1&quot;); \n    Ship s2 = new Ship(0.0, 0.0, 2.0, 135.0, &quot;Ship2&quot;);\n    s1.move();\n    s2.move(3);\n    s1.printLocation();\n    s2.printLocation();\n  }\n}\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10391<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Object Oriented Programming<br \/>Tags:Java\/J2EE\/J2MEObject Oriented Programming<br \/> Post Data:2017-01-02 16:04:39<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>************************ Ship.java A Ship class illustrating object-oriented programming concepts. Incorporates Javadoc comments. See ShipTest.java for a test. ************************ \/** Ship example to demonstrate OOP in Java. * * @author * Larry Brown * @version 2.0 *\/ public class Ship { \/\/ Instance variables private double x=0.0, y=0.0, speed=1.0, direction=0.0; private String name; \/\/ Constructors \/** &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26561\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26561","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10532,"url":"http:\/\/bangla.sitestree.com\/?p=10532","url_meta":{"origin":26561,"position":0},"title":"A Ship class illustrating object-oriented programming concepts","author":"","date":"August 29, 2015","format":false,"excerpt":"************************ Ship.java A Ship class illustrating object-oriented programming concepts. Incorporates Javadoc comments. See ShipTest.java for a test. ************************ \/** Ship example to demonstrate OOP in Java. \u00a0* \u00a0*\u00a0 @author \u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Larry Brown \u00a0*\u00a0 @version 2.0 \u00a0*\/ public class Ship { \u00a0 \/\/ Instance variables \u00a0 private double x=0.0, y=0.0, speed=1.0,\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26557,"url":"http:\/\/bangla.sitestree.com\/?p=26557","url_meta":{"origin":26561,"position":1},"title":"Accesses instance variables in a Ship object. #Programming Code Examples #Java\/J2EE\/J2ME #Object Oriented Programming","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"Test1.java Accesses instance variables in a Ship object. ******************************************************** \/\/ Create a class with five instance variables (fields): \/\/ x, y, speed, direction, and name. Note that Ship1 is \/\/ not declared \"public\", so it can be in the same file as \/\/ Test1. A Java file can only contain\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26563,"url":"http:\/\/bangla.sitestree.com\/?p=26563","url_meta":{"origin":26561,"position":2},"title":"Speedboat.java Illustrates inheritance from Ship class #Programming Code Examples #Java\/J2EE\/J2ME #Object Oriented Programming","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"***************************** Speedboat.java Illustrates inheritance from Ship class. See SpeedboatTest.java for a test. ***************************** \/** A fast Ship. Red and going 20 knots by default. * *********************** public class Speedboat extends Ship { private String color = \"red\"; \/** Builds a red Speedboat going N at 20 knots. *\/ public Speedboat(String\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10528,"url":"http:\/\/bangla.sitestree.com\/?p=10528","url_meta":{"origin":26561,"position":3},"title":"Accesses instance variables in a Ship object.","author":"","date":"August 29, 2015","format":false,"excerpt":"Test1.java Accesses instance variables in a Ship object. ******************************************************** \/\/ Create a class with five instance variables (fields): \/\/ x, y, speed, direction, and name. Note that Ship1 is \/\/ not declared \"public\", so it can be in the same file as \/\/ Test1. A Java file can only contain\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10303,"url":"http:\/\/bangla.sitestree.com\/?p=10303","url_meta":{"origin":26561,"position":4},"title":"Accesses methods in a Ship2 object","author":"","date":"August 26, 2015","format":false,"excerpt":"********************************************* Test2.java Accesses methods in a Ship2 object ********************************************* \/\/ Give the ship public move and printLocation methods. class Ship2 { \u00a0 public double x=0.0, y=0.0, speed=1.0, direction=0.0; \u00a0 public String name = \"UnnamedShip\"; \u00a0 private double degreesToRadians(double degrees) { \u00a0\u00a0\u00a0 return(degrees * Math.PI \/ 180.0); \u00a0 } \u00a0 public\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26559,"url":"http:\/\/bangla.sitestree.com\/?p=26559","url_meta":{"origin":26561,"position":5},"title":"Demonstrates overloading methods in class Ship4 #Programming Code Examples #Java\/J2EE\/J2ME #Object Oriented Programming","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"********************* class Ship4 { public double x=0.0, y=0.0, speed=1.0, direction=0.0; public String name; \/\/ This constructor takes the parameters explicitly. public Ship4(double x, double y, double speed, double direction, String name) { this.x = x; this.y = y; this.speed = speed; this.direction = direction; this.name = name; } \/\/ This\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26561"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26561\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26561"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}