Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DANIEL LAWSON CHAPTER 9 SELF CHECKS, EXERCISES, PROJECTS
- //////////
- Self Check:
- Daniel Lawson
- 9.1 evens
- 2. Overloading is when you have two methods with the same name in a class but wih different parameters.
- Overiding is creating a method that already has the same name in the parent class.
- 4.
- a, b, c, d are legal
- 9.2
- 6.
- It can call the set age method but the age and name fields are private so it cannot directly refer to it.
- 8.
- public void setAge(int age){
- super.setAge(age);
- year++;
- }
- 10.
- outputs:
- vroomvroom
- truck 1
- car 1
- 9.3
- 12.
- flute
- shoe 1
- flute 2
- flute
- blue 1
- flute 2
- moo
- moo 1
- moo 2
- moo
- blue 1
- boo m2
- 14.
- squid
- creature 1
- tentacles
- BIG!
- spout
- create 2
- ocean-dwelling
- create 1
- create 2
- ocean-dwelling
- warm-blooded
- creature 2
- 16.
- Bay 1 Pond 2
- Ocean 2
- Lake 3 Ocean 2
- Pond 1
- Pond 2
- Pond 3
- Pond 1
- Pond 2
- Pond 3 Pond 2
- Bay 1 Pond 2
- Bay 2
- Lake 3 Bay 2
- 18.
- In a "is-a" relationship one class extends another, in a "has-a" relationship one class uses another class as a field.
- You declare the object like you would a normal field.
- 20.
- It is a bad design because you have to create so many different classes. Instead you could make one class for a generic card
- and then possibly have another class called deck with stores large amounts of cards.
- 9.5
- 22.
- Implementing an interface allows for a class to be manipulated in the client class similar to if it was extending a client,
- except it does not inherent methods and fields.
- 24.
- interfaces can't have fields or have actual methods
- correct interface:
- public interface Colored{
- public Color getColor();
- }
- 26.
- // in shape
- public int getSideCount();
- //in triangle
- public int getSideCount(){
- return 3;
- }
- // in rectangle
- public int getSideCount(){
- return 4;
- }
- //in circle
- public int getSideCount(){
- return 0;
- }
- 9.6
- 28.
- 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
- //////////////////////////
- Ticket classes
- /**
- * Created by danie on 10/27/2016.
- */
- public class Ticket {
- private int number;
- public Ticket(int number){
- this.number = number;
- }
- public double getPrice(){
- return 50.0;
- }
- public String toString(){
- return "Number: " + number + ", Price: " + getPrice();
- }
- }
- /**
- * Created by danie on 10/27/2016.
- */
- public class AdvanceTicket extends Ticket {
- private int days;
- public AdvanceTicket(int number, int days){
- super(number);
- this.days = days;
- }
- public double getPrice(){
- if(days>=10){
- return super.getPrice() - 20;
- }
- else{
- return super.getPrice() - 10;
- }
- }
- }
- /**
- * Created by danie on 10/27/2016.
- */
- public class WalkupTicket extends Ticket {
- public WalkupTicket(int number){
- super(number);
- }
- }
- /**
- * Created by danie on 10/27/2016.
- */
- public class StudentAdvanceTicket extends AdvanceTicket{
- public StudentAdvanceTicket(int number, int days){
- super(number,days);
- }
- public double getPrice(){
- return super.getPrice() / 2;
- }
- public String toString(){
- return super.toString() + " (ID REQUIRED)";
- }
- }
- /////////////////////////////////////////
- shape and shape 3D
- shape:
- /**
- * Created by danie on 10/26/2016.
- */
- public interface Shape {
- public double area();
- public double perimeter();
- //public void drawShape();
- }
- /**
- * Created by danie on 10/26/2016.
- */
- import java.awt.*;
- public class Circle implements Shape{
- private double r;
- public Circle(double r){
- this.r = r;
- }
- public double getRadius(){
- return r;
- }
- public double area(){
- return r*r*Math.PI;
- }
- public double perimeter(){
- return 2*Math.PI*r;
- }
- /* public void drawShape(Graphics g, int x, int y, int w, int l){
- g.fillOval(x,y,w,l);
- }
- */
- }
- /**
- * Created by danie on 10/26/2016.
- */
- import java.awt.*;
- public class Rectangle implements Shape{
- private double b;
- private double h;
- public Rectangle(double b, double h){
- this.b = b;
- this.h = h;
- }
- public double area(){
- return b*h;
- }
- public double perimeter(){
- return b*2 + h*2;
- }
- /* public void drawShape(Graphics g, int x, int y, int l, int w){
- g.drawRect(x,y,l,w);
- }*/
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class Square extends Rectangle {
- public Square(double l){
- super(l,l);
- }
- }
- /**
- * Created by danie on 10/26/2016.
- */
- import java.awt.*;
- public class Triangle implements Shape{
- private double a;
- private double b;
- private double c;
- public Triangle(double a,double b,double c){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public double area(){
- double s = (a+b+c)/2;
- return Math.sqrt(s*(s-a)*(s-b)*(s-c));
- }
- public double perimeter(){
- return a + b + c;
- }
- /* public void drawShape(Graphics g, int x1, int y1, int x2 , int y2, int x3, int y3){
- g.drawPolygon(new int[] {x1,x2,x3}, new int[] {y1, y2, y3}, 3);
- }*/
- }
- SHAPE 3D
- /**
- * Created by danie on 10/28/2016.
- */
- public interface Shape3D {
- public double getVolume();
- public double getSurfaceArea();
- }
- /*
- Shape3D interface
- Things with two bases | sphere | cones
- rectangular prism | triangular prism | cylinders|
- cube|
- */
- /**
- * Created by danie on 10/28/2016.
- */
- public abstract class TwoBases implements Shape3D {
- private Shape base;
- private double height;
- public TwoBases(Shape base, double height){
- this.base = base;
- this.height = height;
- }
- public double getVolume(){
- return base.area() * height;
- }
- public double getSurfaceArea(){
- return height * base.perimeter() + 2 * base.area();
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class TriangularPrism extends TwoBases{
- public TriangularPrism(Triangle b, double h){
- super(b,h);
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class RectangularPrism extends TwoBases {
- public RectangularPrism(Rectangle base, double height){
- super(base,height);
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class Cube extends RectangularPrism {
- public Cube(Square s, double h){
- super(s,h);
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class Cylinder extends TwoBases{
- private double radius;
- public Cylinder(Circle c, double h){
- super(c,h);
- radius = c.getRadius();
- }
- public double getRadius(){
- return radius;
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class Sphere implements Shape3D {
- private double radius;
- public Sphere(double r){
- radius = r;
- }
- public double getRadius(){
- return radius;
- }
- public double getVolume(){
- // 4/3 πr3
- return radius*radius*radius*Math.PI*4/3;
- }
- public double getSurfaceArea(){
- //4πr2
- return radius*radius*Math.PI*4;
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class Cone implements Shape3D {
- private Circle c;
- private double height;
- public Cone(Circle circ, double h){
- c = circ;
- height = h;
- }
- public double getRadius(){
- return c.getRadius();
- }
- public double getVolume(){
- //πr2 h/3
- return c.area() + height/3;
- }
- public double getSurfaceArea(){
- //πr(r+ sqrt(h2+r2) )
- return c.area() + (Math.PI*c.getRadius())*(Math.sqrt(height*height + c.getRadius()*c.getRadius()));
- }
- }
- END OF SHAPE AND SHAPE3D
- ////////////////////////////////////////////////////////////////////////////////////
- EMPLOYEE
- /**
- * Created by danie on 10/20/2016.
- */
- public class Employee {
- private int year;
- public Employee(int year){
- year = this.year;
- }
- public int getHours(){
- return 40;
- }
- public double getSalary(){
- return 50000.0;
- }
- public int getVacationDays(){
- return 10 + 2 *year;
- }
- public String getVacationForm(){
- return "yellow";
- }
- public int getYear(){
- return year;
- }
- }
- /**
- * Created by danie on 10/20/2016.
- */
- public class Secretary extends Employee{
- public Secretary(){
- super(0);
- }
- public void takeDictation(String text){
- System.out.println("Taking dictation of text: " + text);
- }
- }
- /**
- * Created by danie on 10/20/2016.
- */
- public class LegalSecretary extends Secretary {
- public LegalSecretary(int years){
- //
- }
- public double getSalary(){
- return super.getSalary() + 5000.0;
- }
- public void fileLegalBriefs(){
- System.out.println("I could file all day!");
- }
- }
- /**
- * Created by danie on 10/20/2016.
- */
- public class Lawyer extends Employee {
- public Lawyer(int years){
- super(years);
- }
- public int getVacationDays(){
- return super.getVacationDays() + 5;
- }
- public String getVacationForm(){
- return "pink";
- }
- public void sueSomeone(String target){
- System.out.println("I will sue " + target);
- }
- public double getSalary(){
- return super.getSalary() + 5000.0 * getYear();
- }
- }
- /**
- * Created by danie on 10/20/2016.
- */
- public class Marketer extends Employee {
- public Marketer(int years){
- super(years);
- }
- public double getSalary(){
- return super.getSalary() + 10000.0;
- }
- public void market(String thing){
- System.out.println("Want to buy some " + thing);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////
- CRITTER PROJECT
- MAIN CRITTER ABSTRACT CLASS:
- /**
- * Created by danie on 11/1/2016.
- */
- import java.util.*;
- import java.awt.*;
- public abstract class Critter {
- private boolean alive = true;
- Random r = new Random();
- private int x;
- private int y;
- public Critter(int x,int y){
- this.x = x;
- this.y = y;
- }
- public int getX(){
- return x;
- }
- public int getY(){
- return y;
- }
- public void setX(int xc){
- x = xc;
- }
- public void setY(int yc){
- y = yc;
- }
- public boolean isAlive(){
- return alive;
- }
- public void setAlive(boolean b){
- alive = b;
- }
- public static void collisions(Critter[] c,Graphics g ){
- boolean[] hits = new boolean[c.length];
- int box = 15;
- for(int i = 0; i < c.length;i++){
- for(int j = 0; j<c.length;j++){
- if(i!=j &&c[i].isAlive()&&c[j].isAlive()) {// if both are alive, share the same coordinates and not itself
- 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){
- Random r = new Random();
- if (r.nextInt(2) == 1) {
- c[j].setAlive(false);
- g.setColor(Color.RED);
- g.fillOval(c[j].getX(),c[j].getY(),10,10);
- } else {
- c[i].setAlive(false);
- g.setColor(Color.RED);
- g.fillOval(c[i].getX(),c[i].getY(),10,10);
- }
- }
- }
- }
- }
- }
- public abstract String toString();
- public abstract void getMove(int scale);
- public void constrain(int l){
- if(getX()>l){
- setX(0);
- }
- if(getX()<0){
- setX(l);
- }
- if(getY()>l){
- setY(0);
- }
- if(getY()<0){
- setY(l);
- }
- }
- //public static
- }
- VARIOUS DIFFERENT ANIMALS:
- /**
- * Created by danie on 11/1/2016.
- */
- import java.util.*;
- public class Bird extends Critter {
- public Bird(int x,int y){
- super(x,y);
- }
- public void getMove(int scale){
- switch(r.nextInt(4)){
- case 0: setX(getX()-1*scale);
- break;
- case 1: setX(getX()+1*scale);
- break;
- case 2: setY(getY()-1*scale);
- break;
- case 3: setY(getY()+1*scale);
- break;
- }
- }
- public String toString(){
- return "B";
- }
- }
- /**
- * Created by danie on 11/1/2016.
- */
- public class Frog extends Critter{
- private int move = 0;
- private int count = 3;
- public Frog(int x,int y){
- super(x,y);
- }
- public void getMove(int scale){
- if(count>=3){
- move = r.nextInt(4);
- count = 0;
- }
- switch(move){
- case 0: setX(getX()-1*scale);
- break;
- case 1: setX(getX()+1*scale);
- break;
- case 2: setY(getY()-1*scale);
- break;
- case 3: setY(getY()+1*scale);
- break;
- }
- }
- public String toString(){
- return "F";
- }
- }
- /**
- * Created by danie on 11/1/2016.
- */
- public class Mouse extends Critter{
- private int move = 1;
- public Mouse(int x,int y){
- super(x,y);
- }
- public void getMove(int scale){
- switch(move){
- case 1: setX(getX()-1*scale);
- move = 2;
- break;
- case 2: setY(getY()+1*scale);
- move = 1;
- break;
- }
- }
- public String toString(){
- return "M";
- }
- }
- /**
- * Created by danie on 11/1/2016.
- */
- public class Rabbit extends Critter {
- private int count = 2;
- private int move = -1;
- public Rabbit(int x,int y){
- super(x,y);
- }
- public void getMove(int scale){// moves north 2 steps, east 2 steps, south 2 steps
- if(count>=2){
- if(move <2){
- move++;
- }
- else{
- move = 0;
- }
- count = 0;
- }
- switch(move){
- case 0: setY(getY()+1*scale);
- count++;
- break;
- case 1: setX(getX()+1*scale);
- count++;
- break;
- case 2: setY(getY()-1*scale);
- count++;
- break;
- }
- }
- public String toString(){
- return "R";
- }
- }
- /**
- * Created by danie on 11/1/2016.
- */
- public class Snake extends Critter {
- private boolean odd = true;
- private boolean everyOther = true;
- private int count = 1;
- private int place = 1;
- public Snake(int x,int y){
- super(x,y);
- }
- 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
- if(odd){
- setY(getY()-1*scale);
- odd = !odd;
- }
- else{
- if(everyOther) {
- setX(getX() + 1*scale);
- }
- else{
- setX(getX()-1*scale);
- }
- if(place >=count){
- count++;
- place =1;
- odd = !odd;
- everyOther = !everyOther;
- }
- place++;
- }
- }
- public String toString(){
- return "S";
- }
- }
- /**
- * Created by danie on 11/1/2016.
- */
- public class Turtle extends Critter{
- private int count = 5;
- private int move = -1;
- public Turtle(int x,int y){
- super(x,y);
- }
- public void getMove(int scale){
- if(count>=5){
- if(move <3){
- move++;
- }
- else{
- move = 0;
- }
- count = 0;
- }
- switch(move){
- case 0: setY(getY()-1*scale);
- count++;
- break;
- case 1: setX(getX()-1*scale);
- count++;
- break;
- case 2: setY(getY()+1*scale);
- count++;
- break;
- case 3: setX(getX()+1*scale);
- count++;
- break;
- }
- }
- public String toString(){
- return "T";
- }
- }
- /**
- * Created by danie on 11/1/2016.
- */
- public class Wolf extends Critter{
- int move = 1;
- private int val = 1;
- public Wolf(int x, int y){
- super(x,y);
- }
- public void getMove(int scale){
- if(move>2){
- if(val == 1)val = -1;
- if(val == -1) val = 1;
- move = 0;
- }
- setX(getX()+val*scale);
- setY(getY()+val*scale);
- move++;
- }
- public String toString(){
- return "W";
- }
- }
- CLIENT CLASS FOR CRITTER:
- /**
- * Created by danie on 11/1/2016.
- */
- import java.util.*;
- import java.awt.*;
- public class test {
- public static final int w = 500;
- public static final int h = 500;
- public static void main(String[] args){
- DrawingPanel p = new DrawingPanel(w,h);
- Graphics g = p.getGraphics();
- Random r = new Random();
- Critter[] animals = new Critter[20];
- int[] count = new int[7];
- for(int i = 0; i<animals.length;i++){
- //Bird, Frog, Mouse, Rabbit, Snake, Turtle, Wolf
- int type = r.nextInt(7);
- switch (type){
- case 0: animals[i] = new Bird(0,0);
- count[0]++;
- break;
- case 1: animals[i] = new Frog(0,0);
- count[1]++;
- break;
- case 2: animals[i] = new Mouse(0,0);
- count[2]++;
- break;
- case 3: animals[i] = new Rabbit(0,0);
- count[3]++;
- break;
- case 4: animals[i] = new Snake(0,0);
- count[4]++;
- break;
- case 5: animals[i] = new Turtle(0,0);
- count[5]++;
- break;
- case 6: animals[i] = new Wolf(0,0);
- count[6]++;
- break;
- }
- animals[i].setY(r.nextInt(500));
- animals[i].setX(r.nextInt(500));
- System.out.println(animals[i].toString() + " "+animals[i].getX() + " " + animals[i].getY());
- g.drawString(animals[i].toString(), animals[i].getX(), animals[i].getY());
- }
- System.out.println("END CREATION");
- while(true){
- g.setColor(Color.GREEN);
- g.fillRect(0,0,w,h);
- g.setColor(Color.RED);
- int c = 0;
- for(int i = 0; i<count.length;i++){
- c+=count[i];
- }
- Critter.collisions(animals,g);
- g.setColor(Color.BLACK);
- for(int i = 0; i<animals.length;i++){
- if(animals[i].isAlive()) {
- animals[i].getMove(5);
- animals[i].constrain(500);
- System.out.println(animals[i].toString() + " " +animals[i].getX() + " " + animals[i].getY());
- g.drawString(animals[i].toString(), animals[i].getX(), animals[i].getY());
- }
- }
- try {
- Thread.sleep(500);
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment