java面向对象基础题库_Java面向对象基础习题

/*1、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(int x0,y0),以及一个

*movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,

*分别调用movePoint方法后,打印p1和p2的坐标

*/

private int x;

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;

}

private int y;

public Point() {}

public Point(int i,int j) {

this.x=i;

this.y=j;

}

public void movePoint(int dx,int dy) {

this.x=dx;

this.y=dy;

}

在测试类中调用

public static void main(String[] args) {

Point p1=new Point();

Point p2=new Point(1,3);

System.out.println(p2.getX()+" "+p2.getY());

p2.movePoint(5, 6);

System.out.println(p2.getX()+" "+p2.getY());

}

运行图:

d47778bf04a5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

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

2.1  定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。

2.2  有2个属性:长length、宽width

2.3  通过构造方法Rectangle(int width, int length),分别给两个属性赋值

2.4  创建一个Rectangle对象,并输出相关信息

*/

private int width;

private int length;

public Rectangle(int width,int length) {

this.width=width;

this.length=length;

}

public void getArea() {

System.out.println(width*length);

}

public void getPer() {

System.out.println((width+length)*2);

}

public void showAll() {

System.out.println(length+" "+width);

}

public static void main(String[] args) {

Rectangle rec=new Rectangle(15,23);

rec.getArea();

rec.getPer();

rec.showAll();

}

运行图:

d47778bf04a5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

/*3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。

3.1 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;

3.2  输出笔记本信息的方法

3.3  然后编写一个测试类,测试笔记本类的各个方法。

*/

char color;

public  Note(char color, int cpuNo) {

// TODO Auto-generated constructor stub

this.color=color;

this.cpuNo=cpuNo;

}

public char getColor() {

return color;

}

public void setColor(char color) {

this.color = color;

}

public int getCpuNo() {

return cpuNo;

}

public void setCpuNo(int cpuNo) {

this.cpuNo = cpuNo;

}

int cpuNo;

@Override

public String toString() {

return "Note [color=" + color + ", cpuNo=" + cpuNo + "]";

}

public  Note() {}

在测试类中加入如下代码:

Note n=new Note();

n.setColor('y');

n.setCpuNo(123);

System.out.println(n.toString());

Note m=new Note('r',875);

System.out.println(m.toString());

运行图:

d47778bf04a5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

/*4、设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,

* 按照成绩从高到低的顺序输出姓名、学号和成绩信息。

*/

int stuNo;

String stuName;

int score;

public int getStuNo() {

return stuNo;

}

public void setStuNo(int stuNo) {

this.stuNo = stuNo;

}

public String getStuName() {

return stuName;

}

public void setStuName(String stuName) {

this.stuName = stuName;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

public Student(int stuNo,String stuName,int score) {

this.stuNo=stuNo;

this.stuName=stuName;

this.score=score;

}

public void show() {

System.out.println("stuNo:"+stuNo+"stuName:"+stuName+"score:"+score);

}

public static void soft(Student[] stus) {

Student stu;

for(int i=0;i

for(int j=0;j

if(stus[j].score>stus[j+1].score) {

stu=stus[j];

stus[j]=stus[j+1];

stus[j+1]=stu;

}

}

}

}

public static void main(String[] args) {

Student[] stus=new Student[3];

stus[0]=new Student(23,"李四",85);

stus[1]=new Student(28,"张三",68);

stus[2]=new Student(18,"王五",73);

Student.soft(stus);

for(Student stu:stus) {

stu.show();

}

}

运行图:

d47778bf04a5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

/*5、定义两个类,描述如下:

5.1定义一个人类Person:

5.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”

5.1.2有三个属性:名字、身高、体重

5.2定义一个PersonCreate类:

5.2.1创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74

5.2.2分别调用对象的sayHello()方法*/

String name;

int height;

double weight;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

public double getWeight() {

return weight;

}

public void setWeight(double weight) {

this.weight = weight;

}

public void sayHello() {

System.out.println("Hello ,My name is "+name);

}

第二个类:

public static void main(String[] args) {

Person p1=new Person();

p1.setName("zhangsan");

p1.setHeight(168);

p1.setWeight(56.8);

Person p2=new Person();

p2.setName("lisi");

p2.setHeight(172);

p2.setWeight(52.3);

p1.sayHello();

p2.sayHello();

}

运行图:

d47778bf04a5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

/*7、定义一个汽车类Vehicle,要求如下:

7.1属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型),并且所有属性为私有。

7.2至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。

7.3为私有属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。

7.4定义一个一般方法run(),用打印语句描述汽车奔跑的功能

7.5定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车*/

private String brand;

private String color;

private double speed=0;

Vehicle(String brand,String color){

this.brand=brand;

this.color=color;

}

void Vehicle(String brand,String color,double speed) {

this.brand=brand;

this.color=color;

this.speed=speed;

}

void run() {

System.out.println("品牌:"+brand+"颜色:"+color+"速度:"+speed);

}

在测试类的主函数中加入如下代码

Vehicle v=new Vehicle("poxie","black");

v.run();

v.Vehicle("benz", "black", 389.6);

v.run();

运行图:

d47778bf04a5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值