Java面向对象实验报告1
实验要求
1. 已知学生类(Student),该类中包含有如下成员变量和成员方法:
1)成员变量
年级、学院、专业、班级、姓名、面向对象程序设计、网络原理、数据库、总成绩
2)成员方法
计算总成绩
2. (1)编写MyStudent类,通过该类在程序中直接为成员变量赋值,并通过控制台输出该生的姓名、学院、专业、班级和总成绩。
(2)通过构造函数为成员变量赋值,并通过控制台输出该生的姓名、学院、专业、班级和总成绩。
注意:要求以上的2种方法编程,输出结果相同
3.定义一个代表员工的Employee类。它的属性包括“员工姓名”、“员工号码”、“员工基本薪水”、“员工薪水增长额”;它的方法包括“构造方法”、“获取员工性名”、“获取员工号码”、“获取员工基本薪水”、“计算薪水增长额”及“计算增长后的工资总额”。
代码实现
public class Employee {
private String e_name;
private int e_id,e_basicSalary,e_increase;
public Employee() {
super();
}
public Employee(String e_name, int e_id, int e_basicSalary, int e_increase) {
super();
this.e_name = e_name;
this.e_id = e_id;
this.e_basicSalary = e_basicSalary;
this.e_increase = e_increase;
}
public String getE_name() {
return e_name;
}
public int getE_id() {
return e_id;
}
public int getE_basicSalary() {
return e_basicSalary;
}
//计算薪水增长额
public int countSIn(int e_basicSalary,double rate){//rate薪水增长率
double e_increase=e_basicSalary*rate;
return (int)e_increase;
}
//计算增长后的工资总额计算增长后的工资总额
public int zong(int e_basicSalary,int e_increase){
return e_basicSalary+e_increase;
}
}
public class student {
public String grade,college,major,class_b,name;
public int FOP,NP,DB,AllScore;
public student() {
super();
}
public student(String grade, String college, String major, String class_b, String name, int fOP, int nP, int dB,
int allScore) {
super();
this.grade = grade;
this.college = college;
this.major = major;
this.class_b = class_b;
this.name = name;
FOP = fOP;
NP = nP;
DB = dB;
AllScore = allScore;
}
public int AllScore(int fOP, int nP, int dB) {
int result=fOP+nP+dB;
return result;
}
@Override
public String toString() {
return "student [grade=" + grade + ", college=" + college + ", major=" + major + ", class_b=" + class_b
+ ", name=" + name + ", FOP=" + FOP + ", NP=" + NP + ", DB=" + DB + ", AllScore=" + AllScore + "]";
}
}
public class MyStudent {
public static void main(String[] args) {
//method1:年级、学院、专业、班级、姓名、面向对象程序设计、网络原理、数据库、总成绩
student s=
new student("大三","计算机学院","软件工程","软件一班","张三",78,89,95,0);
int zong=s.AllScore(78, 89, 95);
s.AllScore=zong;
System.out.println("student [grade=" + s.grade + ", college=" + s.college + ", major=" + s.major + ", class_b=" + s.class_b
+ ", name=" + s.name + ", FOP=" + s.FOP + ", NP=" + s.NP + ", DB=" + s.DB + ", AllScore=" + s.AllScore + "]");
//method2:年级、学院、专业、班级、姓名、面向对象程序设计、网络原理、数据库、总成绩
student s1=new student();
s1.grade="大三";
s1.college="计算机学院";
s1.major="软件工程";
s1.class_b="软件一班";
s1.name="张三";
s1.FOP=78;
s1.NP=89;
s1.DB=95;
int result=s.AllScore(s1.FOP, s1.NP, s1.DB);
s1.AllScore=result;
System.out.println("student [grade=" + s1.grade + ", college=" + s1.college + ", major=" + s1.major + ", class_b=" + s1.class_b
+ ", name=" + s1.name + ", FOP=" + s1.FOP + ", NP=" + s1.NP + ", DB=" + s1.DB + ", AllScore=" + s1.AllScore + "]");
}
}