2-2
public class ex2_2
{ public static void main (String[] args)
{System.out.println(“Welcome to Java World!”);
}
}
2-3
import java.awt.Applet;
import java.awt.Graphics;
public class ex2_3 extends Applet
{public void paint(Graphics g)
{g.drawString(“Welcome to Java Applet World”,10,20);
}
}
2-4
<html>
<body>
<applet code=“ex2_3.class” height=200 width=300>
</applet>
</body>
</html>
3-8
(1)2 (2)3 (3)false (4)8
3-9
(一)
import java.io.*;
public class ex3_9
{ public static void main (String[] args)
{ float x=0f,y=0f;int z=0;
String s;
try
{System.out.print("请输入一个浮点数:");
BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
s=a.readLine();
x=Float.parseFloat(s);
z=(long)x;
y=x-z;
}catch(IOException e){}
System.out.println("整数部分是:"+z);
System.out.println("小数部分是:"+y);
}
}
(二)
import java.io.*;
public class ex3_9
{
public static void main(String args[])
{String s;
double d;
int i;boolean b=false;
do{
try{
System.out.println("请输入一个浮点数:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine();
i=s.indexOf('.');
//System.out.println("position of . is:"+i);
d=Double.parseDouble(s);
System.out.println(d+"整数部分为:"+(long)d);
if(i==-1)
System.out.println(d+"小数部分为:"+0.0);
else
System.out.println(d+"小数部分为:"
+Double.parseDouble(((s.charAt(0)=='-')?"-":"")+"0."+s.substring(i+1,s.length())));
System.out.println(s.length());
b=false;
}
catch(NumberFormatException nfe)
{System.out.println("输入浮点数格式有误!");
b=true;
}
catch(IOException ioe)
{b=false;}
}while(b);
}}
3-10:
import java.io.*;
public class ex3_10
{ public static void main (String[] args)
{ int i,j, max=0,min=0;
String s;
try
{System.out.print("请输入十个整型数:");
BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
s=a.readLine();
i=Integer.parseInt(s);
max=min=i;
for(j=0;j<9;j++)
{BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
s=b.readLine();
i=Integer.parseInt(s);
if(max<i)
max=i;
if(min>i)
min=i;
}
}catch(IOException e){}
System.out.println("十个数字中最大的数是:"+max);
System.out.println("十个数字中最小的数是:"+min);
}
}
4-7
class Student
{
String stuId;
String classId;
String stuName;
char stuSex;
int stuAge;
Student(String a,String b,String c,char d,int e)
{ stuId=a;
classId=b;
stuName=c;
stuSex=d;
stuAge=e;
}
String getStuId()
{return stuId;
}
String getClassId()
{return classId;
}
int getStuAge()
{return stuAge;
}
boolean setStuAge(int newAge)
{if(newAge>0&&newAge<130)
{stuAge=newAge;
return true;
}
else
{System.out.println(“非法学生年龄!”);
return false;
}
}
}
4-8
public class ex4_8
{public static void main(String args[])
{ Student demoStudent=new Student( );
}
}
4-10
答:构造方法是一种特殊的方法,每个类都可以有一个或多个构造方法。在程序创建某类的对象时系统会自动调用执行该类的构造方法,构造方法中定义了对新建对象的初始化操作。构造方法的方法名与它所在的类名相同,它没有返回类型,一般不能由编程人员显式地直接调用。
题目中有两处错误,构造方法没有返回类型,也不需要返回操作。修改后的程序如下:
Student(int sno,String sname)
{ studentNo=sno;
studentName=sname;
}
6-4
(一)public class ex6_4
{public static void main(String args[])
{final int NUMBER=100;
int count=10;
int randomNum=0;
int probability[]=new int[6];
for(int i=0;i<NUMBER;i++)
{randomNum=(int)(Math.random()*6)+1;
probability[randomNum-1]++;}
for(int i=0;i<6;i++)
{ System.out.println(“/n”+(i+1)+”:/t”+ (double)probability[i]/100);}
}
}
(二)
class MathRandom
{
public static void main(String args[])
{
int k=0,a=0,b=0,c=0,d=0,e=0,f=0;
double aa=0.0,bb=0.0,cc=0.0,dd=0.0,ee=0.0,ff=0.0;
System.out.println("以下是100个数:");
for(int i=0;i<100;i++)
{
k=(int)(Math.random()*6)+1;
switch(k)
{
case 1:a++;break;
case 2:b++;break;
case 3:c++;break;
case 4:d++;break;
case 5:e++;break;
case 6:f++;break;
}
}
aa=(double)a/100;bb=(double)b/100; cc=(double)c/100;
dd=(double)d/100; ee=(double)e/100; ff=(double)f/100;
System.out.println("1出现的次数是"+a+"概率是:"+aa);
System.out.println("1出现的次数是"+b+"概率是:"+bb);
System.out.println("1出现的次数是"+c+"概率是:"+cc);
System.out.println("1出现的次数是"+d+"概率是:"+dd);
System.out.println("1出现的次数是"+e+"概率是:"+ee);
System.out.println("1出现的次数是"+f+"概率是:"+ff);
System.out.println("以下是1000个数:");
a=0;b=0;c=0;d=0;e=0;f=0;
for(int i=0;i<1000;i++)
{
k=(int)(Math.random()*6)+1;
switch(k)
{
case 1:a++;break;
case 2:b++;break;
case 3:c++;break;
case 4:d++;break;
case 5:e++;break;
case 6:f++;break;
}
}
aa=(double)a/1000;bb=(double)b/1000; cc=(double)c/1000;
dd=(double)d/1000; ee=(double)e/1000; ff=(double)f/1000;
System.out.println("1出现的次数是"+a+"概率是:"+aa);
System.out.println("1出现的次数是"+b+"概率是:"+bb);
System.out.println("1出现的次数是"+c+"概率是:"+cc);
System.out.println("1出现的次数是"+d+"概率是:"+dd);
System.out.println("1出现的次数是"+e+"概率是:"+ee);
System.out.println("1出现的次数是"+f+"概率是:"+ff);
} }
6-11
import java.io.*;
public class ex6_11
{ public static void main (String[] args)
{ int a[]=new int[10];
int max=0,min=0,sum=0;
double average=0.0;
String s;
try
{System.out.print("请输入十个整型数:");
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
s=aa.readLine();
a[0]=Integer.parseInt(s);
max=min=sum=a[0];
for(int i=1;i<=9;i++)
{BufferedReader bb=new BufferedReader(new InputStreamReader(System.in));
s=bb.readLine();
a[i]=Integer.parseInt(s);
sum=sum+a[i];
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
average=(double)sum/10;
}catch(IOException e){}
System.out.println("最大值是:"+max);
System.out.println("最小值是:"+min);
System.out.println("平均值是:"+average);
System.out.println("所有元素的和是:"+sum);
}
}
6-17
import java.io.*;
public class ex6_17
{ public static void main(String args[])
{ try
{ System.out.println("请输入一个字符串:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
if(isReversable(str))
System.out.println("这个字符串是回文。/n");
else
System.out.println("这个字符串不是回文。/n");
}catch(IOException e){}
}
public static boolean isReversable(String s)
{ for(int i=0,j=s.length()-1; i<j; i++,j--)
{ if(s.charAt(i) != s.charAt(j))
return false;
}
return true;
}
}
7-5
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ex7_5 extends Applet implements ActionListener
{String msg=”I love China!”;
Button btn1=new Button(“放大”);
Button btn2=new Button(“缩小”);
int fontsize;
public void init()
{add(btn1);
add(btn2);
fontsize=12;
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void paint(Graphics g)
{Font newFont,oldFont;
oldFont=g.getFont();
newFont=new Font(oldFont.getFontName(),oldFont.getStyle(),fontsize);
g.setFont(newFont);
g.drawString(msg,10,100);
}
public void actionPerformed(ActionEvent e)
{if (e.getSource()==btn1)
fontsize++;
else if(e.getSource()==btn2)
fontsize--;
repaint();
}
}