zoukankan      html  css  js  c++  java
  • java4.30作业

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

     1 package pro1;
     2 
     3 public class Point {
     4     int x;
     5     int y;
     6 
     7     Point() {
     8 
     9     }
    10 
    11     Point(int x0, int y0) {
    12         this.x = x0;
    13         this.y = y0;
    14     }
    15 
    16     public void movePoint(int dx, int dy) {
    17         this.x += x;
    18         this.y += y;
    19         System.out.println("坐标" + this.x + "," + this.y);
    20     }
    21 }

    测试

     1 package Test1;
     2 
     3 public class Text {
     4     public static void main(String[] args) {
     5     
     6         Point p1 = new Point(1,1);
     7         Point p2 = new Point(4,5);
     8         p1.movePoint(p2.x,p2.y);
     9     }
    10     
    11 }
    12     

    2、定义一个矩形类Rectangle:(知识点:对象的创建和使用)

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

     1 package pro1;
     2 
     3 public class Rectangle {
     4     private int length;
     5     private int width;
     6 
     7     Rectangle(int length, int width) {
     8         this.length = length;
     9         this.width = width;
    10     }
    11 
    12     public void showAll() {
    13         System.out.println("矩形长为:" + length + "	" + "矩形的宽为:" + width + "	" + "矩形的面积为:" + getArea() + "	" + "矩形的周长为:"
    14                 + getPer());
    15     }
    16 
    17     public int getPer() {
    18         return 2 * (length + width);
    19     }
    20 
    21     public int getArea() {
    22         return length * width;
    23     }
    24 }
    25 
    26 class RectangleDemo {
    27     public static void main(String[] args) {
    28         Rectangle r = new Rectangle(2, 3);
    29         r.showAll();
    30     }
    31 }

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

     1 package pro1;
     2 public class bjb {
     3     char color;
     4     int model;
     5 
     6     bjb() {
     7 
     8     }
     9 
    10     bjb(char color, int model) {
    11         this.color = color;
    12         this.model = model;
    13     }
    14 
    15     void a() {
    16         System.out.println("颜色:" + color + "  型号:" + model);
    17     }
    18 
    19     public static void main(String[] args) {
    20         bjb a = new bjb('a', 9527);
    21         a.a();
    22     }
    23 }

    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 pro1;
     2 
     3 public class Person {
     4     String name;
     5     double height;
     6     int age;
     7     void sayHello() {
     8         System.out.println("hello,my name is  " + name+" "+height+"m"+" "+age+"岁");
     9     }
    10 }
     1 package pro1;
     2 public class PersonCreate {
     3 
     4     public static void main(String[] args) {
     5         Person zhangsan = new Person();
     6         zhangsan.name = "zhangsan";
     7         zhangsan.height = 1.73;
     8         zhangsan.age = 33;
     9 
    10         Person lishi = new Person();
    11         lishi.name = "lishi";
    12         lishi.height = 1.74;
    13         lishi.age = 44;
    14 
    15         zhangsan.sayHello();
    16         lishi.sayHello();
    17 
    18     }
    19 
    20 }
  • 相关阅读:
    Thread的第四天学习
    Thread的第三天学习
    Thread的第二天学习
    Thread的第一天学习
    hibernate的简单学习(第一天)
    【转载】jxl操作excel 字体 背景色 合并单元格 列宽等 .
    MySql学习
    sqlserver 数据库隔离级别,数据库死锁
    高并发操作同一条数据,更新丢失数据问题(重复转账,票超卖,订单扣库存问题)
    T4
  • 原文地址:https://www.cnblogs.com/qq1123514689/p/12807425.html
Copyright © 2011-2022 走看看