一、实验目的
二、实验内容及要求
1、实验内容与要求:
2、要求:
3、实验安排方式:
三、实验环境
四、程序核心代码和运行效果
五、实验小结
一、实验目的
(1)熟练掌握类、对象的的概念以及对事物的抽象。
(2)熟练掌握成员、方法的概念以及构造方法的概念。
(3)熟练掌握封装性,多态性的概念
(4)理解面向对象的程序设计方法。
二、实验内容及要求
1、实验内容与要求:
(1)
先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义一个方法setTri对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。在main()中创建一个三角形对象,求给定三点的三角形的周长、面积。
(2)
将上题的方法setTri改用构造方法实现初始化。
(3)
编写JAVA程序创建圆柱体类,具有求表面积和体积的功能。已知给定一个圆柱体,底面圆心p为(0,0),半径r为10,圆柱体高5,求此圆柱体的表面积和体积。
(4)
试声明一个类,类名为student
属性如下:
学号(no):1001
姓名(name):张小林
性别(sex):男
年龄(age):24
行为如下:
上课(HaveClass):返回值为true表示上课;返回值为false表示没上课。
睡觉(Sleep):返回值为true表示在睡觉,返回值为false表示没睡觉。
声明该类的对象(stu),并输出该对象的属性和行为的值。
运行结果为:
学号:1001
姓名:张小林
性别:男
年龄:24
sleep()=true
HaveClass()=false
(5)
定义一个表示学生信息的类Student,要求如下:
a.类Student的成员变量:
sNO表示学号;
sName表示姓名;
sSex表示性别;
sAge表示年龄;
sJava:表示Java课程成绩。
b.类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
c.类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java 课程成绩
根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
(6)
定义一个“圆”类Circle,该圆类的数据成员包括:圆心点位置及圆的半径;方法成员有:设置圆心位置和半径的方法,获取圆心位置和半径的方法及构造方法。要求构造方法可以接收圆心位置参数,而半径使用缺省值1。编写完整的程序并创建Circle类的对象,并且分别调用各种方法,对比这些方法的执行结果。
(7)
创建一个类,类名为StaticMethodTest,包含一个静态方法和一个实例方法,在main方法中分别调用这个两个方法进行测试。
(8)
创建一个Person类,在该类中用不同的访问修饰符设立不同的person属性,再创建一个PersonTest类,包括main方法,在此类中分别访问Person类中各个属性。
2、要求:
能够上机编辑、调试java程序
3、实验安排方式:
每组1人,独立完成上机实验
三、实验环境
硬件环境:微型计算机一台。
软件环境:Window XP/7/8/10操作系统、Eclipse、JDK。
四、程序核心代码和运行效果
(1)
point类:
public class Point {
//构造一个标准的JavaBeen类
private double x;
private double y;
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = 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;
}
}
Trianglele类:
import java.security.PKCS12Attribute;
public class Trianglele {
//定义Point实体
Point p1;
Point p2;
Point p3;
double a, b, c;
//定义setTri方法三个点进行初始化
public void setTri(Point p1, Point p2, Point p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
//定义方法GetLength求出圆的周长
public double GetLength() {
double l;
//调用数学函数,依据两点间距离公式求出任意两点间的距离
a = Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2) + Math.pow((p1.getY() - p2.getY()), 2));
b = Math.sqrt(Math.pow((p1.getX() - p3.getX()), 2) + Math.pow((p1.getY() - p3.getY()), 2));
c = Math.sqrt(Math.pow((p3.getX() - p2.getX()), 2) + Math.pow((p3.getY() - p2.getY()), 2));
//计算周长
l = a + b + c;
return l;
}
//定义方法GetS求出圆的面积
public double GetS() {
double p;
//使用海伦公式求出面积
p = GetLength() / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
Test类:
public class Test {
public static void main(String[] args) {
//创建point对象,并进行初始化赋值
Point p1 = new Point(0, 0);
Point p2 = new Point(3, 0);
Point p3 = new Point(0, 4);
//创建Trianglele对象
Trianglele tri = new Trianglele();
//调用setTri方法
tri.setTri(p1, p2, p3);
//获取圆的周长l和面积s
double l = tri.GetLength();
double s = tri.GetS();
//打印周长l和面积s
System.out.println(l);
System.out.println(s);
}
}
(2)
point类:
public class Point {
private double x;
private double y;
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = 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;
}
}
Trianglele类:
public class Trianglele {
Point p1;
Point p2;
Point p3;
double a, b, c;
//构造方法对三个点进行初始化
public Trianglele(Point p1, Point p2, Point p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public double GetLength() {
double l;
a = Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2) + Math.pow((p1.getY() - p2.getY()), 2));
b = Math.sqrt(Math.pow((p1.getX() - p3.getX()), 2) + Math.pow((p1.getY() - p3.getY()), 2));
c = Math.sqrt(Math.pow((p3.getX() - p2.getX()), 2) + Math.pow((p3.getY() - p2.getY()), 2));
l = a + b + c;
return l;
}
public double GetS() {
double p;
p = GetLength() / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
Test类:
public class Test {
public static void main(String[] args) {
//创建point对象,并进行初始化赋值
Point p1 = new Point(0, 0);
Point p2 = new Point(3, 0);
Point p3 = new Point(0, 4);
//创建Trianglele对象,并进行初始化赋值
Trianglele tri = new Trianglele(p1, p2, p3);
//获取圆的周长l和面积s
double l = tri.GetLength();
double s = tri.GetS();
//打印周长l和面积s
System.out.println(l);
System.out.println(s);
}
}
(3)
Cylind类:
public class Cylinder {
static double PI = 3.14;
private double R;
private double H;
//空参构造
public Cylinder() {
}
//带参构造
public Cylinder(double R, double H) {
this.R = R;
this.H = H;
}
//获得R的值
public double getR() {
return R;
}
//定义R的值
public void setR(double R) {
this.R = R;
}
//获得H的值
public double getH() {
return H;
}
//定义H的值
public void setH(double H) {
this.H = H;
}
//定义求圆柱表面积的方法Surface
public double Surface() {
double s;
s = PI * getR() * getR() * 2 + 2 * PI * getR() * getH();
return s;
}
//定义求圆柱体积的方法Volume
public double Volume() {
double v;
v = PI * getR() * getR() * getH();
return v;
}
}
Test类:
package JavaTest.test3;
public class Test {
public static void main(String[] args) {
//创建Cylinder对象c,并将圆柱的参数传入
Cylinder c = new Cylinder(10, 5);
System.out.println("圆柱的表面积为:" + c.Surface());
System.out.println("圆柱的体积为: " + c.Volume());
}
}
(4)
student类:
public class Student {
private String no;
private String name;
private String gender;
private int age;
//空参构造
public Student() {
}
//带参构造,直接进行初始化
public Student(String no, String name, String gender, int age) {
this.no = no;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getNo() {//定义方法getNo,获得学号
return no;
}
public void setNo(String no) {//定义方法setNo,更改学号
this.no = no;
}
public String getName() {//定义方法getName,获得姓名
return name;
}
public void setName(String name) {//定义方法setName,更改姓名
this.name = name;
}
public String getGender() {//定义方法getGender,获得性别
return gender;
}
public void setGender(String gender) {//定义方法setGender,更改性别
this.gender = gender;
}
public int getAge() {//定义方法getAge,获得年龄
return age;
}
public void setAge(int age) {//定义方法setAge,更改年龄
this.age = age;
}
//定义方法HaveClass,获得是否上课
public boolean HaveClass(int a) {
if (a == 0)
return false;
else
return true;
}
//定义方法Sleep,获得是否上课
public boolean Sleep(int a) {
if (a == 0)
return true;
else
return false;
}
}
Test类:
public class Test {
public static void main(String[] args) {
//创建Student,并进行初始化赋值
Student stu = new Student("1001","张小林","男",24);
//打印学生信息
System.out.println("学号:"+stu.getNo());
System.out.println("姓名:"+stu.getName());
System.out.println("性别:"+stu.getGender());
System.out.println("年龄:"+stu.getAge());
System.out.println("Sleep()="+stu.Sleep(0));
System.out.println("HaveClass()="+stu.HaveClass(0));
}
}
(5)
student类:
public class Student {
//定义成员变量
private String sNo;
private String sName;
private String sGender;
private int sAge;
private double sJava;
public Student() {//空参构造
}
//带参构造,初始化
public Student(String sNo, String sName, String sGender, int sAge, double sJava) {
this.sNo = sNo;
this.sName = sName;
this.sGender = sGender;
this.sAge = sAge;
this.sJava = sJava;
}
//通过get方法对学生的各个信息进行访问
//通过set方法对学生的各个信息进行更改
//学号
public String getsNo() {
return sNo;
}
public void setsNo(String sNo) {
this.sNo = sNo;
}
//姓名
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
//性别
public String getsGender() {
return sGender;
}
public void setsGender(String sGender) {
this.sGender = sGender;
}
//年龄
public int getsAge() {
return sAge;
}
public void setsAge(int sAge) {
this.sAge = sAge;
}
//Java成绩
public double getsJava() {
return sJava;
}
public void setsJava(double sJava) {
this.sJava = sJava;
}
public static double Average(double[] arr) {//定义方法Average求数组的平均值
double sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
return sum / arr.length;
}
public static double Max(double[] arr) {//定义方法Max求数组的最小值
double max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (max < arr[i])
max = arr[i];
}
return max;
}
public static double Min(double[] arr) {//定义方法Min求数组的最大值
double min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (min > arr[i])
min = arr[i];
}
return min;
}
}
Test类:
public class Test {
public static void main(String[] args) {
Student stu1 = new Student("1001", "张三", "男", 19, 96);
Student stu2 = new Student("1002", "李四", "男", 20, 89);
Student stu3 = new Student("1003", "陈五", "女", 18, 92);
Student stu4 = new Student("1004", "赵六", "男", 21, 95);
Student stu5 = new Student("1005", "王七", "女", 20, 98);
double[] arr = {stu1.getsJava(), stu2.getsJava(), stu3.getsJava(), stu4.getsJava(), stu5.getsJava()};
System.out.println("学号" + '\t' + "姓名" + '\t' + "性别" + '\t' + "年龄" + '\t' + "Java成绩");
System.out.println(stu1.getsNo() + '\t' + stu1.getsName() + '\t' + stu1.getsGender() + '\t' + '\t' + stu1.getsAge() + '\t' + '\t' + stu1.getsJava());
System.out.println(stu2.getsNo() + '\t' + stu2.getsName() + '\t' + stu2.getsGender() + '\t' + '\t' + stu2.getsAge() + '\t' + '\t' + stu2.getsJava());
System.out.println(stu3.getsNo() + '\t' + stu3.getsName() + '\t' + stu3.getsGender() + '\t' + '\t' + stu3.getsAge() + '\t' + '\t' + stu3.getsJava());
System.out.println(stu4.getsNo() + '\t' + stu4.getsName() + '\t' + stu4.getsGender() + '\t' + '\t' + stu4.getsAge() + '\t' + '\t' + stu4.getsJava());
System.out.println(stu5.getsNo() + '\t' + stu5.getsName() + '\t' + stu5.getsGender() + '\t' + '\t' + stu5.getsAge() + '\t' + '\t' + stu5.getsJava());
System.out.println("Java成绩平均值:" + Student.Average(arr));
System.out.println("Java成绩最大值:" + Student.Max(arr));
System.out.println("Java成绩最小值:" + Student.Min(arr));
}
}
(6)
Circle类:
public class Circle {
//定义成员变量
private int x;
private int y;
private int r;
//空参构造
public Circle() {
}
//带参构造,初始化
public Circle(int x, int y) {
this.x = x;
this.y = y;
}
//通过get方法对圆的半径,坐标进行访问
//通过set方法对圆的半径,坐标进行更改
//坐标
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;
}
//半径
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
Test类:
public class Test {
public static void main(String[] args) {
System.out.println("创建对象并初始化");
Circle c = new Circle(2, 3);
System.out.println("使用get方法获取参数");
System.out.println("获取圆心位置:" + "x:" + c.getX() + " y:" + c.getX());
System.out.println("获取圆的半径:" + "r:" + c.getR());
System.out.println();
System.out.println("使用set方法重新定义参数");
c.setX(5);
c.setR(7);
c.setR(6);
System.out.println("新圆心位置:" + "x:" + c.getX() + " y:" + c.getX());
System.out.println("新圆的半径:" + "r:" + c.getR());
}
}
(7)
StaticMethodTest类:
public class StaticMethodTest {
public static void main(String[] args) {
//创建StaticMethodTest对象
StaticMethodTest sta = new StaticMethodTest();
//分别调用静态方法和实例方法
StaticMethodTest.Static();
sta.FalSta();
}
//定义静态方法Static
public static void Static() {
System.out.println("成功调用静态方法");
}
//定义实例方法FalSta
public void FalSta() {
System.out.println("成功调用实例方法");
}
}
(8)
Person类:
public class Person {
//用不同的访问修饰符设立不同的成员变量
String no;
public String name;
private String gender;
protected int age;
public Person() {
}
public Person(String no, String name, String gender, int age) {
this.no = no;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
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;
}
}
PersoTest类:
public class PersonTest {
public static void main(String[] args) {
//创建Person,并初始化
Person p = new Person("1000", "张三", "男", 20);
//访问默认类型变量no
System.out.println("学号:" + p.no);
System.out.println("学号:" + p.getNo());
//访问public类型变量name
System.out.println("姓名:" + p.name);
System.out.println("姓名:" + p.getName());
//访问private类型变量gender
System.out.println("性别:" + p.getGender());
//访问protected类型变量age
System.out.println("年龄:" + p.age);
System.out.println("年龄:" + p.getAge());
}
}