2024年3月26日发(作者:)
Java上机实践三
实验题目1
假设字符串s1、s2、s3的定义如下:
String s1 = “A String”;
String s2 = “A String”;
String s3 = s1;
请写出下列表达式的值:
s1 == s2
s1 == s3
()
(s2)
(s3)
eTo(s2)
eTo(s3)
(s3)
f(‘t’)
dexOf(‘t’)
实验目的
(1) 熟悉字符串类String。
(2) 学习创建类String的对象。
(3) 学习使用类String执行字符串操作。
实验步骤
(1) 按题目的要求创建String对象s1、s2,并声明对象引用s3指向s1;
(2) 调用String类的相关方法,执行指定操作并输出结果。
package lab1;
public class Showstr {
public static void main(String[] args){
String s1 ="A String";
String s2 ="A String";
String s3 = s1;
n(s1 == s2);
n(s1 == s3);
n(());
n((s2));
n((s3));
n(eTo(s2));
n(eTo(s3));
n((s3));
}
}
n(f('t'));
n(dexOf('t'));
实验2 Java异常处理
一、实验目的
1) 掌握Java异常的概念及工作机制 2) 掌握异常的分类
3) 掌握抛出异常语句的使用(throw)
4) 掌握抛出捕获处理异常语句的使用(try…catch…finally)
5) 掌握上抛异常语句的使用(throws)
6) 掌握创建自定义异常
二、实验环境
JDK1.6+Eclpise3.2
三、实验内容
1、编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。
测试当输入的数据不是double型数据(如字符串“abc”)会产生什么结果,怎样处理。
package lab2;
import ismatchException;
import r;
public class Err_1 {
public static void main(String[] args){
Scanner reader=new Scanner();
double r;
try{
r=uble();
n(3.14*r*r);
}
//n(3.14*r*r);
catch(InputMismatchException e){
n("数据类型错误"+ng());
}
}
}
2、计算两个正数之和,当任意一个数超出范围时,抛出自己定义的异常
(NumberRangeException)。
package lab2;
import r;
import ;
class NumberRangeException extends Exception{
NumberRangeException(String msg){
super(msg);
}
}
public class Err_2 {
public static void main(String[] args){
Scanner reader=new Scanner();
try{
int a=t();
int b=t();
if(a<=0||b<=0){
NumberRangeException e=new NumberRangeException("The number
beyond range!");
throw e;
}
n(a+b);
}
catch(NumberRangeException e){
n(ng());
}
}
}
实验3 输入和输出
1.实验目的
了解流式输入输出的基本原理;掌握类File、FileInputStream、FileOutputStream的使用
方法。
2.实验内容 (1)编程:检查C:文件是否存在,若在则显示该文
件的名称和内容。
package lab3;
import ;
import ption;
import r;
public class Inout{
private static Scanner in;
public static void main(String[] args)throws IOException{
try{
File f1=new File("C:");
n("Do this file exist?t"+());
n(e());
in = new Scanner(new File("C:"));
while (tLine()) {
String str = ne();
n(str);
}
}
catch(IOException e){
n(sage());
}
}
}
(2)编程:输入5个学生的信息(包含学号、姓名、3科成绩),统计各学生的总分,然后
将学生信息和统计结果存入二进制数据文件中。3.实验要求 编程时,应考
虑异常处理。
package lab3;
import tputStream;
import ption;
import OutputStream;
import AccessFile;
import r;
import leObjectValue;
class Student{
String snum;
String name;
int a,b,c,sum;
public void Info(){
Scanner reader=new Scanner();
n("Please enter the name:");
name=();
n("Please enter the student number:");
snum=();
n("Please enter three courses'sorce:");
a=t();
b=t();
c=t();
sum=a+b+c;
();
}
}
public class Inout_2{
public static void main(String[] args)throws IOException{
}
}
try{
FileOutputStream raf=new FileOutputStream("",true);
ObjectOutputStream out=new ObjectOutputStream(raf);
Student []stu=new Student[5];
for(int i=0;i<5;i++){
stu[i]=new Student();
(stu[i]).Info();
bject(stu[i]);
}
();
}
catch(IOException ioe){
n(ioe);
}
catch(Exception e){
n(e);
}
实验题目4
设计计算器的GUI界面,理解使用Java中的布局管理器。
package lab4;
import yout;
import Event;
import Listener;
import Event;
import Listener;
import ist;
import n;
import ;
import rea;
public class MyFrame extends JFrame{
private JButton btn0=new JButton("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 JButton 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 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
public MyFrame(String title)
{
this();
setTitle(title);
}
private MyFrame(){
setLayout(new FlowLayout(G));
setSize(250, 250);
setResizable(false);
setDefaultCloseOperation(_ON_CLOSE);
setLocationRelativeTo(null);//¾ÓÖÐ
table(false);
table(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);
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"0");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"1");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"2");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"3");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"4");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"5");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"6");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"7");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"8");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+"9");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t(t()+".");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
(t().length());
t(t()+"+");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
(t().length());
t(t()+"-");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
(t().length());
t(t()+"*");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
(t().length());
t(t()+"/");
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double res;
if(!t().isEmpty())//!t().equals("")
{//!t()=="" error
res=ouble(t());
t("sin "+t()+" = "+(res));
t("");
}
else if(!y())
{
res=ouble(result);
t("sin "+result+" = "+(res));
t("");
}
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String in=t();
if(()==0) return;
double first;
String tmp="";
if((0)=='+'||(0)=='-'||(0)=='*'||(0)=='/')
{
first=ouble(result);
tmp=ing(f('=')+1);
}
else
first=ouble(ing(0, (0)));
result=f(operator(first,0,in));
t(tmp+in+" = "+result);
t("");
();
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double res;
if(!t().isEmpty())//!t().equals("")
{//!t()=="" error
res=ouble(t());
t("Sqrt "+t()+" = "+(res));
t("");
}
else if(!y())
{
res=ouble(result);
t("Sqrt "+result+" = "+(res));
t("");
}
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String in=t();
if(y()) return;
t(ing(0, ()-1));
if(th("+")||th("-")||th("*")||th("/"))
(()-1);
}
});
ionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t("");
();
}
});
}
private double operator(double operA,int i,String str)
{
double
operB=i==()-1?ouble(ing((i)+1))
:ouble(ing((i)+1,(i+1)
));
switch(((i)))
{
case '+':
operA+=operB;
break;
case '-':
operA-=operB;
break;
case '*':
operA*=operB;
break;
case '/':
operA/=operB;
break;
}
if(i==()-1)
return operA;
else
return operator(operA,i+1,str);
}
public static void main(String[] args) {
MyFrame mf=new MyFrame("°Ö°Ö×öµÄ¼ÆËãÆ÷");
ible(true);
}
}


发布评论