北科大JAVA上机二 2013.doc_第1页
北科大JAVA上机二 2013.doc_第2页
北科大JAVA上机二 2013.doc_第3页
北科大JAVA上机二 2013.doc_第4页
北科大JAVA上机二 2013.doc_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java程序设计上机作业(二)学号:姓名:上机作业要求:请根据题目的要求,将题目答案、程序源代码、运行结果截图统一填入本word文档中,并将本文档更名为“2012Java_学号_姓名_Lab2.doc”在第六周周日(4月7日)24:00之前以附件的形式发送到,邮件的题目设置为“2013Java_学号_姓名_上机(2)” 。请严格按照以上名称发送邮件,否则邮箱自动拒收,视为未交该次作业。Java程序设计上机题(二)1. 根据下面的要求编程实现复数类ComplexNumber。(1) 复数类ComplexNumber的属性realPart: 实部,代表复数的实数部分。imaginPart: 虚部,代表复数的虚数部分。(2) 复数类ComplexNumber的方法ComplexNumber( ):构造函数,将实部、虚部都置为0。ComplexNumber(double r, double i):构造函数,将实部和虚部分别置为r和i。getRealPart( ):获取复数对象的实部。getImaginaryPart( ):获取复数对象的虚部。setRealPart(double d): 把复数对象的实部设置为d。setImaginaryPart(double d): 把复数对象的虚部设置为plexAdd(ComplexNumber c):将当前对象与c相加,返回所得的结果。complexMinus(ComplexNumber c):将当前对象与形参复数对象相减,返回所得的结果。complexMulti(ComplexNumber c):将当前对象与形参复数对象相乘,返回所得的结果。complexSize():返回当前复数对象的大小,其值为 。toString( ):把当前复数对象的实部、虚部组合成 a + bi的字符串形式返回,其中a和b分别为实部和虚部的数据。在main()方法中创建几个复数类的对象,对以上方法进行调用,实现复数的加、减、乘、输出等操作。请将源程序文本填入下框:import java.text.DecimalFormat;public class ComplexNumber/实部,代表复数的实数部分private double realPart;/虚部,代表复数的虚数部分private double imaginPart;/获取复数对象的实部public double getRealPart() return realPart;/把复数对象的实部设置为dpublic void setRealPart(double d) this.realPart = d;/获取复数对象的虚部public double getImaginPart() return imaginPart;/把复数对象的虚部设置为dpublic void setImaginPart(double d) this.imaginPart = d;/构造函数,将实部、虚部都置为0public ComplexNumber()this.realPart=0;this.imaginPart=0;/构造函数,将实部和虚部分别置为r和ipublic ComplexNumber(double r, double i)this.realPart=r;this.imaginPart=i;/将当前对象与c相加,返回所得的结果public ComplexNumber complexAdd(ComplexNumber c)ComplexNumber temp=new ComplexNumber();temp.setRealPart(this.getRealPart()+c.getRealPart();temp.setImaginPart(this.getImaginPart()+c.getImaginPart();return temp;/将当前对象与形参复数对象相减,返回所得的结果public ComplexNumber complexMinus(ComplexNumber c)ComplexNumber temp=new ComplexNumber();temp.setRealPart(this.getRealPart()-c.getRealPart();temp.setImaginPart(this.getImaginPart()-c.getImaginPart();return temp;/将当前对象与形参复数对象相乘,返回所得的结果public ComplexNumber complexMulti(ComplexNumber c)ComplexNumber temp=new ComplexNumber();temp.setRealPart(this.getRealPart()*c.getRealPart()-this.getImaginPart()*c.getImaginPart();temp.setImaginPart(this.getImaginPart()*c.getRealPart()+c.getImaginPart()*this.getRealPart();return temp;/返回当前复数对象的大小 public double complexSize()return Math.sqrt(this.getRealPart()*this.getRealPart()+this.getImaginPart()*this.getImaginPart();/把当前复数对象的实部、虚部组合成 a + bi的字符串形式返回,其中a和b分别为实部和虚部的数据public String toString( )String tempR=null,tempI=null;if(this.getRealPart()%1.0=0)tempR=String.valueOf(long)this.getRealPart();if(this.getImaginPart()%1.0=0)if(this.getImaginPart()0)tempI=-+String.valueOf(long)(-this.getImaginPart()+i);if(-1i.equals(tempI)tempI=-i;elsetempI=+String.valueOf(long)(this.getImaginPart()+i);if(+1i.equals(tempI)tempI=+i;if(+0i.equals(tempI)tempI=null;return tempR+tempI; public static void main(String args) / TODO Auto-generated method stubComplexNumber cn1=new ComplexNumber(2,3);ComplexNumber cn2=new ComplexNumber(-3,1);DecimalFormat dcmFmt = new DecimalFormat(0.00);System.out.print(负数一:+cn1.toString();System.out.println(t其模为:+dcmFmt.format(plexSize();System.out.println(负数二:+cn2.toString();System.out.println(两数和:+plexAdd(cn2).toString();System.out.println(两数差:+plexMinus(cn2).toString();System.out.println(两数积:+plexMulti(cn2).toString();运行结果截图:2. 设计一个学生类Student,其属性有name、age和Grade(年级)。由Student类派生出本科生类Undergraduate和研究生类Graduate,Undergraduate类增加属性specialty(专业),研究生类增加属性direction(研究方向)。每个类都有构造函数和print方法来输出属性信息。请尽量使用this和super关键字。创建多个各类的对象,并逐一输出其属性信息。请将源程序文本填入下框:package justPrac;public class DifferientStudents public static void main(String args) / TODO Auto-generated method stubStudent st1=new Undergraduate(张三,19,2,计算机科学与技术);Student st2=new Graduate(李四,23,2,云计算);System.out.println(姓名t年龄t年级t专业/研究方向);st1.print();st2.print();/学生类class Studentpublic String getName() return name;public void setName(String name) = name;public int getAge() return age;public void setAge(int age) this.age = age;public int getGrade() return grade;public void setGrade(int grade) this.grade = grade;private String name;private int age;private int grade;/打印信息public void print()/本科生类class Undergraduate extends Studentpublic String getSpecialty() return specialty;public void setSpecialty(String specialty) this.specialty = specialty;/专业private String specialty;/构造函数public Undergraduate(String name,int age,int grade,String specialty)super.setName(name);super.setAge(age);super.setGrade(grade);this.setSpecialty(specialty);public void print()System.out.println(this.getName()+t+this.getAge()+t大 +this.getGrade()+t+this.getSpecialty();/研究生类class Graduate extends Studentpublic String getDirection() return direction;public void setDirection(String direction) this.direction = direction;/研究方向private String direction; /构造函数public Graduate(String name,int age,int grade,String direction)super.setName(name);super.setAge(age);super.setGrade(grade);this.setDirection(direction);public void print()System.out.println(this.getName()+t+this.getAge()+t研 +this.getGrade()+t+this.getDirection();运行结果截图:3. 设计一个抽象类CompareObject,里面有抽象方法CompareTo用于比较两个对象;然后设计一个CompareObject类的子类Position,有属性x、y、z分别表示其坐标位置,Position类实现CompareTo方法,用于比较两个对象距离原点(0,0,0)的距离之差。创建Position类的对象,对以上方法进行调用。请将源程序文本填入下框:package justPrac;import java.text.DecimalFormat;public abstract class CompareObject/* * param args */public static void main(String args) / TODO Auto-generated method stubPosition co1=new Position(1,2,3);Position co2=new Position(3,5,7);DecimalFormat dcmFmt = new DecimalFormat(0.00);System.out.println(dcmFmt.format(pareTo(co2);public abstract double compareTo(Position p);class Position extends CompareObjectpublic double getX() return x;public void setX(double x) this.x = x;public double getY() return y;public void setY(double y) this.y = y;public double getZ() return z;public void setZ(double z) this.z = z;private double x;private double y;private double z;public Position(double x,double y,double z)this.x=x;this.y=y;this.z=z;public double compareTo(Position p)double dist1=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);double dist2=Math.sqrt(p.x*p.x+p.y*p.y+p.z*p.z);return dist1-dist2;运行结果截图:4. 将上题中的比较功能改用接口来实现。请将接口定义源程序文本填入下框:interface CompareObject2public abstract double compareTo(Position2 p);请将Position类定义源程序文本填入下框: public class Position2 implements CompareObject2public double getX() return x;public void setX(double x) this.x = x;public double getY() return y;public void setY(double y) this.y = y;public double getZ() return z;public void setZ(double z) this.z = z;private double x;private double y;private double z;public Position2(double x,double y,double z)this.x=x;this.y=y;this.z=z;public double compareTo(Position2 p)double dist1=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);double dist2=Math.sqrt(p.x*p.x+p.y*p.y+p.z*p.z);return dist1-dist2;public static void main(String args) Position2 co1=new Position2(3,4,5);Position2 co2=new Position2(0,1,2);DecimalFormat dcmFmt = new DecimalFormat(0.00);System.out.println(dcmFmt.format(pareTo(co2);运行结果截图:5. 学校中有教师和学生两类人,而在职研究生既是教师又是学生。设计两个接口StudentInterface和TeacherInterface。其中StudentInterface接口包括setFee和getFee方法,分别用于设置和获取学生的学费;TeacherInterface接口包括setSalary和getSalary方法,分别用于设置和获取教师的工资。定义一个在职研究生类Graduate,实现上述两个接口,属性有name、sex、age、fee(学费)和salary(工资)。创建5个在职研究生类对象(各个属性自己设定,应覆盖多种情况),统计每个人的年收入和学费;如果收入减去学费不足2000元,在屏幕上给出提示。请将接口定义源程序文本填入下框:interface StudentInterfacepublic void setFee(double fee);public double getFee();interface TeacherInterfacepublic void setSalary(double salary);public double getSalary();请将源程序文本填入下框:class Graduate2 implements StudentInterface,TeacherInterfacepublic String getName() return name;public void setName(String name) = name;public String getSex() return sex;public void setSex(String sex) this.sex = sex;public int getAge() return age;public void setAge(int age) this.age = age;private String name;private String sex;private int age;private double fee;private double salary;public Graduate2(String name,String sex,int age)=name;this.sex=sex;this.age=age;Overridepublic void setSalary(double salary) / TODO Auto-generated method stubthis.salary=salary;Overridepublic double getSalary() / TODO Auto-generated method stubreturn this.salary;Overridepublic void setFee(double fee) / TODO Auto-generated method stubthis.fee=fee;Overridepublic double getFee() / TODO Auto-generated method stubreturn this.fee;public class StudentsAndTeachers /* * param args */public static void main(String args) / TODO Auto-generated method stubGraduate2 graduates=new Graduate25;graduates0=new Graduate2(丁一,男,23);graduates1=new Graduate2(牛二,女,22);graduates2=new Graduate2(张三,男,23);graduates3=new Graduate2(李四,女,24);graduates4=new Graduate2(王五,男,23);graduates0.setFee(5000);graduates0.setSalary(12000);graduates1.setFee(5000);graduates1.setSalary(5000);graduates2.setFee(5000);graduates2.setSalary(7000);graduates3.setFee(5000);graduates3.setSalary(6999);graduates4.setFee(5000);graduates4.setSalary(8000);System.out.println(编号t姓名t性别t年龄t学费t工资);for(int i=0;igraduates.length;i+)System.out.print(i+1)+t+graduatesi.getName()+t+graduatesi.getSex() +t+graduatesi.getAge()+t+graduatesi.getFee() +t+graduatesi.getSalary();if(graduatesi.getSalary()-graduatesi.getFee()2000)System.out.print(t收入减去学费不足2000元!);System.out.println();运行结果截图:6. 请设计一个合适的正则表达式来描述3位区号、8位分机号的电话号码,如(010)6233293101062332931。给定字符串str = 咨询电话(01088814653、010)88816888(1)全费学员赠送教材,插班生不赠送教材。国家法定假日正常休息,不另安排补课。(4)全日制班、双休日班自开课一周内可办理调班、退费手续;寒暑班均为开课当日内办理调班、退费手续;已过退费期的插班生一律不予办理调班、退费手续。(5)办理调班、退费手续时所赠教材收回,如有写画或损坏照价付费。(6)咨询电话01088814653、(010)88816888 邮箱:请编写一个程序,从以上字符串查找符合以上正则表达式的字串并输出到屏幕上。请将表示电话号码的正则表达式填入下框:请将源程序文本填入下框:import java.util.regex.*;public class ZhengZeBiaoDaShi /* * param args */public static void main(String args)/ TODO Auto-generated method stubPattern pattern=Ppile(?d3)?-?d8);String s=咨询电话(010)88814653、01088816888(1)全费学员赠送教材,插班生不赠送教材。国家法定假日正常休息,不另安排补课。(4)全日制班、双休日班自开课一周内可办理调班、退费手续;寒暑班均为开课当日内办理调班、退费手续;已过退费期的插班生一律不予办理调班、退费手续。(5)办理调班、退费手续时所赠教材收回,如有写画或损坏照价付费。(6)咨询电话01088814653、(010)88816888 邮箱:;Matcher matcher=pattern.matcher(s);while(matcher.find()String temp = matcher.group();System.out.println(temp);运行结果截图:7. 编写一个程序,从命令行输入两个整数,用第一个数除以第二个数(结果为整数),打印出运算结果。如果命令行没有输入参数或参数不足,引用args则会产生ArrayIndexOutOfBoundsException异常;如果输入的参数不是数字,则在转换参数格式时会产生NumberFormatException异常;如果第二个参数为0,则会产生ArithmeticException异常,编写程序对这些异常进行捕获并打印出相应的信息。请将源程序文本填入下框:import java.util.Scanner;public class Exception1 /* * param args */public static void main(String args) / TODO Auto-generated method stubint i=1;while(i=4)tryargs=new String2;Scanner scanner=new Scanner(System.in);System.out.print(被除数);args0=scanner.nextLine();System.out.print(除数);args1=scanner.nextLine();if(args0.equals()|args1.equals()throw new ArrayIndexOutOfBoundsException(没有参数或参数不足!);int a=Integer.parseInt(args0);int b=Integer.parseInt(args1);System.out.print(运算结果);System.out.println(a/b);catch(NumberFormatException nfe)System.out.println(字符串转整形时发生异常!);catch(ArithmeticException ae)System.out.println(算数异常!);运行结果截图(多次运行,粘贴多个截图):8. 定义异常类AgeException,用属性message表示年龄异常,该类具有构造方法和getMessage方法;定义People类,有私有属性age,和getAge、setAge方法,其中setAge方法有int型参数,当参数值小于0或大于120时抛出异常;定义一个测试类Test,在该类的main方法中People类的对象,多次调用setAge方法设置a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论