java编程代码大全,急简单JAVA编程代码

1,急简单JAVA编程代码public class Testpublic static void main(String[] args)MyRectangle rec = new MyRectangle(3, 5);MyRectangle square = new MySquare(4);System.out.println(rec.toString()); System.out.println(square.toString()); }}class MyRectangle protected double width; protected double length;public MyRectangle(double length, double width) this.width = width; this.length = length; } public double getLength()return length; } public double getWidth()return width; } public void setWidth(double width)this.width = width; }public double getArea() return this.width * this.length; }public String toString() return "长方形的长为:" + length + ", 宽: " + width + ", 面积为:" + getArea(); }}class MySquare extends MyRectangle public MySquare(double length) super(length, length); }public double getArea() return Math.pow(super.width, 2); } public String toString() return "正方形边长为: " + super.length + ", 面积为: " + getArea(); }}----------测试长方形的长为:3.0, 宽: 5.0, 面积为:15.0正方形边长为: 4.0, 面积为: 16.0
2,简单的Java代码System.out.println(Object obj);括号里的对像在打印时自动调用对应的.toString()方法list.toString()是输出什么啊而list[i]是直接指向list数组中的第i个值你这个可以不用转换成数组的啊ArrayList 可以直接用循环输出的你的那个例子就是q.get(i);一起分析一下源代码吧:public static void main(String args[]) ArrayList q = new ArrayList();q.add(new NewClass1(2, 2));q.add(new NewClass1(3, 3));q.add(new NewClass1(4, 4));//定义一个数组,将(2,2),(3,3),(4,4)三个二维数组赋值给数组q;Object[] list = q.toArray();//list得到q的返回值,即(2,2),(3,3),(4,4);for (int i = 0; i <list.length; i++) //循环:数组下标从零开始,所以i=0;//读取list数组的长度,list.length=3,//i<3,即循环是0,1,2,计三次.System.out.println(list[i]);//将list[0],list[1],list[2],依次输出并换行}问题就在最后一句,最后的list[i] 把[i]去掉,依然会有三次循环,但是list只是一个被定义的数组对象,而不是具体的数据,所以每次打印的都是空值;i是个循环变量 , 代表着被循环数组的下标 。如果没有i,永远是空 。怎么能出结果啊你把[I]去掉的话就变成直接循环输出list.length个的对象了 。。。而且这个对象的类型是“数组” 。。。这里要注意下:Java语言中 。。。数组也是对象类型的 。。。肯定打印不出来啊 。。。list是一个数组 。。。在打印时是自动调用对应的 toString方法 。list 只是一个数组的指针?。。∷皇鞘凳翟谠诘氖荩?
3,java线程的经典代码package threadgroup;class ThreadDemo3 extends Threadprivate String name; private int delay; public ThreadDemo3(String sname, int i_delay)name = sname; delay = i_delay; } public void run()trysleep(delay); } catch (InterruptedException e)} System.out.println("多线程测试!\n" + name + "\n" + delay); } } public class testMyThreadpublic static void main(String[] args)ThreadDemo3 th1,th2,th3; th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900)); th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900)); th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900)); th1.start(); th2.start(); th3.start(); } } package threadgroup;public class threadDemopublic static void main(String[] args)Thread t = Thread.currentThread(); t.setName("你好吗?"); System.out.println("正在进行的Thread是:" + t); tryfor (int i = 0; i &lt; 5; i++)System.out.println("我不叫穆继超" + i); Thread.sleep(3000); } } catch (Exception e)// TODO: handle exception System.out.println("Thread has wrong" + e.getMessage()); } }}package threadgroup;public class threadDemo2 implements Runnablepublic threadDemo2()Thread t1 = Thread.currentThread(); t1.setName("第一个主进程"); System.out.println("正在运行" + t1); Thread t2 = new Thread(this, ""); System.out.println("在创建一个进程"); t2.start(); trySystem.out.println("使他进入第一个睡眠状态"); Thread.sleep(2000); } catch (InterruptedException e)System.out.println("Thread has wrong" + e.getMessage()); } System.out.println("退出第一个进程"); } public void run()tryfor (int i = 0; i &lt; 5; i++)System.out.println("进程" + i); Thread.sleep(3000); } } catch (InterruptedException e)// TODO: handle exception System.out.println("Thread has wrong" + e.getMessage()); } System.out.println("退出第二个进程"); } public static void main(String[] args)new threadDemo2(); }}【java编程代码大全,急简单JAVA编程代码】
4,谁能帮忙补充完整下列JAVA程序代码class A//声明一个float型实例变量a//声明一个float型类变量b,即static变量bprivate float a;private static float b;void setA(float a)//将参数a的值赋值给成员变量a this.a = a;}void setB(float b)//将参数b的值赋值给成员变量bthis.b = b;}float getA()return a;}float getB()return b;}void inputA()System.out.println(a);}static void inputB()System.out.println(b);}}public class Examplepublic static void main(String args[])//通过类名操作类变量b,并赋值100//通过类名调用方法inputB()A.b = 100;A.inoutB();A cat=new A();A dog=new A();//cat调用方法setA(int a)将cat的成员 a的值设置为200cat.setA(200);// cat调用方法setB(int B)将cat的成员 b的值设置400cat.setB(400);//dog调用方法setA(int a)将dog的成员 a的值设置为150 dog.setA(150);//dog调用方法setB(int b)将dog的成员 b的值设置为300dog.setB(300);//cat调用inputA()//cat调用inputB()//dog调用inputA()//dog调用inputB()cat.inputA();cat.inputB();dog.inputA();dog.inputB();}}import java.awt.graphics;import java.awt.point;import java.awt.event.mouseadapter;import java.awt.event.mouseevent;import java.awt.event.mousemotionadapter;import java.util.vector;import javax.swing.jframe;public class paint extends jframevector v = new vector(); vector vec = new vector(); public void paint(graphics g)int[] x, y;for (int j = 0; j < vec.size(); j++)x = new int[((vector) vec.get(j)).size()];y = new int[((vector) vec.get(j)).size()];for (int i = 0; i < ((vector) vec.get(j)).size(); ++i)point p = (point) ((vector) vec.get(j)).get(i);x[i] = p.x;y[i] = p.y;}g.drawpolyline(x, y, ((vector) vec.get(j)).size());} } public void init()vec.add(v);addmousemotionlistener(new mousemotionadapter()public void mousedragged(mouseevent e1)point p = e1.getpoint();v.add(p);repaint();}});addmouselistener(new mouseadapter()public void mousereleased(mouseevent e2)v = new vector();vec.add(v);}}); } public static void main(string[] args)paint pa = new paint();pa.setsize(500,500);pa.setvisible(true);pa.init(); }}class A float a;//声明一个float型实例变量astatic float b;//声明一个float型类变量b,即static变量bvoid setA(float a)this.a = a;//将参数a的值赋值给成员变量a}void setB(float b)this.b = b;//将参数b的值赋值给成员变量b}float getA()return a;}float getB()return b;}void inputA()System.out.println(a);}static void inputB()System.out.println(b);}}public class Examplepublic static void main(String args[])A.b=100;//通过类名操作类变量b,并赋值100A.inputB();//通过类名调用方法inputB()A cat=new A();A dog=new A();cat.setA(200);//cat象调用方法setA(int a)将cat的成员a的值设置为200cat.setB(400);//cat调用方法setB(int b)将cat的成员b的值设置为400dog.setA(150);//dog象调用方法setA(int a)将dog的成员a的值设置为150dog.setB(300);//dog调用方法setB(int b)将dog的成员b的值设置为300cat.inputA();//cat调用inputA() 。cat.inputB();//cat调用inputB() 。dog.inputA();//dog调用inputA() 。dog.inputB();//dog调用inputB() 。} }5,JAVA 设计代码/* * 主类用与建立3个取款行为线程 */public class getMoney public static void main(String args[])card c=new card(100);new Thread(c,"ID-1").start();new Thread(c,"ID-2").start();new Thread(c,"ID-3").start();System.out.println("3 ID using\t\t\t\t\t\t title 100$"); }}/* * 实现RUNNABLE的银行卡类 */class card implements Runnable private static int balance; public card(int x)this.balance=x; } public int view()return balance; } public void get(int x)this.balance=this.balance-x; } /** 同步方法块*/ public synchronized void run()int num=0;while(this.view()>0)System.out.println("操作时间:"+num);this.get(10);System.out.println(Thread.currentThread().getName()+"get 10$\t\t\t\t\t\t此时余额为"+this.view());num++;}if(this.view()<=0)System.out.println(Thread.currentThread().getName()+"取光了剩下所有的钱");} }}这个是银行取款的和受票类似,用了同步方法的方式/* * 主类用语建立3个售票窗口线程 */public class MyRun public static void main(String args[])My t=new My();new Thread(t,"t1").start();new Thread(t,"t2").start();new Thread(t,"t3").start();System.out.println("开始售票.................."); }}/* *实现RUNNABLE的售票窗口类 */class My implements Runnable static int sum=20; public void run()synchronized("sldf")//同步语句块的方式while(sum>0)System.out.println("票号("+sum+")被卖掉了---------------------------------------售票窗口:"+Thread.currentThread().getName());sum--;if(sum<=0)System.out.println("售票窗口:"+Thread.currentThread().getName()+"的票卖完了!");}}} }}用了同步语句块的方式 , 你要4个窗口在主类多NEW一个线程就可以了//五子棋java源代码import java.applet.*;import java.awt.*;import java.awt.event.*;import java.applet.applet;import java.awt.color;public class wuziqi extends applet implements actionlistener,mouselistener,mousemotionlistener,itemlistenerint color_qizi=0;//旗子的颜色标识 0:白子 1:黑子int intgame_start=0;//游戏开始标志 0未开始 1游戏中int intgame_body[][]=new int[16][16]; //设置棋盘棋子状态 0 无子 1 白子 2 黑子button b1=new button("游戏开始");button b2=new button("重置游戏");label lblwin=new label(" ");checkbox ckbhb[]=new checkbox[2];checkboxgroup ckghb=new checkboxgroup();public void init()setlayout(null);addmouselistener(this);add(b1);b1.setbounds(330,50,80,30);b1.addactionlistener(this);add(b2);b2.setbounds(330,90,80,30);b2.addactionlistener(this);ckbhb[0]=new checkbox("白子先",ckghb,false);ckbhb[0].setbounds(320,20,60,30);ckbhb[1]=new checkbox("黑子先",ckghb,false);ckbhb[1].setbounds(380,20,60,30);add(ckbhb[0]);add(ckbhb[1]);ckbhb[0].additemlistener(this);ckbhb[1].additemlistener(this);add(lblwin);lblwin.setbounds(330,130,80,30);game_start_csh();}public void itemstatechanged(itemevent e)if (ckbhb[0].getstate()) //选择黑子先还是白子先color_qizi=0;}elsecolor_qizi=1;}}public void actionperformed(actionevent e)graphics g=getgraphics();if (e.getsource()==b1)game_start();}elsegame_re();}}public void mousepressed(mouseevent e)public void mouseclicked(mouseevent e)graphics g=getgraphics();int x1,y1;x1=e.getx();y1=e.gety();if (e.getx()<20 || e.getx()>300 || e.gety()<20 || e.gety()>300)return;}if (x1%20>10)x1+=20;}if(y1%20>10)y1+=20;}x1=x1/20*20;y1=y1/20*20;set_qizi(x1,y1);}public void mouseentered(mouseevent e)public void mouseexited(mouseevent e)public void mousereleased(mouseevent e)public void mousedragged(mouseevent e)public void mousemoved(mouseevent e)public void paint(graphics g)draw_qipan(g);}public void set_qizi(int x,int y) //落子if (intgame_start==0) //判断游戏未开始return;}if (intgame_body[x/20][y/20]!=0)return;}graphics g=getgraphics();if (color_qizi==1)//判断黑子还是白子g.setcolor(color.black);color_qizi=0;}elseg.setcolor(color.white);color_qizi=1;}g.filloval(x-10,y-10,20,20);intgame_body[x/20][y/20]=color_qizi+1;if (game_win_1(x/20,y/20)) //判断输赢lblwin.settext(get_qizi_color(color_qizi)+"赢了!");intgame_start=0;}if (game_win_2(x/20,y/20)) //判断输赢lblwin.settext(get_qizi_color(color_qizi)+"赢了!");intgame_start=0;}if (game_win_3(x/20,y/20)) //判断输赢lblwin.settext(get_qizi_color(color_qizi)+"赢了!");intgame_start=0;}if (game_win_4(x/20,y/20)) //判断输赢lblwin.settext(get_qizi_color(color_qizi)+"赢了!");intgame_start=0;}}public string get_qizi_color(int x)if (x==0)return "黑子";}elsereturn "白子";}}public void draw_qipan(graphics g) //画棋盘 15*15g.setcolor(color.lightgray);g.fill3drect(10,10,300,300,true);g.setcolor(color.black);for(int i=1;i<16;i++)g.drawline(20,20*i,300,20*i);g.drawline(20*i,20,20*i,300);}}public void game_start() //游戏开始intgame_start=1;game_btn_enable(false);b2.setenabled(true);}public void game_start_csh() //游戏开始初始化br /> intgame_start=0;game_btn_enable(true);b2.setenabled(false);ckbhb[0].setstate(true);for (int i=0;i<16 ;i++ )for (int j=0;j<16 ;j++ )intgame_body[i][j]=0;}}lblwin.settext("");}public void game_re() //游戏重新开始repaint();game_start_csh();}public void game_btn_enable(boolean e) //设置组件状态b1.setenabled(e);b2.setenabled(e);ckbhb[0].setenabled(e);ckbhb[1].setenabled(e);}public boolean game_win_1(int x,int y) //判断输赢 横int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ )if (x1>15)break;}if (intgame_body[x1+i][y1]==intgame_body[x][y])t+=1;}elsebreak;}}for (int i=1;i<5 ;i++ )if (x1<1)break;}if(intgame_body[x1-i][y1]==intgame_body[x][y])t+=1;}elsebreak;}}if (t>4)return true;}elsereturn false;}}public boolean game_win_2(int x,int y) //判断输赢 竖int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ )if (x1>15)break;}if (intgame_body[x1][y1+i]==intgame_body[x][y])t+=1;}elsebreak;}}for (int i=1;i<5 ;i++ )if (x1<1)break;}if(intgame_body[x1][y1-i]==intgame_body[x][y])t+=1;}elsebreak;}}if (t>4)return true;}elsereturn false;}}public boolean game_win_3(int x,int y) //判断输赢 左斜int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ )if (x1>15)break;}if (intgame_body[x1+i][y1-i]==intgame_body[x][y])t+=1;}elsebreak;}}for (int i=1;i<5 ;i++ )if (x1<1)break;}if(intgame_body[x1-i][y1+i]==intgame_body[x][y])t+=1;}elsebreak;}}if (t>4)return true;}elsereturn false;}}public boolean game_win_4(int x,int y) //判断输赢 左斜int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ )if (x1>15)break;}if (intgame_body[x1+i][y1+i]==intgame_body[x][y])t+=1;}elsebreak;}}for (int i=1;i<5 ;i++ )if (x1<1)break;}if(intgame_body[x1-i][y1-i]==intgame_body[x][y])t+=1;}elsebreak;}}if (t>4)return true;}elsereturn false;}}}

    推荐阅读