作业
定义一个点类Point, 包含2个成员变量x、y分别表示x和y坐标,
2个构造方法Point()和Point(int x0,y0),
以及一个movePoint(int dx,int dy)方法 实现点的位置移动
package com.neuedu.day04.homework;
public class Point {
// 横坐标
private int x;
// 纵坐标
private int y;
public Point() {
super();
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public void movePoint(int dx, int dy) {
this.x += dx;
this.y += dy;
}
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;
}
}
@Test
public void myTest01() {
Point p1 = new Point(100, 90);
p1.movePoint(5, 5);
System.out.println("p1的坐标为:" + p1.getX() + "," + p1.getY());
Point p2 = new Point(80, 30);
p2.movePoint(5, 5);
System.out.println("p2的坐标为:" + p2.getX() + "," + p2.getY());
}
设计一个类Student,该类包括姓名、学号和成绩。 设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息
package com.neuedu.day04.homework;
public class Student {
private String name;
private int no;
private int score;
public Student() {
super();
}
public Student(String name, int no, int score) {
super();
this.name = name;
this.no = no;
this.score = score;
}
public void sort(Student[] st) {
for (int i = 1; i < st.length; i++) {
for (int j = 0; j < st.length - i; j++) {
if (st[j].score < st[j + 1].score) {
Student s = st[j];
st[j] = st[j + 1];
st[j + 1] = s;
}
}
}
}
public String getName() {
return name;
}
public int getNo() {
return no;
}
public int getScore() {
return score;
}
}
@Test
public void myTest2() {
Student[] st = new Student[2];
st[0] = new Student("申浩", 200001, 90);
Student sunxin = new Student("孙鑫", 200002, 91);
st[1] = sunxin;
for (int i = 0; i < st.length; i++) {
System.out.println(st[i].getName());
}
}
定义一个矩形类Rectangle: a)
定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。 b)
有2个属性:长length、宽width c) 通过构造方法Rectangle(int width, int length),分别给两个属性赋值 d)
创建一个Rectangle对象,并输出相关信息
package com.neuedu.day04.homework;
public class Rectangle {
private int length;
private int width;
public Rectangle() {
super();
}
public Rectangle(int length, int width) {
super();
this.length = length;
this.width = width;
}
public int getArea(int length, int width) {
int area = length * width;
return area;
}
public int getPer(int length, int width) {
int per = 2 * (length + width);
return per;
}
public int getLength() {
return length;
}
public int getWidth() {
return width;
}
public void showAll(int length, int width,int area,int per){
System.out.println("长:" + length);
System.out.println("宽:" + width);
System.out.println("面积:" + area);
System.out.println("周长:" + per);
}
}
@Test
public void myTest3() {
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
int width = sc.nextInt();
Rectangle Re = new Rectangle(length, width);
int area = Re.getArea(length, width);
int per = Re.getPer(length, width);
Re.showAll(length, width, area, per);
}
定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 a) 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
b) 输出笔记本信息的方法 c) 然后编写一个测试类,测试笔记本类的各个方法。
package com.neuedu.day04.homework;
public class Notebook {
private char color;
private int cpu;
public Notebook() {
super();
}
public char getColor() {
return color;
}
public void setColor(char color) {
this.color = color;
}
public int getCup() {
return cpu;
}
public void setCup(int cup) {
this.cpu = cpu;
}
public Notebook(char color, int cpu) {
super();
this.color = color;
this.cpu = cpu;
}
public void sysout(char color, int cpu) {
System.out.println("笔记本颜色是:" + color);
System.out.println("笔记本型号是:" + cpu);
}
}
@Test
public void myTest4() {
Scanner sc = new Scanner(System.in);
char color = sc.next().charAt(0);
int cpu = sc.nextInt();
Notebook n = new Notebook(color, cpu);
n.sysout(color, cpu);
}
定义一个人类Person:定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
,有三个属性:名字、身高、体重 。
package com.neuedu.day04.homework;
public class Person {
private String name;
private float heigth;
private float weigth;
public Person(String name, float heigth, float weigth) {
super();
this.name = name;
this.heigth = heigth;
this.weigth = weigth;
}
public void sayHello(String name) {
System.out.println("hello,my name is " + name);
}
}
@Test
public void myTest5() {
Scanner sc = new Scanner(System.in);
String name = sc.next();
float heigth = sc.nextFloat();
float weigth = sc.nextFloat();
Person p = new Person(name, heigth, weigth);
p.sayHello(name);
}
定义一个PersonCreate类:创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74,分别调用对象的sayHello()方法。
package com.neuedu.day04.homework;
public class PersonCreate {
private String name;
private String age;
private float heigth;
public PersonCreate(String name, String age, float heigth) {
super();
this.name = name;
this.age = age;
this.heigth = heigth;
}
public void sayHello(String name) {
System.out.println("hello,my name is " + name);
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public float getHeigth() {
return heigth;
}
}
@Test
public void myTest6() {
PersonCreate p = new PersonCreate("zhangsan", "33岁", 1.73f);
PersonCreate p1 = new PersonCreate("lishi", "44岁", 1.74f);
p.sayHello(p.getName());
p1.sayHello(p1.getName());
}
}