Advertisement
daniel_lawson9999

All of chapter 8

Oct 19th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.83 KB | None | 0 0
  1. All things from chapter 8:
  2.  
  3. 8 self check:
  4. Daniel Lawson
  5. 9/18/2016
  6.  
  7. 1.
  8. Procedural programming involves executing tasks in a given order. Object oriented programming uses the objects to interact with each other.
  9. 2.
  10. A class is used to create a object. A object has various behaviors and information that can be used and changed.
  11. 3.
  12. 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.
  13. 4.
  14. 14 14
  15. 7 9 14 2
  16. 18 18
  17. 7 9 14 18
  18. 5.
  19. 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.
  20. The state would be the calculator's storing of inputed values and those previously stored in its memory.
  21. 6.
  22. 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.
  23. 7.
  24. public class Name{
  25. private String firstName;
  26. private String lastName;
  27. private char middleInitial;
  28. }
  29. 8.
  30. 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.
  31. 9.
  32. d
  33. 10.
  34.  public double distance(Point other){
  35.         int dx = this.x -other.x;
  36.         int dy = this.y -other.y;
  37.         return Math.sqrt((dx*dx)*(dy*dy));
  38.     }
  39.  11.
  40.  public class Name{
  41.  private String firstName;
  42.  private String lastName;
  43.  private char middleInitial;
  44.  
  45.  public String getNormalOrder(){
  46.  return (firstName + " " +middleInitial +". " + lastName);
  47.  }
  48.  public String getReverseOrder(){
  49.   return (lastName + ", " + firstName + " " +middleInitial +"." );
  50.  }
  51.  }
  52.  12.
  53.  Create a toString method
  54.  13.
  55.  c
  56.  14.
  57.  public String toString(){
  58.          return ("(" +this.x +"," +this.y + ")");
  59.      }
  60.  15.
  61.  public String toString(){
  62.     return (firstName + " " +middleInitial +". " + lastName);
  63.  }
  64.  16.
  65.  Point p1 = new Point(8,2);
  66.  Point p2 = new Point(4,3);
  67.  System.out.println("p1 is " +p1);
  68.  System.out.println("p2 is " +p2);
  69.  System.out.println("p1's distance from origin is " + p1.distance(new Point(0,0)));
  70.  p1.translate(1,2);
  71.  p2.translate(-1,10);
  72.  System.out.println("p1 is now " + p1);
  73.  System.out.println("p2 is now " + p2);
  74.  
  75. Daniel Lawson
  76. 9/22/2016
  77.  
  78.  
  79. 17.
  80. A constructor is used to initialize the fields of a class when the instance is createed
  81. 18.
  82. First a constructor is not supposed to be void (the header should be public Point(initialX, initial)
  83. and y they created and assigned the values to a new integers called x and y in the following constructor Instead of assigning x and y to initialx and initial y to the fields x
  84. 19.
  85. public class Name{
  86.  
  87.     private String firstName;
  88.     private String lastName;
  89.     private char middleInitial;
  90.  
  91.     public Name(String f, char m, String l){
  92.         firstName = f;
  93.         MiddleInitial = m;
  94.         lastName = l;
  95.  
  96.     }
  97.  
  98.     public String getNormalOrder(){
  99.         return (firstName + " " +middleInitial +". " + lastName);
  100.     }
  101.     public String getReverseOrder(){
  102.         return (lastName + ", " + firstName + " " +middleInitial +"." );
  103.     }
  104.  }
  105. 20.
  106. this is used to speficy what you are refering to.This is used to refer to a field instead of a variable. This is also used to call a method that you are in and with the same or different parameters
  107. 21.
  108. Public Point(Point p){
  109.     this.x = p.getX();
  110.     this.y = p.getX();
  111. }
  112. 22.
  113. Abstraction allows the client to know how to use something without knowing how it internally works.
  114. 23.
  115. Public allows things to be accessed outside the program while private does not. You should make a class's fields private.
  116. 24.
  117. By uses a accessor method to see the values of the fields
  118. 25.
  119. public void setX(int x){
  120. this.x = x;
  121. }
  122. public void setY(int y){
  123. this.y = y;
  124. }
  125. 26.
  126. public class Name{
  127.  
  128.    private String firstName;
  129.    private String lastName;
  130.    private char middleInitial;
  131.  
  132.    public Name(String f, char m, String l){
  133.        firstName = f;
  134.        MiddleInitial = m;
  135.        lastName = l;
  136.  
  137.    }
  138.  
  139.    public String getNormalOrder(){
  140.        return (firstName + " " +middleInitial +". " + lastName);
  141.    }
  142.    public String getReverseOrder(){
  143.        return (lastName + ", " + firstName + " " +middleInitial +"." );
  144.    }
  145.    public String getLastName(){
  146.        return lastName();
  147.    }
  148.    public String getFirstName(){
  149.        return firstName();
  150.    }
  151.    public char getMiddleInitial(){
  152.        return middleInitial();
  153.    }
  154. }
  155. 27.
  156. public void setFirstName(String firstName){
  157.    this.firstName = firstName;
  158. }
  159. public void setLastName(String lastName){
  160.    this.lastName = lastName;
  161. }
  162. public void setMiddleInitial(char middleInitial){
  163.    this.middleInitial = middleInitial;
  164. }
  165. 28.
  166. encapsulates makes it so the class is easy to use and values are not set to what they are not intended to be set at.
  167. 29.
  168. cohesion allows the class to represent a single clear abstraction.
  169. 30.
  170. Not including i/o allows the class to have more cohesion.
  171. 31.
  172. public String getSymbol(){
  173.    return symbol;
  174. }
  175. public int getTotalShares(){
  176.    return totalShares;
  177. }
  178. public double getTotalCost(){
  179.    return getTotalCost;
  180. }
  181.  
  182. Exercises:
  183.  
  184. Daniel Lawson
  185. 9/22/2016
  186.  
  187. Exercise 2:
  188. public void flip(){
  189.    int tempX = x;
  190.    x = -y;
  191.    y = -tempX;
  192. }
  193. Exercise 3:
  194. public int manhattanDistance(Point other){
  195. return (Math.abs(x-other.getX()) + Math.abs(y-other.getY()));
  196. }
  197. Exercise 4:
  198. public boolean isVertical(Point other){
  199. return(x == other.getX());
  200. }
  201. Exercise 5:
  202. public double slope(Point other){
  203. if(x == other.getX())throw new IllegalArgumentException("Your x values can't be the same!");
  204. return Math.round((((y2-y1)/(x2-x1)) * 10000.0) / 10000.0); //return((y-other.getY())/(x-other.getX));
  205. }
  206. Exercise 6:
  207. public boolean isCollinear(Point p1, Point p2){
  208. return(slope(p1) == slope(p1));
  209. }
  210. RAW Paste Data
  211.  
  212. Daniel Lawson
  213. 9/22/2016
  214.  
  215. Exercise 2:
  216. public void flip(){
  217.    int tempX = x;
  218.    x = -y;
  219.    y = -tempX;
  220. }
  221. Exercise 3:
  222. public int manhattanDistance(Point other){
  223. return (Math.abs(x-other.getX()) + Math.abs(y-other.getY()));
  224. }
  225. Exercise 4:
  226. public boolean isVertical(Point other){
  227. return(x == other.getX());
  228. }
  229. Exercise 5:
  230. public double slope(Point other){
  231. if(x == other.getX())throw new IllegalArgumentException("Your x values can't be the same!");
  232. return Math.round((((y2-y1)/(x2-x1)) * 10000.0) / 10000.0); //return((y-other.getY())/(x-other.getX));
  233. }
  234. Exercise 6:
  235. public boolean isCollinear(Point p1, Point p2){
  236. return(slope(p1) == slope(p1));
  237. }
  238.  
  239. Bank account:
  240. /**
  241. * Created by danie on 9/25/2016.
  242. */
  243. public class bankAccount {
  244.    private String name;
  245.    private double balance;
  246.    private double transsactionFee;
  247.  
  248.    public void deposit(double amount){
  249.        balance +=amount;
  250.    }
  251.    public void withdraw(double amount){
  252.        if(balance - amount - transsactionFee >=0)balance -= (amount + transsactionFee);
  253.    }
  254.    public String toString(){
  255.  
  256.        return (name + ", $" +balance);
  257.    }
  258.    public void transfer(bankAccount other, double amount){
  259.        if(amount >=5){
  260.            balance -= 5;
  261.            if(balance>=amount){
  262.                balance-=amount;
  263.                other.deposit(amount);
  264.            }
  265.            else{
  266.                other.deposit(balance);
  267.                balance = 0;
  268.            }
  269.        }
  270.    }
  271. }
  272. Rectangle class:
  273. //18-22 rectangle class
  274. public class Rectangle {
  275.    private int x;
  276.    private int y;
  277.    private int width;
  278.    private int height;
  279.    public Rectangle(int x, int y,int width,int height){
  280.        this.x = x;
  281.        this.y = y;
  282.        if(width <0 || height <0)throw new IllegalArgumentException("The width and height must be positive");
  283.        this.height = height;
  284.        this.width = width;
  285.    }
  286.    public Rectangle(Point p, int width, int height){
  287.        x = p.getX();
  288.        x = p.getY();
  289.        if(width <0 || height <0)throw new IllegalArgumentException("The width and height must be positive");
  290.        this.height = height;
  291.        this.width = width;
  292.    }
  293.    public int getHeight(){
  294.        return height;
  295.    }
  296.    public int getWidth(){
  297.        return width;
  298.    }
  299.    public int getX(){
  300.        return x;
  301.    }
  302.    public int getY(){
  303.        return y;
  304.    }
  305.    public boolean contains(int x, int y){
  306.        return(x>=this.x && x <= this.x+width && y>=this.y && y <=this.y +height);
  307.    }
  308.    public boolean contains(Point p){
  309.        return(p.getX()>=this.x && p.getX() <= this.x+width && p.getY()>=this.y && p.getY() <=this.y +height);
  310.    }
  311.    // I don't think the formula for the length/width of the union and intersections are correct
  312.     public Rectangle union(Rectangle rect){
  313.         return new Rectangle(Math.min(x,rect.getX()),Math.min(y,rect.getY()),Math.max(x+width,rect.getX()+rect.getWidth())- Math.max(x,rect.getX()),Math.max(y+height,rect.getY()+rect.getHeight())- Math.max(y,rect.getY()));
  314.     }
  315.     public Rectangle intersection(Rectangle rect){
  316.         return new Rectangle(Math.max(y,rect.getY()),Math.max(x,rect.getX()),Math.min(x+width,rect.getX()+rect.getWidth())- Math.min(x,rect.getX()),Math.min(y+height,rect.getY()+rect.getHeight())- Math.min(y,rect.getY()));
  317.     }
  318.  
  319. }
  320.  
  321. Grocery list / GroceryItem (Project 3):
  322.  
  323. /**
  324.  * Created by danie on 9/29/2016.
  325.  */
  326. public class GroceryItemOrder {
  327.     private String name;
  328.     private int quantity;
  329.     private double pricePerUnit;
  330.     public GroceryItemOrder(String name, int quantity, double pricePerUnit){
  331.         this.name = name;
  332.         this.quantity =  quantity;
  333.         this.pricePerUnit = pricePerUnit;
  334.     }
  335.     public double getCost(){
  336.         return quantity * pricePerUnit;
  337.     }
  338.     public void setQuantity(int quantity){
  339.         this.quantity = quantity;
  340.     }
  341.     public void setPricePerUnit(int cost){
  342.         pricePerUnit = cost;
  343.     }
  344.  
  345. }
  346.  
  347. /**
  348.  * Created by danie on 9/29/2016.
  349.  */
  350. public class GroceryList {
  351.   public static final int size = 10;
  352.   private GroceryItemOrder[] total = new GroceryItemOrder[size];
  353.   private int count = 0;
  354.  
  355.     public GroceryList(){
  356.         for(int i = 0;i<total.length;i++)total[i] = new GroceryItemOrder("null",0,0);
  357.     };
  358.     public void add(GroceryItemOrder item){
  359.         total[count] = item;
  360.         count++;
  361.     }
  362.     public double getTotalCost(){//returns the total cost of all the items
  363.         double sum = 0;
  364.         for(int i = 0;i< total.length; i++)sum+=total[i].getCost();
  365.         return sum;
  366.     }
  367. }
  368. Project 1 (RationalNumber):
  369. /**
  370.  * Created by danie on 9/28/2016.
  371.  */
  372. public class RationalNumber {
  373.     private int numerator;
  374.     private int denominator;
  375.     public RationalNumber(int n, int d){
  376.         if(d==0)throw new IllegalArgumentException("0 can't be in the numerator");
  377.         numerator = n;
  378.         denominator = d;
  379.     }
  380.     public RationalNumber() {
  381.         numerator = 0;
  382.         denominator = 1;
  383.     }
  384.     public int getDenominator(){
  385.         return denominator;
  386.     }
  387.     public int getNumerator(){
  388.         return numerator;
  389.     }
  390.     public void setDenominator(int d){
  391.         if(d == 0)throw new IllegalArgumentException("0 can't be in the denominator");
  392.         denominator = d;
  393.         for(int i = numerator ;i>=1;i--){ //4/8 1/2 4/10  8/2
  394.             if(denominator % i ==0 && numerator % i ==0){
  395.                 denominator /= i;
  396.                 numerator /= i;
  397.             }
  398.         }
  399.  
  400.     }
  401.     public void setNumerator(int n){
  402.         numerator = n;
  403.         for(int i = numerator ;i>=1;i--){ //4/8 1/2 4/10  8/2
  404.             if(denominator % i ==0 && numerator % i ==0){
  405.                 denominator /= i;
  406.                 numerator /= i;
  407.             }
  408.         }
  409.     }
  410.     public String toString(){
  411.         if(denominator < 0 ){
  412.             denominator = -denominator;
  413.             numerator  = -numerator;
  414.         }
  415.         if(numerator == 0){
  416.             return ("0");
  417.         }
  418.         if(denominator == 1){
  419.             return numerator +"";
  420.         }
  421.         else {
  422.             return (numerator + "/" + denominator);
  423.         }
  424.  
  425.     }
  426.  
  427.  
  428.  
  429.  
  430. }
  431. Project 2 (Date class):
  432. /**
  433.  * Created by danie on 9/28/2016.
  434.  */
  435. public class Date {
  436.     private int year;
  437.     private int month;
  438.     private int day;
  439.     public Date(int y,int m, int d){
  440.         if(y<0||m<1||m>12||d>31||d<28)throw new IllegalArgumentException("Invalid values for y,m,d ");
  441.         year = y;
  442.         month = m;
  443.         day  = d;
  444.     }
  445.     public void addDays(int d){
  446.         day *= d;
  447.         int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  448.         while(day>monthDays[month-1]){
  449.             if(month ==12 && day>31){
  450.                 day -= 31;
  451.                 month = 1;
  452.                 year++;
  453.             }
  454.             if(isLeapYear() && month == 2 && day>29){
  455.                 day-=29;
  456.                 month++;
  457.             }
  458.             else {
  459.                 day -= monthDays[month - 1];
  460.                 month++;
  461.             }
  462.  
  463.         }
  464.     }
  465.     public void addWeeks(int w){
  466.         addDays(7*w);
  467.     }
  468.     public int daysTo(Date other){
  469.         return Math.abs(calcDays() - other.calcDays());
  470.     }
  471.     public int calcDays(){
  472.         int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  473.         int totalMonthDays = 0;
  474.         for(int i = 0;i<month;i++){
  475.             totalMonthDays+=monthDays[i];
  476.         }
  477.         int totalYearDays = year*365 + year/4;
  478.         return totalMonthDays + totalYearDays + day;
  479.     }
  480.     public int getDay(){
  481.         return day;
  482.     }
  483.     public int getMonth(){
  484.         return month;
  485.     }
  486.     public int getYear(){
  487.         return year;
  488.     }
  489.     public boolean isLeapYear(){
  490.         return ((year % 4 ==0 && year % 100 != 0)|| year % 400 == 0);
  491.     }
  492.     public String toString(){
  493.         return (year + "/" + month+ "/" + day);
  494.     }
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement