1、 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.
2、println(s3.lastIndexOf( 实验2 Java异常处理一、实验目的1)掌握Java异常的概念及工作机制2)掌握异常的分类3)掌握抛出异常语句的使用(throw)4)掌握抛出捕获处理异常语句的使用(trycatchfinally)5)掌握上抛异常语句的使用(throws)6)掌握创建自定义异常二、实验环境JDK1.6+Eclpise3.2三、实验内容1、编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。测试当输入的数据不是double型数据(如字符串“abc”)会产生什么结果,怎样处理。package lab2;import java.util.I
3、nputMismatchException;import java.util.Scanner;public class Err_1 Scanner reader=new Scanner(System.in); double r; try r=reader.nextDouble(); System.out.println(3.14*r*r); /System.out.println(3.14*r*r); catch(InputMismatchException e) System.out.println(数据类型错误+e.toString();2、计算两个正数之和,当任意一个数超出范围时,抛出自
4、己定义的异常(NumberRangeException)。import piler.sym;class NumberRangeException extends Exception NumberRangeException(String msg) super(msg);public class Err_2 int a=reader.nextInt(); int b=reader.nextInt(); if(a=0|b=0) NumberRangeException e=new NumberRangeException(The number beyond range!); throw e; Sy
5、stem.out.println(a+b); catch(NumberRangeException e) System.out.println(e.toString();实验3 输入和输出 1实验目的 了解流式输入输出的基本原理;掌握类、的使用方法。 2实验内容 (1)编程:检查C:WINDOWSsetuplog.txt文件是否存在,若在则显示该文件的名称和内容。package lab3;import java.io.File;import java.io.IOException;public class Inout private static Scanner in; public stat
6、ic void main(String args)throws IOException File(C:WINDOWSsetuplog.txt System.out.println(Do this ?t+f1.exists(); System.out.println(f1.getName(); in = new Scanner(new File( while (in.hasNextLine() String str = in.nextLine(); System.out.println(str); catch(IOException e) System.out.println(e.getMess
7、age();(2)编程:输入5个学生的信息(包含学号、姓名、3科成绩),统计各学生的总分,然后将学生信息和统计结果存入二进制数据文件STUDENT.DAT中。3实验要求 编程时,应考虑异常处理。import java.io.;import java.io.ObjectOutputStream;import java.io.RandomAccessFile;import javafx.beans.value.WritableObjectValue;class Student String snum; String name; int a,b,c,sum; public void Info()Pl
8、ease enter the name: name=reader.next();Please enter the student number: snum=reader.next();Please enter three coursessorce: a=reader.nextInt(); b=reader.nextInt(); c=reader.nextInt(); sum=a+b+c; reader.close();public class Inout_2 raf=new (dari.txt,true); ObjectOutputStream out=new ObjectOutputStre
9、am(raf); Student stu=new Student5; for(int i=0;i5;i+) stui=new Student(); (stui).Info(); out.writeObject(stui); out.close(); catch(IOException ioe) System.out.println(ioe); catch(Exception e) System.out.println(e);实验题目4设计计算器的GUI界面,理解使用Java中的布局管理器。package lab4;import java.awt.FlowLayout;import java.a
10、wt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.util.ArrayList;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextArea;public class MyFrame extends JFrame private JButton btn0=new J
11、Button(0 private JButton btn1=new JButton(1 private JButton btn2=new JButton(2 private JButton btn3=new JButton(3 private JButton btn4=new JButton(4 private JButton btn5=new JButton(5 private JButton btn6=new JButton(6 private JButton btn7=new JButton(7 private JButton btn8=new JButton(8 private JBu
12、tton btn9=new JButton(9 private JButton btnpoint=new JButton(. private JButton btnsqrt=new JButton( private JButton btnadd=new JButton(+ private JButton btnsub=new JButton(- private JButton btnmul=new JButton(* private JButton btndiv=new JButton(/ private JButton btnback=new JButton( private JButton
13、 btnsin=new JButton(sin private JButton btnequ=new JButton(= private JButton btnclear=new JButton(CE private JTextArea input=new JTextArea(2,21); private JTextArea show=new JTextArea(Result:n,2,21); private String result= private ArrayList signpos=new ArrayList(); public MyFrame(String title) this()
14、; setTitle(title); private MyFrame() setLayout(new FlowLayout(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
15、(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(new ActionListener() Override public void actionPerforme
16、d(ActionEvent e) input.setText(input.getText()+ ); btn1.addActionListener(new ActionListener() btn2.addActionListener(new ActionListener() btn3.addActionListener(new ActionListener() btn4.addActionListener(new ActionListener() btn5.addActionListener(new ActionListener() btn6.addActionListener(new Ac
17、tionListener() btn7.addActionListener(new ActionListener() btn8.addActionListener(new ActionListener() btn9.addActionListener(new ActionListener() btnpoint.addActionListener(new ActionListener() btnadd.addActionListener(new ActionListener() signpos.add(input.getText().length(); btnsub.addActionListe
18、ner(new ActionListener() btnmul.addActionListener(new ActionListener() btndiv.addActionListener(new ActionListener() btnsin.addActionListener(new ActionListener() double res; if(!input.getText().isEmpty()/!input.getText().equals() /!input.getText()= error! res=Double.parseDouble(input.getText(); sho
19、w.setText(sin +input.getText()+ = +Math.sin(res); input.setText( else if(!result.isEmpty() res=Double.parseDouble(result);+result+ btnequ.addActionListener(new ActionListener() String in=input.getText(); if(signpos.size()=0) return; double first; String tmp= 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(new ActionListener() public void acti
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1