public class Fan {
public static void main(String[] args) {
Fan fan1 = new Fan(), fan2 = new Fan();
fan1.modifySpeed(FAST);
fan1.modifyRadius(10);
fan1.modifyColor("yellow");
fan1.modifyOn(true);
System.out.println(fan1.toString());
fan2.modifySpeed(MEDIUM);
fan2.modifyRadius(5);
fan2.modifyColor("blue");
fan2.modifyOn(false);
System.out.println(fan2.toString());
}
public Fan() {
speed = SLOW;
on = false;
radius = 5;
color = "blue";
}
public int getSpeed() {
return speed;
}
public boolean getOn() {
return on;
}
public double getRadius() {
return radius;
}
public String getColor() {
return color;
}
public void modifySpeed(int speed) {
this.speed = speed;
}
public void modifyOn(boolean on) {
this.on = on;
}
public void modifyRadius(double radius) {
this.radius = radius;
}
public void modifyColor(String color) {
this.color = color;
}
public String toString() {
if (on == true) return new String(speed + " " + color + " " + radius);
else return new String("fan is off" + " " + color + " " + radius);
}
final static int SLOW = 1, MEDIUM = 2, FAST = 3;
int speed = SLOW;
boolean on = false;
double radius = 5;
String color = "blue";
}

  

import java.util.Scanner;
public class Cross {
static Line l1, l2;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter the endpoints of the first line segment:");
l1 = new Line();
l2 = new Line();
l1.read();
System.out.println("Enter the endpoints of the second line segment:");
l2.read();
Point ans = getCrossPoint(l1, l2);
System.out.println("The intersecting point is: " + ans.toString());
}
public static Point getCrossPoint(Line l1, Line l2) {
double a = l1.b.y - l1.a.y;
double b = l1.a.x - l1.b.x;
double p = l1.a.x * l1.b.y - l1.b.x * l1.a.y;
double c = l2.b.y - l2.a.y;
double d = l2.a.x - l2.b.x;
double q = l2.a.x * l2.b.y - l2.b.x * l2.a.y;
double u, v;
if (Math.abs(b * c - a * d) > 1e-10) {
v = (p * c - q * a) / (b * c - a * d);
if (Math.abs(a) > 1e-10) u = (p - b * v) / a;
else u = (q - d * v) / c;
}
else {
u = (p * d - q * b) / (a * d - c * b);
if (Math.abs(b) > 1e-10) v = (p - a * u) / b;
else v = (q - c * u) / d;
}
return new Point(u, v);
}
public static class Point {
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point() {
x = y = 0;
}
public void read() {
x = input.nextDouble();
y = input.nextDouble();
}
public String toString() {
return new String("(" + x + ", " + y + ")");
}
double x, y;
}
public static class Line {
public Line(Point a, Point b) {
this.a = a;
this.b = b;
}
public Line() {
a = new Point();
b = new Point();
}
public void read() {
a.read();
b.read();
}
Point a, b;
}
}

  

public class Triangle2D {
public static void main(String[] args) {
Triangle2D t1 = new Triangle2D(new MyPoint(2.5, 2), new MyPoint(4.2, 3), new MyPoint(5, 3.5));
System.out.println("Area is: " + t1.getArea() + "\nPerimeter is: " + t1.getPerimeter());
System.out.println(t1.contains(new MyPoint(3, 3)));
System.out.println(t1.contains(new Triangle2D(new MyPoint(2.9, 2), new MyPoint(4, 1), new MyPoint(1, 3.4))));
System.out.println(t1.overlaps(new Triangle2D(new MyPoint(2, 5.5), new MyPoint(4, -3), new MyPoint(2, 6.5))));
}
public Triangle2D() {
p1 = new MyPoint();
p2 = new MyPoint(1, 1);
p3 = new MyPoint(2, 5);
}
public Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public double getArea() {
double a = MyPoint.distance(p1, p2);
double b = MyPoint.distance(p2, p3);
double c = MyPoint.distance(p3, p1);
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getPerimeter() {
double a = MyPoint.distance(p1, p2);
double b = MyPoint.distance(p2, p3);
double c = MyPoint.distance(p3, p1);
return a + b + c;
}
public boolean contains(MyPoint p) {
MyPoint pa = new MyPoint(p1.x - p.x, p1.y - p.y);
MyPoint pb = new MyPoint(p2.x - p.x, p2.y - p.y);
MyPoint pc = new MyPoint(p3.x - p.x, p3.y - p.y);
double angle = getAngle(pa, pb) + getAngle(pb, pc) + getAngle(pc, pa);
if (Math.abs(angle) > 1e-5) return true;
return false;
}
public boolean contains(Triangle2D t) {
return contains(t.p1) && contains(t.p2) && contains(t.p3);
}
//假定坐标系范围为[-10,10]
public boolean overlaps(Triangle2D t) {
if (contains(t) || t.contains(this)) return true;
if (contains(t.p1) || contains(t.p2) || contains(t.p3)) return true;
MyPoint P = new MyPoint();
for (int i = 0; i < 10000000; i ++) {
P.x = Math.random() * 20 - 10;
P.y = Math.random() * 20 - 10;
if (contains(P) && t.contains(P)) return true;
}
return false;
}
public static double getAngle(MyPoint v1, MyPoint v2) {
double ans = Math.acos(v1.mul(v2) / v1.abs() / v2.abs());
if (v1.x * v2.y - v1.y * v2.x < 0) return ans;
else return -ans;
}
public void setP1(MyPoint p1) {
this.p1 = p1;
}
public void setP2(MyPoint p2) {
this.p2 = p2;
}
public void setP3(MyPoint p3) {
this.p3 = p3;
}
public MyPoint getP1() {
return p1;
}
public MyPoint getP2() {
return p2;
}
public MyPoint getP3() {
return p3;
}
public static class MyPoint {
public MyPoint() {
x = y = 0;
}
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double getx() {
return x;
}
public double gety() {
return y;
}
public double mul(MyPoint that) {
return x * that.x + y * that.y;
}
public double abs() {
return Math.sqrt(x * x + y * y);
}
public static double distance(MyPoint A, MyPoint B) {
double x = A.x - B.x, y = A.y - B.y;
return Math.sqrt(x * x + y * y);
}
public static double distance(double x1, double y1, double x2, double y2) {
double x = x1 - x2, y = y1 - y2;
return Math.sqrt(x * x + y * y);
}
double x, y;
}
MyPoint p1, p2, p3;
}

  

public class CourseSelection {
public static void main(String[] args) {
CourseSelection cs = new CourseSelection();
cs.addStudent("大神");
cs.addStudent("郑涛");
cs.addStudent("邓时庆");
cs.addCourse("大学语文");
cs.addCourse("理论物理");
cs.addCourse("宇宙的起源");
cs.selectCourse("大神", "宇宙的起源");
cs.selectCourse("大神", "理论物理");
cs.selectCourse("邓时庆", "大学语文");
cs.selectCourse("郑涛", "理论物理");
cs.selectCourse("邓时庆", "理论物理");
cs.selectCourse("郑涛", "大学语文");
cs.showStudents();
cs.showCourses();
System.out.println();
cs.dropCourse("郑涛", "理论物理");
cs.dropCourse("邓时庆", "大学语文");
cs.dropCourse("大神", "宇宙的起源");
cs.dropCourse("大神", "理论物理");
cs.selectCourse("大神", "大学语文");
cs.selectCourse("郑涛", "宇宙的起源");
cs.selectCourse("邓时庆", "宇宙的起源");
cs.showStudents();
cs.showCourses();
}
public void showStudents() {
System.out.println("--------------sutdents--------------");
for (int i = 0; i < numberOfStudents; i ++) {
System.out.print(students[i].getStudentName() + ":");
for (int j = 0; j < students[i].numberOfCourses; j ++) {
System.out.print("___" + students[i].getCourses()[j]);
}
System.out.println();
}
}
public void showCourses() {
System.out.println("--------------courses--------------");
for (int i = 0; i < numberOfCourses; i ++) {
System.out.print(courses[i].getCourseName() + ":");
for (int j = 0; j < courses[i].numberOfStudents; j ++) {
System.out.print("___" + courses[i].getStudents()[j]);
}
System.out.println();
}
}
public void addStudent(String studentName) {
students[numberOfStudents] = new Student(studentName);
numberOfStudents ++;
}
public void addCourse(String courseName) {
courses[numberOfCourses] = new Course(courseName);
numberOfCourses ++;
}
public void selectCourse(String studentName, String courseName) {
for (int i = 0; i < numberOfStudents; i ++) {
if (students[i].studentName.equals(studentName)) {
students[i].addCourse(courseName);
}
}
for (int i = 0; i < numberOfCourses; i ++) {
if (courses[i].courseName.equals(courseName)) {
courses[i].addStudent(studentName);
}
}
}
public void dropCourse(String studentName, String courseName) {
for (int i = 0; i < numberOfStudents; i ++) {
if (students[i].studentName.equals(studentName)) {
students[i].dropCourse(courseName);
}
}
for (int i = 0; i < numberOfCourses; i ++) {
if (courses[i].courseName.equals(courseName)) {
courses[i].dropStudent(studentName);
}
}
}
public CourseSelection() {
numberOfCourses = numberOfStudents = 0;
}
public static class Course {
public Course() {
numberOfStudents = 0;
}
public Course(String courseName) {
this.courseName = courseName;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void addStudent(String student) {
students[numberOfStudents] = student;
numberOfStudents ++;
}
public void dropStudent(String student) {
for (int i = 0; i < numberOfStudents; i ++) {
if (this.students[i].equals(student)) {
for (int j = i + 1; j < numberOfStudents; j ++) {
this.students[j - 1] = this.students[j];
}
numberOfStudents --;
i --;
}
}
}
public void clear() {
numberOfStudents = 0;
}
String courseName;
String[] students = new String[100];
int numberOfStudents;
}
public class Student {
public Student() {
numberOfCourses = 0;
}
public Student(String studentName) {
this.studentName = studentName;
}
public void addCourse(String course) {
courses[numberOfCourses] = course;
numberOfCourses ++;
}
public void dropCourse(String course) {
for (int i = 0; i < numberOfCourses; i ++) {
if (this.courses[i].equals(course)) {
for (int j = i + 1; j < numberOfCourses; j ++) {
this.courses[j - 1] = this.courses[j];
}
numberOfCourses --;
i --;
}
}
}
public String[] getCourses() {
return courses;
}
public int getNumberOfCourses() {
return numberOfCourses;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
String studentName;
String[] courses = new String[100];
int numberOfCourses;
}
Course[] courses = new Course[100];
Student[] students = new Student[100];
int numberOfCourses, numberOfStudents;
}

  

[java作业]Fan、求直线交点、Triangle2D、选课的更多相关文章

  1. UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)

    Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...

  2. POJ_1269_Intersecting Lines_求直线交点

    POJ_1269_Intersecting Lines_求直线交点 Description We all know that a pair of distinct points on a plane ...

  3. Uva 11178 Morley's Theorem 向量旋转+求直线交点

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=9 题意: Morlery定理是这样的:作三角形ABC每个 ...

  4. poj 1269 Intersecting Lines——叉积求直线交点坐标

    题目:http://poj.org/problem?id=1269 相关知识: 叉积求面积:https://www.cnblogs.com/xiexinxinlove/p/3708147.html什么 ...

  5. poj1269 (叉积求直线的交点)

    题目链接:https://vjudge.net/problem/POJ-1269 题意:给出4个顶点,表示两条直线,求这两条直线的相交情况,重合输出LINE,平行输出NONE,相交于一点输出该点的距离 ...

  6. 谈谈"求线段交点"的几种算法(js实现,完整版)

    "求线段交点"是一种非常基础的几何计算, 在很多游戏中都会被使用到. 下面我就现学现卖的把最近才学会的一些"求线段交点"的算法总结一下, 希望对大家有所帮助.  ...

  7. 计算几何——直线交点poj1269

    求直线交点还是要推一个公式的.. 见博客https://blog.csdn.net/u013050857/article/details/40923789 还要学一下向量的定点比分法 另外poj精度好 ...

  8. 个人项目作业$\cdot$求交点个数

    个人项目作业\(\cdot\)求交点个数 一.作业要求简介 本次作业是北航计算机学院软件工程课程的个人项目作业,个人开发能力对于软件开发团队是至关重要的,本项目旨在通过一个求几何图形的交点的需求来使学 ...

  9. hdu 2528:Area(计算几何,求线段与直线交点 + 求多边形面积)

    Area Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 数据结构(C语言版)---栈

    1.栈:仅在表尾进行插入和删除操作的线性表.后进先出LIFO. 1)表尾端(允许插入和删除的一端)为栈顶,表头端(不允许插入和删除的一端)为栈底. 2)入栈:插入元素的操作.出栈:删除栈顶元素 3)栈 ...

  2. 2020-3 网络对抗技术 20175120 exp5 信息搜集与漏洞扫描

    目录 实践目标 实践内容 各种搜索技巧的应用 搜索特定类型的文件Google Hacking 搜索网站目录结构 DNS IP注册信息的查询 网络侦查 基本的扫描技术:主机发现.端口扫描.OS及服务版本 ...

  3. jmeter事务控制器

    jmeter事务控制器常用于压力测试时如果一个功能包括多个请求时,需要测试这个功能的压力情况,则需要把多个请求放到一个事务控制器里面

  4. [git] github 推送以及冲突的解决,以及一些命令

    推送以及冲突的解决:(我的觉得先看完) (正常情况就是把修改的文件 git add 然后git commit 然后推送就行啦): 下面是一些命令 1.查看分支状态(查看所有:当前检出分支的前面会有星号 ...

  5. react: typescript import images alias

    1.webpack.config.js resolve: { extensions: ["ts", "tsx", "js", "j ...

  6. pytorch 文本情感分类和命名实体识别NER中LSTM输出的区别

    文本情感分类: 文本情感分类采用LSTM的最后一层输出 比如双层的LSTM,使用正向的最后一层和反向的最后一层进行拼接 def forward(self,input): ''' :param inpu ...

  7. pytorch中tensor的属性 类型转换 形状变换 转置 最大值

    import torch import numpy as np a = torch.tensor([[[1]]]) #只有一个数据的时候,获取其数值 print(a.item()) #tensor转化 ...

  8. SpringBoot【新手学习记录篇】

    1. 启动方式: 在idea中的application.java右键run as 命令行进入项目目录,使用命令 mvn spring-boot:run 使用mvn install进行打包,然后进入ta ...

  9. MySQL数据库缓存操作

    安装: 启动的话: -d:以后台的方式进行: -l:选择监听指定的ip服务地址:-m:给他分配多大的内存:-p:端口号默认的端口为11211的服务端口: 另一个: 安装:telnet 这个可以用来测试 ...

  10. 2019-2020-1 20199328《Linux内核原理与分析》第三周作业

    加载内核 这里可以看出有些东西隔一段时间就会打印出来 查看mymain.c 开头的一些语句不再描述 每10000次循环打印一次 这里还是针对的mymain.c文件,这里我们可以根据自己的计算机对频率进 ...