1、使用循环把26个字母按字典顺序存入数组,在不使用另外数组的情况下将其逆序存放,在根据处理后的字符数组创建一个字符串并输出
public static void main(String[] args) { char a[]=new char[26],c; //中间变量c int i; for(i=0;i<a.length-1;i++){ a[i]=(char)('a'+i); } for(i=0;i<13;i++){ c=a[i]; a[i]=a[25-i]; a[25-i]=c; } String s=new String(a); System.out.println(s); } }
2、设计一个动物接口,并设计相应的动作,如跑,跳,走。在设计一个狗类实现这个动物接口,该狗类具有一些基本属性,如名称,大小,体重等。编写测试类测试是否达到预定功能。要求使用自定义的包。
package javaTest4; public class Dog implements Animal{ private String name; //名称 private int bodySize; //大小 private float weight; //体重 //构造函数 public Dog(String name,int bodySize,float weight) { super(); this.name=name; this.bodySize=bodySize; this.weight=weight; } public String getName(String name){ return this.name=name; } public void setName(String name){ this.name=name; } public int getBodySize(){ return bodySize; } public void setBodySize(int bodySize){ this.bodySize=bodySize; } public float getWeight(){ return weight; } public void setWeight(float weight){ this.weight=weight; } //实现接口必须实现接口的所有方法 public void jump() { System.out.println("jumping"); } @Override public void walk() { System.out.println("walking"); } @Override public void run() { System.out.println("running"); } public String toString (){ return ("I'am "+name+",my body size is "+bodySize+",and my weight is "+weight); } public static void main(String [] args){ Dog ani=new Dog ("tady",5,10.6f); System.out.println(ani.toString()); ani.run(); ani.jump(); ani.walk(); } }
3、定义两个文本框,一个文本框用于提示输入密码,另一个是密码框,以“*”代替输入的密码
public class test2 extends Applet{ TextField text1,text2; public void init(){ text1=new TextField("请输入密码:",10); text1.setEditable(false); text2=new TextField(10); text2.setEchoChar('*'); add(text1); add(text2); } }
4、设置列表选择模型为单选:list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION)
设置列表选择模型为多选:list.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
5、编写一个java applet应用小程序,画三个圆,颜色分别为红黄绿
public class test3 extends Applet { public void paint(Graphics g){ g.setColor(Color.red); g.drawRoundRect(100, 100, 100, 100, 100, 100); g.drawString("红色圆", 100, 100); g.setColor(Color.green); //g.drawRoundRect(x, y, width, height, arcWidth, arcHeight); //x,y表示坐标,对于画圆后面四个参数必须相等 g.drawRoundRect(50, 50, 50, 50, 50, 50); g.fillRoundRect(40, 50, 50, 50, 50, 50);//画圆填充色 g.drawString("绿色圆", 50, 50); } }