> For the complete documentation index, see [llms.txt](https://scls-cs.gitbook.io/scls-apcs-lab/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://scls-cs.gitbook.io/scls-apcs-lab/methods/lab1-calculator.md).

# Lab1: Calculator

本次作业为Lab形式，也就是上机编写程序。这次Lab作业需要你在课上内容基础上，设计一个功能更加强大的计算器。

不要担心，大部分代码是都已经提供，你只需要按照提示的内容一步步完成即可。

打开OnlineGDB，点击左下方的Login来注册一个账号，这样你可以保存自己的代码。![](/files/UAb1uF3Vm4Ub8gUedhLQ)

注册完成后，点击中间编辑窗口左上角**New File**，创建一个Java文件，取名为Calculator.java。将以下代码抄写到Calculator.java里。鼓励大家人工抄写代码，但直接复制粘贴也可以。

```java
public class Calculator {
    public void add(int x, int y) {
        System.out.println(x+y);
    }
}
```

现在点&#x51FB;**`Save`**&#x4FDD;存你的代码，命名为**Lab1**。与Word一样，**定期保存是一个好习惯**。没有人希望辛辛苦苦写的代码，因为突然关机而丢失掉。

到目前为止，我们创建了一个类：`Calculator`，类中包含一个方法：`public void add(int x, int y)`。下面来调用这个方法：

```java
public class Main
{
    public static void main(String[] args) {
        Calculator cal = new Calculator();
	cal.add(1,2);
    }
}
```

这里可以看出，Java通过对象来调用方法。我们需要先创建一个对象`cal`，通过对象.方法名()来调用方法。创建对象在main函数里完成。

点击**Run**运行代码。如果看到显示结果为3，说明add()方法被成功调用。

接下来向类里添加一个新方法`getArea()`，这个方法具有返回值：

```java
public class Calculator {
    public void add(int x, int y) {
        System.out.println(x+y);
    }
    
    public double getArea(int r) {
        return 3.14*r*r;
    }
}
```

`getArea()`的返回类型是 `double`，函数需要返回一个`double`数据给调用函数的地方。

在`main()`函数里调用`getArea()：`

```java
public class Main
{
    public static void main(String[] args) {
	Calculator c = new Calculator();
	double area = c.getArea(1);
	System.out.println(area);
    }
}
```

运行代码，如果程序结果是3.14，说明getArea()被成功调用。这里先将函数返回值保存在**area**变量里，再打印变量的值。

也可以直接打印函数的返回值：

```java
System.out.println(c.getArea(1));
```

### 作业：

**任务一：**

&#x5728;**`Calculator`**&#x7C7B;里实现加、减、乘、除的运算，运算数均为整数。除了除法以外，运算结果应该均为整数。除法需要返回正确的&#x5546;**。所有方法均有返回值，也就是说它们都不是void类型。**

除了实现函数，测试函数也很重要。测试函数最简单的方法就是调用函数，通过观察函数的返回值来检验函数的正确性。

你需要在`main`函数里完成所有方法的调用，每个方法至少要使用三组不同的数据来调用，每一组数据叫做一组测试数据集（Test Datasets）。例如，下面代码中用了三组测试集来测试`getArea()` ：

```java
public static void main(String[] args) {
	Calculator c = new Calculator();
	double area1 = c.getArea(1);	//Dataset 1
	double area2 = c.getArea(5);	//Dataset 2
	double area3 = c.getArea(10);	//Dataset 3

	System.out.println(area1);
	System.out.println(area2);
	System.out.println(area3);
    }
```

**任务二：**

你打算给计算器添加一些新的功能，例如几何图形的运算。请设计方法分别计算长方形的面积、球体和圆柱体的体积。同样，这些方法也都具有返回值。

与任务一一样，你也需要准备至少三组测试数据集来测试你的方法。

如果你不太熟悉几何体的计算公式，你可以查看：<https://www.cuemath.com/geometry-formulas/>

提交方式：

1. 点击**Save**保存你的代码，命名为Lab1。代码需要包含你的所有工作（包含测试程序）。
2. 点击**Share**，将链接上传到作业本里。

**提交截止时间为本周四晚上十点。**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://scls-cs.gitbook.io/scls-apcs-lab/methods/lab1-calculator.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
