Exercise Requirements
提交格式:
学号_姓名_第4次随堂练习/
|- 随堂练习一_Point.cpp
|- 随堂练习二_TestFeature.java
例:2025xxxx_张三_第4次随堂练习/
|- 随堂练习一_Point.cpp
|- 随堂练习二_TestFeature.java
随堂练习一:用C++代码实现下面的描述
一、定义一个简单的二维点类:Point
详细要求
- 定义一个简单的二维点类Point;
- 创建一个点对象p1;
- 完成赋值x为1,y为2;
- 调用display方法显示信息。
| Point |
|---|
| + x: double |
| + y: double |
| + display(): void |
参考代码
#include <iostream>
class Point {
public:
double x;
double y;
void display() const {
std::cout << "坐标点:" << x << "," << y << std::endl;
}
};
int main() {
Point p1;
p1.x = 1;
p1.y = 2;
p1.display();
return 0;
}
二、添加函数isInBounds
详细要求
- 添加函数isInBounds:判断该点位置是否在某个矩形范围内;
- 测试3种情况:内部、边界、外部。
参考代码(在上一步的基础上增量)
#include <iostream>
class Point {
public:
...
bool isInBounds(double minX, double minY, double maxX, double maxY) {
return (x>=minX && x<=maxX && y >= minY && y<=maxY);
}
};
int main() {
...
if (p1.isInBounds(0,0,10,10)) {
cout << "点在范围内" << endl;
} else {
cout << "点不在范围内" << endl;
}
...
return 0;
}
三、添加move函数做传参实验
详细要求
- 添加函数moveValue:传入一个新点p2的值,与当前点累加;
- 添加函数moveReference:传入一个新点p2的引用,与当前点累加。
参考代码(在上一步的基础上增量)
#include <iostream>
class Point {
public:
...
void moveValue(Point p){
p.x += x;
p.y += y;
}
void moveReference(Point& p){
p.x += x;
p.y += y;
}
};
int main(){
…
Point p2;
p2.x = 3;
p2.y = 4;
p1.moveValue(p2);
p2.display();
p1.moveReference(p2);
p2.display();
return 0;
}
四、动态内存管理实验
详细要求
- 动态创建点数组points;
- 分别把p1与p2放到数组内;
- 调用数组元素的display函数;
- 释放动态申请的空间。
参考代码(在上一步的基础上增量)
int main() {
...
Point* points = new Point[2];
points[0] = p1;
points[1] = p2;
points[0].display();
points[1].display();
delete[] points;
points = nullptr;
return 0;
}
五、用vector替代动态数组
详细要求
- 改用vector容器替代动态数组,创建点容器route;
- 使用范围for循环显示所有点;
- 清空容器。
参考代码(在上一步的基础上增量)
#include <iostream>
#include <vector>
...
int main() {
...
std::vector<Point> route;
route.push_back(p1);
route.push_back(p2);
for (const auto& point : route) {
point.display();
}
route.clear();
return 0;
}
六、函数重载
详细要求
- 添加distance:计算当前点与某个位置的距离;
- 添加distance:计算当前点与某个点的距离;
- 分别调用两个distance函数。
参考代码(在上一步的基础上增量)
class Point {
public:
...
double distance(double px, double py) const {
double dx = x - px;
double dy = y - py;
return sqrt(dx*dx + dy*dy);
}
double distance(const Point& p) const {
return distance(p.x, p.y);
}
};
int main() {
...
std::cout << "p1.distance(2,2): " << p1.distance(2,2) << std::endl;
std::cout << "p1.distance(p2): " << p1.distance(p2) << std::endl;
return 0;
}
七、Lambda表达式
详细要求
- 使用Lambda表达式实现distance计算;
- 调用Lambda表达式。
参考代码(在上一步的基础上增量)
int main() {
...
auto dist = [](const Point& a, const Point& b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx*dx + dy*dy);
};
std::cout << “dist(p1,p2): " << dist(p1,p2) << std::endl;
return 0;
}
随堂练习二:用Java代码实现下面的描述
一、定义一个简单的二维点类:Point
详细要求
- 定义一个简单的二维点类Point;
- 创建一个点对象p1;
- 完成赋值x为10,y为20;
- 调用display方法显示信息。
| Point |
|---|
| + x: double |
| + y: double |
| + display(): void |
参考代码
class Point {
public double x;
public double y;
public void display() {
System.out.println("坐标点:"+ x + ", " + y);
}
}
public class TestFeature {
public static void main (String[] args) {
Point p1 = new Point();
p1.x = 10;
p1.y = 20;
p1.display();
}
}
二、添加isInFirstQuadrant与isSamePosition方法
详细要求
- 添加函数isInFirstQuadrant:判断该点位置是否在第一象限;
- 添加函数isSamePosition:判断该点位置是否与另一个点重合。
参考代码(在上一步的基础上增量)
class Point {
...
public boolean isInFirstQuadrant() {
return x > 0 && y > 0;
}
public boolean isSamePosition(Point p) {
return x == p.x && y == p.y;
}
}
public class TestFeature {
public static void main(String[] args) {
...
System.out.println("p1在第一象限:" + p1.isInFirstQuadrant());
Point p2 = new Point();
p2.x = 10;
p2.y = 20;
System.out.println("p1与p2重合:" + p1.isSamePosition(p2));
}
}
三、测试引用类型的赋值
详细要求
- 创建Point引用p3,并用p1对其进行初始化;
- 修改p3的x值与y值;
- 查看p1与p3的坐标。
参考代码(在上一步的基础上增量)
public class TestFeature {
public static void main(String[] args) {
...
Point p3 = p1;
p3.x = 1;
p3.y = 2;
p1.display();
p3.display();
}
四、创建数组并循环遍历
详细要求
- 创建点数组points;
- 分别把p1与p2放到数组内;
- 使用for-each循环显示所有点。
参考代码(在上一步的基础上增量)
public class TestFeature {
public static void main(String[] args) {
...
Point[] points = new Point[2];
points[0] = p1;
points[1] = p2;
System.out.println("length of points: " + points.length);
for (Point p : points) {
p.display();
}
}
五、方法重载
详细要求
- 添加distance:计算当前点与某个位置的距离;
- 添加distance:计算当前点与某个点的距离;
- 调用distance函数。
参考代码(在上一步的基础上增量)
class Point {
public:
...
public double distance(double px, double py) {
double dx = x - px;
double dy = y - py;
return Math.sqrt(dx*dx + dy*dy);
}
public double distance(Point p) {
return distance(p.x, p.y);
}
}
public class TestFeature {
public static void main(String[] args) {
...
System.out.println(p1.distance(2,2));
System.out.println(p1.distance(p2));
}
}