最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

JAVA复习题答案2

来源:动视网 责编:小OO 时间:2025-09-29 02:42:36
文档

JAVA复习题答案2

(三)编程题1.声明复数类,成员变量包括实部和虚部,成员方法包括实现复数加法、减法操作。创建运行程序类,测试银行账户类。publicclassComplex{privateintreal,imag;Complex(intreal,intimag){this.real=real;this.imag=imag;}Complex(Complexcomp){this(comp.real,comp.imag);}Complex(){this(0,0);}publicStringadd(Complexco
推荐度:
导读(三)编程题1.声明复数类,成员变量包括实部和虚部,成员方法包括实现复数加法、减法操作。创建运行程序类,测试银行账户类。publicclassComplex{privateintreal,imag;Complex(intreal,intimag){this.real=real;this.imag=imag;}Complex(Complexcomp){this(comp.real,comp.imag);}Complex(){this(0,0);}publicStringadd(Complexco
(三)编程题

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                    table[i]= Double.parseDouble(str[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 {

        }

    }

}

文档

JAVA复习题答案2

(三)编程题1.声明复数类,成员变量包括实部和虚部,成员方法包括实现复数加法、减法操作。创建运行程序类,测试银行账户类。publicclassComplex{privateintreal,imag;Complex(intreal,intimag){this.real=real;this.imag=imag;}Complex(Complexcomp){this(comp.real,comp.imag);}Complex(){this(0,0);}publicStringadd(Complexco
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top