




已阅读5页,还剩25页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java程序设计实验(NO.1)1、实验目的:使用Java的String类操作字符串和子串。写一个程序可以对两个字符串进行测试,判断第一个字符串是否包含在第二个字符串中,例如字符串“op”包含在字符串“interoperabilityop”中。当第一个字符串包含在第二个字符串中时,显示第一个字符串在第二个字符串中的起始位置;如果被多次包含,则一一列出各个起始位置;如果不包含,也给出相应的提示信息。提示:利用API帮助文档,查找并使用下面方法:boolean regionMatches(int toffset, String other, int ooffset, int len)public class StringTest public static void main(String arg) String big=interoerabilityo; String small=new String(o);/ String small2=a; int i; boolean tag; boolean matchtag=false;/ if(small=a) System.out.println(琵琶); for(i=0;ibig.length();i+) tag=big.regionMatches(i, small, 0, small.length(); if(tag) matchtag=true; System.out.println(匹配的index为+i); if(matchtag=false) System.out.println(没有匹配的); 2、实验目的:掌握java中数组的使用。实验要求:1) 创建一个字符数组测试类 ArrayTest,在其main方法中:将5个元素的数组a 拷贝至8个元素的数组b中。数组a和b初始值均为空,a数组通过命令行输入进行初始化,通过提示信息“准备把a 拷贝到b 数组中,请按任意键开始”,进行数组拷贝,完毕后,输出b数组的内容。提示:第一行写import java.util.*; 利用Scanner 类的new Scanner(System.in) 和nextInt(); import java.util.*;public class ArrayTest public static void main(String arg)int a=new int5;int b=new int8;Scanner input=new Scanner(System.in);System.out.println(请输入数组a的元素,共计5个);for(int i=0;i5;i+)ai=input.nextInt();System.out.println(准备把a copy 到b 数组中,请按任意键开始);for(int i=0;i5;i+)bi=ai;for(int i=0;i=0&month=1)&(day=dayperMonthmonth) return day;if(year%4=0&year%100!=0)|year%400=0)&month=2&day=29)return day; System.out.println(invoid day(+day+)set to 1);return 1;public static void main(String args)Date date1=new Date(1997,13,4);System.out.println(date1.getDate();Date date2=new Date(1999,2,29);System.out.println(date2.getDate();Date date3=new Date(2001,2,29);System.out.println(date3.getDate();2、设计学生类Student包含属性有姓名name和年龄age,由学生类派生出本科生类Undergraduate和研究生类 Graduate。本科生包含的属性有专业specialty,研究生包含的属性有研究方向study_direction 每个类都有相关数据的输出方法,运行效果如下: 要求:在程序中出现this和super()class Student protected String name ; protected int age; protected String degree; protected static final int MALE = 0; protected static final int FEMALE = 1; public Student( String name , int age , String degree ) = name ; this.age = age; this.degree = degree; public void show() System.out.println( name + , + age + , + degree ); public void setName( String name ) = name ; public String getName( ) return ;class Undergraduate extends Student private String specialty; public Undergraduate( String name , int age , String degree , String spe ) super(name ,age , degree ); this.specialty = spe; public void show() System.out.println(姓名: + name); System.out.println(年龄: + age ); System.out.println(学位: + degree); System.out.println(专业: + specialty); class Graduate extends Student private String studydirection; public Graduate( String name , int age , String degree , String stu) super( name ,age , degree ); this.studydirection = stu; public void show() System.out.println(姓名: + name); System.out.println(年龄: + age ); System.out.println(学位: + degree); System.out.println(研究方向: + studydirection); public class StudentTest public static void main( String args ) Undergraduate wj = new Undergraduate(王军,23,本科,工业自动化) ; wj.show(); Graduate lj = new Graduate(刘君,27,硕士,网络技术) ; lj.show(); 3、创建一个点的类Point,要求包含a)以两点坐标为参数的构造方法;以Point为参数的构造方法;b)设置和获取点坐标的方法;创建一个圆的类Circle,要求包含:a)至少两种构造方法;b)设置和获取圆半径、圆心的方法;c)计算圆的直径、圆的周长和圆的面积的方法。创建测试类,建立圆对象,输出它的直径、面积和周长信息;再次改变该圆半径或圆心信息,并输出它的直径、面积和周长信息。class Point private double x ; private double y ; public Point(double x, double y) this.x = x;this.y = y; public Point(Point p) x=p.x; y=p.y;public void setX(double x)this.x=x;public void setY(double y)this.y=y;double getX()return x; double getY() return y; public class Circleprivate Point center;private double radius;private static final double PI=3.1415;public Circle(Point p,double r) center=p; radius=r; public void setCenter(Point p) center=p; public void setRadius(double r) radius=r; public Point getCenter() return center; public double getRadius() return radius; public double getArea() return (PI*radius*radius); / 测试用 public static void main(String args) Point p1,p2; Circle c; p1=new Point(3,2); c=new Circle(p1,5); System.out.println(center of c is: +c.getCenter().getX()+,+c.getCenter().getY(); p2=new Point(5,6); c.setCenter(p2); System.out.println(center of c changed to: +c.getCenter().getX()+,+c.getCenter().getY(); Java程序设计实验(NO.3)实验目的:理解方法的重载、重写,掌握抽象类、接口和多态的使用。1、定义矩形接口Rect,在接口中有计算面积area()、获取宽度getWidth()、获取高度、设置矩形某端点位置等,继承接口Rect实现矩形Rectangle,包含四个成员变量,分别定义该矩形的左上角和右下角的坐标,lefttop_x、lefttop_y、rightdown_x、rightdown_y。要求设计三个同名的不同buildRect()方法为这些实例变量设置值。最后给出一个测试类。提示:正确使用API中Point类的成员变量和方法。import java.awt.Point;interface Rectint area();int getWidth();int getHeight(); class MyRect implements Rectprivate int topLeft_x,topLeft_y;private int bottomRight_x,bottomRight_y;MyRect buildRect(int x1,int y1,int x2,int y2)topLeft_x=x1;topLeft_y=y1;bottomRight_x=x2;bottomRight_y=y2;return this;MyRect buildRect(Point topLeft,Point bottomRight)topLeft_x=topLeft.x;topLeft_y=topLeft.y;bottomRight_x=bottomRight.x;bottomRight_y=bottomRight.y;return this;MyRect buildRect(Point topLeft,int w,int h)topLeft_x=topLeft.x;topLeft_y=topLeft.y;bottomRight_x=(topLeft_x+w);bottomRight_y=(topLeft_y+h);return this;public int area()return Math.abs(topLeft_x-bottomRight_x)*(topLeft_y-bottomRight_y);public int getWidth()return Math.abs(topLeft_x-bottomRight_x);public int getHeight()return Math.abs(topLeft_y-bottomRight_y);void printRect()System.out.print(MyRect:); public class RectTest public static void main(String args)MyRect rec=new MyRect();rec.buildRect(25,25,50,50);rec.printRect();System.out.println(area is +rec.area();System.out.println(*);rec.buildRect(new Point(10,10),new Point(20,20);rec.printRect();System.out.println(area is +rec.area();System.out.println(*);rec.buildRect(new Point(10,10),50,50);rec.printRect();System.out.println(area is +rec.area();System.out.println(*);2、利用抽象方法和多态性完成雇员工资单计算。设计4个类,其中Employee类是抽象基类,包含雇员名字和入职日期,它有三个子类,为每个子类提供一种计算报酬的方法,计酬方法因雇员类型的不同而变化。分别为:SalariedEmployee,每月发放固定工资;HourlyEmployee,根据工作时间长短发放工资(按小时计);CommissionEmloyee,每月除了固定的基本工资外,还根据销售额发放浮动工资。注意:将每个子类都声明为final,因为不需要再继承它们生成子类。各个类里需要定义构造方法,以及各种set和get方法。测试时通过超类引用调用各个类的计算报酬方法,来一般性的处理雇员。import java.util.*;abstract class Employeeprivate String name;private Date hireDate;public Employee(String name,Date hireDate)=name;this.hireDate=hireDate;/System.out.println(name+is a Employee);public String getDetail()return name is +name+nhireDate is +hireDate.getDate();public abstract double earning();class SalariedEmployee extends Employeeprivate double monthSalary;public SalariedEmployee(String name,Date hireDate,double salary)super(name,hireDate);setmonthSalary(salary);public String getDetail() /是方法重写return super.getDetail()+nearning is +earning();public void setmonthSalary(double s)/提供设置月薪的方法monthSalary=(s0?s:0);/要求s大于0public double earning()/非抽象子类必须实现父类中的抽象方法return monthSalary;class CommissionEmloyee extends Employeeprivate double baseSalary;private double monthSales;private double rate;public CommissionEmloyee(String name,Date hireDate,double baseSalary)super(name,hireDate);setBaseSalary(baseSalary);public CommissionEmloyee(String name,Date hireDate,double baseSalary,double monthSales,double rate)/构造方法重载,以提供多种方式初始化super(name,hireDate);setBaseSalary(baseSalary);setmonthSales(monthSales);setRate(rate); public void setBaseSalary(double bs)/设置基本工资的方法,方便变动 baseSalary=(bs0?bs:0);/要求bs大于0 public double getBaseSalary()/设置获取基本工资的方法 return baseSalary;public void setmonthSales(double sale)/提供设置月销售额的方法monthSales=(sale0?sale:0);/要求sale大于0 public double getmonthSales()/设置获取月销售额的方法 return monthSales;public void setRate(double r)/提供设置提成百分比的方法rate=(r0?r:0);/要求提成百分比大于0public double getRate()/设置获取提成百分比的方法 return rate;public String getDetail()return super.getDetail()+nmonthSales is +monthSales+nrate is +rate+nearning is +earning();public double earning()/非抽象子类必须实现父类中的抽象方法return getBaseSalary()+getmonthSales()*getRate();public class MultiEmployeeTest public static void main(String args)Date date1=new Date(1990,2,16);Employee s1=new SalariedEmployee(张三,date1,20000);System.out.println(s1.getDetail();Date date2=new Date(1997,7,1);Employee s2=new SalariedEmployee(李四,date2,9998);System.out.println(s2.getDetail();Date date3=new Date(2008,8,1);Employee s3=new CommissionEmloyee(王五,date3,3000,13000,0.02);System.out.println(s3.getDetail();Date date4=new Date(2004,10,14);Employee s4=new CommissionEmloyee(赵六,date4,3000,20000,0.03);System.out.println(s4.getDetail();(CommissionEmloyee)s4).setRate(0.05);System.out.println(s4.getDetail();3、实验题目:实现一组自定义形状类,其中包括形状的基类,两个具体的形状子类:三角形和长方形。具体要求为:1、基类:Myshape,定义一个name属性,它可以被子类所继承,并提供它的setName和getName方法。提供2个抽象方法,分别用于获取形状的周长、和输出形状的信息(重写String toString())。2、三角形Triangle:自定义3个属性,分别表示三角形的三个边长,并提供各条边的set和get方法。3、长方形Rectangle:自定义2个属性,分别表示矩形的长和宽,添加计算面积方法。abstract class MyShapeprotected String name;public abstract double getGirth();/获取形状的周长public abstract String toString();public String getName()return ;public void setName(String name)=name;class Triangle extends MyShapeprivate double sideA;private double sideB;private double sideC;public Triangle()init();public Triangle(double a,double b,double c)if(isTriangleLegal(a,b,c)/先判断是否合法,如果合法则赋值this.sideA=a;this.sideB=b;this.sideC=c;elseSystem.out.println(Err);init();/否则使用默认三角形private void init()/ 注意:可以为私有的,仅供三角形类使用this.sideA=3;this.sideB=4;this.sideC=5;private boolean isTriangleLegal(double a,double b,double c)/请注意:私有的、布尔型、有参数if(a=0)|(b=0)|(c=0)return false;else if(a+b=c)|(a+c=b)|(b+c=a)return false;return true;public double getGirth()return this.sideA+this.sideB+this.sideC;public String toString()return 三角形的名字是++,它的三条边长分别是:+this.sideA+,+this.sideB+,+this.sideC;public double getSideA()return this.sideA;public void setSideA(double sideA)/ 在设置边长时,依然要检测采用新的边长后,是否能构成三角形if(this.isTriangleLegal(sideA, this.sideB, this.sideC)this.sideA=sideA;public double getSideB()return this.sideB;public void setSideB(double sideB)/ 在设置边长时,依然要检测采用新的边长后,是否能构成三角形if(this.isTriangleLegal(this.sideA,sideB, this.sideC)this.sideB=sideB;public double getSideC()return this.sideC;public void setSideC(double sideC)/ 在设置边长时,依然要检测采用新的边长后,是否能构成三角形if(this.isTriangleLegal(this.sideA, this.sideB, sideC)this.sideC=sideC;public class ShapesTest public static void main(String args)Triangle tri1=new Triangle();tri1.setName(myTriangle1);System.out.println(tri1.toString();Triangle tri2=new Triangle(4,2,1);System.out.println(tri2.toString();tri2.setSideC(3);System.out.println(tri2.toString();System.out.println(tri2.getGirth(); Java程序设计实验(NO.4)1、 实验目的:掌握集合类基本方法的使用。用HashSet和LinkedHashSet创建两个集合对象,产生5个0100间的随机数,添加到这两个集合中。添加完毕后,遍历输出这两个集合元素,然后使用TreeSet 来对这两个集合对象进行重构和排序,并遍历输出。运行过程中,观察集合中元素次序的变化。提示:产生随机数:Math类random()方法import java.util.*;public class SetSortExample public static void main(String args) Set set1 = new HashSet();Set set2 = new LinkedHashSet();for(int i=0;i5;i+)/产生一个随机数, 并将其放入Set 中int s=(int)( Math.random()*100);set1.add(new Integer( s);set2.add(new Integer( s);System.out.println(第 +i+ 次随机数产生为:+s);System.out.println(未排序前HashSet:);Iterator iterator = set1.iterator();/得到一个迭代器while (iterator.hasNext() /遍历Object element = iterator.next();System.out.print(element+ );System.out.println(未排序前LinkedHashSet:);Iterator iterator2 = set2.iterator();/得到一个迭代器while (iterator2.hasNext() /遍历Object element = iterator2.next();System.out.print(element+ );/使用TreeSet 来对另外的Set 进行重构和排序Set sortedSet = new TreeSet(set2);System.out.println(排序后 TreeSet :+sortedSet);2、 实验目的:掌握异常处理的使用。(no-st)实验题目:编写Exception_test程序,具有下面功能:a) 从命令行接收两个数,计算两数相除,捕获除数为零的异常。b) 抛出一个ArrayIndexOutOfBoundsException异常,并捕获处理import java.util.*;import java.util.Scanner;public class AbnormalTreat public static void main(String args)System.out.print(输入两个数:);int array;array=new int2;int i;tryfor(int j=0;jarray.length)throw new Exception(ArrayIndexOutOfBoundsception);i=array0/array1;System.out.print(商为:+i);catch(ArithmeticException e)System.out.print(捕获到除数为零的异常!);catch(Exception e)System.out.println(exception:+e.getMessage();e.printStackTrace();System.exit(0);3、 实验目的:自定义异常类。计算两个正数之差,当结果为负时,抛出自己定义的异常(NegativeValueException)。异常类中有一个带字符串参数的构造方法,将此参数赋给异常类中唯一的字符串数据成员,类中还定义一个方法,打印字符串数据成员。定义一个SubTwoNumber类,具有方法int subAB(),该方法会抛出NegativeValueException异常。最后给出测试类。import java.lang.Exception;class NegativeValueException extends ExceptionString message;NegativeValueException(String message)this.message=message;public String getMessage()return message;class SubTwoNumberint x;int y;int sub(int x,int y) throws NegativeValueExceptionif(x-y)0)throw new NegativeValueException(两数之差为负);return x-y;public class MyExceptionTest public static void main(String args)SubTwoNumber ts=new SubTwoNumber();trySystem.out.println(ts.sub(7,6 );/改变数值有不同结果catch(NegativeValueException e)System.out.println(e.getMessage();4、已经完成上面3题的同学练习书上6-2和6-3,通过查询API帮助文档理解程序。Java程序设计实验(NO.5)1、实验目的:了解java输入输出流的运用。从键盘接受数据,并将数据保存到文件中,文件名由键盘输入,当输入一行字符串为“end”时,程序停止接收键盘的数据。提示:用BufferedReader 包装标准输入流写文件可以用:PrintWriter(Writer out),例如:PrintWriter output=new PrintWriter(new FileWriter(file);或者PrintWriter(File file)有时间的话可以改进一下:在已经存在的文件中追加一些文本public FileWriter(File file,boolean append)或者BufferedWriterimport java.io.*;public class InputFromStdin public static Boolean inputfromStdinToFile(String filename) File file=new File(filename); PrintWriter writer=null; BufferedReader in=null; try writer=new PrintWriter(new FileWriter(file,true); System.out.println(begin input and end with end input); in=new BufferedReader(new InputStreamReader(System.in); String inputline=null; while(inputline=in.readLine()!=null)&(!inputline.equals(end input) writer.println(inputline); writer.flush(); writer.close(); return true; catch(IOException e) System.out.println(e.getMessage(); return false; finally if(in!=null) try in.close(); catch(IOException e) e.printStackTrace(); public static void main(String args) String filename=d:/temp.java; InputFromStdin.inputfromStdinToFile(filename);2、实验目的:简单GUI程序开发创建一个带有两个按钮的窗口,被标记为“Red”和“Green”,点击“Red”按钮,窗口(内容面板)的颜色从蓝色(或原来任意颜色)变为红色,点击“Green”按钮,变为绿色。要求用Swing中的类提示:用ActionListener,e.getActionCommand().equals()import javax.swing.*;import java.awt.*;import java.awt.event.*;public class ButtonDemo extends JFrame public static final int WIDTH=300;public static final
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 福建省住房和城乡建设厅科技创新平台管理办法(试行)
- 传染病宣传知识培训幼儿课件
- 2026届山西省霍州市煤电第一中学高三物理第一学期期末综合测试模拟试题
- 银行名单制管理办法
- 防火队内务管理办法
- 企业管理安全培训材料课件
- 有关巡察考试题库及答案
- 税收管理办法七十三条
- 2025年泌尿外科常见病例诊断与手术设计模拟测试卷答案及解析
- 乡村振兴与基层治理创新-洞察及研究
- 温度计的前世今生
- 产后骨盆修复培训课件
- 给排水检验批容量一览表
- 小区业主公约
- 化妆品配方与工艺(新)
- 初中综合实践课程活动教案5篇(初中综合实践活动课件)
- 旗袍英文介绍课件
- 模块二-化肥减施增效基础知识课件
- 咽喉科内镜诊疗技术管理规范
- 小学各项安全管理制度汇编
- 北京香格里拉饭店庭园环境设计
评论
0/150
提交评论