• 4.30作业


    1、定义一个点类Point,包含2个成员变量x、y分
    别表示x和y坐标,2个构造器Point()和Point(int
    x0,y0),以及一个movePoint(int dx,int dy)方法实
    现点的位置移动,创建两个Point对象p1、p2,分
    别调用movePoint方法后,打印p1和p2的坐标。[
    必作题]

     1 package homework9;
     2 
     3 public class Person {
     4     public static void main(String[] args) {
     5         Point p2=new Point(3,4);
     6         p2.movePoint(6,7);
     7         System.out.println("p2的当前坐标为:"+p2.x+","+p2.y);
     8         Point p1=new Point(1,2);
     9         p1.movePoint(7,8);
    10         System.out.println("p1的当前坐标为:"+p1.x+","+p1.y);
    11     }
    12 }
    13 
    14 package homework9;
    15 
    16 public class Point {
    17     int x;
    18     int y;
    19 
    20     public Point() {
    21 
    22     }
    23 
    24     public Point(int x1, int y1) {
    25         this.x = x1;
    26         this.y = y1;
    27     }
    28 
    29     void movePoint(int x2, int y2) {
    30         this.x += x2;
    31         this.y += y2;
    32     }
    33 }


    2、定义一个矩形类Rectangle:(知识点:对象的
    创建和使用)[必做题]
    • 2.1 定义三个方法:getArea()求面积、getPer()求
    周长,showAll()分别在控制台输出长、宽、面积
    、周长。
    • 2.2 有2个属性:长length、宽width
    • 2.3 通过构造方法Rectangle(int width, int length),
    分别给两个属性赋值
    • 2.4 创建一个Rectangle对象,并输出相关信息

     1 package homework9;
     2 
     3 public class Person {
     4     public static void main(String[] args) {
     5         Rectangle rc = new Rectangle(1, 2);
     6         rc.name = "矩形";
     7         rc.showAll();
     8     }
     9 }
    10 
    11 package homework9;
    12 
    13 public class Rectangle {
    14 
    15     public String name;
    16     int length;
    17     int width;
    18 
    19     public void getchar() {
    20         System.out.println(length + width);
    21     }
    22 
    23     public void getPer() {
    24         System.out.println((length + width) * 2);
    25     }
    26 
    27     public void showAll() {
    28         System.out.println(name + "的长为" + length + name + "的宽为" + width);
    29         System.out.println(name + "的周长为");
    30         getPer();
    31         System.out.println(name + "的面积为");
    32         getchar();
    33     }
    34 
    35     public Rectangle(int length, int width) {
    36         this.length = length;
    37         this.width = width;
    38     }
    39 }

    • 3、定义一个笔记本类,该类有颜色(char)和cpu
    型号(int)两个属性。 [必做题]
    • 3.1 无参和有参的两个构造方法;有参构造方法可
    以在创建对象的同时为每个属性赋值;
    • 3.2 输出笔记本信息的方法
    • 3.3 然后编写一个测试类,测试笔记本类的各个
    方法。

     1 package homework9;
     2 
     3 public class bijiben {
     4     public String name;
     5     char color;
     6     int xinghao;
     7 
     8     public void getDate() {
     9 
    10     }
    11 
    12     public void getDate(char color, int xinghao) {
    13         this.color = color;
    14         this.xinghao = xinghao;
    15     }
    16 
    17     public void showAll() {
    18         System.out.println(name + "颜色为" + color + "型号" + xinghao);
    19     }
    20 
    21 package homework9;
    22 
    23 public class Person {
    24     public static void main(String[] args) {
    25         bijiben rc = new bijiben();
    26         rc.name = "笔记本";
    27         rc.getDate('a', 7);
    28         rc.showAll();
    29     }
    30 }


    6、定义两个类,描述如下: [必做题]
    • 6.1定义一个人类Person:
    • 6.1.1定义一个方法sayHello(),可以向对方发出
    问候语“hello,my name is XXX”
    • 6.1.2有三个属性:名字、身高、年龄
    • 6.1.3通过构造方法,分别给三个属性赋值
    • 6.2定义一个Constructor类:
    • 6.2.1创建两个对象,分别是zhangsan,33岁,
    1.73;lishi,44,1.74
    • 6.2.2分别调用对象的sayHello()方法。

     1 package homework9;
     2 
     3 public class Constructor {
     4     public static void main(String[] args) {
     5         Person p1 = new Person();
     6         Person p2 = new Person();
     7         p1.Person("zhangsan", 11, 22);
     8         p2.Person("lishi", 44, 174);
     9         p1.sayHello();
    10         p2.sayHello();
    11     }
    12 }
    13 
    14 package homework9;
    15 
    16 public class Person {
    17     public String name;
    18     int sg;
    19     int tz;
    20 
    21     public void Person() {
    22 
    23     }
    24 
    25     public void Person(String name, int sg, int tz) {
    26         this.name = name;
    27         this.sg = sg;
    28         this.tz = tz;
    29     }
    30 
    31     public void sayHello() {
    32         System.out.println("hello,my name is " + name);
    33         System.out.println(name + "的身高为:" + sg + "的体重为" + tz);
    34     }
    35 }
  • 相关阅读:
    Power BI 根据用户权限动态生成导航跳转目标
    Power BI Tooltips 增强功能
    Power BI refresh error “could not load file or assembly…provided impersonation level is invalid”
    SQL 错误代码 18456
    如何使用SQL Server Integration Services从多个Excel文件读取数据
    通过表格编辑器将现有表引入Power BI数据流
    Power BI 中动态增长的柱状图
    ambari2.7.3离线安装hdp3.1.0时,ambari-hdp-1.repo中baseurl无值
    ambari 安装 cannot download file mysql-connector-java from http://8080/resource/mysql-connector-java.jar
    洛谷P4180 [BJWC2010]严格次小生成树
  • 原文地址:https://www.cnblogs.com/WangYYY/p/12808436.html
Copyright © 2020-2023  润新知