一、定義一個點(diǎn)類Point,包含2個成員變量x、y分別表示x和y坐標(biāo),2個構(gòu)造器Point()和Point(intx0,y0),以及一個movePoint(intdx,intdy)方法實(shí)現(xiàn)點(diǎn)的位置移動,創(chuàng)建兩個Point對象p1、p2,分別調(diào)用movePoint方法后,打印p1和p2的坐標(biāo)。[必作題]
package abc;
public class Point {
int x;
int y;
public Point(int x0, int y0) {
super();
this.x = x0;
this.y = y0;
}
public Point() {
super();
}
public String movePoint(int dx, int dy) {
x = dx + x;
y = dy + y;
return ("x為" + x + "y為" + y);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Point p1 = new Point(5,8);
System.out.println(p1.movePoint(6, 9));
Point p2 = new Point(2, 3);
System.out.println(p2.movePoint(5, 7));
}
二、定義一個矩形類Rectangle:(知識點(diǎn):對象的創(chuàng)建和使用)[必做題]
1.定義三個方法:getArea()求面積、getPer()求周長,showAll()分別在控制臺輸出長、寬、面積、周長。
2.有2個屬性:長length、寬width
3.通過構(gòu)造方法Rectangle(intwidth,intlength),分別給兩個屬性賦值
4.創(chuàng)建一個Rectangle對象,并輸出相關(guān)信息
package abc;
public class Rectangle {
int length,width;
public int getArea(int length,int width){
return length*width;
}
public int getPer(int length,int width){
return (length+width)*2;
}
public void showAll(){
System.out.println("長方形的長是"+length+"寬是"+width);
System.out.println("周長是"+ (length+width)*2);
System.out.println("面積是"+length*width);
}
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle r=new Rectangle(15, 10);
r.showAll();
}
}
三、定義一個筆記本類,該類有顏色(char)和cpu型號(int)兩個屬性。[必做題]
1.無參和有參的兩個構(gòu)造方法;有參構(gòu)造方法可以在創(chuàng)建對象的同時(shí)為每個屬性賦值;
2.輸出筆記本信息的方法
3.然后編寫一個測試類,測試筆記本類的各個方法。
package abc;
public class computer {
char colour;
int cpu;
public void show() {
System.out.println("我的顏色是:" + colour+"色" + ",型號是:" + cpu);
}
public computer(char colour, int cpu) {
super();
this.colour = colour;
this.cpu = cpu;
}
public computer() {
super();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
computer s1=new computer();
s1.colour='黑';
s1.cpu=121;
s1.show();
computer s2=new computer('藍(lán)',111);
s2.show();
}
}
四、定義兩個類,描述如下:[必做題]
1.定義一個人類Person:
(1)定義一個方法sayHello(),可以向?qū)Ψ桨l(fā)出問候語“hello,mynameisXXX”
(2)有三個屬性:名字、身高、體重
2.定義一個PersonCreate類:
(1)創(chuàng)建兩個對象,分別是zhangsan,33歲,1.73;lishi,44,1.74
(2)分別調(diào)用對象的sayHello()方法。
package abc;
public class Person {
String name;
double height;
int weight;
int age;
public void sayhello() {
System.out.println("hello,my name is " + name);
System.out.println("my height is " + height + "米");
System.out.println("my weight is " + weight + "斤");
System.out.println("my age is " + age + "歲");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Person a = new Person();
Person b = new Person();
a.name ="zhangsan";
a.age = 21;
a.height = 1.70;
a.weight = 130;
b.name = "lishi";
b.age = 44;
b.height = 1.74;
b.weight = 140;
a.sayhello();
b.sayhello();
}
}
五、定義兩個類,描述如下:[必做題]
1.定義一個人類Person:
(1)定義一個方法sayHello(),可以向?qū)Ψ桨l(fā)出問候語“hello,mynameisXXX”
(2)有三個屬性:名字、身高、體重
(3)通過構(gòu)造方法,分別給三個屬性賦值
2.定義一個Constructor類:
(1)創(chuàng)建兩個對象,分別是zhangsan,33歲,1.73;lishi,44,1.74
(2)分別調(diào)用對象的sayHello()方法。
package abc;
public class Constructor {
String name;
double height;
int weight;
int age;
public Constructor (String name, double height, int weight,int age) {
super();
this.name = name;
this.height = height;
this.weight = weight;
this.age=age;
}
public void sayhello() {
System.out.println("hello,my name is " + name);
System.out.println("my height is " + height + "米")
System.out.println("my weight is " + weight + "千克");
System.out.println("my age is " + age + "歲");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Constructor a=new Constructor ("zhangsan",1.70,55,73);
Constructor b=new Constructor ("lishi",1.76,54,74);
a.sayhello();
b.sayhello();
}
}
六、定義一個汽車類Vehicle,要求如下:[選做題]
1.屬性包括:汽車品牌brand(String類型)、顏色color(String類型)和速度speed(double類型),并且所有屬性為私有。
2.至少提供一個有參的構(gòu)造方法(要求品牌和顏色可以初始化為任意值,但速度的初始值必須為0)。
3.為私有屬性提供訪問器方法。注意:汽車品牌一旦初始化之后不能修改。
4.定義一個一般方法run(),用打印語句描述汽車奔跑
的功能
5.定義測試類VehicleTest,在其main方法中創(chuàng)建一個品牌為“benz”、顏色為“black”的汽車。
package abc;
public class Vehicle {
private String brand;
private String color;
private double speed;
public Vehicle(String brand,String color){
this.brand=brand;
this.color=color;
}
Vehicle(String brand, String color, double speed) {
super();
this.brand = brand;
this.color = color;
this.speed = speed;
}
public void run(){
System.out.println("這個汽車的品牌為"+this.brand+"這個汽車的顏色為"+this.color+"這個汽車的速度為"+this.speed);
}
public static void main(String[] args) {
Vehicle v=new Vehicle("benz","red");
v.run();
Vehicle v1=new Vehicle("benz","black",13);
v1.run();
}
}
浙公網(wǎng)安備 33010602011771號