Java面向对象实验报告(java面向对象实验报告是什么)
java面向对象综合实验。
源代码:
import java.util.*;
public class Test1 {
?? ?
public static class Student{
?? ?int id;
?? ?String name;
?? ?int math;
?? ?int english;
?? ?int history;
?? ?
?? ?public void setId(int id){
?? ??? ?this.id=id;
?? ?}
?? ?public int getId(){
?? ??? ?return id;
?? ?}
?? ?public void setName(String name){
?? ??? ?this.name=name;
?? ?}
?? ?public String getName(){
?? ??? ?return name;
?? ?}
?? ?public void setMath(int math){
?? ??? ?this.math=math;
?? ?}
?? ?public int getMath(){
?? ??? ?return math;
?? ?}
?? ?public void setEnglish(int english){
?? ??? ?this.english=english;
?? ?}
?? ?public int getEglish(){
?? ??? ?return english;
?? ?}
?? ?public void setHistory(int history){
?? ??? ?this.history=history;
?? ?}
?? ?public int getHistory(){
?? ??? ?return history;
?? ?}
}
?? ?public static void main(String[] args) {
?? ??? ?
?? ??? ?Scanner scan=new Scanner(System.in);
?? ??? ?System.out.println("请输入学生人数:");
?? ??? ?int renshu=scan.nextInt();
?? ??? ?
?? ??? ?Student[]stu=new Student[renshu];
?? ??? ?for(int i=0;irenshu;i++){
?? ??? ??? ?stu[i]=new Student();
?? ??? ??? ?int id,math,english,history;
?? ??? ??? ?String name;
?? ??? ??? ?System.out.println("请输入学生的学号:");
?? ??? ??? ?Scanner scan1=new Scanner(System.in);
?? ??? ??? ?id=scan1.nextInt();
?? ??? ??? ?System.out.println("请输入该生的姓名:");
?? ??? ??? ?Scanner scan2=new Scanner(System.in);
?? ??? ??? ?name=scan2.next();
?? ??? ??? ?System.out.println("请输入该生的数学、英语、历史成绩:");
?? ??? ??? ?Scanner scan3=new Scanner(System.in);
?? ??? ??? ?math=scan3.nextInt();
?? ??? ??? ?english=scan3.nextInt();
?? ??? ??? ?history=scan3.nextInt();
?? ??? ??? ?
?? ??? ??? ?stu[i].setId(id);
?? ??? ??? ?stu[i].setName(name);
?? ??? ??? ?stu[i].setMath(math);
?? ??? ??? ?stu[i].setEnglish(english);
?? ??? ??? ?stu[i].setHistory(history);
?? ??? ?}
?? ??? ?ListStudentlist=new ArrayList();
?? ??? ?list=Arrays.asList(stu);
?? ??? ?IteratorStudentit=list.iterator();
?? ??? ?while(it.hasNext()){
?? ??? ??? ?Student s=new Student();
?? ??? ??? ?s=it.next();
?? ??? ??? ?System.out.print("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ??? ??? ?System.out.println();
?? ??? ?}
?? ??? ?System.out.println("三门全部通过的同学为:");
?? ??? ?IteratorStudentit1=list.iterator();
?? ??? ?while(it1.hasNext()){
?? ??? ??? ?Student s=new Student();
?? ??? ??? ?s=it1.next();
?? ??? ??? ?if(s.getMath()=60 s.getEglish()=60 s.getHistory()=60)
?? ??? ??? ? System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ??? ?}
?? ??? ?System.out.println("三门全部不通过的同学为:");
?? ??? ?IteratorStudentit2=list.iterator();
?? ??? ?while(it2.hasNext()){
?? ??? ??? ?Student s=it2.next();
?? ??? ??? ?if(s.getMath()60 s.getEglish()60 s.getHistory()60)
?? ??? ??? ? System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ??? ?}
?? ??? ?System.out.println();
?? ??? ?System.out.println("三门至少有一门通过的同学为:");
?? ??? ?IteratorStudentit3=list.iterator();
?? ??? ?while(it3.hasNext()){
?? ??? ??? ?Student s=it3.next();
?? ??? ??? ?if(s.getMath()=60 || s.getEglish()60 || s.getHistory()60)
?? ??? ??? ?? System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ??? ?}
?? ??? ?System.out.println();
?? ??? ?System.out.println("数学不通过的同学为:");
?? ??? ?IteratorStudentit4=list.iterator();
?? ??? ?while(it4.hasNext()){
?? ??? ??? ?Student s=it4.next();
?? ??? ??? ?if(s.getMath()60)
?? ??? ??? ?? System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ??? ??? ?}
?? ??? ?
?? ??? ??? ?}
?? ??? ?}
其实这样get、set不如直接定义一个构造函数方便。
还有下面这个源代码可能不符合要求,没有键盘输入,但是有分别用for和迭代器,更好理解。
import java.util.*;
public class Test {
public static class Student{
?? ?int id;
?? ?String name;
?? ?int math;
?? ?int english;
?? ?int history;
?? ?
?? ?public void setId(int id){
?? ??? ?this.id=id;
?? ?}
?? ?public int getId(){
?? ??? ?return id;
?? ?}
?? ?public void setName(String name){
?? ??? ?this.name=name;
?? ?}
?? ?public String getName(){
?? ??? ?return name;
?? ?}
?? ?public void setMath(int math){
?? ??? ?this.math=math;
?? ?}
?? ?public int getMath(){
?? ??? ?return math;
?? ?}
?? ?public void setEnglish(int english){
?? ??? ?this.english=english;
?? ?}
?? ?public int getEglish(){
?? ??? ?return english;
?? ?}
?? ?public void setHistory(int history){
?? ??? ?this.history=history;
?? ?}
?? ?public int getHistory(){
?? ??? ?return history;
?? ?}
}
?? ?public static void main(String[] args) {
?? ??? ?Student s1=new Student();
?? ??? ?s1.setId(11);
?? ??? ?s1.setName("dr");
?? ??? ?s1.setMath(100);
?? ??? ?s1.setEnglish(90);
?? ??? ?s1.setHistory(80);
?? ??? ?Student s2=new Student();
?? ??? ?s2.setId(12);
?? ??? ?s2.setName("spe");
?? ??? ?s2.setMath(100);
?? ??? ?s2.setEnglish(50);
?? ??? ?s2.setHistory(80);
?? ??? ?Student s3=new Student();
?? ??? ?s3.setId(13);
?? ??? ?s3.setName("wr");
?? ??? ?s3.setMath(50);
?? ??? ?s3.setEnglish(100);
?? ??? ?s3.setHistory(80);
?? ??? ?Student s4=new Student();
?? ??? ?s4.setId(14);
?? ??? ?s4.setName("axe");
?? ??? ?s4.setMath(50);
?? ??? ?s4.setEnglish(50);
?? ??? ?s4.setHistory(50);
?? ??? ?ListStudentlist=new ArrayList();
?? ??? ?list.add(s1);
?? ??? ?list.add(s2);
?? ??? ?list.add(s3);
?? ??? ?list.add(s4);
?? ??? ?System.out.println("使用迭代器:");
?? ??? ?System.out.println("所有学生成绩:");
?? ??? ?IteratorStudentit=list.iterator();
?? ??? ?
?? ?while(it.hasNext()){
?? ??? ?Student s=it.next();
?? ??? ?System.out.print("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ??? ?System.out.println();
?? ?}
?? ?System.out.println();
?? ?System.out.println("三门全部通过的同学为:");
?? ?IteratorStudentit1=list.iterator();
?? ?while(it1.hasNext()){
?? ??? ?Student s=it1.next();
?? ??? ?if(s.getMath()=60 s.getEglish()=60 s.getHistory()=60)
?? ??? ??? ?System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ?}
?? ?System.out.println("三门全部不通过的同学为:");
?? ?IteratorStudentit2=list.iterator();
?? ?while(it2.hasNext()){
?? ??? ?Student s=it2.next();
?? ??? ?if(s.getMath()60 s.getEglish()60 s.getHistory()60)
?? ??? ??? ?System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ?}
?? ?System.out.println();
?? ?System.out.println("三门至少有一门通过的同学为:");
?? ?IteratorStudentit3=list.iterator();
?? ?while(it3.hasNext()){
?? ??? ?Student s=it3.next();
?? ??? ?if(s.getMath()=60 || s.getEglish()60 || s.getHistory()60)
?? ??? ??? ?System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ?}
?? ?System.out.println();
?? ?System.out.println("数学不通过的同学为:");
?? ?IteratorStudentit4=list.iterator();
?? ?while(it4.hasNext()){
?? ??? ?Student s=it4.next();
?? ??? ?if(s.getMath()60)
?? ??? ??? ?System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
?? ?}
?? ?System.out.println();
?? ?System.out.println("使用for语句");
?? ?System.out.println("所有学生成绩:");
?? ?
?? ??? ?for(int i=0;ilist.size();i++)
?? ??? ??? ?System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
?? ??? ?System.out.println();
?? ??? ?System.out.println("三门全部通过的同学为:");
?? ??? ?for(int i=0;ilist.size();i++){
?? ??? ??? ?if(list.get(i).getMath()=60 list.get(i).getEglish()=60 list.get(i).getHistory()=60)
?? ??? ??? ??? ?System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
?? ??? ?}
?? ??? ?System.out.println();
?? ??? ?System.out.println("三门全部不通过的同学为:");
?? ??? ?for(int i=0;ilist.size();i++){
?? ??? ??? ?if(list.get(i).getMath()60 list.get(i).getEglish()60 list.get(i).getHistory()60)
?? ??? ??? ??? ?System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
?? ??? ?}
?? ??? ?System.out.println();
?? ??? ?System.out.println("三门至少有一门通过的同学为:");
?? ??? ?for(int i=0;ilist.size();++i){
?? ??? ??? ?if(list.get(i).getMath()60 || list.get(i).getEglish()60 || list.get(i).getHistory()60)
?? ??? ??? ??? ?System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
?? ??? ?}
?? ??? ?System.out.println();
?? ??? ?System.out.println("数学不通过的同学为:");
?? ??? ?for(int i=0;ilist.size();++i){
?? ??? ??? ?if(list.get(i).getMath()60)
?? ??? ??? ??? ?System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
?? ??? ?}
?? ??? ?System.out.println();
?? ?}
?? ?}
Java的题目有点急!
package?com.test;
public?class?Duck?extends?Animal?{
private?String?name;
public?String?getName()?{
return?name;
}
public?void?shout()?{
System.out.println("嘎嘎嘎……");
}
public?Duck(String?name)?{
super();
this.name?=?name;
}
}
package?com.test;
public?class?Cat?extends?Animal?{
private?String?name;
public?void?shout()?{
System.out.println("喵喵喵……");
}
public?Cat(String?name)?{
super();
this.name?=?name;
}
public?String?getName()?{
return?name;
}
}
package?com.test;
public?class?Dolphin?extends?Animal?{
private?String?name;
public?Dolphin(String?name)?{
super();
this.name?=?name;
}
public?void?shout()?{
System.out.println("海豚音……");
}
public?String?getName()?{
return?name;
}
}
package?com.test;
public?abstract?class?Animal?implements?Bark{
}
package?com.test;
public?interface?Bark?{
void?shout();
}
package?com.test;
public?class?Test?{
public?static?void?main(String[]?args)?{
System.out.println("动物名\t动物叫");
//?多态体现
Animal?duck?=?new?Duck("唐老鸭");
System.out.print(((Duck)?duck).getName()?+?"\t");
duck.shout();
Animal?cat?=?new?Cat("加菲猫");
System.out.print(((Cat)?cat).getName()?+?"\t");
cat.shout();
Animal?dolphin?=?new?Dolphin("海豚奇奇");
System.out.print(((Dolphin)?dolphin).getName()?+?"\t");
dolphin.shout();
System.out.println("\n动物名\t腿的条数\t动物叫");
Animal[]?animals?=?new?Animal[3];
animals[0]?=?duck;
animals[1]?=?cat;
animals[2]?=?dolphin;
for?(Animal?animal?:?animals)?{
if?(animal?instanceof?Duck)?{
System.out.print(((Duck)?animal).getName()?+?"\t2\t");
animal.shout();
}?else?if?(animal?instanceof?Cat)?{
System.out.print(((Cat)?animal).getName()?+?"\t4\t");
animal.shout();
}?else?{
System.out.print(((Dolphin)?animal).getName()?+?"\t4\t");
animal.shout();
}
}
}
}
简单写了下
java面向对象程序设计实验报告怎么写
1。People.java
public class People {
//性别(sex)及出生日期(date);方法成员有:获取人的性别和出生日期及构造方法。要求构造方法可以设置性别和出生日期的初始值。
private int sex;
private Date birth;
public People (int sex, Date birth) {
this.sex = sex;
this.birth = birth;
}
public int getSex() {
return this.sex;
}
public Date getBirth() {
return this.birth;
}
}
2。Student.java
public class Student extends People{
private int sex;
private Date birth;
private String name;
private int stuno;
private double grate;
private String studentNative;
public Student(int sex, Date birth, String name, int stuno, double grate, String studentNative) {
super(sex, birth);
this.name = name;
this.stuno = stuno;
this.grate = grate;
this.studentNative = studentNative;
}
public Date getBirth() {
return birth;
}
public double getGrate() {
return grate;
}
public String getName() {
return name;
}
public int getSex() {
return sex;
}
public String getStudentNative() {
return studentNative;
}
public int getStuno() {
return stuno;
}
}
3。测试类自己编写就好了,创建一个People和Student的对象,然后设置值,取值就可以了。
五。
1.构造方法没有返回值,方法名和类名一样.
2.继承是指子类可以拥有父类的方法和属性;多态是指父类的引用可以指向子类对象的实例
3.重写
4.重载