split函数、Double.valueOf()函数

Double.valueOf(String s)函数:

Double.vlueOf(String s) 可以将字符串转为 Double 类型。

Double aDouble = Double.valueOf("12.02"); // 将 String 类型转换为 Double 类型

split 函数:

字符串截取方法:split

如:String ss=”1,2,3”;

ss.split(“,”)是以”,”为分隔返回一个字符串数组

/**

 * 任务:将键盘输入的三个学生的信息存入 Student 对象中,最后将这些学生信息按预期输出格式打印输出。

 * 类名为:Student

 */

import java.util.Scanner;

public class Student {

    private String name;  // 学生的姓名

    private String num;  // 学生的学号信息

    private double grades;  // 学生的成绩

    public Student(String name, String num, double grades) {

        this.name = name;

        this.num = num;

        this.grades = grades;

    }

    

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getNum() {

        return num;

    }

    public void setNum(String num) {

        this.num = num;

    }

    public double getGrades() {

        return grades;

    }

    public void setGrades(double grades) {

        this.grades = grades;

    }

    public static void main(String[] args) {

        // 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码

        /********** Begin **********/

        Scanner scanner=new Scanner(System.in);

        // 创建可以存放三个对象的对象数组

       Student a[]=new Student[3];

        // 获取键盘输入的学生信息,将数组中的对象进行实例化

       for (int i = 0; i < a.length; i++) {

        String s=scanner.next();

        String ss[]=s.split(",");

        a[i]=new Student(ss[0], ss[1], Double.valueOf(ss[2]));

        }

        // 打印输出每个学生的信息

       for(int i=0;i<3;i++) {

//变量name\num\grades在类内定义,可以直接应用,也可以通过调用函数

           System.out.printf("姓名:%s\t学号:%s\t成绩:%.1f\n",a[i].name,a[i].num,a[i].grades);

//如果变量在类外定义且为private类型,就必须通过以下方式

            //System.out.printf("姓名:%s\t学号:%s\t成绩:%.1f\n",a[i].getName(),a[i].getNum(),a[i].getGrades()); 

       }

    }      

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive中没有直接计算凸包面积的函数,但可以通过UDF来实现。 下面是一个通过Java UDF计算凸包面积的示例代码: ```java import java.util.List; import java.util.ArrayList; import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.hive.ql.exec.Description; @Description(name = "convex_area", value = "_FUNC_(points) - Computes the area of the convex hull of a set of points") public class ConvexArea extends UDF { public double evaluate(List<String> points) { // Parse the input points List<Point> pts = new ArrayList<Point>(); for (String point : points) { String[] coords = point.split(","); double x = Double.parseDouble(coords[0]); double y = Double.parseDouble(coords[1]); pts.add(new Point(x, y)); } // Compute the convex hull List<Point> hull = convexHull(pts); // Compute the area of the convex hull double area = 0.0; int n = hull.size(); for (int i = 0; i < n; i++) { Point p1 = hull.get(i); Point p2 = hull.get((i + 1) % n); area += p1.x * p2.y - p2.x * p1.y; } return Math.abs(area) / 2.0; } // Computes the convex hull of a set of points using the Graham scan algorithm private List<Point> convexHull(List<Point> pts) { // Find the point with the lowest y-coordinate Point minY = pts.get(0); for (Point pt : pts) { if (pt.y < minY.y) { minY = pt; } } // Sort the points by polar angle with minY List<Point> sortedPts = new ArrayList<Point>(pts); sortedPts.remove(minY); sortedPts.sort((p1, p2) -> { double angle1 = angle(minY, p1); double angle2 = angle(minY, p2); return Double.compare(angle1, angle2); }); sortedPts.add(0, minY); // Compute the convex hull using the Graham scan algorithm List<Point> hull = new ArrayList<Point>(); hull.add(sortedPts.get(0)); hull.add(sortedPts.get(1)); for (int i = 2; i < sortedPts.size(); i++) { Point p = sortedPts.get(i); while (hull.size() >= 2 && orientation(hull.get(hull.size() - 2), hull.get(hull.size() - 1), p) <= 0) { hull.remove(hull.size() - 1); } hull.add(p); } return hull; } // Computes the polar angle between two points with respect to a reference point private double angle(Point ref, Point pt) { double dx = pt.x - ref.x; double dy = pt.y - ref.y; return Math.atan2(dy, dx); } // Computes the orientation of three points (returns a positive value if they are in counterclockwise order) private double orientation(Point p1, Point p2, Point p3) { return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x); } // A simple class for representing 2D points private static class Point { public double x; public double y; public Point(double x, double y) { this.x = x; this.y = y; } public String toString() { return "(" + x + ", " + y + ")"; } } } ``` 使用方法: 首先需要将上述代码编译成jar包,然后将jar包添加到Hive的classpath中: ``` ADD JAR /path/to/convex-area.jar; ``` 然后可以在Hive中使用该UDF计算凸包面积: ``` SELECT convex_area(array("0,0", "1,2", "3,1", "2,3")) AS area; ``` 该语句将计算坐标为(0,0),(1,2),(3,1),(2,3)的点集的凸包面积。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值