PTA-JAVA-类和对象


目录:


1.函数题

1、6-1 圆锥体计算

在这里插入图片描述
裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:
import java.util.Scanner;
public class Main {
     public static void main(String[] args) {
       Scanner scanner=new Scanner(System.in);      
       double r=scanner.nextDouble();
       double h=scanner.nextDouble();
       Circle c = new Circle();
       c.setRadius(r);
       Cone cone=new Cone();
       cone.setBottom(c);
       cone.setHeight(h);
       System.out.printf("底面面积和周长分别为:%.2f %.2f\n",cone.getBottom().getArea(),cone.getBottom().getLength());
       System.out.printf("体积为:%.2f\n",cone.getVolume());      
       scanner.close();
   }
}
/* 请在这里填写答案 */

代码:

class Circle{
    double r;
    public double getRadius() {
        return r;
    }
    public void setRadius(double r)
    {
        this.r = r;
    }
    public double getArea()
    {
        return r*r*Math.PI;
    }
    public double getLength()
    {
        return r*2*Math.PI;
    }
}
class Cone{
    Circle bottom;
    double h;
    public Circle getBottom() {
        return  bottom;
    }
    public void setBottom(Circle bottom)
    {
        this.bottom = bottom;
    }
    public double getHeight()
    {
        return h;
    }
    public void setHeight(double h)
    {
        this.h = h;
    }
    public double getVolume()
    {
        return h*bottom.getArea()/3;
    }
}

2.编程题

1、7-2 两点一线

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

Point 类如下所示:

public class Point {
    private int x, y;// x,y为点的坐标
    //求两点之间的距离
    public double distance(Point p1) {
        return Math.sqrt((p1.x -this.x)*(p1.x -this.x)+(p1.y-this.y)*(p1.y-this.y));
    }
    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Point() {
        super();
        x = y =0;
    }
    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }
}

输入样例:
在这里给出一组输入。例如:

2
0 0 2 3
1 3 2 5

输出样例:
在这里给出相应的输出。例如:

Line [p1=Point [x=0, y=0], p2=Point [x=2, y=3]]
此线段的长度为:3.6
Line [p1=Point [x=1, y=3], p2=Point [x=2, y=5]]
此线段的长度为:2.2

代码:

import java.util.Scanner;

class Point {
    private int x, y;// x,y为点的坐标
    //求两点之间的距离
    public double distance(Point p1) {
        return Math.sqrt((p1.x -this.x)*(p1.x -this.x)+(p1.y-this.y)*(p1.y-this.y));
    }
    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Point() {
        super();
        x = y =0;
    }
    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }
}

class Line{
    public Point p1 = new Point();
    public Point p2 = new Point();

    public Line(int x1, int y1, int x2, int y2){
        p1.setX(x1); 
        p1.setY(y1);
        p2.setX(x2);
        p2.setY(y2);
    }

    public double getLength(){
        return p1.distance(p2);
    }

    public void reString(){
        System.out.println("Line [p1=" + p1 + ", p2=" + p2 + "]");
        System.out.printf("此线段的长度为:%.1f\n", getLength());
    }
}

public class Main {
    public static void main(String[] args) {
        int q = 0;
        Scanner sc = new Scanner(System.in);
        q = sc.nextInt();
        int i = 0, j = 0;
        int[] data = new int[4];
        for(i = 0; i < q; i++){
            for (j = 0; j < 4; j++){
                data[j] = sc.nextInt();
            }
            Line L1 = new Line(data[0], data[1], data[2], data[3]);
            L1.reString();
        }
    }
}

2、7-3 定义类2

在这里插入图片描述
输入格式:
在一行中给出5个不超过1000的实数。
输出格式:
输出排在中间的那个数,保留小数点后两位。

输入样例:

1 2 5 4 3

输出样例:

5.00

代码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        Scanner in =new Scanner(System.in);
        double []A = new double[5];
        for(int i=0; i<5; i++)
        {
            double a = in.nextDouble();
            A[i] = a;
        }
        RR r = new RR();
        r.setA(A[2]);
        double dd = RR.fun();
        System.out.printf("%.2f",dd);
    }
}
class RR
{
    public static double a;

    public void setA(double a) {
        this.a = a;
    }

    public double getA() {
        return a;
    }

    public static double fun()
    {
        return a;
    }
}

3、7-4 设计一个矩形类Rectangle

在这里插入图片描述
输入样例:
在这里给出一组输入。例如:

4 40 3.5 35.9

输出样例:
在这里给出相应的输出。例如:

4.0 40.0 160.0 88.0
3.5 35.9 125.64999999999999 78.8

代码:

import java.util.Scanner;
class Retangle{
    double width;
    double height;
    Retangle(){
        width = 1;
        height = 1;
    }
    Retangle(double w, double h){
        width = w;
        height = h;
    }
    double getArea()
    {
        return width*height;
    }
    double getPerimeter(){
        return 2*(width + height);
    }
}

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Retangle ret_1 = new Retangle(sc.nextDouble(),sc.nextDouble());
        Retangle ret_2 = new Retangle(sc.nextDouble(),sc.nextDouble());
        System.out.print(ret_1.width+" "+ ret_1.height+" "+ ret_1.getArea()+" "+ ret_1.getPerimeter()+"\n");
        System.out.print(ret_2.width+" "+ ret_2.height+" "+ ret_2.getArea()+" "+ ret_2.getPerimeter()+"\n");
    }
}
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个Java编写的圆柱体类及其测试类: 圆柱体类可以包含以下属性和方法: 属性: - 半径(radius) - 高度(height) 方法: - 构造函数(Constructor):接受半径和高度作为参数 - 获取表面积(getSurfaceArea):返回圆柱体的表面积 - 获取体积(getVolume):返回圆柱体的体积 以下是一个示例实现: ```java public class Cylinder { private double radius; private double height; public Cylinder(double radius, double height) { this.radius = radius; this.height = height; } public double getSurfaceArea() { return 2 * Math.PI * radius * (radius + height); } public double getVolume() { return Math.PI * radius * radius * height; } } ``` 接下来,我们可以编写一个测试类来测试这个圆柱体类的功能: ```java public class CylinderTest { public static void main(String[] args) { // 创建一个圆柱体对象 Cylinder cylinder = new Cylinder(3, 5); // 测试获取表面积 double surfaceArea = cylinder.getSurfaceArea(); if (Math.abs(surfaceArea - 150.7964474) > 1e-9) { System.out.println("获取表面积方法测试失败!"); } else { System.out.println("获取表面积方法测试通过!"); } // 测试获取体积 double volume = cylinder.getVolume(); if (Math.abs(volume - 141.3716694) > 1e-9) { System.out.println("获取体积方法测试失败!"); } else { System.out.println("获取体积方法测试通过!"); } } } ``` 在测试类中,我们首先创建一个圆柱体对象,并使用 if 语句测试它的 `getSurfaceArea()` 和 `getVolume()` 方法是否返回正确的结果。如果测试通过,我们就打印出一条消息来表明测试已经完成。 希望这个示例能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值