zoukankan      html  css  js  c++  java
  • JAVA学习日报 7.19

    迫害作业,迫害作业……

    今天要在前一个作业的基础上再派生一个球类

    代码如下:

    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");
        }
    }

    在此特别提及一下,我原本以为包是个类似文件夹的玩意,结果发现这东西里所有的类都是共同的,再定义新类会造成冲突,因此重新分包着实花了点时间

    运行结果如下:

  • 相关阅读:
    [noip2013]华容道
    [tyvj 1061] Mobile Service (线性dp 滚动数组)
    [bzoj 2726] 任务安排 (斜率优化 线性dp)
    [洛谷 P2365] 任务安排 (线性dp)
    [poj 3666] Making the Grade (离散化 线性dp)
    【模板】manacher算法
    [tyvj 1071] LCIS
    [NOIP 2012] 国王游戏
    [NOIP 2010] 关押罪犯 (二分+二分图判定 || 并查集)
    [NOI 2002] 银河英雄传说 (带权并查集)
  • 原文地址:https://www.cnblogs.com/Sakuraba/p/13348104.html
Copyright © 2011-2022 走看看