Wednesday, August 11, 2010

import java.util.*;//so that we can use Scanner class
import java.text.*;//so that we can use Decimalformat class
public class CircleFormula {

public static void main (String[] args) {

Scanner ask =new Scanner(System.in);//instantiate Scanner class
DecimalFormat df=new DecimalFormat("#.00");//instantiate DecimalFormat class with argument of the format we want
String tmp,str[];// note str is array because we use split method which result array values
System.out.print("Coordinates of Radius: ");tmp=ask.nextLine();

if(inputValid(tmp)){//conditions and passing to the inputValid method we made to validate the input if false go to else
//calls the computeDistance method then with the decimalformat object we format it
System.out.println("Radius of the circle is "+df.format(computeDistance(tmp))+" unit/s");
System.out.println("Circumference of the circle is "+df.format(computeCircumference(computeDistance(tmp)))+" unit/s");
System.out.println("Area of the circle is "+df.format(computeArea(computeDistance(tmp)))+" square unit/s");

}else{
System.out.print("The coordinates is out of range....");
}
}

public static boolean inputValid(String tmp){//boolean for true and false returns
String[] str;
str=tmp.split(",");//split the tmp to array with a delimeter of -
int x=0;// variable for flags
//next 5 lines declares an array and assign the value of str array to it
Double[] points=new Double[4];
points[0]=Double.parseDouble(str[0]);
points[1]=Double.parseDouble(str[1]);
points[2]=Double.parseDouble(str[2]);
points[3]=Double.parseDouble(str[3]);
//next is to find if coordinates is between 0 to 99
for (int i = 0; i if(!(points[i]>-1 && points[i]<100)){
x++;
}
}
//next is if x is more than 0 then false because it finds one or more less than 0 or greater than 99
return x>0?false:true;
}

public static double computeDistance(String tmp){
String[] str;
str=tmp.split(",");
Double[] points=new Double[4];
points[0]=Double.parseDouble(str[0]);
points[1]=Double.parseDouble(str[1]);
points[2]=Double.parseDouble(str[2]);
points[3]=Double.parseDouble(str[3]);
//next is using the distance formula of the cartesian plane with the use of Math.sqrt and Math.pow methods
return Math.sqrt(Math.pow((points[2]-points[0]),2)+Math.pow((points[3]-points[1]),2));
}

public static double computeCircumference(double tmp){
final double PI=3.1416;
return 2*PI*tmp;
}
public static double computeArea(double tmp){
final double PI=3.1416;
return PI*Math.pow(tmp,2);
}
}