作业
第三章 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的时间:\ 计算Car007的时间:\
如果第3种交通工具为Ship, 则只需要编写Ship.java,运行时输入:\22 33 44\
提示:充分利用接口的概念,接口对象充当参数。
实例化一个对象的另外一种办法:Class.forName(str).newInstance();例如需要实例化一个Plane对象的话,则只要调用Class.forName(\便可。 访到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(\交通工具: \ System.out.println(\参数A: \
System.out.println(\参数B: \System.out.println(\参数C: \double A=Double.parseDouble(args[1]); double B=Double.parseDouble(args[2]); double C=Double.parseDouble(args[3]); double v,t; try {
Common d=(Common) Class.forName(\+args[0]).newInstance(); // CalTime.vehicle是对应的包名 v=d.runTimer(A,B,C); t=1000/v;
System.out.println(\平均速度: \ System.out.println(\运行时间:\小时\}
catch(Exception e) { System.out.println(\}
} }
2.Plane.java
package CalTime.vehicle;
import CalTime.vehicle.all.Common;
public class Plane implements Common {
public 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_2 { public static void main(String[]args) { Student student = new Student(01,\李明\ System.out.println(student.getAge()); System.out.println(student.getPhone()); System.out.println(student.toString()); } }
class Student { public 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; this.name = 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.name+ \ }
3、利用接口编写三角形、矩形的面积和周长的程序。 import java.awt.image.renderable.RenderableImageOp; public class S3_3 { public static void main(String[] args) { Rectangle rectangle = new Rectangle(10, 8);
System.out.println(\矩形面积:\
System.out.println(\矩形周长:\Triangle triangle = new Triangle(3, 4, 5);
System.out.println(\三角形面积:\ System.out.println(\三角形周长:\ } }
class Rectangle implements Cal { double longth; double width; public Rectangle(double longth, double width) { this.longth = longth; this.width = width; } public double calArea() { return this.longth * this.width; } @Override public double calPerimeter() { return (this.longth + this.width) * 2; } }
class Triangle implements Cal { double 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、编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如\我ABC\,应该截为\我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(\ return; }
byteNum = src.length();
byte bt[] = src.getBytes(); // 将String转换成byte字节数组
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库第二次java作业(3-5)和实验(3-14)参考答案 - 图文在线全文阅读。
相关推荐: