面向对象封装方法的一些练习

1.封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩,封装方法,求总分,平均分,以及打印学生的信息。
public class practice {

public static void main(String[] args) {
	
	Student s = new Student();
	s.setAge(18);
	s.setName("qiaofeng");
	s.setGender("男");
	s.setChineseScore(140);
	s.setMathScore(150);
	s.setEnglishScore(150);
	s.getTotalScore();
	s.showStudentInfo();
}

}
class Student{
private String name;
private int age;
private String gender;

private double englishscore;
private double mathscore;
private double chinesescore;

// 构造方法
public Student () {
}

public Student(String name, int age, String gender, double englishscore, double mathscore, double chinesescore) {

	 this.name = name;
	 this.age = age;
	 this.gender = gender;
	 this.englishscore = englishscore;
	 this.mathscore = mathscore;
	 this.chinesescore = chinesescore;
}

// 普通方法

public double getTotalScore() {
	return englishscore + mathscore + chinesescore;
}

public double getAvrage() {
	return (englishscore + mathscore + chinesescore) / 3;
}

public void showStudentInfo() {
	System.out.println("姓名: " + getName() + ",年龄:" + getAge() + ",性别:" + getGender() + ",英语:" + getEnglishScore() + ",数学:" + getMathScore() + ",语文:" + getChineseScore());
}

// 特殊方法

public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getGender() {
	return gender;
}
public void setGender(String gender) {
	this.gender = gender;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}

public double getMathScore() {
	return mathscore;
}
public void setMathScore(double mathscore) {
	this.mathscore = mathscore;
}

public double getChineseScore() {
	return chinesescore;
}
public void setChineseScore(double chinesescore) {
	this.chinesescore = chinesescore;
}

public double getEnglishScore() {
	return englishscore;
}
public void setEnglishScore(double englishscore) {
	this.englishscore = englishscore;
}

}

2.定义一个长方形类,定义 求周长和面积的方法,然后定义一个测试类,进行测试。

public class practice {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 6);
rectangle.show();
}
}
class Rectangle{
private double width;
private double length;

public Rectangle() {		
}
public Rectangle(double length, double width) {
	this.length = length;
	this.width = width;
}

public double getWidth() {
	return width;
}
public void setWidth(double Width) {
	this.width = width;
}

public double getLength() {
	return length;
}
public void setLength(double Length) {
	this.length = length;
}

public double getArea() {
	return width*length;
}
public double getPerimeter() {
	return (width + length)*2;
}

public void show() {
	System.out.println("周长为: " + getPerimeter() + ",面积为: " + getArea());
}

}

3.定义一个“点”(Point)x y set get类用来表示二维空间中的点。要求如下:
A 可以生成具有特定坐标的点对象。
B 提供可以设置坐标的方法。
C 提供可以计算该点距离另一点距离的方法。
D 提供可以计算三个点构成图形的面积的方法。
面积可以使用海伦公式:边长分别为a,b,c p=(a+b+c)/2

		  s=Math.sqrt(p*(p-a)(p-b)(p-c))

public class practice{
public static void main(String[] args) {
// 构造点

	Point p1 = new Point(6, 6);

	Point p2 = new Point(66, 66);
	System.out.println(p1.getPosition() + "到" + p2.getPosition() + "的距离:" + p1.getDistance(p1, p2));
	
	Point p3 = new Point(666, 666);
	System.out.println(p1.getPosition() + "," + p2.getPosition() + "," + p3.getPosition() + "三点构成图形的面积为:" + p1.getArea(p1, p2, p3));
}

}

class Point{
private double x;
private double y;

// 构造方法
// 无参方法
public Point() {
}
// 全参构造方法
// 生成具有特定坐标的点对象和设置坐标的方法。
public Point(double x, double y) {
this.x = x;
this.y = y;
}

/*
 * 功能:计算该点到另一点的距离
 * 返回值:double
 * 参数列表:Point p1, Point p2;
 * 方法名:getDistance;
 */
public double getDistance(Point p1, Point p2) {
	double xD = p1.getX() - p2.getX();
	double yD = p1.getY() - p2.getY();
	double distance = Math.sqrt(Math.pow(xD, 2) + Math.pow(yD, 2));
	return distance;
}

// 第二种写法
/* public double getDistance(Point p) {

	double xD = this.getX() - p.getX();
	double yD = this.getY() - p.getY();
	return Math.sqrt(Math.pow(xD, 2) + Math.pow(yD, 2));
}*/

// 功能:计算面积 可以使用海伦公式:边长分别为a,b,c p=(a+b+c)/2
// s=Math.sqrt(p*(p-a)(p-b)(p-c))
// 返回值:double
// 参数列表:Point p1, Point p2, Point p3
// 方法名: getArea

public double getArea(Point p1, Point p2, Point p3) {
	double a = getDistance(this, p2);
	double b = getDistance(this, p3);
	double c = getDistance(p2, p3);
	double p = (a + b + c) / 2;
	double s=Math.sqrt(p * (p - a) * (p - b) * (p - c));
	return s;
}

// 生成具有特定坐标的点对象和设置坐标的方法。
public void setX(double x) {

	this.x = x;
}

// 生成具有特定坐标的点对象和设置坐标的方法。
public void setY(double y) {
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}

public String getPosition() {
	return "(" + x + ","+ y +")";
}

}

猜你喜欢

转载自blog.csdn.net/Ariqiao/article/details/89390076