Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- All things from chapter 8:
- 8 self check:
- Daniel Lawson
- 9/18/2016
- 1.
- Procedural programming involves executing tasks in a given order. Object oriented programming uses the objects to interact with each other.
- 2.
- A class is used to create a object. A object has various behaviors and information that can be used and changed.
- 3.
- 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.
- 4.
- 14 14
- 7 9 14 2
- 18 18
- 7 9 14 18
- 5.
- 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.
- The state would be the calculator's storing of inputed values and those previously stored in its memory.
- 6.
- 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.
- 7.
- public class Name{
- private String firstName;
- private String lastName;
- private char middleInitial;
- }
- 8.
- 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.
- 9.
- d
- 10.
- public double distance(Point other){
- int dx = this.x -other.x;
- int dy = this.y -other.y;
- return Math.sqrt((dx*dx)*(dy*dy));
- }
- 11.
- public class Name{
- private String firstName;
- private String lastName;
- private char middleInitial;
- public String getNormalOrder(){
- return (firstName + " " +middleInitial +". " + lastName);
- }
- public String getReverseOrder(){
- return (lastName + ", " + firstName + " " +middleInitial +"." );
- }
- }
- 12.
- Create a toString method
- 13.
- c
- 14.
- public String toString(){
- return ("(" +this.x +"," +this.y + ")");
- }
- 15.
- public String toString(){
- return (firstName + " " +middleInitial +". " + lastName);
- }
- 16.
- Point p1 = new Point(8,2);
- Point p2 = new Point(4,3);
- System.out.println("p1 is " +p1);
- System.out.println("p2 is " +p2);
- System.out.println("p1's distance from origin is " + p1.distance(new Point(0,0)));
- p1.translate(1,2);
- p2.translate(-1,10);
- System.out.println("p1 is now " + p1);
- System.out.println("p2 is now " + p2);
- Daniel Lawson
- 9/22/2016
- 17.
- A constructor is used to initialize the fields of a class when the instance is createed
- 18.
- First a constructor is not supposed to be void (the header should be public Point(initialX, initial)
- 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
- 19.
- public class Name{
- private String firstName;
- private String lastName;
- private char middleInitial;
- public Name(String f, char m, String l){
- firstName = f;
- MiddleInitial = m;
- lastName = l;
- }
- public String getNormalOrder(){
- return (firstName + " " +middleInitial +". " + lastName);
- }
- public String getReverseOrder(){
- return (lastName + ", " + firstName + " " +middleInitial +"." );
- }
- }
- 20.
- 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
- 21.
- Public Point(Point p){
- this.x = p.getX();
- this.y = p.getX();
- }
- 22.
- Abstraction allows the client to know how to use something without knowing how it internally works.
- 23.
- Public allows things to be accessed outside the program while private does not. You should make a class's fields private.
- 24.
- By uses a accessor method to see the values of the fields
- 25.
- public void setX(int x){
- this.x = x;
- }
- public void setY(int y){
- this.y = y;
- }
- 26.
- public class Name{
- private String firstName;
- private String lastName;
- private char middleInitial;
- public Name(String f, char m, String l){
- firstName = f;
- MiddleInitial = m;
- lastName = l;
- }
- public String getNormalOrder(){
- return (firstName + " " +middleInitial +". " + lastName);
- }
- public String getReverseOrder(){
- return (lastName + ", " + firstName + " " +middleInitial +"." );
- }
- public String getLastName(){
- return lastName();
- }
- public String getFirstName(){
- return firstName();
- }
- public char getMiddleInitial(){
- return middleInitial();
- }
- }
- 27.
- public void setFirstName(String firstName){
- this.firstName = firstName;
- }
- public void setLastName(String lastName){
- this.lastName = lastName;
- }
- public void setMiddleInitial(char middleInitial){
- this.middleInitial = middleInitial;
- }
- 28.
- 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.
- 29.
- cohesion allows the class to represent a single clear abstraction.
- 30.
- Not including i/o allows the class to have more cohesion.
- 31.
- public String getSymbol(){
- return symbol;
- }
- public int getTotalShares(){
- return totalShares;
- }
- public double getTotalCost(){
- return getTotalCost;
- }
- Exercises:
- Daniel Lawson
- 9/22/2016
- Exercise 2:
- public void flip(){
- int tempX = x;
- x = -y;
- y = -tempX;
- }
- Exercise 3:
- public int manhattanDistance(Point other){
- return (Math.abs(x-other.getX()) + Math.abs(y-other.getY()));
- }
- Exercise 4:
- public boolean isVertical(Point other){
- return(x == other.getX());
- }
- Exercise 5:
- public double slope(Point other){
- if(x == other.getX())throw new IllegalArgumentException("Your x values can't be the same!");
- return Math.round((((y2-y1)/(x2-x1)) * 10000.0) / 10000.0); //return((y-other.getY())/(x-other.getX));
- }
- Exercise 6:
- public boolean isCollinear(Point p1, Point p2){
- return(slope(p1) == slope(p1));
- }
- RAW Paste Data
- Daniel Lawson
- 9/22/2016
- Exercise 2:
- public void flip(){
- int tempX = x;
- x = -y;
- y = -tempX;
- }
- Exercise 3:
- public int manhattanDistance(Point other){
- return (Math.abs(x-other.getX()) + Math.abs(y-other.getY()));
- }
- Exercise 4:
- public boolean isVertical(Point other){
- return(x == other.getX());
- }
- Exercise 5:
- public double slope(Point other){
- if(x == other.getX())throw new IllegalArgumentException("Your x values can't be the same!");
- return Math.round((((y2-y1)/(x2-x1)) * 10000.0) / 10000.0); //return((y-other.getY())/(x-other.getX));
- }
- Exercise 6:
- public boolean isCollinear(Point p1, Point p2){
- return(slope(p1) == slope(p1));
- }
- Bank account:
- /**
- * Created by danie on 9/25/2016.
- */
- public class bankAccount {
- private String name;
- private double balance;
- private double transsactionFee;
- public void deposit(double amount){
- balance +=amount;
- }
- public void withdraw(double amount){
- if(balance - amount - transsactionFee >=0)balance -= (amount + transsactionFee);
- }
- public String toString(){
- return (name + ", $" +balance);
- }
- public void transfer(bankAccount other, double amount){
- if(amount >=5){
- balance -= 5;
- if(balance>=amount){
- balance-=amount;
- other.deposit(amount);
- }
- else{
- other.deposit(balance);
- balance = 0;
- }
- }
- }
- }
- Rectangle class:
- //18-22 rectangle class
- public class Rectangle {
- private int x;
- private int y;
- private int width;
- private int height;
- public Rectangle(int x, int y,int width,int height){
- this.x = x;
- this.y = y;
- if(width <0 || height <0)throw new IllegalArgumentException("The width and height must be positive");
- this.height = height;
- this.width = width;
- }
- public Rectangle(Point p, int width, int height){
- x = p.getX();
- x = p.getY();
- if(width <0 || height <0)throw new IllegalArgumentException("The width and height must be positive");
- this.height = height;
- this.width = width;
- }
- public int getHeight(){
- return height;
- }
- public int getWidth(){
- return width;
- }
- public int getX(){
- return x;
- }
- public int getY(){
- return y;
- }
- public boolean contains(int x, int y){
- return(x>=this.x && x <= this.x+width && y>=this.y && y <=this.y +height);
- }
- public boolean contains(Point p){
- return(p.getX()>=this.x && p.getX() <= this.x+width && p.getY()>=this.y && p.getY() <=this.y +height);
- }
- // I don't think the formula for the length/width of the union and intersections are correct
- public Rectangle union(Rectangle rect){
- 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()));
- }
- public Rectangle intersection(Rectangle rect){
- 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()));
- }
- }
- Grocery list / GroceryItem (Project 3):
- /**
- * Created by danie on 9/29/2016.
- */
- public class GroceryItemOrder {
- private String name;
- private int quantity;
- private double pricePerUnit;
- public GroceryItemOrder(String name, int quantity, double pricePerUnit){
- this.name = name;
- this.quantity = quantity;
- this.pricePerUnit = pricePerUnit;
- }
- public double getCost(){
- return quantity * pricePerUnit;
- }
- public void setQuantity(int quantity){
- this.quantity = quantity;
- }
- public void setPricePerUnit(int cost){
- pricePerUnit = cost;
- }
- }
- /**
- * Created by danie on 9/29/2016.
- */
- public class GroceryList {
- public static final int size = 10;
- private GroceryItemOrder[] total = new GroceryItemOrder[size];
- private int count = 0;
- public GroceryList(){
- for(int i = 0;i<total.length;i++)total[i] = new GroceryItemOrder("null",0,0);
- };
- public void add(GroceryItemOrder item){
- total[count] = item;
- count++;
- }
- public double getTotalCost(){//returns the total cost of all the items
- double sum = 0;
- for(int i = 0;i< total.length; i++)sum+=total[i].getCost();
- return sum;
- }
- }
- Project 1 (RationalNumber):
- /**
- * Created by danie on 9/28/2016.
- */
- public class RationalNumber {
- private int numerator;
- private int denominator;
- public RationalNumber(int n, int d){
- if(d==0)throw new IllegalArgumentException("0 can't be in the numerator");
- numerator = n;
- denominator = d;
- }
- public RationalNumber() {
- numerator = 0;
- denominator = 1;
- }
- public int getDenominator(){
- return denominator;
- }
- public int getNumerator(){
- return numerator;
- }
- public void setDenominator(int d){
- if(d == 0)throw new IllegalArgumentException("0 can't be in the denominator");
- denominator = d;
- for(int i = numerator ;i>=1;i--){ //4/8 1/2 4/10 8/2
- if(denominator % i ==0 && numerator % i ==0){
- denominator /= i;
- numerator /= i;
- }
- }
- }
- public void setNumerator(int n){
- numerator = n;
- for(int i = numerator ;i>=1;i--){ //4/8 1/2 4/10 8/2
- if(denominator % i ==0 && numerator % i ==0){
- denominator /= i;
- numerator /= i;
- }
- }
- }
- public String toString(){
- if(denominator < 0 ){
- denominator = -denominator;
- numerator = -numerator;
- }
- if(numerator == 0){
- return ("0");
- }
- if(denominator == 1){
- return numerator +"";
- }
- else {
- return (numerator + "/" + denominator);
- }
- }
- }
- Project 2 (Date class):
- /**
- * Created by danie on 9/28/2016.
- */
- public class Date {
- private int year;
- private int month;
- private int day;
- public Date(int y,int m, int d){
- if(y<0||m<1||m>12||d>31||d<28)throw new IllegalArgumentException("Invalid values for y,m,d ");
- year = y;
- month = m;
- day = d;
- }
- public void addDays(int d){
- day *= d;
- int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
- while(day>monthDays[month-1]){
- if(month ==12 && day>31){
- day -= 31;
- month = 1;
- year++;
- }
- if(isLeapYear() && month == 2 && day>29){
- day-=29;
- month++;
- }
- else {
- day -= monthDays[month - 1];
- month++;
- }
- }
- }
- public void addWeeks(int w){
- addDays(7*w);
- }
- public int daysTo(Date other){
- return Math.abs(calcDays() - other.calcDays());
- }
- public int calcDays(){
- int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
- int totalMonthDays = 0;
- for(int i = 0;i<month;i++){
- totalMonthDays+=monthDays[i];
- }
- int totalYearDays = year*365 + year/4;
- return totalMonthDays + totalYearDays + day;
- }
- public int getDay(){
- return day;
- }
- public int getMonth(){
- return month;
- }
- public int getYear(){
- return year;
- }
- public boolean isLeapYear(){
- return ((year % 4 ==0 && year % 100 != 0)|| year % 400 == 0);
- }
- public String toString(){
- return (year + "/" + month+ "/" + day);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement