自考JAVA语言程序设计一课后习题答案与源代码实验大纲Word格式文档下载.docx
《自考JAVA语言程序设计一课后习题答案与源代码实验大纲Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《自考JAVA语言程序设计一课后习题答案与源代码实验大纲Word格式文档下载.docx(43页珍藏版)》请在冰豆网上搜索。
elseif((c[i]>
A'
c[i]<
Z'
)||(c[i]>
a'
z'
))
letterCount++;
}
Stringresult="
输入内容:
\n"
+str+"
\n数字字符:
+numberCount+"
个;
"
+"
\n字母:
+letterCount
+"
个"
;
JOptionPane.showMessageDialog(null,result,"
结果:
JOptionPane.INFORMATION_MESSAGE);
}
}
2找质数程序
输出质数原文件:
PrintPrime.java
importjavax.swing.JOptionPane;
*2找质数程序,利用对话框读入整数,输出2至这个整数之间的质数.<
publicclassPrintPrime
privateintnumber;
//正整数
privateStringresult="
publicPrintPrime()//构造方法
number=getIntegerNumber("
输入整数n"
0);
//要求是>
=0的整数
if(number<
0)
return;
//出现错误,程序结束
else
//如果大于等于2,开始用循环计算结果
for(inti=2;
=number;
i++)//计算素数和
{
if(isPrimeNumber(i))
result+=i+"
}
//显示最后的和
JOptionPane.showMessageDialog(null,number+"
之前所有素数为:
\n“"
+result+"
”"
"
显示结果"
JOptionPane.INFORMATION_MESSAGE);
/**
*通过图形界面,得到符合规则的正整数的方法
*@parammessage-在弹出的对话框中,显示提示信息
*@parammin-要求此数必须大于等于min
*@return-返回符合规则的整数
publicintgetIntegerNumber(Stringmessage,intmin)
Stringstr=JOptionPane.showInputDialog(null,message,"
提示信息"
intnumber=-1;
try
number=Integer.parseInt(str);
//得到输入的正整数
catch(Exceptione)
JOptionPane.showMessageDialog(null,"
输入非数字字符\n程序结束"
错误警告"
JOptionPane.ERROR_MESSAGE);
return-1;
//输入的不是数字字符,程序结束
min)
输入的数不符合规则,不是正整数\n程序结束"
JOptionPane.ERROR_MESSAGE);
//输入的数不是大于2的正整数时候,程序结束
returnnumber;
*判断是否是素数的方法
*@paramn-需要判断的数
*@return-是素数返回true,否则返回false
publicbooleanisPrimeNumber(intn)
for(inti=2;
n;
if(n%i==0)
returnfalse;
returntrue;
/**main方法*/
newPrintPrime();
3类的继承定义,包括几何形状类Shape、圆形类Circle.、矩形类Rectangle
*几何图形类,抽象类
abstractclassShape
publicfloatarea()
return0.0f;
*圆形类
classCircleextendsShape
privatefloatR;
publicCircle(floatr)
R=r;
return(float)(Math.PI*R*R);
*矩形类
classRectangleextendsShape
privatefloatw,h;
publicRectangle(floatw,floath)
this.w=w;
this.h=h;
returnw*h;
publicclassWork11_3
publicstaticvoidmain(Stringargs[])
Circlec;
Rectangler;
c=newCircle(2.0f);
r=newRectangle(3.0f,5.0f);
System.out.println("
圆面积"
+returnArea(c));
长方形面积"
+returnArea(r));
staticfloatreturnArea(Shapes)
returns.area();
4数组排序程序
源文件:
Work11_4.java
importjava.util.*;
*4数组排序程序.<
*输入整数序列,对输入的整数进行排序,输出结果.<
publicclassWork11_4
publicstaticfinalintRISE=0;
publicstaticfinalintLOWER=1;
StringTokenizertoken=newStringTokenizer(str,"
.;
:
intmode=Work11_4.RISE;
//排列模式,默认为升序排列
intcount=token.countTokens();
//输入的整数的个数
intarray[]=newint[count];
intindex=0;
while(token.hasMoreTokens())
try
array[index]=Integer.parseInt(token.nextToken());
index++;
catch(Exceptione)
JOptionPane.showMessageDialog(null,"
输入非法字符"
return;
//输入非法字符时候,直接结束程序
sort(array,mode);
//按mode模式,进行排序
Stringresult=newString();
StringmodeString=newString();
if(mode==Work11_4.RISE)
modeString="
升序排列结果为:
if(mode==Work11_4.LOWER)
降序排列结果为:
array.length;
result=result+array[i]+"
,"
if(result!
=null)
JOptionPane
.showMessageDialog(null,result,modeString,JOptionPane.INFORMATION_MESSAGE);
*给数组排序的方法
*@paramarray-需要排序的数组
*@parammode-排序的模式,可以为RISE,LOWER
publicstaticvoidsort(intarray[],intmode)
for(intj=0;
j<
array.length-1;
j++)
if(mode==Work11_4.RISE&
array[j]>
array[j+1])
{
inttemp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
if(mode==Work11_4.LOWER&
array[j]<
5字符串处理程序,括号匹配
括号匹配检测源文件:
CheckBrackets.java
importjava.awt.*;
importjava.awt.event.*;
importunit9.MyFileFilter;
importjava.io.*;
*5字符串处理程序.<
*输入程序的源程序代码行,找出可能存在圆括号,花括号不匹配的错误.<
publicclassCheckBracketsextendsJFrameimplementsActionListener,ItemListener
privatestaticfinallongserialVersionUID=1L;
privateJFileChooserfileChooser;
privateJButtonopenFileButton;
privateJComboBoxcomboBox;
privateJTextFieldshowRowStringField;
privateJTextFieldshowMessageField;
privateJTextAreatextArea;
privateJPanelnorthPanel,control_panel;
privateStringrowString[];
privateFilefile=null;
publicCheckBrackets()
super("
检测圆、花括号匹配程序"
fileChooser=newJFileChooser(System.getProperty("
user.dir"
));
openFileButton=newJButton("
打开文件"
showRowStringField=newJTextField();
showMessageField=newJTextField(20);
textArea=newJTextArea();
comboBox=newJComboBox();
northPanel=newJPanel();
control_panel=newJPanel();
rowString=newString[1000];
fileChooser.addChoosableFileFilter(newMyFileFilter("
txt"
//文件筛选
textArea.setLineWrap(true);
showRowStringField.setEditable(false);
showRowStringField.setBackground(Color.WHITE);
showMessageField.setEditable(false);
showMessageField.setBackground(Color.WHITE);
openFileButton.addActionListener(this);
comboBox.addItemListener(this);
comboBox.addItem("
请选择"
control_panel.add(openFileButton);
control_panel.add(newJLabel("
选择代码行:
control_panel.add(comboBox);
检测结果:
control_panel.add(showMessageField);
northPanel.setLayout(newGridLayout(2,1,10,10));
northPanel.add(control_panel);
northPanel.add(showRowStringField);
this.add(northPanel,BorderLayout.NORTH);
this.add(newJScrollPane(textArea),BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(50,50,550,500);
this.setVisible(true);
this.validate();
publicvoidactionPerformed(ActionEvente)
showMessageField.setText("
intmessage=fileChooser.showOpenDialog(this);
if(message==JFileChooser.APPROVE_OPTION)
file=fileChooser.getSelectedFile();
comboBox.removeAllItems();
comboBox.addItem("
readFile(file);
publicvoiditemStateChanged(ItemEvente)
intindex=comboBox.getSelectedIndex();
if(index>
=1)
showRowStringField.setText(rowString[index-1]);
charc[]=rowString[index-1].toCharArray();
intcount1=0;
intcount2=0;
for(inti=0;
if(c[i]=='
{'
count1++;
}'
count1--;
('
count2++;
)'
count2--;
System.out.println("
大括号"
+count1+"
小括号:
+count2);
if(count1!
=0)
showMessageField.setText("
第"
+index+"
行,大括号'
{}'
匹配错误"
elseif(count2!
行,小括号'
()'
elseif(count1!
=0&
count2!
行,大、小括号都匹配错误"
elseif(count1==0&
count2==0)
括号匹配正确"
publicvoidreadFile(Filef)
if(f!
FileReaderfile=newFileReader(f);
BufferedReaderin=newBufferedReader(file);
Strings=newString();
inti=0;
textArea.setText("
while((s=in.readLine())!
textArea.append((i+1)+"
:
+s+"
rowString[i]=s;
comboBox.addItem(i+1);
i++;
+e.toString());
newCheckBrackets();
6计算器程序。
简易计算器程序原文件:
Calculator.java
*计算器程序.<
*三个文本框,加、减、乘、除按钮在前两个文本框分别输入两个运算数点击按钮后,在第三个文本框中显示计算结果.<
publicclassCalculatorextendsJFrameimplementsActionListener
privateJTextFieldoneField,twoField,resultField;
privateJButtonaddButton,subtractButton,multiplyButton,divideButton,cleanButton;
privateJPanelpanel1,panel2,panel3;
publicCalculator()
简易计算器"
oneField=newJTextField(10);
twoField=newJTextField(10);
resultField=newJTextField(20);
addButton=newJButton("
+"
subtractButton=newJButton("
-"
multiplyButton=newJButton("
*"
divideButton=newJButton("
/"
cleanButton=newJButton