一、本次实验主要考查以下知识点:
(1)面向对象的类和对象
(2)成员变量的访问方法(getter)和修改方法(setter)
(3)成员方法定义
(4)构造方法
二、实验题目(使用 Java 语言编程)
1、定一个名为 Person 的类,其中含有一个 String 类型的成员变量 name 和一个 int 类型的成员变量 age, 分别为这两个变量定义访问方法和修改方法,另外再 为该类定义一个名为 speak 的方法, 在其中输出 name 和 age 的值。编写一应用 程序,使用上面定义的 Person 类,实现数据的访问、修改。
public class Person {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void speak()
{
System.out.println("name:"+name+",age:"+age);
}
}
//测试类:
public class PersonTest {
public static void main(String[] args) {
Person person = new Person();
person.setName("jianghui");
person.setAge(38);
person.speak();
}
}
2、定义一个名为 Rectangle 的类表示矩形,其中含有 length、width 两个 double 型的成员变量表示矩形的长和宽。 要求为每个变量定义访问方法和修改方法, 定义求矩形周长的方法 perimeter()和求面积的方法 area()。 编写一个类测试这个 矩形类。
public class Rectangle {
private double length;
private double width;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double perimeter() {
return (length + width) * 2;
}
public double area() {
return length * width;
}
}
//测试类:
public class RectangleTest {
public static void main(String[] args) {
//使用无参构造方法创建对象(该构造方法由编译器提供)
Rectangle rectangle1 = new Rectangle();
rectangle1.setLength(8.5);
rectangle1.setWidth(8.5);
System.out.println("perimeter="+rectangle1.perimeter());
System.out.println("area="+rectangle1.area());
}
}
3、为上题的 Rectangle 类编写一个带参数的构造方法,通过用户给出的长、宽 创建矩形对象,再编写一个默认构造方法(无参构造方法),使用默认构造方法创建 矩形对象然后通过 setter 方法为对象属性赋值。编写一个类测试这个矩形类。
public class Rectangle {
private double length;
private double width;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double perimeter() {
return (length + width) * 2;
}
public double area() {
return length * width;
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
//该无参构造方法需要显式定义,因为已经定义了带参构造(编译器不再提供默认的无参构造方法)
public Rectangle() {
}
}
//测试类:
public class RectangleTest {
public static void main(String[] args) {
//使用带参构造方法创建对象
Rectangle rectangle = new Rectangle(7.5,7.5);
System.out.println("perimeter="+rectangle.perimeter());
System.out.println("area="+rectangle.area());
//使用无参构造方法创建对象
Rectangle rectangle1 = new Rectangle();
rectangle1.setLength(8.5);
rectangle1.setWidth(8.5);
System.out.println("perimeter="+rectangle1.perimeter());
System.out.println("area="+rectangle1.area());
}
}
4、定义一个 Triangle 类表示三角形,其中包括 3 个 double 型变量 a、b、c,用 于表示 3 条边长。为该类定义两个构造方法 :默认构造方法(无参构造方法) 设置三角形的 3 条边长都为 0.0;带 3 个参数的构造方法通过传递 3 个参数创建 三角形 对 象 。 定 义 求 三 角 形 面 积 的 方 法 area() , 面 积 计 算 公 式 为 : area=Math.sqrt(s*(s-a)(s-b)(s-c)),其中 s=(a+b+c) /2。编写程序测试该类。
public class Triangle {
private double a;
private double b;
private double c;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
public Triangle() {
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double area()
{
if( a + b < c || a + c < b || b + c < a )
{
System.out.println("无法构成三角形");
return 0.0;
}
else {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
}
}
//测试类:
public class TriangleTest {
public static void main(String[] args) {
Triangle triangle = new Triangle(3.0,1.0,1.5);
System.out.println("area="+triangle.area());
}
}
5、定义一个表示二维平面上的点 Point 类,并在该类中定义一个计算两点之间 距离的方法,其格式如下: • public double getDistance(Point p) 编写一个类测试这个 Point 类。
public class Point {
private double x;
private double y;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getDistance(Point p)
{
return Math.sqrt((this.x - p.x) * (this.x - p.x) +(this.y - p.y) *(this.y - p.y));
}
}
//测试类:
import java.util.Scanner;
public class PointTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个点的横坐标和纵坐标:");
double x1 = sc.nextDouble();
double y1 = sc.nextDouble();
Point p1 = new Point(x1, y1);
System.out.print("请输入第二个点的横坐标和纵坐标:");
double x2 = sc.nextDouble();
double y2 = sc.nextDouble();
Point p2 = new Point(x2, y2);
sc.close();
System.out.println("(" + x1 + "," + y1 + ")到(" + x2 + "," + y2 + ")之间的距离是:" + p1.getDistance(p2));
}
}