if(isEqual(list.get(i).toString(),buf)){//10、
System.out.println(list.get(i));
}}}
publicstaticvoidmain(String[]args){
int[]arr={1,2,3,4,5};
perc1(arr);
}}
本题为什么采用List?
有何优点?
对程序中10个注释点进行注释。
总结本程序回溯的程序设计方法。
以下操作结合数据库进行,首先请将“student”数据库恢复到你的机中
恢复数据库操作步骤:
1、进入SQLServer企业管理器
选MicroseftSQLServer2005→SQLServerManagementStudioExpress,选数据库,例如B200\SQLEXPRESS,连接。
2、将数据文件“附加”恢复数据库。
右键点击“数据库”(database);选恢复(attach);点击按钮“添加”(add);找到数据文件:
student.mdf;
点击“确定”(ok)。
应当发现在数据库中新加了数据库:
student。
2、建立数据源(这次实验预定数据源名为sql1)
操作:
开始、设置、进入控制面板、管理工具、数据源(ODBC)、按添加、选驱动程序:
SQLServer、按“完成”、输入数据源名称:
(本例中用sql1)、“描述”中也填sql1、选当前服务器(一般默认的服务器名为【计算机名】\SQLEXPRESS。
例如,在控制面板的“系统”中查到完整计算机名为“B200”,则服务器名常为:
B200\SQLEXPRESS。
如果这个不正确而下拉框中没有选项,有时输入“local(本地)”即可。
要是还不行,可以进入SQLServer,在连接时观察其数据库名。
如果还不行,就必须找安装数据库的程序员询问了)、下一步、选WindowsNT验证,下一步选“更改默认的数据库为(D)、选“student”数据库、选下一步、完成、测试数据源、确定。
于是建立了数据源sql1,
之后再练习以下各题。
3、设有“学生”数据表,包括学号、姓名、性别、年龄等字段,需要设计一个数据输入程序,通过键盘输入学生数据,存入到计算机中。
importjava.awt.*;
importjava.awt.event.*;
importjava.applet.*;
importjavax.swing.*;
importjava.sql.*;
publicclassa082extendsJApplet{
privateJFrameframe;//1、
privateJButtonb1,b2,b3;//2、
staticJTextFieldf1,f2,f3,f4;//3、
privateJLabell1,l2,l3,l4;//4、
static{
try{//5、
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//6、
}
catch(java.lang.ClassNotFoundExceptione){//7、
System.out.println("ForName:
"+e.getMessage());
}}
publicstaticvoidmain(String[]args){
a082ca=newa082();
ca.go();}
voidgo(){//8、
frame=newJFrame("a082");//9、
frame.setSize(330,210);//10、
frame.setVisible(true);//11、
ContainercontentPane=frame.getContentPane();//12、
contentPane.setLayout(null);//13、
b1=newJButton("录入");//14、
contentPane.add(b1);//15、
b3=newJButton("退出");contentPane.add(b3);
b1.setBounds(60,110,80,20);//16、
b3.setBounds(170,110,80,20);
b1.addActionListener(newButtonHandler1());//17、
frame.getContentPane().add(b1,BorderLayout.CENTER);
b3.addActionListener(newButtonHandler3());
frame.getContentPane().add(b3,BorderLayout.CENTER);
l1=newJLabel("学号");//18、
contentPane.add(l1);//19、
l2=newJLabel("姓名");contentPane.add(l2);
l3=newJLabel("性别");contentPane.add(l3);
l4=newJLabel("年龄");contentPane.add(l4);
f1=newJTextField(0);//20、
contentPane.add(f1);//21、
f2=newJTextField(0);contentPane.add(f2);
f3=newJTextField(0);contentPane.add(f3);
f4=newJTextField(0);contentPane.add(f4);
l1.setBounds(30,30,40,20);//22、
f1.setBounds(70,30,80,20);//
l2.setBounds(160,30,40,20);f2.setBounds(190,30,80,20);
l3.setBounds(30,60,40,20);f3.setBounds(70,60,80,20);
l4.setBounds(160,60,40,20);f4.setBounds(190,60,80,20);
}
publicclassButtonHandler1implementsActionListener{//23、
publicvoidactionPerformed(ActionEvente){
Connectioncon=null;
Stringurl="jdbc:
odbc:
sql1";//24、
try{
con=DriverManager.getConnection(url);
Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Strings1=f1.getText();
Strings2=f2.getText();Strings3=f3.getText();
Strings4=f4.getText();ints4_i=Integer.parseInt(s4);
Stringsss="insertinto学生values('"+s1+"','"+s2+"','"+s3+"',"+s4_i+")";
PreparedStatementps=con.prepareStatement(sss);
intc=ps.executeUpdate();//25、
stmt.close();//26、
}
catch(Exceptione1){
e1.printStackTrace();}
}}
publicclassButtonHandler3implementsActionListener{//23、
publicvoidactionPerformed(ActionEvente){
System.exit(0);
}}}
对以上26个注释点进行注释。
二、执行以下程序,说明程序功能,总结其算法与程序设计方法。
将每题运行结果复制粘贴到同一个Word文件中(界面设计要求将结果截图粘到Word文件中)。
每题注意写明题号(从4到12)。
4、利用Map进行单词词频的统计
importjava.util.*;
publicclassFreq{
publicstaticvoidmain(Stringargs[]){
String[]words={"if","it","is","to","be","it","is","up",
"to","me","to","delegate"};
Integerfreq;
Mapm=newTreeMap();
//构造字符串数组words的单词频率表.以单词为key,以词频为value。
for(inti=0;ifreq=(Integer)m.get(words[i]);//获取指定单词的词频。
//词频递增
if(freq==null){
freq=newInteger
(1);
}else{
freq=newInteger(freq.intValue()+1);
}
m.put(words[i],freq);//在Map中更改词频。
}
System.out.println(m.size()+"distinctwordsdetected:
");
System.out.println(m);
}
}
5、创建学生数组列表集合,用迭代器方法,删除一个元素、添加一个元素。
importjava.util.*;
classStudent
{
Stringname;
charsex;
intscore;
publicStudent(Stringname,charsex,intscore)
{
this.name=name;
this.sex=sex;
this.score=score;
}
publicvoidprintStudent()
{
System.out.println("姓名"+name+"性别"+sex+"分数"+score);
}
}
classArrayStudentList{
publicstaticvoidmain(String[]args){
ArrayListal=newArrayList();
al.add(newStudent("a",'f',90));
al.add(newStudent("b",'m',80));
al.add(newStudent("c",'f',80));
ListIteratorlt=al.listIterator();
System.out.println("增加一个学生");
lt.add(newStudent("q",'m',90));
System.out.println("删掉第二学生");
lt.next();lt.next();lt.remove();
for(ListIteratori=al.listIterator();i.hasNext();){
(i.next()).printStudent();
}}}
6、设计红色、绿色、蓝色三个按钮,使用最基本布局,当按下某按钮时窗口变成相应颜色。
6_1程序1
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
classButtonPanelextendsJPanelimplementsActionListener
//JPanel:
具有布局管理器的面板,ActionListener:
侦听器接口
{publicButtonPanel()
{yellowButton=newJButton("红色");//定义三个按钮对象
blueButton=newJButton("绿色");
redButton=newJButton("蓝色");
add(yellowButton);//加到窗口中
add(blueButton);
add(redButton);
yellowButton.addActionListener(this);//定义点击按钮的事件方法
blueButton.addActionListener(this);
redButton.addActionListener(this);
}
publicvoidactionPerformed(ActionEventevt)
//ActionEvent:
组件定义的动作的高一级事件
{Objectsource=evt.getSource();//返回源事件名称
Colorcolor=getBackground();
if(source==yellowButton)color=Color.red;
elseif(source==blueButton)color=Color.green;
elseif(source==redButton)color=Color.blue;
setBackground(color);//刷新面板颜色
//repaint();
}
privateJButtonyellowButton;//定义三个按钮对象名
privateJButtonblueButton;
privateJButtonredButton;
}
classButtonFrameextendsJFrame
{publicButtonFrame()
{setTitle("ButtonTest");
setSize(300,200);
ContainercontentPane=getContentPane();
contentPane.add(newButtonPanel());//建立面板
}
}
publicclassmyButtons
{publicstaticvoidmain(String[]args)
{JFrameframe=newButtonFrame();
frame.setVisible(true);//窗口显示
}
}
试验一下:
将JButton("红色"中的红色二字增加到6个汉字或更多,执行一下;再试验在界面中增加B1、B2、B3三个按钮,(每增加一个按钮,至少需要privateJButtonb1、b1=newJButton("红色");add(b1);等三条语句,另外再考虑事件定义语句。
6_2、设计红色、绿色、蓝色三个按钮,不使用布局管理器,当按下某按钮时窗口变成相应颜色。
importjava.awt.*;
importjava.awt.event.*;
importjava.applet.*;
importjavax.swing.*;
publicclassmyButtonsextendsJApplet{
privateJFrameframe;//定义窗口对象
privateJButtonyellowButton,blueButton,redButton;//按钮对象
privateColorcolor=getBackground();//颜色对象
publicstaticvoidmain(String[]args){
myButtonsca=newmyButtons();
ca.go();
}
voidgo(){
frame=newJFrame("ButtonTest");
ContainercontentPane=frame.getContentPane();
contentPane.setLayout(null);
frame.setSize(300,200);
frame.setVisible(true);
yellowButton=newJButton("红色");//定义三个按钮对象
blueButton=newJButton("绿色");
redButton=newJButton("蓝色");
contentPane.add(yellowButton);//安装到容器中
contentPane.add(blueButton);
contentPane.a