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

本文回顾了面试中遇到的三道编程题,涉及Java判断三角形、C++实现ZPoint和几何对象面积计算。通过实例展示了面向对象编程技巧和几何形状的计算原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

前段时间有一个面试要考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("无法构成三角形");
        }
    }
}

C++

#include <iostream>
#include <algorithm>
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;
}

问题二

题目描述

编写一个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;
    }
}

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));
    }
}

C++

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

ZPoint.h

#ifndef ZPOINT_H
#define ZPOINT_H
#include <iostream>
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 

ZPoint.cpp

#include "ZPoint.h"
#include <cmath>

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;
}

Test.cpp

#include <iostream>
#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;
}

问题三

问题描述

请运用面向对象程序设计思想编写完成几何对象的面积计算程序,具体要求为: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 <iostream>
#include <cmath>
#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

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;
}

GeoAreaSum.cpp

#include <iostream>
#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;
}

Java

GeometricObject.java

public abstract class GeometricObject {
    abstract double findArea();
    abstract double findPerimeter();
}

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;
    }
}

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);
    }
}

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;
    }
}

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. C++子类继承父类时如何写构造函数
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeSlogan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值