Chap 2
实践2-1
class Ex2_1{
public static void main(String[] args) {
float sum=0.0f,count=1.0f;
for(int i=1;i<=20;++i){
count=1.0f;
for(int j=1;j<=i;++j)
count*=j;
sum+=count;
}
System.out.println("1+2!+3!+...+20!的值为:"+sum);
}
}
实践2-2
public class Ex2_2{
public static void main(String[] args) {
System.out.println("** 菲波拉挈数列的前20个数为:**");
long f1 = 1, f2 = 1;
for (int i = 1; i <= 10; i ++) {
System.out.print(f1 + " " + f2 + " ");
if (i % 2 == 0) {
System.out.println();
}
f1 = f1 + f2;
f2 = f2 + f1;
}
}
}
Chap 3
实践3-1
public class Student
{ static String name="张三";
static int age;
public Student()
{
age=18;
}
public static void main(String args[])
{
System.out.println("姓名:"+name+",年龄:"+age);
}
}
实践3-2
class People{
String name;
People(String name){
this.name=name ; //对成员变量name 初始化
}
}
class Student extends People{
String stuID;
Student(String name,String stuID){
super(name) ; //对继承自父类的成员变量name初始化
this.stuID=stuID ; //对成员变量stuID 初始化
}
}
public class PeopleDemo{
public static void main(String arg[]) {
Student stu=new Student("张三","20080601") ; //定义对象
System.out.println(stu.name+" "+stu.stuID) ; //输出对象的姓名和学号
}
}
Chap 4
实践4-1
public class Ex4_1 {
String str;
public Ex4_1(String str) {
this.str = str;
}
int getlength(){
return(str.length());
}
public static void main(String[] args) {
Ex4_1 test = new Ex4_1("Hello");
System.out.println("字符串是:"+test.str+" 长度为:"+test.getlength());
}
}
实践4-2
import java.util.*;
import java.io.*;
public class Ex4_2{
public static void main(String[] args) {
String str1 = new String();
String str2 = new String();
char ch;
Scanner reader= new Scanner(System.in);
System.out.println("输入字符串:");
str1=reader.next(); //输入字符串
System.out.println("输入要删除的字符:");
str2=reader.next(); //输入穿要删除的字符 ,以字符串的形式输入
ch=str2.charAt(0); //将字符串转换为字符
str2=str1.replace(ch,' '); //用空格替代指定字符
System.out.println("删除字符后的字符串 "+str2);
}
}
Chap 5
实践5-1
public class Ex5_1{
public static void main(String[] args) {
try {
int num[] = new int [10];
System.out.println("num[10] is " + num[10]);
}
catch (Exception ex){
System.out.println("Exception");
}
catch (RuntimeException ex){
System.out.println("RuntimeException");
}catch (ArithmeticException ex){
System.out.println("ArithmeticException");
}
}
}
实践5-2
public class Ex5_2{
static double cal(double a, double b)
throws IllegalArgumentException
{ double value;
if ( b == 0 )
{ // 抛出IllegalArgumentException异常
throw new IllegalArgumentException("除数不能为0");
} else {
value = a/b;
if ( value < 0 )
{ // 抛出IllegalArgumentException异常
throw new IllegalArgumentException("运算结果小于0");
}
}
return value;
}
public static void main(String[] args)
{ double result;
try
{ double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
result = cal(a, b);
System.out.println("运算结果是: " + result);
}
catch( IllegalArgumentException e )
{ // 处理llegalArgumentException异常
System.out.println("异常说明: "+e.getMessage());
}
}
}
Chap 6
实践6-1
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
public class ex6_1 {
public static void main(String[] args){
JFrame frame=new JFrame();
JLabel label1=new JLabel("JAVA Programming");
Font font1=label1.getFont();
font1=new Font("Courier", font1.getStyle(), 20);
label1.setFont(font1);
frame.getContentPane().add(label1);
frame.pack();
frame.setVisible(true);
}
}
实践6-2
import javax.swing.*;
public class ex6_2 extends JFrame {
public ex6_2(){
super("第一个窗体");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String [] args){
new ex6_2();
}
}
Chap 7
实践7-1
import javax.swing.*;
import java.awt.*;
public class ex7_1 extends JFrame{
ex7_1(){
super("程序调试");
setLayout(new FlowLayout());
JButton jbtn1=new JButton("确定");
JButton jbtn2=new JButton("取消");
this.getContentPane().add(jbtn1);
this.getContentPane().add(jbtn2);
this.setVisible(true);
this.setSize(300,150);
}
public static void main( String args[]) {
new ex7_1 ();
}
}
实践7-2
import javax.swing.*;
import java.awt.*;
public class Ex7_2 extends JFrame{
Panel pan0,pan1,pan2,pan3,pan4,pan5,pan6,pan7;
Panel[] pan;
String[] numStr={"1","2 ABC","3 DEF","4 GHI","5 JKL","6 MNO","7 PQRS","8 TUV","9 WXYZ","*","0","#"};
Ex7_2(){
super("手机键盘界面");
pan=new Panel[8];
for(int j=0;j<8;j++){
pan[j]=new Panel();
}
ImageIcon icon=new ImageIcon("JM1.jpg");
JLabel jlab1=new JLabel(icon,SwingConstants.CENTER);
pan[5].add(jlab1);
pan[0].setLayout(new GridLayout(4,3));
for(int i=0;i