河北工业大学Java程序设计实验报告.doc

上传人:w****2 文档编号:6500861 上传时间:2020-02-27 格式:DOC 页数:92 大小:558KB
返回 下载 相关 举报
河北工业大学Java程序设计实验报告.doc_第1页
第1页 / 共92页
河北工业大学Java程序设计实验报告.doc_第2页
第2页 / 共92页
河北工业大学Java程序设计实验报告.doc_第3页
第3页 / 共92页
点击查看更多>>
资源描述
Java程序设计实验报告 班级: 网络 151 姓名: 徐毅民 学号: 153299 实验一 Java语言基础一实验目的掌握 Java语言的基本语法、基本数据类型的使用方法,掌握从键盘输入基本类型的数据,熟练运用分支、循环等语句控制程序流程。2 实验内容 1、编写一个Java应用程序,用户从键盘输入十名学生的信息,至少包括姓名、年龄、出生年月日、java课程实验成绩,成绩使用浮点数,年龄使用整型,程序将输出年龄、java课程实验成绩的平均值。提示:Scanner对象调用nextDouble()或nextFloat()可以获取用户从键盘输入的浮点数。2、使用Arrays类实现数组排序:使用java.util包中的Arrays类的类方法public static void sort(double a)可以把参数a指定的double类型数组按升序排序;public static void sort(double a, int start , int end)可以把参数a指定的double类型数组中从位置start到end位置的值按升序排序。给定数组 int a=12,34,9,-23,45,6,90,123,19,45,34; 从键盘读入一个整数,使用折半查找判断该整数是否在这个数组中,并将结果输出。3、输出100200之间的所有素数。4、采用for循环求1至1000之内的所有“完全数”。所谓“完全数”是指一个数,恰好等于它的因子之和。例如,6是一个完全数,因为6的因子为1、2、3,而61+2+3。5、已知XYZ+YZZ=532,其中X、Y和Z为数字,编程求出X,Y和Z的值。三实验步骤实验1以最终学生信息管理系统为准实验2代码如下:import java.util.Arrays;import java.util.Scanner;public class test2 public static void printDoubleArray(double a) for(int i = 0; i a.length; i+) System.out.print(ai+ );System.out.println();public static void printfIntegerArray(int a) for(int i = 0; i a.length; i+) System.out.print(ai+ );System.out.println();public static void main(String args) double a = 2.6, 4.6, 2, 8, 888, 569.5, 454.5, 100, -84.5, 45;System.out.println(原double数组a:);printDoubleArray(a);Arrays.sort(a, 0, a.length/2);System.out.println(排序double数组a前一半:);printDoubleArray(a);Arrays.sort(a);System.out.println(排序整个double数组a:);printDoubleArray(a);int b = 12, 34, 9, -23, 45, 6, 90, 123, 19, 45, 34;Arrays.sort(b);System.out.println(排序整个double数组b:);printfIntegerArray(b);Scanner in = new Scanner(System.in);System.out.print(输入要查询的数字:);int key = in.nextInt();int i = Arrays.binarySearch(b, key);if(i = b.length) System.out.println(i+输入数字不在数组中!); else System.out.println(bi+在数组b第+(i+1)+位!);实验3代码如下:public class test3 public static boolean isPrime(int x) if(x = 1) return false;int s = (int)Math.sqrt(x);for(int i = 2; i = s; i+) if(x%i = 0) return false;return true;public static void main(String args) int cnt = 0;for(int i = 100; i 200; i+) if(isPrime(i) cnt+;System.out.printf(%4d, i);if(isPrime(i) & cnt%10 = 0) System.out.println();实验4代码如下:public class test4 public static boolean isCompleteNumber(int x) int sum = 0;for(int i = 1; i x; i+) if(x%i = 0) sum += x;return x = sum;public static void main(String args) int cnt = 0;for(int i = 1; i = 1000; i+) if(isCompleteNumber(i) cnt+;System.out.printf(%4d, i);if(isCompleteNumber(i) & cnt%10 = 0) System.out.println();实验5代码如下:public class test5 public static void main(String args) for(int x = 0; x 10; x+) for(int y = 0; y 10; y+) for(int z = 0; z MyDate.getDaysOfMonth(year, month) return false;return true;public static boolean isLeapYear(int year) return year%400=0 | year%100!=0 & year%4=0;public static void main(String args) int sel, y, m, d;Scanner in = new Scanner(System.in);while(true) System.out.println(请输入年月日:);y = in.nextInt();m = in.nextInt();d = in.nextInt();if(MyDate.isValidDate(y, m, d) System.out.println(y+年+m+月+d+日+信息正确!); else System.out.println(y+年+m+月+d+日+信息不正确!);System.out.println(是否继续输入?(1:是,0:否);sel = in.nextInt();if(sel = 0) break;实验2以最终学生信息管理系统为准实验3代码如下:Shape接口:public interface Shape public abstract double getArea();public abstract double getPerimeter();Rectangle类:public class Rectangle implements Shape private double width;private double height;private Coordinate c; /矩形左下角的点public Rectangle() this.width = 0;this.height = 0;this.c = new Coordinate(0, 0);public Rectangle(double width, double height, int x, int y) this.width = width;this.height = height;this.c = new Coordinate(x, y);Overridepublic double getArea() return this.width*this.height;Overridepublic double getPerimeter() return 2*(this.width+this.height);Circle类public class Circle implements Shape private double radius;private Coordinate center;public Circle() this.radius = 0;this.center = new Coordinate(0, 0);public Circle(double radius, long x, long y) this.radius = radius;this.center = new Coordinate(x, y);Overridepublic double getArea() return Math.PI*this.radius*this.radius;Overridepublic double getPerimeter() return 2*Math.PI*this.radius;Triangle类public class Triangle implements Shape private double a, b, c;private Coordinate c1, c2, c3;public Triangle() this.a = 0;this.b = 0;this.c = 0;this.c1 = new Coordinate(0, 0);this.c2 = new Coordinate(0, 0);this.c3 = new Coordinate(0, 0);Overridepublic double getArea() double s = this.getPerimeter()/2;return Math.sqrt(s*(s-a)*(s-b)*(s-c);Overridepublic double getPerimeter() return this.a+this.b+this.c;public static boolean isVaild(double a, double b, double c) if(a+b = c | a+c = b | b+c = c | Math.abs(a-c) = b | Math.abs(b-c) = a) return false;return true;测试类public class test3 public static void main(String args) Shape shape;Scanner in = new Scanner(System.in);System.out.println(请输入矩形的宽、高,左下角点的横坐标、纵坐标:);shape = new Rectangle(in.nextDouble(), in.nextDouble(), in.nextInt(), in.nextInt();System.out.println(矩形的面积:+shape.getArea()+ 周长+shape.getPerimeter();System.out.println(请输入圆的半径,圆心的横坐标、纵坐标:);shape = new Circle(in.nextDouble(), in.nextInt(), in.nextInt();System.out.println(圆形的面积:+shape.getArea()+ 周长+shape.getPerimeter();double a, b, c;System.out.println(请输入三角形的三条边长:);a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble();while(!Triangle.isVaild(a, b, c) System.out.println(这不是一个三角形,请重新输入三条边长:);a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble();System.out.println(请分别输入三个顶点的横坐标、纵坐标:);shape = new Triangle(a, b, c, in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt();System.out.println(三角形的面积:+shape.getArea()+ 周长+shape.getPerimeter();实验4代码如下:5 详细的调试和运行结果实验1运行结果:请输入年月日:2001 12 562001年12月56日信息不正确!是否继续输入?(1:是,0:否)1请输入年月日:1900 2 291900年2月29日信息不正确!是否继续输入?(1:是,0:否)0实验2以最终学生信息管理系统为准实验3运行结果:请输入矩形的宽、高,左下角点的横坐标、纵坐标:7 8 9 10矩形的面积:56.0 周长30.0请输入圆的半径,圆心的横坐标、纵坐标:2 3 4圆形的面积:12.566370614359172 周长12.566370614359172请输入三角形的三条边长:3 4 5 请分别输入三个顶点的横坐标、纵坐标:4 5 8 5 4 8三角形的面积:6.0 周长12.0实验4运行结果:五实验感想通过本次从实验对java语言的基本语法有了更深刻的了解,对java中类的定义和面向对象的设计有了基本的了解。实验三 异常处理程序设计1 实验目的了解Java中异常处理(exception)的作用及常用的异常类,掌握异常处理的设计方法。2 实验内容1、用try-catch-finally结构实现异常处理。编译并运行程序,写出程序运行结果。2、设计一个Java程序,自定义异常类,从命令行(键盘)输入一个字符串,如果该字符串值为“XYZ”,则抛出一个异常信息“This is a XYZ”,如果从命令行输入ABC,则没有抛出异常。(只有XYZ和ABC两种输入)。3、声明一个Average接口,其中约定求平均值的方法;声明多个类实现Average接口,分别给出求平均值的方法实现。例如,在一组数值中,一种算法是,全部数值相加后求平均值,另一种算法是,去掉一个最高分和一个最低分后,再将总分求平均等;使用键盘输入数据时,对于不能转换成数值的字符串进行异常处理。三实验步骤实验1代码如下:public class ExceptionTest public static void main(String args) int i = 0;String greeting = Hello, Only, Test;while(i 4) try System.out.println(greetingi); catch(ArrayIndexOutOfBoundsException e) System.out.println(数组越界); finally System.out.println(总会运行);i+;实验2代码如下:import java.util.Scanner;public class MyException extends Exception private static final long serialVersionUID = 1L;String information;public MyException() this.information = this.toString();public MyException(String s) this.information = s;public String getString() return this.information;public static void main(String args) int sel;String input;Scanner in = new Scanner(System.in);while(true) try input = in.next();if(input.equals(XYZ) throw new MyException(This is a XYZ); catch(MyException e) System.out.println(e.getString(); finally System.out.print(是否继续输入?(1:是,0:否);sel = in.nextInt();if(sel = 0) break;实验3以最终学生信息管理系统为准4 详细的调试和运行结果实验1运行结果:Hello总会运行Only总会运行Test总会运行数组越界总会运行实验2运行结果:请输入XYZ或ABC:ABC是否继续输入?(1:是,0:否):1请输入XYZ或ABC:XYZThis is a XYZ是否继续输入?(1:是,0:否):05 实验感想通过本次实验对java的异常处理机制有了很深的了解。实验四 图形用户界面程序设计 1.实验目的:掌握组件的使用方法,理解委托事件处理模型。熟悉图形用户界面基本组件的使用方法,熟悉如何使用布局管理器对组件进行管理及如何使用Java的事件处理机制。实验内容:1、 输入一个整数,分别显示其百位、十位和个位数字,图形用户界面如图4.1所示。要求:整数文本行可编辑且能实现事件处理,当输入数据错误时,处理异常,弹出对话框,提示重新输入信息;其他文本行仅用于显示不可编辑。源代码:package sy4;import java.awt.FlowLayout;import java.awt.Label;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;public class Mineframe extends JFrame implements ActionListenerstatic TextField T=new TextField(20); static TextField T1=new TextField(20); static TextField T2=new TextField(20);static TextField T3=new TextField(20);public Mineframe() super(Display the number of integers); this.setSize(300,140); this.setLocation(300,240); this.setLayout(new FlowLayout(); this.add(new Label(整数); this.add(T); this.add(new Label(百位); T1.setEditable(false); this.add(T1); this.add(new Label(十位); T2.setEditable(false); this.add(T2); this.add(new Label(个位); T3.setEditable(false); this.add(T3); JButton button=new JButton(确定); this.add(button); button.addActionListener(this); this.setResizable(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); T.addActionListener(this); public void actionPerformed(ActionEvent e)throws NumberFormatException try String str=T.getText(); int num=Integer.parseInt(str); int t1,t2,t3; t1=num/100; t2=(num/10)%10; t3=(num%100)%10; T1.setText(String.valueOf(t1); T2.setText(String.valueOf(t2); T3.setText(String.valueOf(t3); catch(NumberFormatException nfe) JOptionPane.showMessageDialog(this, 不能转换为整数); finally String str=T.getText(); int num=Integer.parseInt(str); int t1,t2,t3; t1=num/100; t2=(num/10)%10; t3=(num%100)%10; T1.setText(String.valueOf(t1); T2.setText(String.valueOf(t2); T3.setText(String.valueOf(t3); public static void main(String args) new Mineframe(); 实验结果:2、 模拟实现一个可视化的简单计算器,至少提供进行加法、减法、乘法、除法等基本运算的功能,希望能支持加正负号、求平方根、清零等其他功能。源代码:package sy4;import java.awt.*;import javax.swing.*;import javax.swing.event.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Counter extends JFrame implements ActionListener/,WindowListenerprivate JTextField text;private JButton button;private MessageJDialog jdialog;static String ope=;/标记操作数static String flag=;/标记运算符号private static String number=;/number用来表示当前数字static double save=0,result=0,num=0;/save保留前一值,result为结果,num用来记录小数点个数String operation= sqrt, +/-, backspace, C, 7, 8, 9, /, 4, 5, 6, *, 1, 2, 3, -, 0, .,=, + ;public Counter()super(计算器);button=new JButtonoperation.length;this.setBounds(280,100,300,240);this.setDefaultCloseOperation(EXIT_ON_CLOSE);JPanel panel=new JPanel(new FlowLayout();this.getContentPane().add(panel);JPanel panelR=new JPanel(new GridLayout(1,1);/设置text文本框panel.add(panelR);text=new JTextField(0,6);panelR.add(text);JPanel panelID=new JPanel5;/设置按钮for (int i=0;ioperation.length; i+) if(i%4=0)/四个键为一行panelIDi/4=new JPanel(new GridLayout(1,4);panel.add(panelIDi/4);buttoni=new JButton(operationi);/ 这里是按钮的布局buttoni.addActionListener(this);panelIDi/4.add(buttoni);this.setVisible(true);jdialog=new MessageJDialog(this);public void actionPerformed(ActionEvent e)if(e.getSource()=button16)/ 0 if(ope=)if(!text.getText().equals(0)number=text.getText()+0;text.setText(number);elsetext.setText(0);elseope=;text.setText(0);if(flag=)save=Double.parseDouble(text.getText();if(e.getSource()=button12)/ 1if(ope=)if(!text.getText().equals(0)number=text.getText()+1;text.setText(number);elsetext.setText(1);elseope=;text.setText(1);if(flag=)save=Double.parseDouble(text.getText();if(e.getSource()=button13)/ 2,if(ope=)if(!text.getText().equals(0)number=text.getText()+2;text.setText(number);elsetext.setText(2);elseope=;text.setText(2);if(flag=)save=Double.parseDouble(text.getText();if(e.getSource()=button14)/ 3 if(ope=)if(!text.getText().equals(0)number=text.getText()+3;text.setText(number);elsetext.setText(3);elseope=;text.setText(3);if(flag=)save=Double.parseDouble(text.getText();if(e.getSource()=button8)/4if(ope=)if(!text.getText().equals(0)number=text.getText()+4;text.setText(number);elsetext.setText(4);elseope=;text.setText(4);if(flag=)save=Double.parseDouble(text.getText();if(e.getSource()=button9)/ 5if(ope=)if(!text.getText().equals(0)number=text.getText()+5;text.setText(number);elsetext.setText(5);elseope=;text.setText(5);if(flag=)save=Double.parseDouble(text.getText();if(e.getSource()=button10)/6 if(ope=)if(!text.getText().equals(0)number=text.getText()+6;text.setText(number);elsetext.setText(6);elseope=;text.setText(6);if(flag=)save=Double.parseDouble(text.getText();if(e.getSource()=button4)/ 7if(ope=)if(!text.getText().equals(0)number=text.getText()+7;text.setText(number);elsetext.setText(7);elseope=;text.setText(7);if(flag=)save=Double.parseDouble(text.getText();if(e.getSource()=button5)/ 8if(ope=)if(!text.getText().equals(0)
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 管理文书 > 工作总结


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!