1. 声明复数类,成员变量包括实部和虚部,成员方法包括实现复数加法、减法操作。创建运行程序类,测试银行账户类。
public class Complex {
private int real,imag;
Complex(int real,int imag){
this.real=real;
this.imag=imag;
}
Complex(Complex comp){
this(comp.real,comp.imag);
}
Complex(){
this(0,0);
}
public String add(Complex comp1,Complex comp2){
this.real=comp1.real+comp2.real;
this.imag=comp1.imag+comp2.imag;
return toString();
}
public String subtract(Complex comp1,Complex comp2){
this.real=comp2.real-comp1.real;
this.imag=comp2.imag-comp1.imag;
return toString();
}
public String toString(){
return "("+this.real+"+"+this.imag+"i)";
}
public static void main(String[] args) {
Complex comp1=new Complex(1,2);
Complex comp2=new Complex(2,3);
Complex result_comp=new Complex();
result_comp.add(comp1,comp2);
System.out.println(comp1+"+"+comp2+"="+result_comp);
result_comp.subtract(comp1, comp2);
System.out.println(comp2+"-"+comp1+"="+result_comp);
}
}
2. 采用面向对象的程序设计方法编写一个计算矩形和椭圆形面积的程序。要求:定义面积接口以增强程序的扩展性。
public interface Area {
public abstract double area();
}
public class Rectangle implements Area {
protected double length, width; // 长度和宽度
Rectangle(double length, double width)// 构造方法
{
this.length = length;
this.width = width;
}
public double area() // 计算矩形面积,实现Area接口中的抽象方法
{
return this.width * this.length;
}
public String toString() {
return "一个矩形,长度" + this.length + ",宽度" + this.width + ",面积为"
+ this.area();
}
public static void main(String args[]) {
System.out.println(new Rectangle(10, 20).toString());
}
}
public class Ellipse implements Area {
private int x, y;
Ellipse(int x, int y) {
this.x = x;
this.y = y;
}
public double area() {
// TODO Auto-generated method stub
return Math.PI * x * y;
}
public static void main(String args[]) {
Ellipse e = new Ellipse(20, 10);
System.out.print("椭圆的长轴为:" + e.x + ",椭圆的短轴为:" + e.y + ",椭圆的面积为:" + e.area());
}
}
3. 声明一个Average接口,其中约定求平均值的方法;声明多个类实现Average接口,分别给出求平均值的方法实现。并创建运行程序类。
(1) 全部数值相加后求平均值
(2) 去掉一个最高分和一个最低分后,再将总分求平均值
(3) 使用命令行参数作为输入数据,对于不能转换成数值的字符串进行异常处理。
public interface Average {
public abstract double average(String[] table);
}
class AverageAll implements Average {
public double average(double table[]) {
double average = 0.0;
if (table != null && table.length > 0) {
double sum = 0.0;
for (int i = 0; i < table.length; i++) {
sum += table[i];
}
average = sum / table.length;
}
return average;
}
public double[] todoubleArray(String str[]) throws NumberFormatException {
if (str != null && str.length > 0) {
double table[] = new double[str.length];
for(int i=0;i } return table; } return null; } } class AverageExceptMaxMin implements Average { public double average(double table[]) { double average = 0.0; if (table != null && table.length > 2) { double max = table[0]; double min = table[0]; double sum = 0.0; for (int i = 0; i < table.length; i++) { if (table[i] < min) { min = table[i]; } if (table[i] > max) { max = table[i]; } sum += table[i]; } average = (sum - min - max) / (table.length - 2); } return average; } } public class TestAverage { /** * @param args */ public static void main(String[] args) { double[] tab = null; AverageAll avg = new AverageAll(); System.out.println(avg.average(tab)); AverageExceptMaxMin avgMaxMin = new AverageExceptMaxMin(); System.out.println(avgMaxMin.average(tab)); try{ System.out.println(avg.average(avg.todoubleArray(args))); }catch(NumberFormatException e){ System.out.print("字符串不能转化成浮点数"); } } } 4. 编写一个Java应用程序,要求:(1) 从命令行获取两个int型数据,针对这两个数据求和,并且输出结果。(2) 考虑异常的捕获和处理。 public class Customeradd { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.print(a + b); } catch (NumberFormatException nfe) { System.out.print("不能转换成整数,请重新输入!"); } finally { } } }