daniel_lawson9999

Daniel Lawson Chapter 8 Self Check 1-16

Sep 18th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. Daniel Lawson
  2. 9/18/2016
  3.  
  4. 1.
  5. Procedural programming involves executing tasks in a given order. Object oriented programming uses the objects to interact with each other.
  6. 2.
  7. A class is used to create a object. A object has various behaviors and information that can be used and changed.
  8. 3.
  9. The state is the data of the string class wich are the various characters that make up the string. The behaviors are the methods like charAt,indexOf,substring,etc.
  10. 4.
  11. 14 14
  12. 7 9 14 2
  13. 18 18
  14. 7 9 14 18
  15. 5.
  16. The calculator's behaviors would be its ability to calculate operations like addition, multiplication, subtraction, division. Another behavior would be to print out the results of a operation.
  17. The state would be the calculator's storing of inputed values and those previously stored in its memory.
  18. 6.
  19. Fields are in a class while parameters are values that are passed through methods. The field of a class is normally made private for encapsulation.
  20. 7.
  21. public class Name{
  22. private String firstName;
  23. private String lastName;
  24. private char middleInitial;
  25. }
  26. 8.
  27. An accessor gets a value from the object and returns it, a mutator changes a value of the object. "get" is used for accessors while "set" is used for mutators.
  28. 9.
  29. d
  30. 10.
  31.  public double distance(Point other){
  32.         int dx = this.x -other.x;
  33.         int dy = this.y -other.y;
  34.         return Math.sqrt((dx*dx)*(dy*dy));
  35.     }
  36.  11.
  37.  public class Name{
  38.  private String firstName;
  39.  private String lastName;
  40.  private char middleInitial;
  41.  
  42.  public String getNormalOrder(){
  43.  return (firstName + " " +middleInitial +". " + lastName);
  44.  }
  45.  public String getReverseOrder(){
  46.   return (lastName + ", " + firstName + " " +middleInitial +"." );
  47.  }
  48.  }
  49.  12.
  50.  Create a toString method
  51.  13.
  52.  c
  53.  14.
  54.  public String toString(){
  55.          return ("(" +this.x +"," +this.y + ")");
  56.      }
  57.  15.
  58.  public String toString(){
  59.     return (firstName + " " +middleInitial +". " + lastName);
  60.  }
  61.  16.
  62.  Point p1 = new Point(8,2);
  63.  Point p2 = new Point(4,3);
  64.  System.out.println("p1 is " +p1);
  65.  System.out.println("p2 is " +p2);
  66.  System.out.println("p1's distance from origin is " + p1.distance(new Point(0,0)));
  67.  p1.translate(1,2);
  68.  p2.translate(-1,10);
  69.  System.out.println("p1 is now " + p1);
  70.  System.out.println("p2 is now " + p2);
Advertisement
Add Comment
Please, Sign In to add comment