CMPS 200 Homework Assignment 11, Fall 2011

American University of Beirut · Introduction to Programming · 12 pages

All CMPS 200 files
CMPS 200 Homework Assignment 11, Fall 2011, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 200 Homework Assignment 11, Fall 2011, page 2 CMPS 200 Homework Assignment 11, Fall 2011, page 3 CMPS 200 Homework Assignment 11, Fall 2011, page 4 CMPS 200 Homework Assignment 11, Fall 2011, page 5 CMPS 200 Homework Assignment 11, Fall 2011, page 6 CMPS 200 Homework Assignment 11, Fall 2011, page 7 CMPS 200 Homework Assignment 11, Fall 2011, page 8 CMPS 200 Homework Assignment 11, Fall 2011, page 9 CMPS 200 Homework Assignment 11, Fall 2011, page 10

Showing the first 10 of 12 pages. Download the PDF to see all of it.

More CMPS 200 files

Show the text of this homework
// Hagop M. Harfoushian, AUBnet: hmh51, aubID: 201201447 // Card data type with basic operations like rank, suite, stronger, toString. public class Card { private String rank; private String suit; public Card(String r, String s){ rank=r; suit=s; } public String rank(){ return rank; } public String suite(){ return suit; } public boolean isOfSuit(String s){ if(s.equals(suit)) return true; return false; } public boolean stronger(Card c){ String[] r={"2","3","4","5","6","7","8","9","10","J","Q","Q","K"}; String[] s={"Clubs","Diamonds","Hearts","Spades"}; int indxOfGivenRank=0; int indxOfCardRank=0; int suitOfOurCard=0; int suitOfGivenCard=0; for(int i=0; i<13; i++) if(r[i].equals(c.rank)) indxOfGivenRank=i; // finding the rank of the given card in terms of integer where 2 is 0 and K is 12 for(int i=0; i<13; i++) if(r[i].equals(rank)) indxOfCardRank=i; // the same, of the card in hand. for(int i=0; i<4; i++) if(s[i].equals(c.suit))suitOfGivenCard=i; // finding the suite of the given card in terms of integers.(indecies of array s); for(int i=0; i<4; i++) if(s[i].equals(suit)) suitOfOurCard=i; if(indxOfGivenRank>indxOfCardRank) return false; else if(indxOfGivenRank==indxOfCardRank && suitOfGivenCard>suitOfOurCard) return false; else return true; } public String toString(){ String suitInitial=""; if (suit=="Clubs") suitInitial="S"; // getting the suite initials. if (suit=="Diamonds") suitInitial="D"; if (suit=="Hearts") suitInitial="H"; if (suit=="Spades") suitInitial="S"; return rank + suitInitial; } } // Hagop M. Harfoushian, AUBnet: hmh51, aubID: 201201447 // the fraction data type with basic operations like plus, times, print, less than, getNum, getDen, toString. public class Fraction { private int num; private int den; public Fraction(int n, int d){ num=n; den=d; } public Fraction(int v){ num=v; den=1; } public Fraction times(Fraction f){ int n=num; // so that the original fraction is not changed int d=den; n*=f.num; d*=f.den; return new Fraction(n, d); } public Fraction plus(Fraction f){ int a=num; int c=den; int b=f.num; int d=f.den; return new Fraction(((a*d)+(b*c)),(c*d)); // common denominator } public boolean equals(Fraction f){ if((f.num/f.den)==(num/den)) return true; return false; } public boolean lessThan(Fraction f){ if((num/den)<(f.num/f.den)) return true; return false; } public String toString(){ String s=(num+"/"+den); return s; } public int getNum(){ return num; } public int getDen(){ return den; } } public class Point { private double x; private double y; public Point(double x0,double y0){ x=x0; y=y0; } public double getX(){ return x; } public double getY(){ return y; } public String toString(){ return "("+ x + ", "+ y + ")"; } } // Hagop M. Harfoushian, AUBnet: hmh51, aubID: 201201447 // This is the rectangle data Type whose API and description is in the book. // a rectangle is defined as a given point, and two "vectors" drawn perp. from it, called hight and width. public class Rectangle { private int x; private int y; private int width; private int height; private Point p; public Rectangle(int initialX, int initialY, int initialWidth, int initialHeight){ x = initialX; y = initialY; width = initialWidth; height = initialHeight; if(height <= 0 || width <= 0) System.out.println("Error"); } public int getHeight(){ return height; } public int getWidth(){ return width; } public int getX(){ return x; } public int getY(){ return y; } public String toString(){ String s = "Rectangle[x="+x+", y="+y+", width=" + width +", height="+height+"]"; return s; } public Rectangle(Point p2, int initialWidth, int initialHeight){ x = (int)p2.getX(); y = (int)p2.getY(); width=initialWidth; height= initialHeight; p=p2; } public Point getPoint(){ Point p2 = new Point(p.getX(),p.getY()); return p2; } public boolean contains(int initialX, int initialY){ if(initialX<=x && initialY<=y) return true; else return false; } public boolean contains(Point p){ int a = (int)p.getX(); int b = (int)p.getY(); if(a>=x && b>=y && a<=x+width && b<=y+height)return true; else return false; } public Rectangle union(Rectangle r) { int x2 = r.getX(); int y2 = r.getY(); int w2 = r.getWidth(); int h2 = r.getHeight(); int union_X = Math.min(x,x2); // the upper most left point (taking the origin in the buttom left) int union_Y = Math.min(y, y2); int union_W = Math.max(width+x, w2+x2)-union_X; // max width from the 1st rectangle to the second. int union_H = Math.max(height+y,h2+y2)-union_Y;// max height from the 1st rectangle to the second. Rectangle union = new Rectangle (union_X,union_Y,union_W,union_H); return union; } public Rectangle intersection(Rectangle rect) { int w2 = rect.getWidth(); int x2 = rect.getX(); int h2 = rect.getHeight(); int y2 = rect.getY(); int inter_X = Math.max(x,x2);// x intersection int inter_Y = Math.max(y,y2);// y intersection int inter_Width = Math.min(width+x, w2+x2)-inter_X;// min width from the 1st rectangle to the second. int inter_Height = Math.min(height+y, h2+y2)-inter_Y;// min height from the 1st rectangle to the second. Rectangle intersection = new Rectangle (inter_X,inter_Y,inter_Width,inter_Height); return intersection; } } // Hagop M. Harfoushian, AUBnet: hmh51, aubID: 201201447 // this is a Segment data type whose API is the description in the book p.575, exc. 9-12. public class Segment { private int x1; private int y1; private int x2; private int y2; public Segment(Point p1, Point p2){ x1=(int) p1.getX(); y1=(int) p1.getY(); } public Segment(int PassedX1, int PassedY1, int PassedX2, int PassedY2){ x1=PassedX1; y1=PassedY1; x2=PassedX2; y2=PassedY2; } public Point getP1(){ return new Point(x1, y1); } public Point getP2(){ return new Point(x2, y2); } public String toString(){ return "[(" + x1 + ", " + y1 + ")" + ", (" + x2 + ", " + y2+ ")]"; } public double getSlope(){ double slope=(y2-y1)/(x2-x1); return slope; } public boolean isCollinear(Point p){ Point p1=new Point(x1, x2); Segment newSeg= new Segment (p1, p); if ((p.getX()==x1 && p.getY()==y1) || (p.getX()==x2 && p.getY()==y2)) // if p is the same point(defining the segment) return true; else if(round(newSeg.getSlope())==round(((y2-y1)/(x2-x1)))) return true; else return false; } public static double round(double d){ // round to the nearest ten thousandth. double n=d; n*=10000; n=Math.round(n); n/=10000; return n; } } public class TestCards { public static void main(String[] args) { Card c1 = new Card("10", "Hearts"); Card c2 = new Card("Q", "Spades"); System.out.println(c1); System.out.println(c2); System.out.println(c1.isOfSuit("Hearts")); // should print true System.out.println(c2.isOfSuit("Hearts")); // should print false System.out.println(c1.stronger(c2)); // should print false } } public class TestFraction { public static void main(String[] args) { Fraction x = new Fraction(1, 2); Fraction y = new Fraction(1, 4); Fraction z = x.times(y); Fraction w = x.plus(y); System.out.println(x + " * " + y + " = " + z); System.out.println(x + " + " + y + " = " + w); } } public class TestRectangle { public static void main(String[] args){ Rectangle r=new Rectangle(2,5,20,10); System.out.println(r.getHeight()); System.out.println(r.getWidth()); System.out.println(r.intersection(new Rectangle(2,5,3,4))); System.out.println(r.union(new Rectangle(5,4,3,2))); } } public class TestSegment { public static void main(String[] args){ Point p=new Point(22,2); Segment s=new Segment(2,4,3,6); System.out.println(s); System.out.println(s.getP1()); System.out.println(s.isCollinear(p)); System.out.println(s.getSlope()); } } Segment: public static void main(String[] args){ Point p=new Point(22,2); Segment s=new Segment(2,4,3,6); System.out.println(s); System.out.println(s.getP1()); System.out.println(s.isCollinear(p)); System.out.println(s.getSlope()); } ======================= [(2, 4), (3, 6)] (2.0, 4.0) false 2.0 Fraction: public static void main(String[] args) { Fraction x = new Fraction(1, 2); Fraction y = new Fraction(1, 4); Fraction z = x.times(y); Fraction w = x.plus(y); System.out.println(x + " * " + y + " = " + z); System.out.println(x + " + " + y + " = " + w); } =================== 1/2 * 1/4 = 1/8 1/2 + 1/4 = 6/8 Cards: public static void main(String[] args) { Card c1 = new Card("10", "Hearts"); Card c2 = new Card("Q", "Spades"); System.out.println(c1); System.out.println(c2); System.out.println(c1.isOfSuit("Hearts")); // should print true System.out.println(c2.isOfSuit("Hearts")); // should print false System.out.println(c1.stronger(c2)); // should print false } ========================= 10H QS true false false Rectangle: public static void main(String[] args){ Rectangle r=new Rectangle(2,5,20,10); System.out.println(r.getHeight()); System.out.println(r.getWidth()); System.out.println(r.intersection(new Rectangle(2,5,3,4))); System.out.println(r.union(new Rectangle(5,4,3,2))); } ======================== 10 20 Rectangle[x=2, y=5, width=3, height=4] Rectangle[x=2, y=4, width=20, height=11]