A. Introduction
So far we have been creating objects within a class. This is not necessary. As a matter of fact, inappropriate since main() is not part of the class definition. In addition, if we need creating objects of multiple classes, which class should main() reside?
As seen in the Student class example, the best place to create Student objects is outside the Student class since instantiating and using a class is NOT logically part of the class itself. So once a class is designed and implemented, it is typically used in another class. If we have just a Student class and we'd like to test all of its methods, we create a Tester class for this purpose, where objects of Student class are created in main() of the Tester class.
Another thinking paradigm switch is the method call. Compare the non object-oriented approach and the object-oriented approach below. Do you see how the method calls are different?
import java.util.*;
class Triangles {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter x-value of first coordinate");
double x1 = scan.nextDouble();
System.out.println("Enter y-value of first coordinate");
double y1 = scan.nextDouble();
System.out.println("Enter x-value of second coordinate");
double x2 = scan.nextDouble();
System.out.println("Enter y-value of second coordinate");
double y2 = scan.nextDouble();
System.out.println("Enter x-value of third coordinate");
double x3 = scan.nextDouble();
System.out.println("Enter y-value of third coordinate");
double y3 = scan.nextDouble();
double length1 = Math.sqrt((Math.pow(x2-x1,2))+(Math.pow(y2-y1,2)));
double length2 = Math.sqrt((Math.pow(x3-x2,2))+(Math.pow(y3-y2,2)));
double length3 = Math.sqrt((Math.pow(x3-x1,2))+(Math.pow(y3-y1,2)));
double perimeter = length1 + length2 + length3;
double angle1RAD = Math.cos((Math.pow(length2,2) + Math.pow(length3,2) - Math.pow(length1,2))/(2*length2*length3));
double angle2RAD = Math.cos((Math.pow(length1,2) + Math.pow(length3,2) - Math.pow(length2,2))/(2*length1*length3));
double angle3RAD = Math.cos((Math.pow(length1,2) + Math.pow(length2,2) - Math.pow(length3,2))/(2*length1*length2));
double angle1DEG = angle1RAD * (180/Math.PI);
double angle2DEG = angle2RAD * (180/Math.PI);
double angle3DEG = angle3RAD * (180/Math.PI);
double area = 0.5 * length1 * length2 * Math.sin(angle3DEG);
System.out.println("Lengths: " + length1 + ", " + length2 + ", " + length3);
System.out.println("Angles: " + angle1DEG + ", " + angle2DEG + ", " + angle3DEG);
System.out.println("Perimeter: " + perimeter);
System.out.println("Area: " +area);
}
}
Tester.java:
import java.util.*;
class Tester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int Ax = in.nextInt();
int Ay = in.nextInt();
int Bx = in.nextInt();
int By = in.nextInt();
int Cx = in.nextInt();
int Cy = in.nextInt();
triangle a = new triangle (Ax, Ay, Bx, By, Cx, Cy);
a.angle();
a.printtriangle();
}
}
triangle.java:
class triangle {
private int Ax;
private int Ay;
private double ABlength;
private int Bx;
private int By;
private double BClength;
private int Cx;
private int Cy;
private double CAlength;
private double [] angles= new double [3];
private int Xx;
private int Xy;
private double AXlength;
private double AXslope;
private double AXyintercept;
private double BCyintercept;
private double AXBCinterceptx;
private double AXBCintercepty;
triangle (int Ax, int Ay, int Bx, int By, int Cx, int Cy){
this.Ax=Ax;
this.Ay=Ay;
this.Bx=Bx;
this.By=By;
this.Cx=Cx;
this.Cy=Cy;
this.ABlength= Math.sqrt(Math.pow(Bx-Ax,2)+Math.pow(By-Ay,2));
this.BClength= Math.sqrt(Math.pow(Cx-Bx,2)+Math.pow(Cy-By,2));
this.CAlength= Math.sqrt(Math.pow(Ax-Cx,2)+Math.pow(Ay-Cy,2));
this.AXslope=(-1/((By-Cy)/(Bx-Cx)));
this.AXyintercept=Ay-AXslope*Ax;
this.BCyintercept=By-((By-Cy)/(Bx-Cx))*Bx;
this.AXBCinterceptx=((AXyintercept-BCyintercept)/(AXslope-(By-Cy)/(Bx-Cx)));
this.AXBCintercepty=AXslope*AXBCinterceptx+AXyintercept;
}
public double Perm (){
double perimeter=ABlength+BClength+CAlength;
return perimeter;
}
public void angle (){
angles[0]=(Math.acos((Math.pow(ABlength,2)+Math.pow(CAlength,2)-Math.pow(BClength,2))/(2*ABlength*CAlength)))*(180/Math.PI);
angles[1]=(Math.acos((Math.pow(ABlength,2)+Math.pow(BClength,2)-Math.pow(CAlength,2))/(2*ABlength*BClength)))*(180/Math.PI);
angles[2]=180-angles[0]-angles[1];
}
public double area (){
AXlength= Math.sqrt(Math.pow(AXBCinterceptx-Ax,2)+Math.pow(AXBCintercepty-Ay,2));
double area=(BClength*AXlength)/2;
return area;
}
public void printtriangle(){
System.out.println("Lengths\nAB: "+ABlength+"\nBC: "+BClength+"\nCA: "+CAlength+"\nAngles\nA: "+angles[0]+"\u00B0\nB: "+angles [1]+"\u00B0\nC: "+angles[2]+"\u00B0\nPerimeter: "+Perm()+"m\nArea: "+area()+"m");
}
}
So the public interface of a class is the collection of all public methods that are exposed to other classes for calling.
When we design a class, we start by planning how other classes will use this class logically.