编程语言
首页 > 编程语言> > JAVA学习日报 9/28

JAVA学习日报 9/28

作者:互联网

今天是9月28日,明天又是JAVA了,因此今天写了几个小程序练了练手

求球体积圆面积类:

package natusyasumi;
import java.util.Scanner;
class Point{
    protected double x=0;
    protected double y=0;
    public Point() {}
    public Point(double xv,double yv) {
        x=xv;y=yv;
    }
    public Point(Point p){
        x=p.x;y=p.y;
    }
    public void show(){
        System.out.print("("+x+","+y+")");
    }
    public void setX(double xv) {
        x=xv;
    }
    public void setY(double yv) {
        y=yv;
    }
    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }
}
class Circle extends Point{
    public double radius=0;
    public double PI = 3.1415;
    public double  circumference() {
        return radius*2*PI;
    }
    public double  area() {
        return radius*radius*PI;
    }   
    public void  setRadius(double r) {
        radius=r;
    }
    public double  getRadius() {
        return radius;
    }
    public Circle(double x1, double y1, double radius1){
        this.radius=radius1;
        this.x=x1;this.y=y1;
           }
    public Circle() {}
    public Circle(Circle c)
           {
               super();
               this.x=c.getX();
               this.y=c.getY();
               this.radius=c.getRadius();
           }
    public void show() {
        System.out.print("("+x+","+y+"),Radius="+radius);
    }
}
class Sphere extends Circle{
    public Sphere(double x1,double y1,double r1){
               this.x=x1;
               this.y=y1;
               this.radius=r1;
            }
    public Sphere(Sphere a){
                super();
                this.x=a.x;
                this.y=a.y;
                this.radius=a.radius;
            }
    public double volume() {
                return (4*PI*radius*radius*radius)/3;
            }
    public double area() {
                return 4*PI*radius*radius;
            }
}
public class Class5 {
    public static void main(String[] args) {
        double  r;
        Scanner input=new Scanner(System.in);
        r=input.nextDouble();
        input.close();
        Sphere s1=new Sphere(1,2,4);
        Sphere s2=new Sphere(s1);
        s1.show();
        System.out.print("\nArea="+s1.area()+"\nVolume="+s1.volume()+"\n");
        s2.setRadius(r);
        s2.show();
        System.out.print("\nArea="+s2.area()+"\nVolume="+s1.circumference()+"\n");
    }
}

三角形判定

package test2;
import java.lang.Math;
import java.util.Scanner;
class point
{
    public double x=0;
    public double y=0;
    public point(double x1,double y1){
        this.x=x1;this.y=y1;
    }
}
class Triangle
{
    private double d=0,e=0,f=0;
    public Triangle() {}
    public Triangle(double a,double b,double c){
              this.d=a;this.e=b;this.f=c;
         }
        void settri(point a,point b,point c)
        {
            d=Math.sqrt(((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
            e=Math.sqrt(((c.x-b.x)*(c.x-b.x))+((c.y-b.y)*(c.y-b.y)));
            f=Math.sqrt(((a.x-c.x)*(a.x-c.x))+((a.y-c.y)*(a.y-c.y)));
        }
        double fabs(double x){
            if(x<=0)
                return x;
            else
                return 0-x;
        }
        void checkitout()
        {
            int g=0,zj=0,dy=0,db=0;
            if(d+e<f||f+e<d||d+f<e)
            {System.out.print("这三个点不能构成三角形\n");}
            else
            {
                System.out.print("这三个点可以构成三角形\n");
                g=1;
            }
            if(g==1)
            {
                if(fabs((f*f)+(d*d)-(e*e))<=0.001||fabs((f*f)-(d*d)+(e*e))<0.001||fabs((f*f)+(d*d)-(e*e))<0.001)
                {zj=1;}
                if(d==e||f==e||d==f) 
                {dy=1;}
                if(e==d&&e==f)
                {db=1;}
                if(db==1) 
                {System.out.print("为等边三角形\n");}
                else if(dy==1)
                {
                    if(zj==1)
                    {System.out.print("为等腰直角三角形\n");}
                    else
                    {System.out.print("为等腰三角形\n");}
                }
                else if(zj==1)
                {System.out.print("为直角三角形\n");}
                if(db==0&&zj==0&&dy==0)
                {
                    System.out.print("这是一个普通三角形\n");
                }
            }
        }
};
public class Class12 {

    public static void main(String[] args) {
        Triangle t=new Triangle();
        double a,b;
        Scanner input=new Scanner(System.in);
        System.out.print("输入三个点\n");
        System.out.print("x1:");a=input.nextDouble();
        System.out.print("y1:");b=input.nextDouble();
        point p1=new point(a,b);
        System.out.print("x2:");a=input.nextDouble();  
        System.out.print("y2:");b=input.nextDouble();
        point p2=new point(a,b);
        System.out.print("x3:");a=input.nextDouble();
        System.out.print("y3:");b=input.nextDouble();
        point p3=new point(a,b);
        t.settri(p1,p2,p3);
        t.checkitout();
        input.close();
    }

}

明天的JAVA课可能还要写程序……说实话有点小慌啊……

好,今天就这些

标签:JAVA,日报,double,void,28,Sphere,radius,return,public
来源: https://www.cnblogs.com/Sakuraba/p/14103591.html