- 前言
- 问题一
- 问题描述
- Java
- C++
- 问题二
- 题目描述
- Java
- ZPoint.java
- Test.java
- C++
- ZPoint.h
- ZPoint.cpp
- Test.cpp
- 问题三
- 问题描述
- C++
- GeometricObject.h
- GeometricObject.cpp
- GeoAreaSum.cpp
- Java
- GeometricObject.java
- Circle.java
- Rectangle.java
- Cylinder.java
- GeoAreaSum.java
- 参考资料
前段时间有一个面试要考Java,在翻复习资料时,发现了三道题(谢谢张老师)基本上概括了一个语言的常用语法。特此记录,围绕这三道题用不同的语言编写,以便后续进行快速地复习。如果想要详细复习,请看C/C++要点复习
问题一 问题描述请从控制台输入一个三角形的三条边长,并判断这三条边能不能构成一个三角形。
如能构成,请打印输出这个三角形的周长(要求输出宽度为8,保留两位小数),如不能构成,请打印输出具体提示。
考察点:输入、输出、if条件判断
Javapublic 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.2fn", c);
} else {
System.out.println("无法构成三角形");
}
}
}
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.2fn", 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.javapublic 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关键字
该关键字只能出现在类内部的声明语句
#ifndef ZPOINT_H #define ZPOINT_H #includeZPoint.cppusing 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
#include "ZPoint.h" #includeTest.cppZPoint::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; }
#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; }
请运用面向对象程序设计思想编写完成几何对象的面积计算程序,具体要求为: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 #includeGeometricObject.cpp#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
#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#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));
}
}
参考资料
- C++子类继承父类时如何写构造函数



