Show Menu
Cheatography

Java Fundies 2 Cheat Sheet (DRAFT) by

Fundamentals of Java

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Contains (CartPt)

  public boolean contai­ns(­CartPt pt) {     return (this.nw.x <= pt.x) && (pt.x <= this.nw.x + this.size) &&            (this.nw.y <= pt.y) && (pt.y <= this.nw.y + this.s­ize);   } }
This occurs when we have CartPt nw as a field

String Methods

s.length()
s.char­At(/)
s.subs­tri­ng(­start, end)
s.toUp­per­Case()
s.toLo­wer­Case()
s.inde­xOf(x)
s.repl­ace­(old, new)
s.spli­t(r­egex)
s.trim()
s.equa­ls(s2)
s.comp­are­To(s2) (Compares two strings Lexiog­rap­hic­ally)
s.cont­ain­s(C­har­Seq­uence cs)

Arithmetic Operators

x + y
add
x - y
subtract
x * y
Multiply
x/y
divide
x % y
modulo

Boolean Operators

! x
not
x && y
and
x || y
or

Abstracted Sameness

abstract class AFoo implements IFoo {   public boolean sameX(X that) { return false; }   public boolean sameY(Y that) { return false; }   public boolean sameZ(Z that) { return false; } } class X extends AFoo {   public boolean sameFo­o(IFoo that) { return that.s­ame­X(t­his); }   public boolean sameX(X that) { ... compares two X values ... } } class Y extends AFoo {   public boolean sameFo­o(IFoo that) { return that.s­ame­Y(t­his); }   public boolean sameY(Y that) { ... compares two Y values ... } } class Z extends AFoo {   public boolean sameFo­o(IFoo that) { return that.s­ame­Z(t­his); }   public boolean sameZ(Z that) { ... compares two Z values ... } }
 

Compar­isons

x < y
less
x <= y
less than or equal
x > y
greater than
x >= y
greater than or equal
x == y
Equal
! x
Not equal

Distance formula (Origin)

  public double distan­ceT­oOr­igin() {     return Math.s­qrt­(this.x  this.x + this.y  this.y) - this.r­adius;   } }
 - this.r­adius 
is only for circle

DistanceTo Formula

class CartPt {   ...   double distan­ceT­o(C­artPt that) {     return Math.sqrt(       (this.x - that.x)  (this.x - that.x)       + (this.y - that.y)  (this.y - that.y));   } }

Insert()

public ILoBook insert­(Book b) {   if (this.f­ir­st.c­he­ape­rTh­an(b)) {     return new ConsLo­Boo­k(t­his.first, this.r­est.in­ser­t(b));   }   else {     return new ConsLo­Book(b, this);   } }
ConsLo­Book()
public ILoBook insert­(Book b) {   return new ConsLo­Book(b, this); }
MtLoBook()

append­(IL­oItem other)

public ILoItem allNum­bers() { return this.l­eft.al­lNu­mbe­rs(­).a­ppe­nd(­thi­s.r­igh­t.a­llN­umb­ers()); }
// In interf­ace-- ILoItem append­(IL­oItem other);
// in consLoItem -- public ILoItem append­(IL­oItem other) { new ConsLo­Ite­m(t­his.first, this.r­est.ap­pen­d(o­ther)); }
// MtLoItem --- public ILoItem append­(IL­oItem other) { return other; }
 

Younger IAT (In IAT)

In IAT
IAT younge­rIA­T(IAT other); ------- IAT younge­rIA­THe­lp(IAT other, int otherYob);

Younger IAT and Helper

public IAT younge­rIA­T(IAT other) {return other.y­ou­nge­rIA­THe­lp(­this, this.yob);
IAT younge­rIA­THe­lp(IAT other, int otherYob) {  if (this.yob > otherYob) {         return this;     }     else {         return other;     } }

Youngest Parent

public IAT younge­stP­arent() {     return this.m­om.y­ou­nge­rIA­T(t­his.dad); }

Youngest GrandP­arent

public IAT younge­stG­ran­dpa­rent() { return this.m­om.y­ou­nge­stP­are­nt(­).y­oun­ger­IAT­(th­is.d­ad.yo­ung­est­Par­ent()); }

Abstract Interface

abstract class AShape implements IShape {   CartPt loc;   String color;     AShape­(CartPt loc, String color) {     this.loc = loc;     this.color = color;   } }

Abstract Class

class Circle extends AShape {   int radius;     Circle­(CartPt center, int radius, String color) {     super(­center, color);     this.r­adius = radius;   } }  

Subclass entension

class Square extends Rect {     Square­(CartPt nw, int size, String color) {     super(nw, size, size, color);   }
size, size represent length and width.
You would need to override the method to use size rather than length­/width

Tips

Don't:
Do:
Casting
Design Recipe
Field of Field
Tests: test helpers
isFoo() (isEmp­ty())
Purpose statements
Getters
Shorthand
 
Dynamic Dispatch
 

Abstract with Range in constr­uctor

interface ITetri­sPiece {   int SCREEN­_HEIGHT = 30; } 
abstract class ATetri­sPiece implements ITetri­sPiece {   ...   ATetri­sPi­ece(int x, int y) {     this.xPos = x;     this.yPos = y;   }   ATetri­sPi­ece(int x) {     this(x, SCREEN­_HE­IGHT);   } }

Utilis Class

class Utils {   int checkR­ang­e(int val, int min, int max, String msg) {     if (val >= min && val <= max) {       return val;     }     else {       throw new Illega­lAr­gum­ent­Exc­ept­ion­(msg);     }   } }
This allows a constr­uctor to be general

Illegal Exception Message in Constr­uctor

// In class Date ------­---­---­---­---­---­Dat­e(int year, int month, int day) {   this.year = new Utils(­).c­hec­kRa­nge­(year, 1500, 2100,     "­Invalid year: " + Intege­r.t­oSt­rin­g(y­ear));   this.month = new Utils(­).c­hec­kRa­nge­(month, 1, 12,     "­Invalid month " + Intege­r.t­oSt­rin­g(m­onth));   this.day = new Utils(­).c­hec­kRa­nge­(day, 1, 31,     "­Invalid day: " + Intege­r.t­oSt­rin­g(d­ay));
intege­r.t­oSt­rin­g(y­ear))
changes the invalid integer to a string and combines (+)

Testing Exceptions

// In Tester ---- boolean checkC­ons­tru­cto­rEx­cep­tio­n(E­xce­ption e,                                     String className,                                     ... constr args ...);

Sameness Interface

interface IShape {   boolean sameSh­ape­(IShape that);   boolean sameCi­rcl­e(C­ircle that);   boolean sameRe­ct(Rect that);   boolean sameSq­uar­e(S­quare that); }
// In Circle: public boolean sameSh­ape­(IShape that) {   return that.s­ame­Cir­cle­(this); }
Others would be false in this class