运行环境要求jdk1.7以上,毕竟有个Objects类,学校电脑1.6大概不行? 没试过,反正大部分都是些自动生成的东西罢了(虽然手敲也挺快乐的?)。
———————————————————————————————————————————————————————————————— ————————————————————————————————————————————————————————————————
/*1、定义一个点类Point,包含2个成员变量x,y分别表示x和y坐标, 2个构造器Point()和Point(int x0,y0).以及一个movePoint (int dx,int dy) 方法实现点的位置移动,创建两个Point对象pl,p2, 分别调用movePoint方法后,打印pl和p2的坐标。[必作题*/
import java.util.Objects; public class Homework0521_01 { public static void main(String[] args) { Point p1 = new Point(2, 4); Point p2 = new Point(-3, 0); p1.movePoint(5,99); System.out.println(p1.toString()); p2.movePoint(-8,33); System.out.println(p2.toString()); } } class Point { private int x; private int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } @Override public String toString() { return "Point{" + "x=" + x + ", y=" + y + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } public void movePoint(int x, int y) { this.setX(x); this.setY(y); } }
/*.2、定义一个矩形类Rectangle: (知识点:对象的创建和使用)[必做题]. 2.1定义三个方法: getArea()求面积、getPer()求周长, showAll()分别在控制台输出长、宽、面积周长。 2.2有2个属性:长length、宽width 2.3通过构造方法Rectangle(int width, int length),分别给两个属性赋值 2.4创建一个Rectangle对象,并输出相关信息*/
import java.util.Objects; public class Homework0521_02 { public static void main(String[] args) { Rectangle rectangle = new Rectangle(5, 6); rectangle.getAll(); } } class Rectangle { private int length; private int width; public Rectangle() { } public Rectangle(int length, int width) { this.length = length; this.width = width; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } @Override public String toString() { return "Rectangle{" + "length=" + length + ", width=" + width + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Rectangle rectangle = (Rectangle) o; return length == rectangle.length && width == rectangle.width; } @Override public int hashCode() { return Objects.hash(length, width); } public int getArea() { return this.getLength() * this.getWidth(); } public int getPer() { return (this.getLength() + this.getWidth()) * 2; } public void getAll() { System.out.println(this.toString()); System.out.println("Area = " + getArea() +"\tPer = " + getPer()); } }
/*3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。[必做题] 3.1无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值; 3.2输出笔记本信息的方法 3.3然后编写一个测试类,测试笔记本类的各个方法。*/
import java.util.Objects; public class Homework0521_03 { public static void main(String[] args) { Book book1 = new Book('白', 9750); Book book2 = new Book('黑', 9700); Book book3 = new Book('白', 9750); System.out.println(book1.toString()); System.out.println(book1.equals(book3)); System.out.println(book2); book2.setCpu(7600); System.out.println(book2); } } class Book { private char colour; private int cpu; public Book() { } public Book(char colour, int cpu) { this.colour = colour; this.cpu = cpu; } public char getColour() { return colour; } public void setColour(char colour) { this.colour = colour; } public int getCpu() { return cpu; } public void setCpu(int cpu) { this.cpu = cpu; } @Override public String toString() { return "Book{" + "colour=" + colour + ", cpu=" + cpu + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Book book = (Book) o; return colour == book.colour && cpu == book.cpu; } @Override public int hashCode() { return Objects.hash(colour, cpu); } }
————————————————————————————————————————
————————————————————————————————————————
2021年5月21日作业完成
(虽然是5.22了)
以及名副其实的晚安;