• Java的对象与类,继承


    Java的对象与类,继承

    题目1.Java类的建立与使用

    设计一个用来描述汽车的类,使用类的非静态成员变量来表示汽车的车主姓名、当前的速率和当前方向盘的转向角度,使用类的非静态成员方法来表示改变汽车的速率和停车两个操作。

    package test;
    
    public class t1 {
            private String Name;//车主姓名
            private float Speed;//当前车速
            private float Degree;//当前方向盘转向角度
            public t1(String Name){
                this.Name = Name;
            }
            public t1(String name,float speed,float degree){
                this(name);
                this.Speed = speed;
                this.Degree = degree;
            }
            public String getName(){//车主姓名访问
                return Name;
            }
            public float getDegree(){//当前方向盘转向角度访问
                return Degree;
            }
            public float getSpeed(){//当前车速访问
                return Speed;
            }
            public void changeSpeed(float Speed){//提供改变当前的车速
                this.Speed = Speed;
            }
            public void stop(){//提供停车
                this.Speed = 0;
            }
        public static void main(String[] args){
            t1 car = new t1("小明:",200f,25f);
            System.out.println("车主姓名:" +car.getName());
            System.out.println("当前车速:"+car.getSpeed());
        }
    }
    
    

    题目2.Java类的建立与使用

    定义一个类实现银行帐户的概念,包括的变量有"帐号"和"存款余额",包括的方法有"存款"、"取款"、"查询余额"和”显示帐号”。定义主类,创建帐户类的对象,并完成相应操作。

    package test;
    import java.util.*;
    public class t2 {
        private String User;//账户
        private double Money;//当前余额
        private double add;//存款金额
        private double subtract;//取款金额
        public t2(String User){
            this.User = User;
        }
        public t2(String User,double Money){
            this(User);
            this.Money = Money;
        }
        public String getUser(){//用户账号访问
            return User;
        }
        public double getMoney(){//当前余额访问
            return Money;
        }
        public void addMoney(double Money){//存款
            this.Money+=Money;
        }
        public void subtractMoney(double Money){//取款
            this.Money-= Money;
        }
        public static void main(String[] args){
            t2 bank = new t2("12345678",34537.876);
            System.out.println("账号:" +bank.getUser());
            System.out.println("当前余额:"+bank.getMoney());
            Scanner in = new Scanner(System.in);
            System.out.println("输入存款金额");
            double add = in.nextDouble();
            System.out.println("输入取款金额");
            double sub = in.nextDouble();
            bank.addMoney(add);
            System.out.println("存款后,当前余额:" +bank.getMoney());
            bank.subtractMoney(sub);
            System.out.println("取款后,当前余额:"+bank.getMoney());
        }
    }
    

    题目3. 继承

    设计一个类Shape(图形)包含求面积和周长的area()方法和perimeter()方法以及设置颜色的方法SetColor(),并利用Java多态技术设计其子类Circle(圆形)类、Rectangle(矩形)类和Triangle(三角形)类,并分别实现相应的求面积和求周长的方法。每个类都要覆盖toString方法。海伦公式:三角形的面积等于s(s-a)(s-b)(s-c)的开方,其中s=(a+b+c)/2。

    package test;
    public class t3 {
        public static void main(String[] args) {
            Circle c = new Circle(4,"white");
            System.out.println(c.toString());
            Rectangle r = new Rectangle(4, 5, "white");
            System.out.println(r.toString());
            Triangle t = new Triangle(3,4,5,"black");
            System.out.println(t.toString());
        } }
    abstract class Shape{
        String color="red";
        public double area(){
            return -1;
        }
        public double perimeter(){
            return -1;
        }
        public String SetColor(String a){
            return color=a;
        }
    }
    class Circle extends Shape{
        private double bj;
        Circle(){
            bj=1;
        }
        Circle(double i,String col){
            bj=i;
            super.color=super.SetColor(col);
        }
        public void setBj(double b){
            this.bj = b;
        }
        public double area(){
            return Math.PI * bj * bj; }
        public double perimeter(){
            return Math.PI * bj * 2;
        }
        public String toString(){
            return( "圆的半径为" + this.bj+"圆的面积:"+this.area()+"圆的周长:"+this.perimeter());
        } }
    class Rectangle extends Shape{
        double h;
        double w;
        Rectangle(){
        }
        Rectangle(double hi,double wi,String col){
            h=hi;
            w=wi;
            super.color=super.SetColor(col);
        }
        public double area(){
            return h * w; }
        public double perimeter(){
            return (w+h)* 2;
        }
        public String toString(){
            return( "矩形的长为" + this.h+"矩形的宽为"+this.w+"矩形的面积:"+this.area()+"矩形的周长:"+this.perimeter());
        } }
    class Triangle extends Shape{
        double a;
        double b;
        double c;
        Triangle(){
        }
        Triangle(double aa,double bb,double cc,String col){
            a=aa;
            b=bb;
            c=cc;
            super.color=super.SetColor(col);
        }
        public double area(){
            double s =((a+b+c)/2);
            return Math.sqrt (s*(s-a)*(s-b)*(s-c));
        }
        public double perimeter(){
            return a+b+c;
        }
        public String toString(){
            return( "三角形的长为" + this.a+"三角形的宽为"+this.b+"三角形的斜边"+this.c+"三角形的面积:"+this.area()+"三角形的周长:"+this.perimeter());
        }
    }
    
    
  • 相关阅读:
    【onenet-edp传输】1、调试上报数据点和端对端透传
    【PYQT5快速开发】重定义边框、QSS美化皮肤主题
    MySQL
    Flask-Login一个账号单用户在线
    DataTable按钮,排序,单元格颜色
    python
    MegaCli64 raid对应关系
    openstack server status
    js中使用JSON.parse转换json
    linux使用pyodbc和freetds连接sqlserver
  • 原文地址:https://www.cnblogs.com/xgcl/p/14208857.html
Copyright © 2020-2023  润新知