Java上机题目及答案代码Word格式文档下载.docx
《Java上机题目及答案代码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Java上机题目及答案代码Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。
![Java上机题目及答案代码Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2022-12/19/f5f0f094-14dc-484c-b828-b315341a7d6d/f5f0f094-14dc-484c-b828-b315341a7d6d1.gif)
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s1.length());
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(pareTo(s2));
System.out.println(pareTo(s3));
System.out.println(s1.concat(s3));
System.out.println(s1.indexOf('
t'
));
System.out.println(s3.lastIndexOf('
}
}
实验2
Java异常处理
一、实验目的
1)
掌握Java异常的概念及工作机制
2)
掌握异常的分类
3)
掌握抛出异常语句的使用(throw)
4)
掌握抛出捕获处理异常语句的使用(try…catch…finally)
5)
掌握上抛异常语句的使用(throws)
6)
掌握创建自定义异常
二、实验环境
JDK1.6+Eclpise3.2
三、实验内容
1、编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。
测试当输入的数据不是double型数据(如字符串“abc”)会产生什么结果,怎样处理。
packagelab2;
importjava.util.InputMismatchException;
importjava.util.Scanner;
publicclassErr_1{
Scannerreader=newScanner(System.in);
doubler;
try{
r=reader.nextDouble();
System.out.println(3.14*r*r);
}
//System.out.println(3.14*r*r);
catch(InputMismatchExceptione){
System.out.println("
数据类型错误"
+e.toString());
2、计算两个正数之和,当任意一个数超出范围时,抛出自己定义的异常(NumberRangeException)。
importpiler.sym;
classNumberRangeExceptionextendsException{
NumberRangeException(Stringmsg){
super(msg);
publicclassErr_2{
inta=reader.nextInt();
intb=reader.nextInt();
if(a<
=0||b<
=0){
NumberRangeExceptione=newNumberRangeException("
Thenumberbeyondrange!
"
);
throwe;
System.out.println(a+b);
catch(NumberRangeExceptione){
System.out.println(e.toString());
实验3输入和输出
1.实验目的
了解流式输入输出的基本原理;
掌握类、的使用方法。
2.实验内容
(1)编程:
检查C:
\WINDOWS\setuplog.txt文件是否存在,若在则显示该文件的名称和内容。
packagelab3;
importjava.io.File;
importjava.io.IOException;
publicclassInout{
privatestaticScannerin;
publicstaticvoidmain(String[]args)throwsIOException{
File("
C:
\\WINDOWS\\setuplog.txt"
System.out.println("
Dothis?
\t"
+f1.exists());
System.out.println(f1.getName());
in=newScanner(newFile("
while(in.hasNextLine()){
Stringstr=in.nextLine();
System.out.println(str);
catch(IOExceptione){
System.out.println(e.getMessage());
(2)编程:
输入5个学生的信息(包含学号、姓名、3科成绩),统计各学生的总分,然后将学生信息和统计结果存入二进制数据文件STUDENT.DAT中。
3.实验要求编程时,应考虑异常处理。
importjava.io.;
importjava.io.ObjectOutputStream;
importjava.io.RandomAccessFile;
importjavafx.beans.value.WritableObjectValue;
classStudent{
Stringsnum;
Stringname;
inta,b,c,sum;
publicvoidInfo(){
Pleaseenterthename:
name=reader.next();
Pleaseenterthestudentnumber:
snum=reader.next();
Pleaseenterthreecourses'
sorce:
a=reader.nextInt();
b=reader.nextInt();
c=reader.nextInt();
sum=a+b+c;
reader.close();
publicclassInout_2{
raf=new("
dari.txt"
true);
ObjectOutputStreamout=newObjectOutputStream(raf);
Student[]stu=newStudent[5];
for(inti=0;
i<
5;
i++){
stu[i]=newStudent();
(stu[i]).Info();
out.writeObject(stu[i]);
out.close();
catch(IOExceptionioe){
System.out.println(ioe);
catch(Exceptione){
System.out.println(e);
实验题目4
设计计算器的GUI界面,理解使用Java中的布局管理器。
packagelab4;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowEvent;
importjava.awt.event.WindowListener;
importjava.util.ArrayList;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JTextArea;
publicclassMyFrameextendsJFrame{
privateJButtonbtn0=newJButton("
0"
privateJButtonbtn1=newJButton("
1"
privateJButtonbtn2=newJButton("
2"
privateJButtonbtn3=newJButton("
3"
privateJButtonbtn4=newJButton("
4"
privateJButtonbtn5=newJButton("
5"
privateJButtonbtn6=newJButton("
6"
privateJButtonbtn7=newJButton("
7"
privateJButtonbtn8=newJButton("
8"
privateJButtonbtn9=newJButton("
9"
privateJButtonbtnpoint=newJButton("
."
privateJButtonbtnsqrt=newJButton("
¡
Ì
privateJButtonbtnadd=newJButton("
+"
privateJButtonbtnsub=newJButton("
-"
privateJButtonbtnmul=newJButton("
*"
privateJButtonbtndiv=newJButton("
/"
privateJButtonbtnback=newJButton("
û
privateJButtonbtnsin=newJButton("
sin"
privateJButtonbtnequ=newJButton("
="
privateJButtonbtnclear=newJButton("
CE"
privateJTextAreainput=newJTextArea(2,21);
privateJTextAreashow=newJTextArea("
Result:
\n"
2,21);
privateStringresult="
privateArrayList<
Integer>
signpos=newArrayList<
();
publicMyFrame(Stringtitle)
{
this();
setTitle(title);
privateMyFrame(){
setLayout(newFlowLayout(FlowLayout.LEADING));
setSize(250,250);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//¾
Ó
Ö
Ð
show.setEditable(false);
input.setEditable(false);
add(input);
add(show);
add(btn7);
add(btn8);
add(btn9);
add(btndiv);
add(btnsqrt);
add(btn4);
add(btn5);
add(btn6);
add(btnmul);
add(btnback);
add(btn1);
add(btn2);
add(btn3);
add(btnsub);
add(btnclear);
add(btn0);
add(btnpoint);
add(btnsin);
add(btnadd);
add(btnequ);
btn0.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
input.setText(input.getText()+"
});
btn1.addActionListener(newActionListener(){
btn2.addActionListener(newActionListener(){
btn3.addActionListener(newActionListener(){
btn4.addActionListener(newActionListener(){
btn5.addActionListener(newActionListener(){
btn6.addActionListener(newActionListener(){
btn7.addActionListener(newActionListener(){
btn8.addActionListener(newActionListener(){
btn9.addActionListener(newActionListener(){
btnpoint.addActionListener(newActionListener(){
btnadd.addActionListener(newActionListener(){
signpos.add(input.getText().length());
btnsub.addActionListener(newActionListener(){
btnmul.addActionListener(newActionListener(){
btndiv.addActionListener(newActionListener(){
btnsin.addActionListener(newActionListener(){
doubleres;
if(!
input.getText().isEmpty())//!
input.getText().equals("
)
{//!
input.getText()=="
error!
!
res=Double.parseDouble(input.getText());
show.setText("
sin"
+input.getText()+"
="
+Math.sin(res));
input.setText("
elseif(!
result.isEmpty())
res=Double.parseDouble(result);
+result+"
btnequ.addActionListener(newActionListener(){
Stringin=input.getText();
if(signpos.size()==0)return;
doublefirst;
Stringtmp="
if(in.charAt(0)=='
+'
||in.charAt(0)=='
-'
*'
/'
first=Double.parseDouble(result);
tmp=result.substring(result.indexOf('
='
)+1);
else
first=Double.parseDouble(in.substring(0,signpos.get(0)));
result=String.valueOf(operator(first,0,in));
show.setText(tmp+in+"
+result);
signpos.clear();
btnsqrt.addActionListener(newActionListener(){
publicvoidacti