zoukankan      html  css  js  c++  java
  • 9-19练习

     三角形周长
    1
    public class sjx 2 { 3 double a; 4 double b; 5 double c; 6 public double zc(double a1,double b1,double c1) 7 { 8 a=a1; 9 b=b1; 10 c=c1; 11 return a+b+c; 12 } 13 14 }
     1 public class 测试
     2 {
     3     public static void main(String[] args) 
     4     {
     5         sjx x =new sjx();
     6         System.out.println(x.zc(2, 3, 4));
     7     }
     8 
     9 }

    按要求编写Java应用程序。

    1)创建一个叫做People的类:

    属性:姓名、年龄、性别、身高

    行为:说话、计算加法、改名

    编写能为所有属性赋值的构造方法;

    2)创建主类:

    创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80

    让该对象调用成员方法:

    说出“你好!”

    计算23+45的值

    将名字改为“李四”

     1 public class people 
     2 {
     3     String name;
     4     int nl;
     5     String xb;
     6     int sg;
     7     
     8      people(String name1, int nl1, String xb1, int sg1)
     9        {
    10            this.name=name1;
    11            this.nl=nl1;
    12            this.xb=xb1;
    13            this.sg=sg1;
    14        }
    15     
    16     public String say()
    17     {
    18         return ("你好"); 
    19     }
    20     
    21     public double js(double a,double b)
    22     {
    23         return(a+b);
    24     }
    25     
    26     public String gaiming(String name1)
    27     {
    28         this.name=name1;
    29         return(name);
    30     }
    31 }
     1 public class 测试
     2 {
     3 
     4     public static void main(String[] args) 
     5     {
     6         sjx x =new sjx();
     7         System.out.println(x.zc(2, 3, 4));
     8 
     9         
    10         people y = new people("张三", 18, "男", 180);
    11         System.out.println(y.say());
    12         System.out.println(y.js(23, 45));
    13         System.out.println(y.gaiming("李四"));
    14         
    15     }
    16 
    17 }

    11.按要求编写Java应用程序。

    1)创建一个叫做机动车的类:

    属性:车牌号(String),车速(int),载重量(double)

    功能:加速(车速自增)、减速(车速自减)、修改车牌号,查询车的载重量。

    编写两个构造方法:一个没有形参,在方法中将车牌号设置“XX1234”,速

    度设置为100,载重量设置为100;另一个能为对象的所有属性赋值;

    2)创建主类:

    在主类中创建两个机动车对象。

    创建第一个时调用无参数的构造方法,调用成员方法使其车牌为“辽

    A9752”,并让其加速。

    创建第二个时调用有参数的构造方法,使其车牌为“B5086”,车速为150,

    载重为200,并让其减速。

    输出两辆车的所有信息

     1 public class jdc
     2 {
     3     String chepai;
     4     int chesu;
     5     double zaizhong;
     6 
     7     jdc()
     8     {
     9         chepai="XX1234";
    10         chesu=100;
    11         zaizhong=100;
    12     }
    13     
    14     jdc(String chepai1,int chesu1,double zaizhong1)
    15     {
    16         chepai=chepai1;
    17         chesu=chesu1;
    18         zaizhong=zaizhong1;
    19     }
    20 
    21     public int chesujian(int cs1)
    22     {
    23         this.chesu=chesu-cs1;
    24         return(chesu);
    25     }
    26     
    27     int chesujia(int cs1)
    28     {
    29         this.chesu=chesu+cs1;
    30         return(chesu);
    31     }
    32     
    33     
    34 }
     1 jdc z = new jdc("XX1234", 100, 100);
     2         System.out.println("车牌号:"+z.chepai+"   车速:"+z.chesu+"   载重:"+z.zaizhong);
     3         
     4         jdc z1=new jdc("辽A9752",100,100);
     5         System.out.println("车牌号:"+z1.chepai+"   车速:"+z1.chesu+"   载重:"+z1.zaizhong);
     6         System.out.println("加速20,车速:"+z1.chesujia(20)+"km/h");
     7                
     8         jdc z2=new jdc("辽B5086",150,200);
     9         System.out.println("车牌号:"+z2.chepai+"   车速:"+z2.chesu+"   载重:"+z2.zaizhong);
    10         System.out.println("减速30,车速:"+z.chesujian(30));
  • 相关阅读:
    【整数划分系列】
    【51nod-1183】编辑距离
    【河南第十届省赛-D】年终奖金
    【河南第十届省赛-B】情报传递
    【河南省第十届ACM 省赛 A-谍报分析】
    Node.js函数介绍(参数为一个函数)
    Webstorm设置Node.js智能提示
    TortoiseSVN服务器ip地址修改后如何使用
    vue项目组件的全局注册
    ES6 类(Class)基本用法和静态属性+方法详解
  • 原文地址:https://www.cnblogs.com/jingfengling/p/5886564.html
Copyright © 2011-2022 走看看