System.out.print(z.matrix[i][j]+"");//访问矩阵元素方法2,前提是matrix
前无private
System.out.println();}
}
}
习题六
9、
publicclassVehicle
Stringcolor,kind;
intspeed;
Vehicle()
{
color=”Red”;
kind=”卡车”;
speed=0;
}
publicvoidsetColor(Stringcolor1){
color=color1;
}
publicvoidsetSpeed(Stringspeed1){
speed=speed1;
}
publicvoidsetKind(Stringkind1){
kind=kind1;
}
publicStringgetColor(){
returncolor;
}
publicStringgetKind(){
returnkind;
}
publicintgetSpeed(){
returnspeed;
}
publicstaticvoidmain(String[]args)
{
Vehicleche=newVehicle();
Che.setColor(“Blue”);
Che.setSpeed(150);
Che.setKind(“跑车”);
System.out.println(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公
路上”);
System.out.println(“时速”+che.getSpeed()+”km/h”);}
}
习题七7、
publicclassVehicle,
Stringcolor,kind;
intspeed;
Vehicle()
{
color=””;
kind=””;
speed=0;
}
publicvoidsetColor(Stringcolor1)
{
color=color1;
}
publicvoidsetSpeed(Stringspeed1)
{
speed=speed1;
}
publicvoidsetKind(Stringkind1)
{
kind=kind1;
}
publicStringgetColor(){
returncolor;
}
publicStringgetKind(){
returnkind;
}
publicintgetSpeed(){
returnspeed;
}
}
publicclassCarextendsVehicle
{
intpassenger;
publicCar()
{
super();
passenger=0;
}
publicvoidsetPassenger(intpassenger)
{
this.passenger=passenger;}
publicintgetPassenger(){
returnpassenger;
}
publicstaticvoidmain(String[]args)
{
Carbenz=newCar();
benz.setColor(“Yellow”);
benz.setKind(“roadster”);
benz.setSpeed(120);
benz.setPassenger(4);
System.out.println(“benz:
“);
System.out.println(“Color“+benz.getColor());System.out.print(“Speed(km/h)“);
System.out.println(benz.getSpeed());System.out.println(“Kind:
“+benz.getKind());System.out.print(“Passenger:
“);
System.out.println(benz.getPassenger());
}
}
习题九
4、
importjava.io.*;
publicclassUseException
{
publicstaticvoidmain(String[]args)
{
System.out.println("请输入一个整数字符串");
try
{
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
inta=Integer.parseInt(in.readLine());
System.out.println("您输入的整数是:
"+a);
}
catch(IOExceptione)
{
System.out.println("IO错误");
}
catch(NumberFormatExceptione1)
{
System.out.println("您输入的不是一个整数字符串");
}
}
}
习题十7、
importjava.io.*;
publicclassSaveName{
publicstaticvoidmain(String[]args)
{
try
{
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
BufferedWriterbw=newBufferedWriter(newFileWriter("name.txt"));
Strings;
while(true)
{
System.out.println("请输入姓名:
");
s=br.readLine();
if(s.length()==0)
break;
bw.write(s);
bw.newLine();
}
br.close();
bw.close();
}
catch(FileNotFoundExceptione)
{
System.out.println(e.toString());
}
catch(IOExceptione1)
{
System.out.println(e1.toString());
}
}
}
8、
importjava.io.*;
publicclassSaveGrade
{
publicstaticvoidmain(String[]args)
{
try
{
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
BufferedWriterbw=newBufferedWriter(newFileWriter("grade.txt"));
Strings,ss;
while(true)
{
System.out.println("请输入姓名:
");
s=br.readLine();
if(s.length()==0)
break;
bw.write(s);
bw.newLine();
System.out.println("请输入学号:
");
s=br.readLine();
bw.write(s);
bw.newLine();
System.out.println("请输入成绩:
");
s=br.readLine();
bw.write(s);
bw.newLine();
}
br.close();
bw.close();
intmax=0,min=100,total=0,num=0;
BufferedReaderbf=newBufferedReader(newFileReader("grade.txt"));
while(true)
{
ss=bf.readLine();
if(ss==null)
break;
ss=bf.readLine();
ss=bf.readLine();
intgrade=Integer.parseInt(ss);
total+=grade;
num+=1;
if(grade>max)max=grade;
if(grade}
System.out.println("学生成绩中最高为:
"+max+",最低为:
"+min+",平均分为:
"+total*1.0/num);
bf.close();
}
catch(FileNotFoundExceptione)
{
System.out.println(e.toString());
}
catch(IOExceptione1)
{
System.out.println(e1.toString());
}
}
}
习题十一
6、
importjava.awt.*;
importjava.awt.event.*;
publicclassChangeColorextendsFrame{
privateButtonred=newButton("红");
privateButtongreen=newButton("绿");
privateButtonblue=newButton("蓝");
privateTextFieldtext=newTextField();
publicChangeColor()
{
super("改变颜色");
this.setLayout(null);
text.setBackground(Color.WHITE);
red.setBounds(25,50,50,20);
this.add(red);
green.setBounds(125,50,50,20);
this.add(green);
blue.setBounds(225,50,50,20);
this.add(blue);
text.setBounds(25,100,250,30);
this.add(text);
red.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente)
{
text.setBackground(Color.RED);
}
});
green.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente)
{
text.setBackground(Color.GREEN);
}
});
blue.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente)
{
text.setBackground(Color.BLUE);
}
});
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
setSize(300,200);
setVisible(true);
}
publicstaticvoidmain(String[]args)
{
ChangeColorcolor=newChangeColor();
}
}
习题十二
5、
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassGoodsextendsJFrame
{
privateJComboBoxlist;
privateJTextAreainfo;
privateStringnames[]={
"请选择你要查询的商品",
"A商品",
"B商品",
"C商品",
"D商品",
"E商品",
"F商品"
};
privateStringgoods[][]={
{"","",""},
{"A商品","北京",",300"},
{"B商品","上海",",400"},
{"C商品","广州",",500"},
{"D商品","长沙",",600"},
{"E商品","武汉",",700"},
{"F商品","天津",",800"}
};
publicGoods()
{
super("商品信息");
Containerpane=this.getContentPane();
pane.setLayout(newBorderLayout());
list=newJComboBox(names);
info=newJTextArea(5,20);
pane.add(list,BorderLayout.NORTH);
pane.add(info,BorderLayout.CENTER);
list.addItemListener(newItemListener(){
publicvoiditemStateChanged(ItemEvente)
{
intindex=list.getSelectedIndex();
info.setText("商品名:
"+goods[index][0]+"\n");
info.append("产地:
"+goods[index][1]+"\n");
info.append("价格:
"+goods[index][2]+"\n");
}
});
this.setSize(250,300);
this.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
Goodsccc=newGoods();
ccc.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
}
}