daniel_lawson9999

All of Chapter 9

Nov 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.89 KB | None | 0 0
  1.  
  2.  
  3. DANIEL LAWSON CHAPTER 9 SELF CHECKS, EXERCISES, PROJECTS
  4. //////////
  5. Self Check:
  6. Daniel Lawson
  7. 9.1 evens
  8.  
  9. 2. Overloading is when you have two methods with the same name in a class but wih different parameters.
  10. Overiding is creating a method that already has the same name in the parent class.
  11.  
  12. 4.
  13. a, b, c, d are legal
  14.  
  15. 9.2
  16.  
  17. 6.
  18.  
  19. It can call the set age method but the age and name fields are private so it cannot directly refer to it.
  20.  
  21. 8.
  22.  
  23. public void setAge(int age){
  24.    super.setAge(age);
  25.    year++;
  26.  
  27. }
  28.  
  29. 10.
  30. outputs:
  31.  
  32. vroomvroom
  33. truck 1
  34. car 1
  35.  
  36. 9.3
  37.  
  38. 12.
  39.  flute
  40.  shoe 1
  41.  flute 2
  42.  
  43.  flute
  44.  blue 1
  45.  flute 2
  46.  
  47.  moo
  48.  moo 1
  49.  moo 2
  50.  
  51.  moo
  52.  blue 1
  53.  boo m2
  54.  
  55. 14.
  56. squid
  57. creature 1
  58. tentacles
  59.  
  60. BIG!
  61. spout
  62. create 2
  63.  
  64. ocean-dwelling
  65. create 1
  66. create 2
  67.  
  68. ocean-dwelling
  69. warm-blooded
  70. creature 2
  71.  
  72. 16.
  73. Bay 1 Pond 2
  74. Ocean 2
  75. Lake 3 Ocean 2
  76.  
  77. Pond 1
  78. Pond 2
  79. Pond 3
  80.  
  81. Pond 1
  82. Pond 2
  83. Pond 3 Pond 2
  84.  
  85. Bay 1 Pond 2
  86. Bay 2
  87. Lake 3 Bay 2
  88.  
  89. 18.
  90. In a "is-a" relationship one class extends another, in a "has-a" relationship one class uses another class as a field.
  91. You declare the object like you would a normal field.
  92.  
  93. 20.
  94. It is a bad design because you have to create so many different classes. Instead you could make one class for a generic card
  95. and then possibly have another class called deck with stores large amounts of cards.
  96.  
  97. 9.5
  98.  
  99. 22.
  100.  
  101. Implementing an interface allows for a class to be manipulated in the client class similar to if it was extending a client,
  102. except it does not inherent methods and fields.
  103.  
  104. 24.
  105. interfaces can't have fields or have actual methods
  106. correct interface:
  107.  
  108. public interface Colored{
  109.        public Color getColor();
  110. }
  111.  
  112. 26.
  113. // in shape
  114. public int getSideCount();
  115.  
  116. //in triangle
  117. public int getSideCount(){
  118.    return 3;
  119. }
  120. // in rectangle
  121. public int getSideCount(){
  122.    return 4;
  123. }
  124. //in circle
  125. public int getSideCount(){
  126.    return 0;
  127. }
  128.  
  129. 9.6
  130.  
  131. 28.
  132. The OrdereredByLength class has the GetElement method which it inherented and it also must have a arrange method because it was abstracted in the inherited class
  133.  
  134.  
  135.  
  136.  
  137.  
  138. //////////////////////////
  139. Ticket classes
  140. /**
  141. * Created by danie on 10/27/2016.
  142. */
  143. public class Ticket {
  144.    private int number;
  145.    public Ticket(int number){
  146.        this.number = number;
  147.    }
  148.    public double getPrice(){
  149.        return 50.0;
  150.    }
  151.    public String toString(){
  152.        return "Number: " + number + ", Price: " + getPrice();
  153.    }
  154. }
  155.  
  156. /**
  157. * Created by danie on 10/27/2016.
  158. */
  159. public class AdvanceTicket extends Ticket {
  160.    private int days;
  161.    public AdvanceTicket(int number, int days){
  162.        super(number);
  163.        this.days = days;
  164.    }
  165.    public double getPrice(){
  166.        if(days>=10){
  167.            return super.getPrice() - 20;
  168.        }
  169.        else{
  170.            return super.getPrice() - 10;
  171.        }
  172.    }
  173. }
  174.  
  175. /**
  176. * Created by danie on 10/27/2016.
  177. */
  178. public class WalkupTicket extends Ticket {
  179.    public WalkupTicket(int number){
  180.        super(number);
  181.    }
  182. }
  183.  
  184. /**
  185. * Created by danie on 10/27/2016.
  186. */
  187. public class StudentAdvanceTicket extends AdvanceTicket{
  188.    public StudentAdvanceTicket(int number, int days){
  189.        super(number,days);
  190.    }
  191.    public double getPrice(){
  192.        return super.getPrice() / 2;
  193.    }
  194.    public String toString(){
  195.        return super.toString() + " (ID REQUIRED)";
  196.    }
  197. }
  198. /////////////////////////////////////////
  199. shape and shape 3D
  200. shape:
  201. /**
  202. * Created by danie on 10/26/2016.
  203. */
  204. public interface Shape {
  205.    public double area();
  206.    public double perimeter();
  207.    //public void drawShape();
  208.  
  209. }
  210.  
  211. /**
  212. * Created by danie on 10/26/2016.
  213. */
  214. import java.awt.*;
  215. public class Circle implements Shape{
  216.    private double r;
  217.    public Circle(double r){
  218.        this.r = r;
  219.    }
  220.    public double getRadius(){
  221.        return r;
  222.    }
  223.    public double area(){
  224.        return r*r*Math.PI;
  225.    }
  226.    public double perimeter(){
  227.        return 2*Math.PI*r;
  228.    }
  229.  /*  public void drawShape(Graphics g, int x, int y, int w, int l){
  230.        g.fillOval(x,y,w,l);
  231.    }
  232. */
  233. }
  234. /**
  235. * Created by danie on 10/26/2016.
  236. */
  237. import java.awt.*;
  238. public class Rectangle implements Shape{
  239.    private double b;
  240.    private double h;
  241.    public Rectangle(double b, double h){
  242.        this.b = b;
  243.        this.h = h;
  244.    }
  245.    public double area(){
  246.        return b*h;
  247.    }
  248.    public double perimeter(){
  249.        return b*2 + h*2;
  250.    }
  251.   /* public void drawShape(Graphics g, int x, int y, int l, int w){
  252.        g.drawRect(x,y,l,w);
  253.    }*/
  254. }
  255.  
  256. /**
  257. * Created by danie on 10/30/2016.
  258. */
  259. public class Square extends Rectangle {
  260.    public Square(double l){
  261.        super(l,l);
  262.    }
  263. }
  264.  
  265. /**
  266. * Created by danie on 10/26/2016.
  267. */
  268. import java.awt.*;
  269. public class Triangle implements Shape{
  270.    private double a;
  271.    private double b;
  272.    private double c;
  273.    public Triangle(double a,double b,double c){
  274.        this.a = a;
  275.        this.b = b;
  276.        this.c = c;
  277.    }
  278.    public double area(){
  279.        double s = (a+b+c)/2;
  280.        return Math.sqrt(s*(s-a)*(s-b)*(s-c));
  281.    }
  282.    public double perimeter(){
  283.        return a + b + c;
  284.    }
  285.   /* public void drawShape(Graphics g, int x1, int y1, int x2 , int y2, int x3, int y3){
  286.        g.drawPolygon(new int[] {x1,x2,x3}, new int[] {y1, y2, y3}, 3);
  287.    }*/
  288. }
  289. SHAPE 3D
  290. /**
  291. * Created by danie on 10/28/2016.
  292. */
  293. public interface Shape3D {
  294.    public double getVolume();
  295.    public double getSurfaceArea();
  296. }
  297. /*
  298.                                                    Shape3D interface
  299.            Things with two bases                |                      sphere            |                cones
  300. rectangular prism | triangular prism | cylinders|
  301.        cube|
  302.  
  303.  
  304.  
  305.  */
  306.  
  307. /**
  308. * Created by danie on 10/28/2016.
  309. */
  310. public abstract class TwoBases implements Shape3D {
  311.    private Shape base;
  312.    private double height;
  313.    public TwoBases(Shape base, double height){
  314.        this.base = base;
  315.        this.height = height;
  316.    }
  317.    public double getVolume(){
  318.        return base.area() * height;
  319.    }
  320.    public double getSurfaceArea(){
  321.        return height * base.perimeter() + 2 * base.area();
  322.    }
  323. }
  324. /**
  325. * Created by danie on 10/30/2016.
  326. */
  327. public class TriangularPrism extends TwoBases{
  328.    public TriangularPrism(Triangle b, double h){
  329.        super(b,h);
  330.    }
  331. }
  332. /**
  333. * Created by danie on 10/30/2016.
  334. */
  335. public class RectangularPrism extends TwoBases {
  336.    public RectangularPrism(Rectangle base, double height){
  337.        super(base,height);
  338.    }
  339. }
  340.  
  341. /**
  342. * Created by danie on 10/30/2016.
  343. */
  344. public class Cube extends RectangularPrism {
  345.    public Cube(Square s, double h){
  346.        super(s,h);
  347.    }
  348. }
  349.  
  350. /**
  351. * Created by danie on 10/30/2016.
  352. */
  353. public class Cylinder extends TwoBases{
  354.    private double radius;
  355.    public Cylinder(Circle c, double h){
  356.        super(c,h);
  357.        radius = c.getRadius();
  358.    }
  359.    public double getRadius(){
  360.        return radius;
  361.    }
  362. }
  363.  
  364. /**
  365. * Created by danie on 10/30/2016.
  366. */
  367. public class Sphere implements Shape3D {
  368.    private double radius;
  369.    public Sphere(double r){
  370.        radius = r;
  371.    }
  372.    public double getRadius(){
  373.        return radius;
  374.    }
  375.  
  376.    public double getVolume(){
  377.        // 4/3 πr3
  378.        return radius*radius*radius*Math.PI*4/3;
  379.    }
  380.    public double getSurfaceArea(){
  381.        //4πr2
  382.        return radius*radius*Math.PI*4;
  383.    }
  384. }
  385.  
  386. /**
  387. * Created by danie on 10/30/2016.
  388. */
  389. public class Cone implements Shape3D {
  390.    private Circle c;
  391.    private double height;
  392.    public Cone(Circle circ, double h){
  393.        c = circ;
  394.        height = h;
  395.    }
  396.    public double getRadius(){
  397.        return c.getRadius();
  398.    }
  399.    public double getVolume(){
  400.        //πr2 h/3
  401.        return c.area() + height/3;
  402.    }
  403.    public double getSurfaceArea(){
  404.        //πr(r+ sqrt(h2+r2) )
  405.        return c.area() + (Math.PI*c.getRadius())*(Math.sqrt(height*height + c.getRadius()*c.getRadius()));
  406.    }
  407. }
  408. END OF SHAPE AND SHAPE3D
  409. ////////////////////////////////////////////////////////////////////////////////////
  410. EMPLOYEE
  411. /**
  412. * Created by danie on 10/20/2016.
  413. */
  414. public class Employee {
  415.    private int year;
  416.    public Employee(int year){
  417.        year = this.year;
  418.    }
  419.    public int getHours(){
  420.        return 40;
  421.    }
  422.    public double getSalary(){
  423.        return 50000.0;
  424.    }
  425.    public int getVacationDays(){
  426.        return 10 + 2 *year;
  427.    }
  428.    public String getVacationForm(){
  429.        return "yellow";
  430.    }
  431.    public int getYear(){
  432.        return year;
  433.    }
  434. }
  435.  
  436. /**
  437. * Created by danie on 10/20/2016.
  438. */
  439. public class Secretary extends Employee{
  440.    public Secretary(){
  441.        super(0);
  442.    }
  443.    public void takeDictation(String text){
  444.        System.out.println("Taking dictation of text: " + text);
  445.    }
  446. }
  447. /**
  448. * Created by danie on 10/20/2016.
  449. */
  450. public class LegalSecretary extends Secretary {
  451.    public LegalSecretary(int years){
  452.    //
  453.    }
  454.    public double getSalary(){
  455.        return super.getSalary() + 5000.0;
  456.    }
  457.    public void fileLegalBriefs(){
  458.        System.out.println("I could file all day!");
  459.    }
  460. }
  461.  
  462. /**
  463. * Created by danie on 10/20/2016.
  464. */
  465. public class Lawyer extends Employee {
  466.    public Lawyer(int years){
  467.        super(years);
  468.    }
  469.    public int getVacationDays(){
  470.        return super.getVacationDays() + 5;
  471.    }
  472.    public String getVacationForm(){
  473.        return "pink";
  474.    }
  475.    public void sueSomeone(String target){
  476.        System.out.println("I will sue " + target);
  477.    }
  478.    public double getSalary(){
  479.        return super.getSalary() + 5000.0 * getYear();
  480.    }
  481. }
  482. /**
  483. * Created by danie on 10/20/2016.
  484. */
  485. public class Marketer extends Employee {
  486.    public Marketer(int years){
  487.        super(years);
  488.    }
  489.    public double getSalary(){
  490.        return super.getSalary() + 10000.0;
  491.    }
  492.    public void market(String thing){
  493.        System.out.println("Want to buy some " + thing);
  494.    }
  495.  
  496. }
  497.  
  498. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  499. CRITTER PROJECT
  500. MAIN CRITTER ABSTRACT CLASS:
  501. /**
  502. * Created by danie on 11/1/2016.
  503. */
  504. import java.util.*;
  505. import java.awt.*;
  506. public abstract class Critter {
  507.    private boolean alive = true;
  508.    Random r = new Random();
  509.    private int x;
  510.    private int y;
  511.    public Critter(int x,int y){
  512.        this.x = x;
  513.        this.y = y;
  514.    }
  515.    public int getX(){
  516.        return x;
  517.    }
  518.    public int getY(){
  519.        return y;
  520.    }
  521.    public void setX(int xc){
  522.        x = xc;
  523.    }
  524.    public void setY(int yc){
  525.        y = yc;
  526.    }
  527.    public boolean isAlive(){
  528.        return alive;
  529.    }
  530.    public void setAlive(boolean b){
  531.        alive = b;
  532.    }
  533.  
  534.    public static void collisions(Critter[] c,Graphics g ){
  535.        boolean[] hits = new boolean[c.length];
  536.        int box = 15;
  537.        for(int i = 0; i < c.length;i++){
  538.                for(int j = 0; j<c.length;j++){
  539.                    if(i!=j &&c[i].isAlive()&&c[j].isAlive()) {// if both are alive, share the same coordinates and not itself
  540.                        if (c[i].getX() <= c[j].getX() +box  &&   c[i].getX() >= c[j].getX() -box  && c[i].getY() >= c[j].getY()-box && c[i].getY() <= c[j].getY() +box){
  541.                            Random r = new Random();
  542.                            if (r.nextInt(2) == 1) {
  543.                                     c[j].setAlive(false);
  544.                                     g.setColor(Color.RED);
  545.                                     g.fillOval(c[j].getX(),c[j].getY(),10,10);
  546.                            } else {
  547.                                c[i].setAlive(false);
  548.                                g.setColor(Color.RED);
  549.                                g.fillOval(c[i].getX(),c[i].getY(),10,10);
  550.                            }
  551.                         }
  552.                    }
  553.                }
  554.        }
  555.    }
  556.    public abstract String toString();
  557.    public abstract void getMove(int scale);
  558.    public void constrain(int l){
  559.        if(getX()>l){
  560.            setX(0);
  561.        }
  562.        if(getX()<0){
  563.            setX(l);
  564.        }
  565.        if(getY()>l){
  566.            setY(0);
  567.        }
  568.        if(getY()<0){
  569.            setY(l);
  570.        }
  571.    }
  572.    //public static
  573. }
  574.  
  575. VARIOUS DIFFERENT ANIMALS:
  576. /**
  577. * Created by danie on 11/1/2016.
  578. */
  579. import java.util.*;
  580. public class Bird extends Critter {
  581.    public Bird(int x,int y){
  582.       super(x,y);
  583.    }
  584.    public void getMove(int scale){
  585.        switch(r.nextInt(4)){
  586.            case 0: setX(getX()-1*scale);
  587.                break;
  588.            case 1: setX(getX()+1*scale);
  589.                break;
  590.            case 2: setY(getY()-1*scale);
  591.                break;
  592.            case 3: setY(getY()+1*scale);
  593.                break;
  594.        }
  595.    }
  596.    public String toString(){
  597.        return "B";
  598.    }
  599. }
  600. /**
  601. * Created by danie on 11/1/2016.
  602. */
  603. public class Frog extends Critter{
  604.    private int move = 0;
  605.    private int count = 3;
  606.    public Frog(int x,int y){
  607.        super(x,y);
  608.    }
  609.    public void getMove(int scale){
  610.        if(count>=3){
  611.            move = r.nextInt(4);
  612.            count = 0;
  613.        }
  614.        switch(move){
  615.            case 0: setX(getX()-1*scale);
  616.                break;
  617.            case 1: setX(getX()+1*scale);
  618.                break;
  619.            case 2: setY(getY()-1*scale);
  620.                break;
  621.            case 3: setY(getY()+1*scale);
  622.                break;
  623.        }
  624.  
  625.    }
  626.    public String toString(){
  627.        return "F";
  628.    }
  629. }
  630. /**
  631. * Created by danie on 11/1/2016.
  632. */
  633. public class Mouse extends Critter{
  634.    private int move = 1;
  635.    public Mouse(int x,int y){
  636.        super(x,y);
  637.    }
  638.    public void getMove(int scale){
  639.        switch(move){
  640.            case 1: setX(getX()-1*scale);
  641.                move = 2;
  642.                break;
  643.            case 2: setY(getY()+1*scale);
  644.                move = 1;
  645.                break;
  646.        }
  647.  
  648.    }
  649.    public String toString(){
  650.        return "M";
  651.    }
  652.  
  653. }
  654.  
  655. /**
  656. * Created by danie on 11/1/2016.
  657. */
  658. public class Rabbit extends Critter {
  659.    private int count = 2;
  660.    private int move = -1;
  661.    public Rabbit(int x,int y){
  662.        super(x,y);
  663.    }
  664.    public void getMove(int scale){// moves north 2 steps, east 2 steps, south 2 steps
  665.        if(count>=2){
  666.            if(move <2){
  667.                move++;
  668.            }
  669.            else{
  670.                move = 0;
  671.            }
  672.            count = 0;
  673.        }
  674.        switch(move){
  675.            case 0: setY(getY()+1*scale);
  676.                count++;
  677.                break;
  678.            case 1: setX(getX()+1*scale);
  679.                count++;
  680.                break;
  681.            case 2: setY(getY()-1*scale);
  682.                count++;
  683.                break;
  684.        }
  685.    }
  686.    public String toString(){
  687.        return "R";
  688.    }
  689. }
  690.  
  691. /**
  692. * Created by danie on 11/1/2016.
  693. */
  694. public class Snake extends Critter {
  695.    private boolean odd = true;
  696.    private boolean everyOther = true;
  697.    private int count = 1;
  698.    private int place = 1;
  699.    public Snake(int x,int y){
  700.        super(x,y);
  701.    }
  702.    public void getMove(int scale){//move south 1 step, east 1 step, south 1 step, west 2 steps, south 1 step, east 3 steps, south 1, west 4 step
  703.        if(odd){
  704.            setY(getY()-1*scale);
  705.            odd = !odd;
  706.        }
  707.        else{
  708.            if(everyOther) {
  709.                setX(getX() + 1*scale);
  710.            }
  711.            else{
  712.                setX(getX()-1*scale);
  713.            }
  714.            if(place >=count){
  715.                count++;
  716.                place =1;
  717.                odd = !odd;
  718.                everyOther = !everyOther;
  719.            }
  720.            place++;
  721.        }
  722.  
  723.    }
  724.  
  725.    public String toString(){
  726.        return "S";
  727.    }
  728. }
  729.  
  730. /**
  731. * Created by danie on 11/1/2016.
  732. */
  733. public class Turtle extends Critter{
  734.    private int count = 5;
  735.    private int move = -1;
  736.    public Turtle(int x,int y){
  737.        super(x,y);
  738.    }
  739.    public void getMove(int scale){
  740.        if(count>=5){
  741.            if(move <3){
  742.                move++;
  743.            }
  744.            else{
  745.                move = 0;
  746.            }
  747.            count = 0;
  748.        }
  749.        switch(move){
  750.            case 0: setY(getY()-1*scale);
  751.                count++;
  752.                break;
  753.            case 1: setX(getX()-1*scale);
  754.                count++;
  755.                break;
  756.            case 2: setY(getY()+1*scale);
  757.                count++;
  758.                break;
  759.            case 3: setX(getX()+1*scale);
  760.                count++;
  761.                break;
  762.        }
  763.    }
  764.    public String toString(){
  765.        return "T";
  766.    }
  767. }
  768.  
  769. /**
  770. * Created by danie on 11/1/2016.
  771. */
  772. public class Wolf extends Critter{
  773.    int move = 1;
  774.    private int val = 1;
  775.    public Wolf(int x, int y){
  776.        super(x,y);
  777.    }
  778.    public void getMove(int scale){
  779.       if(move>2){
  780.           if(val == 1)val = -1;
  781.           if(val == -1) val = 1;
  782.           move = 0;
  783.       }
  784.       setX(getX()+val*scale);
  785.       setY(getY()+val*scale);
  786.        move++;
  787.    }
  788.    public String toString(){
  789.        return "W";
  790.    }
  791. }
  792.  
  793. CLIENT CLASS FOR CRITTER:
  794.  
  795. /**
  796. * Created by danie on 11/1/2016.
  797. */
  798. import java.util.*;
  799. import java.awt.*;
  800. public class test {
  801.    public static final int w = 500;
  802.    public static final int h = 500;
  803.    public static void main(String[] args){
  804.        DrawingPanel p = new DrawingPanel(w,h);
  805.        Graphics g = p.getGraphics();
  806.        Random r = new Random();
  807.       Critter[] animals = new Critter[20];
  808.       int[] count = new int[7];
  809.        for(int i = 0; i<animals.length;i++){
  810.         //Bird, Frog, Mouse, Rabbit, Snake, Turtle, Wolf
  811.            int type = r.nextInt(7);
  812.  
  813.  
  814.           switch (type){
  815.               case 0: animals[i] = new Bird(0,0);
  816.                   count[0]++;
  817.                   break;
  818.               case 1: animals[i] = new Frog(0,0);
  819.                   count[1]++;
  820.                   break;
  821.               case 2: animals[i] = new Mouse(0,0);
  822.                   count[2]++;
  823.                   break;
  824.               case 3: animals[i] = new Rabbit(0,0);
  825.                   count[3]++;
  826.                   break;
  827.               case 4: animals[i] = new Snake(0,0);
  828.                   count[4]++;
  829.                   break;
  830.               case 5: animals[i] = new Turtle(0,0);
  831.                   count[5]++;
  832.                   break;
  833.               case 6: animals[i] = new Wolf(0,0);
  834.                   count[6]++;
  835.                   break;
  836.           }
  837.  
  838.  
  839.            animals[i].setY(r.nextInt(500));
  840.            animals[i].setX(r.nextInt(500));
  841.            System.out.println(animals[i].toString() + " "+animals[i].getX() + " " + animals[i].getY());
  842.            g.drawString(animals[i].toString(), animals[i].getX(), animals[i].getY());
  843.        }
  844.        System.out.println("END CREATION");
  845.  
  846.  
  847.       while(true){
  848.  
  849.           g.setColor(Color.GREEN);
  850.           g.fillRect(0,0,w,h);
  851.           g.setColor(Color.RED);
  852.           int c  = 0;
  853.           for(int i  = 0; i<count.length;i++){
  854.               c+=count[i];
  855.           }
  856.    
  857.           Critter.collisions(animals,g);
  858.            g.setColor(Color.BLACK);
  859.            for(int i = 0; i<animals.length;i++){
  860.                if(animals[i].isAlive()) {
  861.                   animals[i].getMove(5);
  862.                    animals[i].constrain(500);
  863.                    System.out.println(animals[i].toString() + " " +animals[i].getX() + " " + animals[i].getY());
  864.                    g.drawString(animals[i].toString(), animals[i].getX(), animals[i].getY());
  865.                }
  866.            }
  867.            try {
  868.                Thread.sleep(500);
  869.            } catch(InterruptedException ex) {
  870.                Thread.currentThread().interrupt();
  871.            }
  872.  
  873.       }
  874.    }
  875. }
Add Comment
Please, Sign In to add comment