栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

PTA-JAVA-类和对象

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PTA-JAVA-类和对象

PTA-JAVA-类和对象
  • 目录:
    • 1.函数题
      • 1、6-1 圆锥体计算
    • 2.编程题
      • 1、7-2 两点一线
      • 2、7-3 定义类2
      • 3、7-4 设计一个矩形类Rectangle


目录:


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 %.2fn",cone.getBottom().getArea(),cone.getBottom().getLength());
       System.out.printf("体积为:%.2fn",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("此线段的长度为:%.1fn", 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");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926899.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号