//Author: Johnny Shaieb //Date: 2-19-2002 //Purpose: //1. the slope and length of the line between the two points //2. the coordinates of the midpoint between the two points //3. the area and perimeter of the rectangle //4. the circumference and area of a circle with diameter // equal to the length from (x1, y1) to (x2, y2). import java.text.NumberFormat; public class program1 { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); String exit = "Y"; int i = 1; while(exit.equalsIgnoreCase("Y")) { System.out.print("X1 Value: "); double x1value = console.readDouble(); System.out.print("Y1 Value: "); double y1value = console.readDouble(); System.out.print("X2 Value: "); double x2value = console.readDouble(); System.out.print("Y2 Value: "); double y2value = console.readDouble(); LinearCalc shape = new LinearCalc(x1value, y1value, x2value, y2value); shape.getSlope(); shape.getLength(); shape.getMidX(); shape.getMidY(); shape.getArea(); shape.getPerimeter(); shape.getCircleArea(); //Have to have one static method, so I will not use this method in class //LinearCalc //shape.getCircumference(); System.out.println("------------------------------------------"); System.out.println("Data Set " + i ); System.out.println("------------------------------------------"); shape.Display(); Circumference(x1value, y1value, x2value, y2value); System.out.println(); System.out.print("Do you want to Continue [Y/y|N/n]: "); ConsoleReader cont = new ConsoleReader(System.in); String strexit = cont.readLine(); exit = strexit; i++; System.out.println(); } } //This is my one static method public static void Circumference(double x1, double y1, double x2, double y2) { NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(4); formatter.setMinimumFractionDigits(4); //Debug// //System.out.println("x1: " + x1 + " y1: " + y1 + " //x2: " + x2 + " y2: " + y2); double diameter = Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2)); double cir = Math.PI * diameter; System.out.println("Cir Circ : " + (new Sprintf(formatter.format(cir),10,"r")).formatIt()); } } class LinearCalc { //Constructor public LinearCalc(double X1, double Y1, double X2, double Y2) { x1 = X1; y1 = Y1; x2 = X2; y2 = Y2; } //Copy Constructor public LinearCalc(LinearCalc P) { x1 = P.x1; y1 = P.y1; x2 = P.x2; y2 = P.y2; } public double getSlope() { double m = (y2 - y1)/(x2 - x1); return m; } public double getLength() { double l = Math.sqrt(Math.pow(x2 - x1,2) + Math.pow(y2 - y1,2)); return l; } public double getArea() { double a = Math.abs(x2 - x1) * Math.abs(y2 - y1); return a; } public double getPerimeter() { double p = 2 * Math.abs(x2 - x1) + 2 * Math.abs(y2 - y1); return p; } public double getMidX() { double midX = (x2 + x1)/2; return midX; } public double getMidY() { double midY = (y2 + y1)/2; return midY; } public double getDiameter() { double dia = getLength(); return dia; } public double getRadius() { double radius = getDiameter()/2; return radius; } public double getCircleArea() { double ca = Math.PI * Math.pow(getRadius(),2); return ca; } public double getCircumference() { double cir = Math.PI * getDiameter(); return cir; } public void Display() { formatter.setMaximumFractionDigits(4); formatter.setMinimumFractionDigits(4); System.out.println("x1, y1 : (" + (new Sprintf(formatter.format(x1),8,"r")).formatIt() + "," + (new Sprintf(formatter.format(y1),8,"r")).formatIt() + ")"); System.out.println("x2, y2 : (" + (new Sprintf(formatter.format(x2),8,"r")).formatIt() + "," + (new Sprintf(formatter.format(y2),8,"r")).formatIt() + ")"); System.out.println(); System.out.println("Slope : " + (new Sprintf(formatter.format(getSlope()),10,"r")).formatIt()); System.out.println("Length : " + (new Sprintf(formatter.format(getLength()),10,"r")).formatIt()); System.out.println("MidX pt : " + (new Sprintf(formatter.format(getMidX()),10,"r")).formatIt()); System.out.println("MidY pt : " + (new Sprintf(formatter.format(getMidY()),10,"r")).formatIt()); System.out.println("Area : " + (new Sprintf(formatter.format(getArea()),10,"r")).formatIt()); System.out.println("Perimeter: " + (new Sprintf(formatter.format(getPerimeter()),10,"r")).formatIt()); System.out.println(); System.out.println("Cir Diam : " + (new Sprintf(formatter.format(getDiameter()),10,"r")).formatIt()); System.out.println("Cir Rad : " + (new Sprintf(formatter.format(getRadius()),10,"r")).formatIt()); System.out.println("Cir Area : " + (new Sprintf(formatter.format(getCircleArea()),10,"r")).formatIt()); //This is commented out before this is getting printed in the static method //in main //System.out.println("Cir Circ : " + formatter.format(getCircumference())); } private double x1; private double y1; private double x2; private double y2; NumberFormat formatter = NumberFormat.getNumberInstance(); } //This is an Sprintf class that is acting like the c/perl sprintf class Sprintf { public Sprintf(String sValue, int spaceValue, String strJustify) { str = sValue; spacing = spaceValue; justify = strJustify; } public Sprintf(int sValue, int spaceValue, String strJustify) { str = Integer.toString(sValue); spacing = spaceValue; justify = strJustify; } public Sprintf(double sValue, int spaceValue, String strJustify) { str = Double.toString(sValue); spacing = spaceValue; justify = strJustify; } public Sprintf(Sprintf o) { str = o.str; spacing = o.spacing; justify = o.justify; } public String formatIt() { int l = str.length(); String strFormat; if(l < spacing) { int diff = spacing - l; String spaces = ""; for(int k=1; k<=diff; k++) { spaces = spaces + " "; } if(justify.equalsIgnoreCase("l")) { strFormat = str + spaces; } else if(justify.equalsIgnoreCase("r")) { strFormat = spaces + str; } else { //if user value does not match l or r, //r will be default strFormat = spaces + str; } } else { strFormat = str; } return strFormat; } public void sprintfDisplay() { System.out.println(formatIt()); } private String str; private int spacing; private String justify; }