第二次java作业(3-5)和实验(3-14)参考答案.doc_第1页
第二次java作业(3-5)和实验(3-14)参考答案.doc_第2页
第二次java作业(3-5)和实验(3-14)参考答案.doc_第3页
第二次java作业(3-5)和实验(3-14)参考答案.doc_第4页
第二次java作业(3-5)和实验(3-14)参考答案.doc_第5页
已阅读5页,还剩51页未读 继续免费阅读

下载本文档

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

文档简介

作业第三章 Java面向对象1、为某研究所编写一个通用程序,用来计算每一种交通工具运行1000公里所需的时间,已知每种交通工具的参数都是3个整数A、B、C的表达式。现有两种工具:Car007 和Plane,其中Car007 的速度运算公式为:A*B/C,Plane 的速度运算公式为:A+B+C。需要编写三类:ComputeTime.java, Plane.java, Car007.java和接口Common.java,要求在未来如果增加第3种交通工具的时候,不必修改以前的任何程序,只需要编写新的交通工具的程序。其运行过程如下,从命令行输入ComputeTime的四个参数,第一个是交通工具的类型,第二、三、四个参数分别时整数A、B、C,举例如下: 计算Plane的时间:java ComputeTime Plane 20 30 40 计算Car007的时间:java ComputeTime Car007 23 34 45 如果第3种交通工具为Ship, 则只需要编写Ship.java,运行时输入:java ComputeTime Ship 22 33 44提示:充分利用接口的概念,接口对象充当参数。实例化一个对象的另外一种办法:Class.forName(str).newInstance();例如需要实例化一个Plane对象的话,则只要调用Class.forName(Plane).newInstance()便可。访到classpath 路径下即可,请从下往上编译 目录结构 CalTime -|- | | | | vehicle ComputTime.java | - | | | | all Palne.java /Car.java | | Common.java 1. ComputTime.java 请确保输入正确,其中没有捕捉NumberFromatException import CalTime.vehicle.all.Common; import java.lang.*;public class ComputeTime public static void main(String args) System.out.println(交通工具: +args0); System.out.println( 参数A: +args1); System.out.println( 参数B: +args2); System.out.println( 参数C: +args3); double A=Double.parseDouble(args1); double B=Double.parseDouble(args2); double C=Double.parseDouble(args3); double v,t; tryCommon d=(Common) Class.forName(CalTime.vehicle.+args0).newInstance();/ CalTime.vehicle是对应的包名 v=d.runTimer(A,B,C); t=1000/v; System.out.println(平均速度: +v+ km/h); System.out.println(运行时间:+t+ 小时); catch(Exception e) System.out.println(class not found); 2.Plane.java package CalTime.vehicle; import CalTime.vehicle.all.Common;public class Plane implements Commonpublic double runTimer(double a, double b, double c) return (a+ b + c); 3. Car.java package CalTime.vehicle; import CalTime.vehicle.all.Common;public class Car implements Common public double runTimer(double a, double b, double c) return ( a*b/c ); 4.Common.java package CalTime.vehicle.all;public interface Common double runTimer(double a, double b, double c); 2、编写一个学生类 Student ,要求: (1) 学生类 Student 属性有: id : long型,代表学号 name : String类对象,代表姓名 age : int型,代表年龄 sex : boolen型,代表性别(其中:true表示男,false表示女) phone : String类对象,代表联系电话 (2) 学生类 Student的方法有: Student(long i , String n , int a , boolean s , long p) : 有参构造函数,形参表中的参数分别初始化学号、姓名、年龄、性别和联系电话。 int getAge() ( ) : 获取年龄作为方法的返回值。 boolean getSex( ) ( ) : 获取性别作为方法的返回值。 long getPhone ( ) : 获取联系电话作为方法的返回值。 public String toString( ) : 以 姓名:联系电话 的形式作为方法的返回值。public class test_S3_2public static void main(Stringargs) Student student = new Student(01,李明,20,true,13xxx); System.out.println(student.getAge(); System.out.println(student.getPhone(); System.out.println(student.toString(); class Studentpublic long id; public String name; public int age; public boolean sex; public String phone; public Student(long i,String n,int a,boolean s,String p) this.id = i; = n; this.age = a; this.sex = s; this.phone = p; public int getAge() return this.age; public boolean getSex() return this.sex; public String getPhone() return this.phone; public String toString() return + : + this.phone; 3、利用接口编写三角形、矩形的面积和周长的程序。import java.awt.image.renderable.RenderableImageOp;public class S3_3public static void main(String args)Rectangle rectangle = new Rectangle(10, 8);System.out.println(矩形面积:+rectangle.calArea();System.out.println(矩形周长:+rectangle.calPerimeter();Triangle triangle = new Triangle(3, 4, 5);System.out.println(三角形面积:+triangle.calArea();System.out.println(三角形周长:+triangle.calPerimeter();class Rectangle implements Caldouble longth;double width;public Rectangle(double longth, double width)this.longth = longth;this.width = width;public double calArea()return this.longth * this.width;Overridepublic double calPerimeter()return (this.longth + this.width) * 2;class Triangle implements Caldouble a;double b;double c;public Triangle(double a, double b, double c)this.a = a;this.b = b;this.c = c;public double calArea()double p = (a+b+c)/2;return Math.sqrt(p*(p-a)*(p-b)*(p-c);public double calPerimeter()return (a+b+c);4、编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如我ABC4,应该截为我AB,输入我ABC汉DEF,6,应该输出为我ABC而不是我ABC+汉的半个。public class Test3 public static void main(String args) String srcStr1 = 我ABC; String srcStr2 = 我ABC汉DEF; splitString(srcStr1, 4); splitString(srcStr2, 6); public static void splitString(String src, int len) int byteNum = 0; if (null = src) System.out.println(The source String is null!); return; byteNum = src.length(); byte bt = src.getBytes(); / 将String转换成byte字节数组 if (len byteNum) len = byteNum; / 判断是否出现了截半,截半的话字节对于的ASC码是小于0的值 if (btlen 0) String subStrx = new String(bt, 0, -len); System.out.println(subStrx= + subStrx); else String subStrx = new String(bt, 0, len); System.out.println(subStrx= + subStrx); 第四章 Java异常处理1、 写程序运行结果:public class A static int some() try System.out.println(try); return 1; finally System.out.println(finally); public static void main(String arg) System.out.println(some(); tryfinally12、先从键盘输入一个十六进制数,再将其转化为十进制数,然后输出。若输入的不是一个有效的十六进制数,则抛出异常。import java.util.InputMismatchException;import java.util.Scanner;public class S4_2public static void main(String args)System.out.println(请输入一个十六进制的数:);Scanner cin = new Scanner(System.in);trywhile(cin.hasNext()int val = cin.nextInt(16);System.out.print(转化为十进制为:);System.out.println(val);System.out.println(请输入一个十六进制的数:);catch(InputMismatchException e)e.printStackTrace();3、先编写一个方法,它将格式为“yyyy/mm/dd”形式的日期字符串转化为日期对象。若日期字符串不符合以上规定,则抛出异常。再在main方法中对正常和异常输入的日期字符串分别进行验证,并输出转换后的日期对象。import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;public class S4_3public static void main(String args) throws ParseExceptionSystem.out.println(Please input a date, by the format like this 2013/9/1 :);Scanner scanner = new Scanner(System.in);String temp = scanner.nextLine();trySystem.out.println(getDate(temp); catch (ParseException e)e.printStackTrace();throw e;public static Date getDate(String date) throws ParseExceptionDateFormat format = new SimpleDateFormat(yyyy/MM/dd);try return new Date(format.parse(date).getTime(); catch (ParseException e) e.printStackTrace();throw e;第五章 Java常见类1、设有一个由10个英文单词构成的字符串数组,要求:(1) 统计以字母“w”开头的单词数;(2) 统计单词中含“or”字符串的单词数;(3) 统计长度为3的单词数。public class S5_1public static void main(String args)String string = hello,girl,boy,word,world,sword,for,hi,hunter,lore;int countW = 0;int countOr = 0;int countLentghEqual3 = 0;for (int i = 0; i string.length; i+)if (stringi.substring(0, 1).equals(w)countW+;if (stringi.indexOf(or) != -1)countOr+;if (stringi.length() = 3)countLentghEqual3+;System.out.println(以字母w开头的单词数:+countW);System.out.println(单词中含or字符串的单词数:+countOr);System.out.println(长度为3的单词数:+countLentghEqual3);2、利用随机函数产生20个1090之间的不重复整数, 将这些数拼接在一个字符串中, 用逗号隔开(每产生一个新数, 要保证在该串中不存在)。然后将字符串中的整数分离存放到一个数组中,将数组的内容按由小到大的顺序输出。 import javax.naming.ldap.SortControl;public class S5_2public static void main(String args)StringBuffer stringBuffer = new StringBuffer();String string;int a = new int20;stringBuffer = generateInt();string = stringBuffer.toString();System.out.println(string);a = intoArray(string);a = sort(a);for (int i = 0; i 20; i+)System.out.print(ai+,);static StringBuffer generateInt()int integer;StringBuffer string = new StringBuffer();String tempString;for (int i = 0; i 20; i+)integer = (int)(Math.random()*90);if (integer 10)i-;else if (string.indexOf(Integer.toString(integer) != -1) i-;else tempString = Integer.toString(integer);string.append(tempString+,);return string;static int intoArray(String string)int a = new int20;String tempString;for (int i = 0; i a.length; i+)tempString = string.substring(3*i, 3*i+2);ai = Integer.parseInt(tempString);return a;static int sort(int a)int tempInt;for (int i = 0; i a.length-1; i+)for (int j = i+1; j aj)tempInt = ai;ai = aj;aj = tempInt;return a;3、先任意输入不超过10个人的姓名和电话到电话簿,然后从电话簿中查询指定人的电话。import java.util.Scanner;public class S5_3public static void main(String args)String person = new String32;String searchName;String phone;Scanner myInput = new Scanner(System.in);person = input(person);System.out.println(请输入要查询的姓名:);searchName = myInput.next();phone = searchPhone(person, searchName);static String input(String person)Scanner myInput = new Scanner(System.in);for (int i = 0; i person.length; i+)System.out.println(请输入第+(i+1)+个人的姓名:);personi0 = myInput.next();System.out.println(请输入该人的电话:);personi1 = myInput.next();return person;static String searchPhone(String person, String name)String phone = null;boolean flag = false;for (int i = 0; i person.length; i+)if (personi0.equals(name)phone = personi1;System.out.println(查询的号码是:+phone);flag = true;if (!flag)System.out.println(搜索无果);return phone;4、编写一个日历,按照格式yyyy-MM-dd输入一个指定日期,可以显示当月的日历,该指定日期前面加*以示是你输入的日期。package cn.zjy.oop.date;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Scanner;public class CalendarFinalpublic static void main(String args)System.out.println(Please input a date, by the format like this 2013-9-1 :);Scanner scanner = new Scanner(System.in);String temp = scanner.nextLine();DateFormat format = new SimpleDateFormat(yyyy-MM-dd);tryDate date = format.parse(temp);/计算时间Calendar calendar = new GregorianCalendar();calendar.setTime(date);/时间设置进去/时间设置进去之后才可以进行下面的运算int day = calendar.get(Calendar.DATE);/这个月的第几天/考虑进行计算calendar.set(Calendar.DATE, 1);/日期从1号打印,日期变为1号System.out.println(日t一t二t三t四t五t六);for (int i = 0; i calendar.get(Calendar.DAY_OF_WEEK)-1; i+)System.out.print(t);for (int i = 1; i b?a:b;max2=cd?c:d;max=max1max2?max1:max2;System.out.println(最大值是+max);评语: 日期: 年 月 日实验05:输入/输出处理 实验目的:掌握输入输出程序的处理。实验数据记录及分析(或程序及运行结果)1. 编写程序,从键盘上输入一个整数作为圆的半径,分别计算并输出圆周长、圆面积。 public class S05_1 public static void main(String args) double pi=Math.PI; System.out.println(请输入你要求的圆的半径:); Scanner scanner = new Scanner( System.in ); String a=scanner.next(); int b=Integer.parseInt(a); System.out.println(圆的周长为:+b*2*pi); System.out.println(圆的面积为:+b*b*pi); 2. 编写程序,从键盘上输入三个实数,比较大小,分别输出最大值和最小值。public class S05_2 public static void main(String args)int a = new int3;a = input_3();a = rankUp(a);System.out.println(最大值是+a2);System.out.println(最小值是+a0);static int input_3()int a = new int3;String string;for (int i = 0; i 3; i+) System.out.println(输入一个整数);Scanner scanner = new Scanner(System.in);string = scanner.next();ai = Integer.parseInt(string);return a;static int rankUp(int a)int temp;for (int i = 0; i a.length-1; i+) for (int j = i+1; j aj)temp = ai;ai = aj;aj = temp;return a;3. 运行下面的程序,写出运行结果,解释每种格式的含义。import java.util.Calendar;public class CommandParameter public static void main(String args) long n = 461012; System.out.printf(%d%n, n); System.out.printf(%08d%n, n); System.out.printf(%+8d%n, n); System.out.printf(%,8d%n, n); System.out.printf(%+,8d%n%n, n); double pi = Math.PI; System.out.printf(%f%n, pi);System.out.printf(%.3f%n, pi); System.out.printf(%10.3f%n, pi); System.out.printf(%-10.3f%n, pi); Calendar c = Calendar.getInstance(); System.out.printf(%tB %te, %tY%n, c, c, c);System.out.printf(%tl:%tM %tp%n, c, c, c);System.out.printf(%tD%n, c); 运行结果为:46101200461012 +461012 461,012+461,0123.1415933.142 3.1423.142 十一月 4, 20134:15 下午11/04/13评语: 日期: 年 月 日实验06:流程控制(一)实验目的:1. 能够正确使用if,switch语句,并且能正确使用合法的参数类型。 2. 在不同情况下,能够正确选择使用循环语句,能正确使用break,continue,能计算在循环中或循环后循环计数器的值。 实验数据记录及分析(或程序及运行结果)1. 编写程序,声明一个double型变量,并任意赋值,然后判断该书是否在1到10000之间,根据结果输出下面其中一行结果(x需用实际的数值代替):The number x is between 1 and 10000.The number x is not between 1 and 10000.public class S06_1 public static void main(String args) System.out.println(请输入一个double型变量:);Scanner scanner = new Scanner(System.in);double b = scanner.nextDouble();if (b= 1 & b = 10000)System.out.println(The number x is between 1 and 10000.);elseSystem.ou

温馨提示

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

评论

0/150

提交评论