• 【匠心之作】三道题带你简单复习C++和Java


    前言

    前段时间有一个面试要考Java,在翻复习资料时,发现了三道题(谢谢张老师)基本上概括了一个语言的常用语法。特此记录,围绕这三道题用不同的语言编写,以便后续进行快速地复习。如果想要详细复习,请看C/C++要点复习

    问题一

    问题描述

    请从控制台输入一个三角形的三条边长,并判断这三条边能不能构成一个三角形。

    如能构成,请打印输出这个三角形的周长(要求输出宽度为8,保留两位小数),如不能构成,请打印输出具体提示。

    考察点:输入输出if条件判断

    Java

    public class TestTriangle {
        public static void main(String[] args) {
            double []a = new double[3];
            Scanner scanner = new Scanner(System.in);
            for (int i = 0; i < 3; i++) {
                System.out.print("第"+ (i+1) + "条边的边长:");
                a[i] = scanner.nextDouble();
            }
            Arrays.sort(a);
            if (a[0] + a[1] > a[2]) {
                double c = a[0]+a[1]+a[2];
                System.out.printf("三角形的周长为:%8.2f\n", c);
            } else {
                System.out.println("无法构成三角形");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    C++

    #include 
    #include 
    using namespace std;
    
    int main() {
    	double a[3];
    
    	for (int i = 0; i < 3; i++) {
    		cout << "第" << i + 1 << "条边的边长:";
    		cin >> a[i];
    	}
    	sort(a, a + 3);
    	if (a[0] + a[1] > a[2]) {
    		double c = a[0] + a[1] + a[2];
    		printf("三角形的周长为: % 8.2f\n", c);
    	}
    	else {
    		printf("无法构成三角形");
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    问题二

    题目描述

    编写一个ZPoint类,其中包括:

    • 两个私有成员double型变量x、y
    • 一个构造方法ZPoint(double x,double y)
    • 两个访问器分别访问x和y
    • 一个静态方法distance(ZPoint p,ZPoint q)计算两点p、q的距离
    • 一个实例方法distance(double x,double y)计算形参定义的点与当前点对象之间的距离。

    在此基础上再编写一个测试类Test,用ZPoint实例化两个点对象(11,9)和(100,21.5),然后打印输出这两个点对象之间的距离,此外打印输出点对象(11,9)与x=71,y=19所定义的点的距离。

    考察点:类的定义与创建实例方法静态方法

    Java

    ZPoint.java

    public class ZPoint {
        private double x;
        private double y;
    
        public ZPoint(double x, double y) {
            this.x = x;
            this.y = y;
        }
    
        public double getX() {
            return x;
        }
    
        public double getY() {
            return y;
        }
    
        public static double distance(ZPoint p, ZPoint q) {
            double disX = Math.abs(p.getX() - q.getX());
            double disY = Math.abs(p.getY() - q.getY());
            double dis = Math.sqrt(disX*disX + disY*disY);
            return dis;
        }
    
        public double distance(double x, double y) {
            double disX = Math.abs(this.x - x);
            double disY = Math.abs(this.y - y);
            double dis = Math.sqrt(disX*disX + disY*disY);
            return dis;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    Test.java

    public class Test {
        public static void main(String[] args) {
            ZPoint p1 = new ZPoint(11, 9);
            ZPoint p2 = new ZPoint(100, 21.5);
            System.out.println("两个点对象之间的距离为:");
            System.out.println(ZPoint.distance(p1, p2));
            System.out.println("点对象(11,9)与x=71,y=19所定义的点的距离为:");
            System.out.println(p1.distance(71,19));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    C++

    当在类的外部定义静态成员时,不能重复static关键字
    该关键字只能出现在类内部的声明语句

    ZPoint.h

    #ifndef ZPOINT_H
    #define ZPOINT_H
    #include 
    using namespace std;
    
    class ZPoint
    {
    public:
    	ZPoint(double x, double y);
    	double getX();
    	double getY();
    	double distance(double x, double y);
    	static double distance(ZPoint& p, ZPoint& q);
    private:
    	double x;
    	double y;
    };
    
    #endif 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ZPoint.cpp

    #include "ZPoint.h"
    #include 
    
    ZPoint::ZPoint(double x, double y) {
    	this->x = x;
    	this->y = y;
    }
    
    double ZPoint::getX() {
    	return this->x;
    }
    
    double ZPoint::getY() {
    	return this->y;
    }
    
    double ZPoint::distance(double x, double y) {
    	double disX = abs(this->x - x);
    	double disY = abs(this->y - y);
    	double dis = sqrt(disX * disX + disY * disY);
    	return dis;
    }
    
    // 当在类的外部定义静态成员时,不能重复static关键字
    // 该关键字只能出现在类内部的声明语句
    double ZPoint::distance(ZPoint& p, ZPoint& q) {
    	double disX = abs(p.getX() - q.getX());
    	double disY = abs(p.getY() - q.getY());
    	double dis = sqrt(disX * disX + disY * disY);
    	return dis;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    Test.cpp

    #include 
    #include "ZPoint.h"
    
    int main() {
    	ZPoint p1(11, 9);
    	ZPoint p2(100, 21.5);
    	cout << "两个点对象之间的距离为:" << endl;
    	cout << ZPoint::distance(p1, p2) << endl;
    	cout << "点对象(11,9)与x=71,y=19所定义的点的距离为:" << endl;
    	cout << p1.distance(71, 19) << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    问题三

    问题描述

    请运用面向对象程序设计思想编写完成几何对象的面积计算程序,具体要求为:GeometricObject(抽象类)、Circle、Cylinder和Rectangle之间的继承关系如下图所示,

    请分按类图分别实现这四个类;

    然后编写一测试类GeoAreaSum,在该类中编写一个方法求数组中所有几何对象的面积之和,其方法头为:sumArea(GeometricObject[ ] a)

    再在GeoAreaSum的main方法中创建一个包括三个对象(一个半径为5的圆、一个半径为10、高为5的圆柱体和一个宽和高分别为6和8的矩形)的GeometricObject数组,使用sumArea方法计算出它们的总面积并在屏幕上打印输出。

    (2) 另请思考如果将Cylinder与GeometricObject的继承关系改成与Cylinder与Circle的继承关系,同时将Cylinder中的私有变量r去掉,Cylinder又该如何编写?

    考察点:继承多态接口抽象类虚函数纯虚函数虚基类

    C++

    如果子类调用父类带参数的构造方法,只能使用列表生成式

    GeometricObject.h

    #ifndef GEOMETRIC_H
    #define GEOMETRIC_H
    #include 
    #include 
    #define PI acos(-1)
    using namespace std;
    
    class GeometricObject
    {
    public:
    	virtual double findArea() = 0;
    	virtual double findPerimeter() = 0;
    	virtual ~GeometricObject() { }
    };
    
    class Circle : public GeometricObject
    {
    public:
    	Circle();
    	Circle(double r);
    	double getR();
    	double findArea();
    	double findPerimeter();
    private:
    	double r;
    };
    
    class Rectangle : public GeometricObject
    {
    public:
    	Rectangle();
    	Rectangle(double w, double h);
    	double findArea();
    	double findPerimeter();
    private:
    	double w;
    	double h;
    };
    
    class Cylinder : public Circle
    {
    public:
    	Cylinder();
    	Cylinder(double h, double r);
    	double findArea();
    	double findPerimeter();
    private:
    	double h;
    };
    
    // 需要使用指针数组
    double sumArea(GeometricObject *a[], int n);
    #endif // !GEOMETRIC_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    GeometricObject.cpp

    #include "GeometricObject.h"
    
    Circle::Circle() {
    	this->r = 0;
    }
    
    Circle::Circle(double r) {
    	this->r = r;
    }
    
    double Circle::getR() {
    	return this->r;
    }
    
    double Circle::findArea() {
    	return PI * r * r;
    }
    
    double Circle::findPerimeter() {
    	return 2 * PI * r;
    }
    
    Rectangle::Rectangle() {
    	this->w = 0;
    	this->h = 0;
    }
    
    Rectangle::Rectangle(double w, double h) {
    	this->w = w;
    	this->h = h;
    }
    
    double Rectangle::findArea() {
    	return w * h;
    }
    
    double Rectangle::findPerimeter() {
    	return 2 * (w + h);
    }
    
    Cylinder::Cylinder(): Circle(0) {
    	this->h = 0;
    }
    
    // 如果子类调用父类带参数的构造方法,只能使用列表生成式
    Cylinder::Cylinder(double h, double r) : Circle(r) {
    	this->h = h;
    }
    
    double Cylinder::findArea() {
    	return 2 * PI * getR() * h + 2 * PI * getR() * getR();
    }
    
    double Cylinder::findPerimeter() {
    	return 2 * PI * getR() * 2;
    }
    
    double sumArea(GeometricObject* a[], int n) {
    	double sum = 0;
    	for (int i = 0; i < n; i++) {
    		sum += a[i]->findArea();
    	}
    	return sum;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    GeoAreaSum.cpp

    #include 
    #include "GeometricObject.h"
    
    int main() {
        GeometricObject *objects[3];
        objects[0] = new Circle(5);
        objects[1] = new Cylinder(5, 10);
        objects[2] = new Rectangle(6, 8);
        cout << "总面积为:" << sumArea(objects, 3) << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Java

    GeometricObject.java

    public abstract class GeometricObject {
        abstract double findArea();
        abstract double findPerimeter();
    }
    
    • 1
    • 2
    • 3
    • 4

    Circle.java

    public class Circle extends GeometricObject{
        private double r;
        public Circle(double r) {
            this.r = r;
        }
        public double getR() {
            return r;
        }
        @Override
        double findArea() {
            return Math.PI*r*r;
        }
        @Override
        double findPerimeter() {
            return 2*Math.PI*r;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    Rectangle.java

    public class Rectangle extends GeometricObject{
        private double w;
        private double h;
        public Rectangle(double w, double h) {
            this.w = w;
            this.h = h;
        }
        @Override
        double findArea() {
            return w*h;
        }
        @Override
        double findPerimeter() {
            return 2*(w+h);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Cylinder.java

    public class Cylinder extends Circle{
        private double h;
    
        public Cylinder(double h, double r) {
            super(r);
            this.h = h;
        }
        @Override
        double findArea() {
            return 2*Math.PI*getR()*h+2*Math.PI*getR()*getR();
        }
        @Override
        double findPerimeter() {
            return 2*Math.PI*getR()*2;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    GeoAreaSum.java

    public class GeoAreaSum {
        public static double sumArea(GeometricObject[] a) {
            double sum = 0;
            for (GeometricObject geometricObject : a) {
                sum += geometricObject.findArea();
            }
            return sum;
        }
    
        public static void main(String[] args) {
            GeometricObject[] objects = new GeometricObject[3];
            objects[0] = new Circle(5);
            objects[1] = new Cylinder(5,10);
            objects[2] = new Rectangle(6,8);
            System.out.println("总面积为:" + sumArea(objects));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    参考资料

    1. C++子类继承父类时如何写构造函数
  • 相关阅读:
    基于BERT模型进行文本处理(Python)
    Spring-依赖注入
    Windows10环境下如何打开远程桌面
    旅游出行类APP如何找到策略优势,最大化流量红利
    0046-量化第十一天:PythonGo-第一个简单策略
    瑞吉外卖项目 基于spring Boot+mybatis-plus开发 超详细笔记,有源码链接
    应用程序出现了未经处理的异常
    【算法|动态规划No.23】leetcode376. 摆动序列
    【柳工智慧矿山】隐私政策
    NtripShare Mos地铁自动化监测终端盒子硬件设计
  • 原文地址:https://blog.csdn.net/comscience/article/details/126161693