1、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。[必作题]
package homework;
//定义点类Point
public class Point {
int x;
int y;
public Point() {}
public Point(int x0,int y0) {
}
public void movePoint(int dx,int dy) {
dx+=10;
dy+=10;
System.out.println("("+dx+","+dy+")");
}
}
package homework;
public class Ps6 {
public static void main(String[] args) {
/*
* 1、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,
* 2个构造器Point()和Point(int x0,y0),以及一个
* movePoint(int dx,int dy)方法实现点的位置移动,创建两个
* Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。
* [必作题]
*/
Point p1=new Point();
Point p2=new Point();
p1.movePoint(10, 10);
p2.movePoint(5, 5);
}
}
2、定义一个矩形类Rectangle:(知识点:对象的创建和使用)[必做题]
2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2.2 有2个属性:长length、宽width
2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
2.4 创建一个Rectangle对象,并输出相关信息
package homework;
public class Rectangle {
//定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
//2.2 有2个属性:长length、宽width
//2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
//2.4 创建一个Rectangle对象,并输出相关信息
int width;
int lenght;
public Rectangle(int width,int lenght) {
this.width=width;
this.lenght=lenght;
}
public double getArea() {
return this.width*this.lenght;
}
public double getPer() {
return (this.width+this.lenght)*2;
}
public void showAll() {
System.out.println("长"+this.lenght+"宽"+this.width);
System.out.println("面积:"+getArea());
System.out.println("周长:"+getPer());
}
}
package homework;
public class Ps6_2 {
/*
2、定义一个矩形类Rectangle:(知识点:对象的创建和使用)[必做题]
2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2.2 有2个属性:长length、宽width
2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
2.4 创建一个Rectangle对象,并输出相关信息
*/
public static void main(String[] arg) {
Rectangle s1=new Rectangle(10, 10);
s1.showAll();
}
}
3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 [必做题]
3.1 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
3.2 输出笔记本信息的方法
3.3 然后编写一个测试类,测试笔记本类的各个方法。
package homework;
public class Book {
/*
3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 [必做题]
3.1 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
3.2 输出笔记本信息的方法
3.3 然后编写一个测试类,测试笔记本类的各个方法。
*/
char color;
int cpu;
public Book() {
}
public Book(char color,int cpu) {
this.color=color;
this.cpu=cpu;
}
public void show() {
System.out.println("笔记本颜色:"+this.color+";cpu:"+this.cpu);
}
/**
* @return the color
*/
public char getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(char color) {
this.color = color;
}
/**
* @return the cpu
*/
public int getCpu() {
return cpu;
}
/**
* @param cpu the cpu to set
*/
public void setCpu(int cpu) {
this.cpu = cpu;
}
}
package homework;
public class Test_book {
public static void main(String[] args) {
Book b=new Book();
b.show();
Book c=new Book('红',52);
c.show();
}
}
4、设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。[选做题]
package homework;
public class Student {
/*
* 4、设计一个类Student,该类包括姓名、学号和成绩。
* 设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。[选做题]
*/
String name;
int stid;
int score;
public Student() {
}
public Student(String name,int stid, int score) {
this.name=name;
this.stid=stid;
this.score=score;
}
public void sortByScore(Student[] students) {
//int max=students[0].score;
for(int i=0;i<students.length;i++) {//有多少个数比较
for(int j=0;j<students.length;j++) {
//一个数需要和每个数比较的次数
if(students[i].getScore()<students[j].getScore()) {
Student tem=students[i];
students[i]=students[j];
students[j]=tem;
}
}
}
System.out.println("按成绩排序输出:");
for(int i=0;i<students.length;i++) {
System.out.println("name:"+students[i].name+"stid:"+students[i].stid+"score:"+students[i].score);
}
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the stid
*/
public int getStid() {
return stid;
}
/**
* @param stid the stid to set
*/
public void setStid(int stid) {
this.stid = stid;
}
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
public static void main(String[] args) {
}
}
package homework;
public class StudentTest {
public static void main(String[] args) {
Student s1=new Student("zhansan",01,80);
Student s2=new Student("lisi",02,91);
Student s3=new Student("zhaosi",03,82);
Student[] students=new Student[] {s1,s2,s3};
//Student s=new Student();
new Student().sortByScore(students);
}
}