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;
for(int i=0;i《renshu;i++){
stu=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.setId(id);
stu.setName(name);
stu.setMath(math);
stu.setEnglish(english);
stu.setHistory(history);
}
List《Student》list=new ArrayList《》();
list=Arrays.asList(stu);
Iterator《Student》it=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("三门全部通过的同学为:");
Iterator《Student》it1=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("三门全部不通过的同学为:");
Iterator《Student》it2=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("三门至少有一门通过的同学为:");
Iterator《Student》it3=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("数学不通过的同学为:");
Iterator《Student》it4=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);
List《Student》list=new ArrayList《》();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
System.out.println("使用迭代器:");
System.out.println("所有学生成绩:");
Iterator《Student》it=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("三门全部通过的同学为:");
Iterator《Student》it1=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("三门全部不通过的同学为:");
Iterator《Student》it2=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("三门至少有一门通过的同学为:");
Iterator《Student》it3=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("数学不通过的同学为:");
Iterator《Student》it4=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;i《list.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;i《list.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;i《list.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;i《list.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;i《list.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语言进行面向对象设计:ATM柜员机模拟程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AtmSystem extends JFrame implements ActionListener
{
private JPanel buttonPanel=new JPanel();
private JPanel area1Panel=new JPanel();
private JPanel area2Panel=new JPanel();
private JTextField note=new JTextField(33);
private JButton buttons;
private JButton dot = new JButton(".");
private JButton doublezero = new JButton("00");
private JButton ok = new JButton("确定");
private JButton exit = new JButton("退出");
private JButton cancel = new JButton("取消");
private JButton change=new JButton("更正");
private JButton getmoney = new JButton("取款");
private JButton search = new JButton("查询余额");
private JButton changekey = new JButton("修改密码");
private JButton putmoney= new JButton("存款");;
private boolean flag0, flag1, flag2,flag3,flag4;
int status=0;
int k,s,k1;
String str1="\t欢迎使用ATM柜员机!";
String str2="\t请输入用户名:";
String str3="\t请输入密码:";
String str4="\t请输入取款金额:";
String str5="\t请输入存款金额:";
String str6="\t请输入新密码:";
String str7="\t您的余额为:";
String str8="\t用户名错误,请重新输入:";
String str9="\t密码错误,请重新输入:";
String str10="\t余额不足,请重新输入:";
String str11="\t密码修改成功!";
String store1="",store2="";
double moneysum=10000;
double getMoney;
String username="123456";
String password="123456";
public AtmSystem()
{
super("ATM柜员机");
Container container=getContentPane();
buttons=new JButton;
dot.addActionListener(this);
doublezero.addActionListener(this);
change.addActionListener(this);
cancel.addActionListener(this);
ok.addActionListener(this);
exit.addActionListener(this);
changekey.addActionListener(this);
putmoney.addActionListener(this);
getmoney.addActionListener(this);
search.addActionListener(this);
area2Panel.add(changekey);
area2Panel.add(putmoney);
area2Panel.add(getmoney);
area2Panel.add(search);
area2Panel.setLayout(new GridLayout(1,3));
container.add(area2Panel,BorderLayout.NORTH);
note.setText(str1);
note.setEditable(false);
area1Panel.add(note);
buttonPanel.setLayout(new GridLayout(4,4));
for(int count=0;count《buttons.length;count++)
{
buttons=new JButton(""+count);
buttons.addActionListener(this);
buttonPanel.add(buttons);
}
buttonPanel.add(doublezero);
buttonPanel.add(dot);
buttonPanel.add(change);
buttonPanel.add(cancel);
buttonPanel.add(ok);
buttonPanel.add(exit);
container.add(buttonPanel,BorderLayout.SOUTH);
container.add(area1Panel);
area1Panel.setVisible(true);
area2Panel.setVisible(false);
setSize(400,250);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
JButton operation = (JButton) event.getSource();
if(operation==ok)
{
if(status==0)
{
note.setText(""+str2);
status=1;
}
else if(flag4==true)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag4=false;
}
else if(status==1&&k==1)
{
note.setText(""+str3);
status=2;
k=0;
}
else if(status==1&&k==0)
{
note.setText(""+str8);
store1="";
}
else if(status==2&&k1==1)
{
k1=0;
area1Panel.setVisible(false);
area2Panel.setVisible(true);
status=3;
}
else if(status==2&&k1==0)
{
note.setText(""+str9);
store2="";
}
else if(status==3)
{
if(flag0==true)
{
getMoney=Double.valueOf(store1).doubleValue();
if(getMoney《moneysum)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag0=false;
moneysum=moneysum-getMoney;
}
else
{
area2Panel.setVisible(false);
area1Panel.setVisible(true);
note.setText(""+str10);
store1="";
}
}
else if(flag1==true)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag1=false;
}
else if(flag2==true)
flag2=false;
else if(flag3=true)
{
flag4=true;
note.setText(""+str11);
password=store2;
flag3=false;
}
}
}
else if(operation==cancel)
{
if(status==1)
{
note.setText(""+str1);
status=0;
store1="";
store2="";
}
else if(status==2)
{
note.setText(""+str2);
status=1;
store1="";
store2="";
}
else if(status==3&&flag2)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag2=false;
store1="";
store2="";
}
else if(status==3&&flag0)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag0=false;
store1="";
store2="";
}
else if(status==3&&flag3)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag3=false;
store1="";
store2="";
}
}
else if(operation==change)
{
if(status==1)
{
note.setText(""+str2);
store1="";
store2="";
}
else if(status==2)
{
note.setText(""+str3);
store1="";
store2="";
}
else if(status==3&&flag2)
{
note.setText(""+str5);
store1="";
store2="";
}
else if(status==3&&flag0)
{
note.setText(""+str4);
store1="";
store2="";
}
else if(status==3&&flag3)
{
note.setText(""+str6);
store1="";
store2="";
}
}
else if(operation==doublezero&&status==3&&flag0==true)
{
note.setText(note.getText()+"00");
}
else if(operation==dot&&status==3&&flag0==true)
{
note.setText(note.getText()+".");
}
else if(operation==getmoney)
{
note.setText(""+str4);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
store1="";
flag0=true;
}
else if(operation==search)
{
note.setText(""+str7+moneysum);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
flag1=true;
}
else if(operation==putmoney)
{
note.setText(""+str5);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
flag2=true;
}
else if(operation==changekey)
{
note.setText(""+str6);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
store2="";
flag3=true;
}
else if(operation==exit)
{
note.setText(""+str1);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
status=0;
flag0=flag1=flag2=flag3=flag4=false;
store1="";
store2="";
k=k1=s=0;
}
else if(status==1||(status==3&&flag2==true)||(status==3&&flag0==true))
{
for(int count=0;count《buttons.length;count++)
if (operation== buttons)
{
note.setText(note.getText() + count);
store1+=event.getActionCommand();
}
user(store1);
}
else if(status==2||(status==3&&flag3==true))
{
for(int count=0;count《buttons.length;count++)
if (operation== buttons)
{
note.setText(note.getText()+ "*");
store2+=event.getActionCommand();
}
key(store2);
}
}
public void user(String load)
{
if(username.equals(load))
{
k=1;
s=1;
}
}
public void key(String load)
{
if(password.equals(store2))
{
k1=1;
}
}
public static void main(String args)
{
AtmSystem application=new AtmSystem();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
java面向对象程序设计
/*
对象的多态性:动物 x = new 猫();
函数的多态性:函数重载、重写
1、多态的体现
父类的引用指向了自己的子类对象
父类的引用也可以接收自己的对象
2、多态的前提
必须是类与类之间只有关系,要么继承或实现
通常还有一个前提,存在覆盖
3、多态的好处
多态的出现大大的提高了程序的扩展性
4、多态的弊端
只能使用父类的引用访问父类的成员
5、多态的应用
6、注意事项
*/
/*
需求:
猫,狗。
*/
abstract class Animal
{
abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("吃鱼");
}
public void catchMouse()
{
System.out.println("抓老鼠");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("吃骨头");
}
public void kanJia()
{
System.out.println("看家");
}
}
class DuoTaiDemo
{
public static void main(String args)
{
function(new Cat());
function(new Dog());
Animal a = new Cat();//向上转型
a.eat();
Cat c = (Cat)a;//向下转型
c.catchMouse();
}
public static void function(Animal a)
{
a.eat();
//用于子类型有限
//或判断所属类型进而使用其特有方法
if(a instanceof Cat)
{
Cat c = (Cat)a;
c.catchMouse();
}
else if(a instanceof Dog)
{
Dog c = (Dog)a;
c.kanJia();
}
}
}

更多文章:
delete where(mysql delete中where后能套用select么)
2026年7月11日 03:15
php+mysql动态网站开发黑马程序员(PHP+MySQL能做什么)
2025年8月12日 02:15
眼镜王蛇去毒腺(大理工地发现巨型眼镜王蛇,关于这种蛇你知道多少呢)
2026年8月17日 03:45
静态页面免费下载(怎么样能把网站上的动态网页保存下来完全成为静态网页)
2026年2月24日 05:45
大疆mini2开启fcc教程(请问大疆mini遥控器能充电吗)
2026年6月7日 22:45
mocha javascript(javascript用什么开发工具)
2025年7月17日 04:30
为什么安装了silverlight用不了(为什么下载安装完成silverlight双击点开还是打不开,只会跳出这个页面)
2025年5月24日 20:45
directionsforuse什么意思中文(英语考试directions是什么)
2025年12月10日 02:45
century love 世纪情歌(张学有 有咩好听ge 歌)
2026年1月4日 21:15
linux常用命令chmod的使用(Linux chmod命令及权限的理解)
2026年1月21日 17:15












