代碼如下:
//?App1.javapublic?class?App1?{
public?static?void?main(String[]?args)?{
String?str?=?"kjkjdJKSDJFK932849384KSDJFK&&*&2334989~~2343L4ks";
int?upperCnt?=?0,?lowerCnt?=?0,?otherCnt?=?0;
for?(int?i?=?0;?i?<?str.length();?i++)?{
char?ch?=?str.charAt(i);
if?(ch?>=?'A'?&&?ch?<=?'Z')?{
upperCnt++;
}?else?if?(ch?>=?'a'?&&?ch?<=?'z')?{
lowerCnt++;
}?else?{
otherCnt++;
}
}
System.out.println("大寫字母:"?+?upperCnt);
System.out.println("小寫字母:"?+?lowerCnt);
System.out.println("非字母字符:"?+?otherCnt);
}
}//?App2.java
import?java.util.Random;
public?class?App2?{
public?static?void?main(String[]?args)?{
int[]?numbers?=?new?int[10];
Random?random?=?new?Random(System.nanoTime());
for?(int?i?=?0;?i?<?numbers.length;?i++)?{
numbers[i]?=?random.nextInt(100)?+?1;
}
int?max,?min;
max?=?numbers[0];
min?=?numbers[0];
for?(int?i?=?1;?i?<?numbers.length;?i++)?{
if?(numbers[i]?>?max)
max?=?numbers[i];
if?(numbers[i]?<?min)
min?=?numbers[i];
}
System.out.println("最大值:"?+?max);
System.out.println("最小值:"?+?min);
}
}//?App3.java
public?class?App3?{
public?static?void?main(String[]?args)?{
int[]?data?=?new?int[5];
try?{
data[6]?=?100;
}?catch?(ArrayIndexOutOfBoundsException?e)?{
System.out.println("數組索引溢出。");
e.printStackTrace();
}
try?{
data[0]?=?Integer.parseInt("123abc");
}?catch?(NumberFormatException?e)?{
System.out.println("轉換失敗。");
e.printStackTrace();
}
}
}//?App4.java
abstract?class?Animal?{
private?String?name;
private?int?weight;
public?Animal(String?name,?int?weight)?{
this.name?=?name;
this.weight?=?weight;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getWeight()?{
return?weight;
}
public?void?setWeight(int?weight)?{
this.weight?=?weight;
}
public?void?run()?{
System.out.println("Animal?run.");
}
public?void?catchFeed()?{
System.out.println("Animal?catchFeed.");
}
}
class?Cat?extends?Animal?{
public?Cat(String?name,?int?weight)?{
super(name,?weight);
}
@Override
public?void?run()?{
System.out.println(getName()?+?"?run.");
}
@Override
public?void?catchFeed()?{
System.out.println(getName()?+?"?catchFeed.");
}
}
class?Dog?extends?Animal?{
public?Dog(String?name,?int?weight)?{
super(name,?weight);
}
@Override
public?void?run()?{
System.out.println(getName()?+?"?run.");
}
@Override
public?void?catchFeed()?{
System.out.println(getName()?+?"?catchFeed.");
}
}
public?class?App4?{
public?static?void?main(String[]?args)?{
Cat?cat?=?new?Cat("Tom",?10);
cat.run();
cat.catchFeed();
Dog?dog?=?new?Dog("Goff",?25);
dog.run();
dog.catchFeed();
}
}