導航:首頁 > 編程語言 > 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面向對象程序設計實驗相關的資料

熱點內容
python集成包 瀏覽:303
如何用電腦解壓縮文件 瀏覽:446
pubg用什麼伺服器 瀏覽:526
田漢pdf 瀏覽:661
記錄儀如何安裝安卓系統 瀏覽:594
python求灰度均值 瀏覽:756
c編譯器是系統軟體嗎 瀏覽:694
獲取伺服器內網地址 瀏覽:536
新手媽媽如何帶新生兒APP 瀏覽:157
java日程管理 瀏覽:376
高清視頻鏈接加密 瀏覽:407
新買的阿里雲伺服器怎麼配置 瀏覽:612
在線編譯器為什麼刷新還在 瀏覽:213
雲伺服器系統盤可以裝資料庫 瀏覽:908
php繪制圖形 瀏覽:589
支付伺服器異常怎麼辦 瀏覽:77
java撥號 瀏覽:868
er5200如何設置虛擬伺服器 瀏覽:573
網路中心伺服器叫什麼 瀏覽:460
isplay單片機下載器 瀏覽:482