Thursday, September 29, 2011

Triangle

public class point{
public double x;
public double y;
public point(double x,double y){
this.x=x;
this.y=y;
}
public void setx(double nx){x=nx;}
public void sety(double ny){y=ny;}
public double getx(){return x;}
public double gety(){return y;}
public String toString(){
return"("+x+","+y+")";
}
}

........................................................................................

public class line{
point p1,p2;
 public line(point p1, point p2){
 this.p1=p1;
 this.p2=p2;
 }
public line(double x1,double y1,double x2, double y2){
this.p1=new point(x1,y1);
this.p2=new point(x2,y2);
}
public double getlength(){
double dx=p1.x-p2.x;
double dy=p1.y-p2.y;
return Math.round(Math.sqrt(dx*dx+dy*dy)*100)/100.0;
}
public point getp1(){return p1;}
public point getp2(){return p2;}
public void setp1(point np){p1=np;}
public void setp2(point np){p2=np;}
public void setp1(double x,double y){
p1=new point(x,y);
}
public void setp2(double x,double y){
p2=new point(x,y);
}
public String toString(){
return "line "+p1+"__"+p2+"of length "+getlength();
}
}
............................................................................................

public class triangle{
private point p1,p2,p3;
private double area,perimeter;
public triangle(double x1,double y1,double x2,double y2,double x3,double y3){
p1=new point(x1,y1);
p2=new point(x2,y2);
p3=new point(x3,y3);
computearea();
computeperimeter();
}
public triangle(point p1,point p2,point p3){
this.p1=p1;
this.p2=p2;
this.p3=p3;
computearea();
computeperimeter();
}
public point getp1(){return p1;}
public point getp2(){return p2;}
public point getp3(){return p3;}
public void computearea(){
double a=new line(p1,p2).getlength();
double b=new line(p1,p3).getlength();
double c=new line(p3,p2).getlength();
double s=(a+b+c)/2.0;
area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
public void computeperimeter(){
double a=new line(p1,p2).getlength();
double b=new line(p1,p3).getlength();
double c=new line(p3,p2).getlength();
perimeter=a+b+c;
}
public double getarea(){return Math.round(area*100)/100.0;}
public double getperimeter(){return perimeter;}
public boolean isrectangledtriangle(){
double a=new line(p1,p2).getlength();
double b=new line(p1,p3).getlength();
double c=new line(p3,p2).getlength();
double tmp;
if(b>a){tmp=a;a=b;b=tmp;}
if(c>a){tmp=a;a=c;c=tmp;}
return(a*a==b*b+c*c);
}
public String toString(){
String retString="";
String arectangled="not a rectangled";
if(isrectangledtriangle()) arectangled=" a rectangled";
retString="triangle "+p1+"::"+p2+"::"+p3+"::"+"is"+arectangled+"triangle\n";
retString=retString+"and it is of aea::"+getarea()+", and primeter::"+getperimeter();
return retString;
}
}
....................................................................................................
import javax.swing.JOptionPane;
public class triangledemo{

public static void main(String[] argc){
triangle t1=new triangle(0,0,4,4,2,3);
line l1=new line(new point(0,0),new point(4,4));
line l2=new line(new point(0,0),new point(2,3));
line l3=new line(4,4,2,3);
point A=new point(3,4);
point B=new point(6,4);
point C=new point(3,8);
triangle t2=new triangle(A,B,C);
JOptionPane.showMessageDialog(null,l1+"\n"+l2+"\n"+l3+"\n"+t1+"\n"+t2,"triangles and line",1);
}
}

Wednesday, September 28, 2011

print

public class print{
public static void main(String[] a){
int intNum1=100;
int intNum2=50;
System.out.print("the values are; ");
System.out.printf("10%d and %10d\n",intNum1,intNum2);
System.out.printf("%-10d\n",intNum1);
System.out.printf("%-10d\n",intNum2);
System.out.printf("%10d\n",intNum1);
System.out.printf("%10d\n",intNum2);

}
}
/*the values are; 10100 and         50
100
50
       100
        50
*/

JOptionPane

import java.util.*;
import javax.swing.JOptionPane;
public class WelcometoJavaDialog{

public static void main(String[] a){
Scanner input=new Scanner(System.in);
String fullname=JOptionPane.showInputDialog("What is your name,in full?");
String nam1=input.next();
String nam2=input.next();

System.out.print("\nhellow  "+nam1+" "+nam2);
System.out.println("-congratuations on write your first "+" java program which features some input!\n\n");

JOptionPane.showMessageDialog(null,"\nhello  "+nam1+" "+nam2+"\n-congratuations on write your first "+"\njava program which features some input!","welcome to java input",JOptionPane.PLAIN_MESSAGE);
}
}

Friday, September 23, 2011

Z score

import java.util.*;
public class Z{
public double mew(int[] c){
double n=0;
for(int i=0;i<c.length;i++){
n+=c[i];
}
return (n/c.length);
}
public double valu(int[] c){
double k=0;
for(int i=0;i<c.length;i++){
k+=Math.pow((c[i]-mew(c)),2);
}
return Math.sqrt((k/(c.length-1)));
}
public double zs(int[] c,int n){
return ((c[n-1]-mew(c))/valu(c));
}
public static void main(String[] a){
Scanner s=new Scanner(System.in);
int[] c={80,99,40,30,98,48,55,88,66,63,11,20,30,70,50,90,32,33,65,69,89,90,70};
int n;
System.out.println("enter the number");
n=s.nextInt();
Z b=new Z();
System.out.println("Z= "+b.zs(c,n));

}
}

yesterday and tomorrow

import java.util.*;
public class date{
public static boolean isleapeyear(int y){
if(y%100==0){
return (y%400==0);}
else{
return (y%4==0);}
}

public static int daysinthemonth(int m,int y){
int d=0;
switch(m){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:d=31;break;
case 4:case 6:case 9:case 11:d=30;break;
case 2:
if(isleapeyear(y)){
d=29;}
else {d=28;}break;
default:d=-1;
}
return d;
}

public void tomorrow(int d,int m,int y){
if((d==31)&&(m==12)){
System.out.print("tomorrow is"+" "+"1"+" "+"1"+" "+(y+1));
}
else if(d==daysinthemonth(m,y)){
System.out.print("tomorrow is "+"1"+" "+(m+1)+" "+(y));
}
else{
System.out.print("tomorrow is "+(d+1)+" "+m+" "+y);
}
}

public void yesterday(int d,int m,int y){
if((d==1)&&(m==1)){
System.out.print("yesterday is"+" "+"31"+" "+"12"+" "+(y-1));
}
else if(d==1){
System.out.print("yesterday is"+" "+(daysinthemonth((m-1),y))+" "+(m-1)+" "+y);
}
else{
System.out.print("yesterday is"+" "+(d-1)+" "+m+" "+y);
}
}


public static void main(String[] a){
Scanner input=new Scanner(System.in);
int m,y,n,d;
System.out.println("year");
y=input.nextInt();
System.out.println("month");
m=input.nextInt();
System.out.println("day");
d=input.nextInt();
date b=new date();


b.yesterday(d,m,y);
System.out.println();
System.out.println("today "+d+" "+m+" "+y);
b.tomorrow(d,m,y);

}
}