C c1 = new C(); } public void method() { B b = new B(); b.sleep(); } } public class Y { A [] a = new A[5]; } public class A extends B { C c1, c2; public A() { c1 = new C(); } public void method() { D d = new D(); d.working(); } } public class X { public void m() { B var = new A(); double x – Math.sqrt(5); } }
ss or function should h a ve one, a nd only one, job. • Keep functions focused on a single t a sk. • Bre a k down your code into sm a ller, reus a ble modules or functions. • Ensure e a ch module or function h a s a cle a r, well-de f ined purpose. 32
a t expl a in why something is done, not wh a t is done. • Keep comments up to d a te with code ch a nges. • Document public APIs a nd complex logic thoroughly. 38
form a tting guidelines. https://google.github.io/styleguide/javaguide.html • Use indent a tion, whitesp a ce, a nd comments e ff ectively to enh a nce re a d a bility. 39
if the distance is "small" you can consider the line clicked (selected) // calculates the distance from P3(x3,y3) // to the line defined by P1(x1,y1) and P2(x2,y2) public static double distance (int x1, int y1, int x2, int y2, int x3, int y3) { double A = y2 - y1; double B = x1 - x2; double C = x2 * y1 - x1 * y2; double distance = Math.abs(A * x3 + B * y3 + C) / Math.sqrt(A * A + B * B); return distance; } 51
Summer 2024 Copyright. These slides can only be used as study material for the class CSC307 at Cal Poly. They cannot be distributed or used for another purpose.