导航:首页 > 编程语言 > java面向对象程序设计实验

java面向对象程序设计实验

发布时间:2023-06-11 02:10:34

A. java 面向对象设计 运用GUI技术的开发 进行小游戏程序的开发

/*原始图片分为16块第一块为空白

*我是这样想的:

*因为无法将图片分成15份而放到16块的区域中(至少是很难实现)

*因此将原世散始图片中一部分处理成白色,这样就好处理了

*/

importjava.awt.Graphics;

importjava.awt.GridLayout;

importjava.awt.Image;

importjava.awt.MediaTracker;

importjava.awt.event.MouseEvent;

importjava.awt.event.MouseListener;

importjava.awt.image.BufferedImage;

importjava.util.HashSet;

importjava.util.Iterator;

importjava.util.Random;

importjava.util.Set;

importjavax.print.attribute.standard.Media;

importjavax.swing.AbstractButton;

importjavax.swing.Icon;

importjavax.swing.ImageIcon;

importjavax.swing.JButton;

importjavax.swing.JFrame;

{

intcurrx,curry,cx,cy;

Imageimg,whiteimg;

Iconimg1;

IconcurrIcon;

BufferedImagebimg,white;

BufferedImage[][]subimg;

intwidth,height;

JButton[][]jb;

publicPintu(){

this.setLayout(newGridLayout(4,4));

img=this.getToolkit().getImage("吵瞎E:\1.jpg");

whiteimg=this.getToolkit().getImage("E:\2.jpg");

MediaTrackerme=newMediaTracker(this);

me.addImage(img,0);

try{

me.waitForAll(0);

}catch(InterruptedExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

width=img.getWidth(this);

height=img.getHeight(this);

bimg=newBufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

Graphicsbg=bimg.getGraphics();

bg.drawImage(img,0,0,this);

subimg=newBufferedImage[4][4];

inta[]=newint[4];

for(inti=0;i<a.length;i++){

a[i]=(int)(Math.random()*4);

for(intk=0;k<i;k++){

if(a[i]==a[k]){

i--;

continue;

}

}

}

for(intx=0;x<4;x++){

for(inty=0;y<4;y++)

subimg[a[x]][a[y]]=bimg.getSubimage(width*x/4,height*y/4,width/4,height/4);

}

currx=a[0];curry=a[0];

white=subimg[currx][curry];

jb=newJButton[4][4];

for(inti=0;i<4;i++){

for(intj=0;j<4;j++){

jb[i][j]=newJButton();

jb[i][j].addMouseListener((MouseListener)升返空this);

this.add(jb[i][j]);

jb[i][j].setIcon(newImageIcon(subimg[i][j]));

}

}

setSize(img.getWidth(this),img.getHeight(this));

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

/*publicvoidpaint(Graphicsg){

g.drawImage(subimg[0],0,0,this);

}*/

publicstaticvoidmain(String[]args){

newPintu();

}

publicvoidmouseClicked(MouseEvente){

for(inti=0;i<4;i++)

for(intj=0;j<4;j++)

if(((AbstractButton)e.getSource())==jb[i][j]){cx=i;cy=j;}

if(((cx==currx)&&(((cy-curry)==1)||((curry-cy)==1)))||((cy==curry)&&(((cx-currx)==1)||((currx-cx)==1))))

{

System.out.println(""+cx+""+cy);

currIcon=jb[cx][cy].getIcon();

jb[cx][cy].setIcon(newImageIcon(white));

jb[currx][curry].setIcon(currIcon);

currx=cx;curry=cy;

}

}

@Override

publicvoidmouseEntered(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmouseExited(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmousePressed(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmouseReleased(MouseEvente){

//TODOAuto-generatedmethodstub

}

}

就是照着你说的那个改的。。。。效果图如下:

B. 使用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[10];
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[count]=new JButton(""+count);
buttons[count].addActionListener(this);
buttonPanel.add(buttons[count]);

}
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[count])
{
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[count])
{
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);
}
}

C. 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.重载

其他的没什么了,自己完成以下吧。O(∩_∩)O~

D. java程序实验十一 面向对象 实验目的: 数组及集合的使用及实现; 要求: 掌握数组及集合操作的

packagecom;

importjava.util.*;publicclassStudent{
privateStringname;
privateStringid;
privatedoublesumscore;
publicStudent(Stringname,Stringid,doublesumscore){
this.name=name;
this.id=id;
this.sumscore=sumscore;
}
publicStringtoString(){
return"name:"+getName()+"id:"+getId()+"sumscore:"+getSumscore();
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetId(){
returnid;
}

publicvoidsetId(Stringid){
this.id=id;
}

publicdoublegetSumscore(){
returnsumscore;
}

publicvoidsetSumscore(doublesumscore){
this.sumscore=sumscore;
}

publicstaticvoidmain(String[]args){
Student[]s={newStudent("A","1",12),newStudent("B","2",22),newStudent("C","3",32),
newStudent("D","4",142),newStudent("E","5",152),newStudent("F","6",262),newStudent("G","7",72),
newStudent("H","8",182),newStudent("I","9",92),newStudent("J","10",102)
};
s=bubbleSort(s);

Vector<Student>vector=newVector<Student>();
vector.addAll(Arrays.asList(s));
for(Iterator<Student>iter=vector.iterator();iter.hasNext();)
{
Studentstudent=iter.next();
System.out.println(student.toString());
}

List<Student>list=newArrayList<Student>();
list=Arrays.asList(s);
for(Studentstr:list){
System.out.println(str.toString());
}

Hashtable<Integer,Student>table=newHashtable<Integer,Student>();
for(inti=0;i<s.length;i++){
table.put(i,s[i]);
}
for(inti=0;i<s.length;i++){
System.out.println(table.get(i).toString());
}
}
publicstaticStudent[]bubbleSort(Student[]args){//冒泡排序算法
Studenttemp;
for(inti=0;i<args.length-1;i++){
for(intj=i+1;j<args.length;j++){
if(args[i].getSumscore()>args[j].getSumscore()){
temp=args[i];
args[i]=args[j];
args[j]=temp;
}
}
}
returnargs;
}

}
//楼主,说实话你的要求真多,而且分少~~~~

阅读全文

与java面向对象程序设计实验相关的资料

热点内容
手机主页设文件夹 浏览:956
安卓投屏极米用什么 浏览:415
老程序员总结的16条经验教训 浏览:194
linux修改服务器时间 浏览:446
检查文件夹新增文件 浏览:287
代理服务器和脚本地址 浏览:529
如何让宽带一直连接网络连接服务器 浏览:718
编译阶段形成逻辑地址 浏览:328
设置中应用加密是哪个 浏览:682
php当前函数名 浏览:161
程序员睡眠不好心跳快 浏览:232
python怎么将电脑作为服务器 浏览:779
腰椎压缩性骨折算几级伤残 浏览:302
传统资产配置策略加密货币市场 浏览:990
id加密门禁卡可以复制到手机吗 浏览:674
路由器如何控制某个app 浏览:45
C51编译器在标准C的基础上 浏览:262
银行卡掉了可以办车贷解压吗 浏览:317
没解压可以贷款吗 浏览:519
最小pdf阅读器 浏览:810