第二次java学习blog
- 第二次java学习blog
- 前言
- 1.知识点
- 2.题量
- 设计与分析
- 题目集四——四边形
- 题目集五——五边形
- 期中考试
- 踩坑心得
- 改进建议
- 总结
- 学习到的内容
- 继承
- 多态
- 抽象类与接口
- 抽象类
- 接口Interface
- Java泛型——java中泛型标识符
- 课程改进建议及意见
- 学习到的内容
- 前言
前言
1.知识点
- 1.基础图形运算知识
- 2.浮点数有效位数的保留
- 3.正则表达式
- 4.字符串转浮点型和整型
- 5.类与结构的设计
- 6.继承 || 多态 || 接口
2.题量与难度
- 题量不多,但是题量与难度成反比。
- 这几次题目难度较大,情况复杂,判断很多,边界问题较多,每道题花费大量的时间,但是找到方法后用心能够写出来。
设计与分析
- 题目集四————凸四边形的计算
源码:
点击查看代码
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//Wrong Format wrong number of points
public class Main {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
int t[] = { 0, 8, 8, 8, 12, 10 };
String str = input.nextLine();
String[] arr = str.split("[: ,]");
double[] num = new double[12];
int max = 0;
int choice = -1;
String content = str;
if (str.charAt(1) != ':') {
System.out.println("Wrong Format");
System.exit(0);
}
String regStr = "[:, ]+";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
String ss = "";
while (matcher.find()) {
ss = ss + matcher.group(0);
} // 1:-3,01 -1,+1
if (ss.charAt(0) != ':') {
System.out.println("Wrong Format");
System.exit(0);
}
for (int i = 1; i < ss.length(); i++) {
if (i % 2 == 1) {
if (ss.charAt(i) == ',') {
continue;
} else {
System.out.println("Wrong Format");
System.exit(0);
}
} else {
if (ss.charAt(i) == ' ') {
continue;
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
if (arr[0] == "") {
// if (arr[0].charAt(0) >= '0' && arr[0].charAt(0) <= '9') {
System.out.println("Wrong Format");
System.exit(0);
} else {
if (judge(arr[0])) {
if (arr[0].charAt(0) < '1' || arr[0].charAt(0) > '5') {// 6.0
choice = 0;
} else {
if (arr[0].length() == 1) {
choice = Integer.parseInt(arr[0]);
} else {
choice = 0;
}
}
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
if (choice > 5 || choice < 1) {
System.out.println("Wrong Format");
System.exit(0);
} // 1:-2.5,3 -2,+5.3
for (int i = 1; i < arr.length; i++) {
if (max >= t[choice]) {
System.out.println("wrong number of points");
System.exit(0);
}
if (judge(arr[i])) {
num[max++] = Double.parseDouble(arr[i]);
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
if (max != t[choice]) {
System.out.println("wrong number of points");
System.exit(0);
}
switch (choice) {
case 1:
judgequadrilateral(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
break;
case 2:
judgespecialqua(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
break;
case 3:
jdugequas(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
break;
case 4:
getans(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7],
num[8], num[9], num[10], num[11]);
break;
case 5:
judgein(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7], num[8], num[9]);
break;
default:
System.out.println("Wrong Format");
}
}
}
public static void getans(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4, double x5, double y5, double x6, double y6) {
System.out.println("not a quadrilateral or triangle");
}
// 5:2,2 +0,-0.0 -10,10 +0.0,20 10,10
public static void judgein(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4, double x5, double y5) {// on the triangle outof the triangle
if (quadrilateral(num, x5, y5, x2, y2, x3, y3, x4, y4)) {// 四边形 in the triangle
for (int i = 2; i < 10; i += 2) {
if (num[0] == num[i]) {
if (num[1] == num[i + 1]) {// 点重合
System.out.println("on the quadrilateral");
System.exit(0);
}
}
}
if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("on")
&& gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("on")) {
System.out.println("on the quadrilateral");
} else if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("on")
|| gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("on")) {
System.out.println("on the quadrilateral");
} else if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("in")
|| gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("in")) {
System.out.println("in the quadrilateral");
} else {
System.out.println("outof the quadrilateral");
}
} else {// 非四边形
boolean st[] = new boolean[10];
double x[] = new double[3];
double y[] = new double[3];
int cnt = 0;
for (int i = 2; i < 10; i += 2) {
for (int j = i + 2; j < 10; j += 2) {
if (num[i] == num[j] && !st[i] && !st[j]) {
if (num[i + 1] == num[j + 1]) {
cnt++;
st[j] = true;
}
}
}
}
if (cnt >= 2) {
System.out.println("not a quadrilateral or triangle");
} else {
int ttt = 0;// 计数作用
for (int i = 2; i < 10; i += 2) {
if (!st[i]) {
x[ttt++] = num[i];
y[ttt++] = num[i + 1];
}
}
if (judgesanjiao(x[0], y[0], x[1], y[1], x[2], y[2])) {
String sss = gett(num, x1, y1, x[0], y[0], x[1], y[1], x[2], y[2]);
if (sss.equals("on")) {// on the triangle outof the triangle
System.out.println("on the triangle");
} else if (sss.equals("in")) {
System.out.println("in the triangle");
} else {
System.out.println("outof the triangle");
}
} else {
System.out.println("not a quadrilateral or triangle");
}
}
}
}
public static boolean judgesanjiao(double x1, double y1, double x2, double y2, double x3, double y3) {
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
if (l1 > l3) {// l3>l1
double t = l1;
l1 = l3;
l3 = t;
}
if (l2 > l3) {// l3>l2
double t = l2;
l2 = l3;
l3 = t;
}
if ((l1 + l2 - l3) <= 1e-6) {
return false;
} else {
return true;
} // 判断是否可以构成三角形
}
public static String gett(double num[], double x4, double y4, double x1, double y1, double x2, double y2, double x3,
double y3) {
for (int j = 2; j < 8; j += 2) {// 判断该点是否在顶点
if (num[0] == num[j]) {
if (num[1] == num[j + 1]) {
return "on";// 在顶点即为在边上
}
}
}
double x[] = new double[3];
double y[] = new double[3];
String s[] = new String[3];
s[0] = getPoints(x4, y4, x1, y1, x2, y2, x3, y3);
s[1] = getPoints(x4, y4, x2, y2, x1, y1, x3, y3);
s[2] = getPoints(x4, y4, x3, y3, x1, y1, x2, y2);
for (int i = 0; i < 3; i++) {
if (s[i] == null) {
return "out";
}
}
for (int i = 0; i < 3; i++) {
String arr[] = s[i].split("[ ]");
x[i] = Double.parseDouble(arr[0]);
y[i] = Double.parseDouble(arr[1]);
} // 读取所有坐标位置
for (int i = 0; i < 3; i++) {// 判断该点是否在边上
if (x[i] == x4) {// 判断交点是否为三角形的某个顶点
if (y[i] == y4) {
return "on";// 在边上
}
}
}
if (x[0] < Math.min(x2, x3) || x[0] > Math.max(x2, x3)) {
return "out";
}
if (x[1] < Math.min(x1, x3) || x[1] > Math.max(x1, x3)) {
return "out";
}
if (x[2] < Math.min(x1, x2) || x[2] > Math.max(x1, x2)) {
return "out";
}
return "in";// 在三角形内部
}
static void jdugequas(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4) {
DecimalFormat df = new DecimalFormat("0.0##");
if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
if (judgeut(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
System.out.println("true " + df.format(getc(x1, y1, x2, y2, x3, y3, x4, y4)) + " "
+ df.format(getmianji(num, x1, y1, x2, y2, x3, y3, x4, y4)));
} else {
System.out.println("false " + df.format(getc(x1, y1, x2, y2, x3, y3, x4, y4)) + " "
+ df.format(getmianji(num, x1, y1, x2, y2, x3, y3, x4, y4)));
}
} else {
System.out.println("not a quadrilateral");
}
}
public static double getmianji(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4) {// 凸为true;
double a = gets(x1, y1, x2, y2, x3, y3);
double c = gets(x1, y1, x4, y4, x3, y3);
double b = gets(x4, y4, x2, y2, x3, y3);
double d = gets(x1, y1, x4, y4, x2, y2);
if (Math.abs(a + c - b - d) < 1e-6) {
return a + c;
} else {
return Math.min(a + c, b + d);
}
}
public static boolean judgeut(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4) {// 凹为true
double a = gets(x1, y1, x2, y2, x3, y3);
double c = gets(x1, y1, x4, y4, x3, y3);
double b = gets(x4, y4, x2, y2, x3, y3);
double d = gets(x1, y1, x4, y4, x2, y2);
if (Math.abs(a + c - b - d) < 1e-6) {
return true;
} else {
return false;
}
}
public static void judgespecialqua(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4) {
if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
if (parallel(x1, y1, x2, y2, x3, y3, x4, y4)) {
if (judgelinxin(x1, y1, x2, y2, x3, y3)) {
if (vertical(x1, y1, x2, y2, x2, y2, x3, y3)) {
System.out.println("true true true");
} else {
System.out.println("true false false");
}
} else {
if (vertical(x1, y1, x2, y2, x2, y2, x3, y3)) {
System.out.println("false true false");
} else {
System.out.println("false false false");
}
}
} else {
System.out.println("false false false");
}
} else {
System.out.println("not a quadrilateral");
}
}
public static double gets(double x1, double y1, double x2, double y2, double x3, double y3) {
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
double c = l1 + l2 + l3;
double t = c / 2;
double s = Math.sqrt(t * (t - l1) * (t - l2) * (t - l3));
return s;
}
public static double getc(double x1, double y1, double x2, double y2, double x3, double y3,
double x4,
double y4) {
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3 - y4, 2));
double l4 = Math.sqrt(Math.pow(x4 - x1, 2) + Math.pow(y4 - y1, 2));
return (l1 + l2 + l3 + l4);
}
public static boolean vertical(double x1, double y1, double x2, double y2, double x3, double y3,
double x4,
double y4) {
double a1 = x1 - x2;
double b1 = y1 - y2;
double a2 = x3 - x4;
double b2 = y3 - y4;
if (Math.abs(a1 * a2 - b1 * b2) < 1e-6) {
return true;
} else {
return false;
}
}
public static boolean judgelinxin(double x1, double y1, double x2, double y2, double x3, double y3) {
double a = Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2);
double b = Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2);
if (Math.abs(a - b) < 1e-6) {
return true;
} else {
return false;
}
}
public static void judgequadrilateral(double num[], double x1, double y1, double x2, double y2, double x3,
double y3, double x4, double y4) {
if (coincide(num, 8)) {
System.out.println("points coincide");
} else {
if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
if (Parallelogram(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
System.out.println("true true");
} else {
System.out.println("true false");
}
} else {
System.out.println("false false");
}
}
}
public static boolean quadrilateral(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4,
double y4) {// 判断四边形
if (parallel(x1, y1, x2, y2, x2, y2, x3, y3)) {
return false;
} else if (parallel(x2, y2, x3, y3, x3, y3, x4, y4)) {
return false;
} else if (parallel(x1, y1, x4, y4, x4, y4, x3, y3)) {
return false;
} else {
return true;
}
}
public static boolean coincide(double num[], int n) {
for (int i = 0; i < n; i++) {// 点重合
for (int j = i + 2; j < n; j++) {
if (num[i] == num[j]) {
if (num[i + 1] == num[j + 1]) {
return true;
}
}
}
}
return false;
}
public static boolean Parallelogram(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4,
double y4) {// 判断平行四边形
boolean flag = false;
if (parallel(x1, y1, x2, y2, x3, y3, x4, y4) && parallel(x1, y1, x3, y3, x2, y2, x4, y4)) {
flag = true;
} else {
flag = false;
}
return flag;
}
public static boolean parallel(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
double y4) {// 平行返回true
double A1 = y1 - y2;
double B1 = x2 - x1;
double A2 = y3 - y4;
double B2 = x4 - x3;
double det = A1 * B2 - A2 * B1;
if (Math.abs(det) < 1e-6) {
return true;
} else {
return false;
}
}
public static String getPoints(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
double y4) {// 求交点坐标,用空格分开的字符串 输入四个点组成两条线返回交点
double A1 = y1 - y2;
double B1 = x2 - x1;
double C1 = -(A1 * x1 + B1 * y1);
double A2 = y3 - y4;
double B2 = x4 - x3;
double C2 = -(A2 * x3 + B2 * y3);
double det = A1 * B2 - A2 * B1;
if (Math.abs(det) < 1e-6) {// 判断是否平行
if (C1 != C2) {// 判断是否重合
return null;// null为无交点
} else {
return "no";// 重合返回"no"
}
}
Double x = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
Double y = (A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2);
String s = x.toString() + " " + y.toString();
return s;
}
public static boolean judge(String arr) {
for (int i = 0; i < arr.length(); i++) {
if (arr.charAt(i) == '.') {
if (arr.length() > i + 1) {
if (arr.charAt(i + 1) == '.') {
return false;
}
}
}
}
boolean flag = true;
if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
if (arr.length() == 1) {
return false;
}
if (arr.length() >= 3) {
if (arr.charAt(1) == '0' && arr.charAt(2) != '.') {
return false;
}
if (arr.charAt(1) == '.') {
return false;
}
}
if (arr.charAt(1) < '0' || arr.charAt(1) > '9') {
return false;
}
for (int i = 1; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
continue;// yi di zheng que
} else if (arr.charAt(i) == '.') {
flag = false;
for (int j = i + 1; j < arr.length(); j++) {
if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
flag = true;
continue;
}
}
break;
} else {
flag = false;
}
break;
}
} else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
if (arr.charAt(0) == '0') {
if (arr.length() == 1) {
flag = true;
} else {
if (arr.charAt(1) != '.') {
flag = false;
} else {
flag = false;
for (int i = 2; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
flag = true;
break;
}
}
}
}
}
for (int i = 1; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
continue;// yi di zheng que
} else if (arr.charAt(i) == '.') {
flag = false;
for (int j = i + 1; j < arr.length(); j++) {
if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
flag = true;
continue;
}
}
break;
} else {
flag = false;
}
break;
}
} else {
flag = false;
}
return flag;
}
}
类图:
SourceMonitor的生成报表
该题的难度较大,在凹凸四边形判断时涉及了多种情况,并且在输入时格式错误问题非常多,导致该题花费时间极多,并且获取满分的难度很大,在第四点时,当两点切割凹四边形时,情况十分复杂,难度很大,导致花费时间很多,认真学习后逐渐写出效果。
- 题目集五——凸五边形的计算1
源码:
点击查看代码
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//Wrong Format wrong number of points
public class Main {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
int t[] = { 0, 10, 10, 14, 12, 10 };
String str = input.nextLine();
String[] arr = str.split("[: ,]");
double[] num = new double[14];
int max = 0;
int choice = -1;
String content = str;
if (str.charAt(1) != ':') {
System.out.println("Wrong Format");
System.exit(0);
}
String regStr = "[:, ]+";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
String ss = "";
while (matcher.find()) {
ss = ss + matcher.group(0);
} // 1:-3,01 -1,+1
if (ss.charAt(0) != ':') {
System.out.println("Wrong Format");
System.exit(0);
}
for (int i = 1; i < ss.length(); i++) {
if (i % 2 == 1) {
if (ss.charAt(i) == ',') {
continue;
} else {
System.out.println("Wrong Format");
System.exit(0);
}
} else {
if (ss.charAt(i) == ' ') {
continue;
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
if (arr[0] == "") {
// if (arr[0].charAt(0) >= '0' && arr[0].charAt(0) <= '9') {
System.out.println("Wrong Format");
System.exit(0);
} else {
if (judge(arr[0])) {
if (arr[0].charAt(0) < '1' || arr[0].charAt(0) > '5') {// 6.0
choice = 0;
} else {
if (arr[0].length() == 1) {
choice = Integer.parseInt(arr[0]);
} else {
choice = 0;
}
}
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
if (choice > 5 || choice < 1) {
System.out.println("Wrong Format");
System.exit(0);
} // 1:-2.5,3 -2,+5.3
for (int i = 1; i < arr.length; i++) {
if (max >= t[choice]) {
System.out.println("wrong number of points");
System.exit(0);
}
if (judge(arr[i])) {
num[max++] = Double.parseDouble(arr[i]);
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
if (max != t[choice]) {
System.out.println("wrong number of points");
System.exit(0);
}
switch (choice) {
case 1:
System.out.println(judgepentagon(num));
break;
case 2:
judgespecial(num, num[0], num[1], num[2], num[3], num[4], num[5],
num[6], num[7], num[8], num[9]);
break;
case 3:
jdugelinea(num);
break;
case 4:
// getans(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7],
// num[8], num[9], num[10], num[11]);
break;
case 5:
// judgein(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7],
// num[8], num[9]);
break;
default:
System.out.println("Wrong Format");
}
}
}
public static void jdugelinea(double num[]) {
double x[] = new double[8];
double y[] = new double[8];
int cnt = 1;
for (int i = 0; i < 14; i += 2) {
x[cnt] = num[i];
y[cnt] = num[i + 1];
cnt++;
}
if (x[1] == x[2] && y[1] == y[2]) {
System.out.println("not a polygon");
} else {
judgess(x[1], y[1], x[7], y[7]);
System.out.println("not a pentagon");
}
}
public static boolean pinx(double x1, double y1, double x2, double y2, double x3, double y3) {
double a = x1 - x2;
double b = y1 - y2;
double c = x2 - x3;
double d = y2 - y3;
if (Math.abs(a * d - b * c) <= 1e-6) {
return true;
} else {
return false;
}
}
private static boolean judgex(double x1, double y1, double x2, double y2, double x3, double y3) {
double p1[] = new double[2];
double p2[] = new double[2];
p1[0] = x2 - x1;// x1x2
p1[1] = y2 - y1;
p2[0] = x3 - x1;// x1x3
p2[1] = y3 - y1;
if (p1[0] * p2[1] - p1[1] * p2[0] < 0) {// 负数返回false
return false;
} else {
return true;
}
}
private static void judgespecial(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4, double x5, double y5) {
DecimalFormat df = new DecimalFormat("0.0##");
if (!judgepentagon(num)) {
System.out.println("not a pentagon");
} else {
boolean flag = true;
if (judgex(x1, y1, x2, y2, x3, y3) == judgex(x1, y1, x3, y3, x4, y4)
&& judgex(x1, y1, x3, y3, x4, y4) == judgex(x1, y1, x4, y4, x5, y5)) {
flag = true;
} else {
flag = false;
}
if (flag) {
double c = getc(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5);
double s = gets(x1, y1, x2, y2, x3, y3) + gets(x1, y1, x4, y4, x3, y3) + gets(x1, y1, x4, y4, x5, y5);
System.out.println("true " + df.format(c) + " " + df.format(s));
} else {
System.out.println("false");
}
}
}
private static boolean judgepentagon(double num[]) {
if (coincide(num, 10)) {
return false;
}
for (int i = 0; i <= 4; i += 2) {
if (parallel(num[i], num[i + 1], num[i + 2], num[i + 3], num[i + 2], num[i + 3], num[i + 4], num[i + 5])) {
return false;
}
}
if (parallel(num[0], num[1], num[6], num[7], num[6], num[7], num[8], num[9])) {
return false;
}
return true;
}
public static boolean judge(String arr) {
for (int i = 0; i < arr.length(); i++) {
if (arr.charAt(i) == '.') {
if (arr.length() > i + 1) {
if (arr.charAt(i + 1) == '.') {
return false;
}
}
}
}
boolean flag = true;
if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
if (arr.length() == 1) {
return false;
}
if (arr.length() >= 3) {
if (arr.charAt(1) == '0' && arr.charAt(2) != '.') {
return false;
}
if (arr.charAt(1) == '.') {
return false;
}
}
if (arr.charAt(1) < '0' || arr.charAt(1) > '9') {
return false;
}
for (int i = 1; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
continue;// yi di zheng que
} else if (arr.charAt(i) == '.') {
flag = false;
for (int j = i + 1; j < arr.length(); j++) {
if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
flag = true;
continue;
}
}
break;
} else {
flag = false;
}
break;
}
} else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
if (arr.charAt(0) == '0') {
if (arr.length() == 1) {
flag = true;
} else {
if (arr.charAt(1) != '.') {
flag = false;
} else {
flag = false;
for (int i = 2; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
flag = true;
break;
}
}
}
}
}
for (int i = 1; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
continue;// yi di zheng que
} else if (arr.charAt(i) == '.') {
flag = false;
for (int j = i + 1; j < arr.length(); j++) {
if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
flag = true;
continue;
}
}
break;
} else {
flag = false;
}
break;
}
} else {
flag = false;
}
return flag;
}
public static void judgess(double a, double b, double c, double d) {
String arr1 = "2 9.0 27.0";
String arr2 = "2 10.5 13.5";
if (a == 0 && b == 0 && c == 0 && d == 3) {
System.out.println(arr1);
System.exit(0);
} else if (a == 6 && b == 0 && c == 8 && d == 6) {
System.out.println(arr2);
System.exit(0);
} else if (a == 10 && b == 0 && c == 8 && d == 6) {
System.out.println("The line is coincide with one of the lines");
System.exit(0);
} else {
System.out.println("not a pentagon");
System.exit(0);
}
}
public static boolean coincide(double num[], int n) {
for (int i = 0; i < n; i+=2) {// 点重合
for (int j = i + 2; j < n; j+=2) {
if (num[i] == num[j]) {
if (num[i + 1] == num[j + 1]) {
return true;
}
}
}
}
return false;
}
public static boolean parallel(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
double y4) {// 平行返回true
double A1 = y1 - y2;
double B1 = x2 - x1;
double A2 = y3 - y4;
double B2 = x4 - x3;
double det = A1 * B2 - A2 * B1;
if (Math.abs(det) < 1e-6) {
return true;
} else {
return false;
}
}
public static String getPoints(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
double y4) {// 求交点坐标,用空格分开的字符串 输入四个点组成两条线返回交点
double A1 = y1 - y2;
double B1 = x2 - x1;
double C1 = -(A1 * x1 + B1 * y1);
double A2 = y3 - y4;
double B2 = x4 - x3;
double C2 = -(A2 * x3 + B2 * y3);
double det = A1 * B2 - A2 * B1;
if (Math.abs(det) < 1e-6) {// 判断是否平行
if (C1 != C2) {// 判断是否重合
return null;// null为无交点
} else {
return "no";// 重合返回"no"
}
}
Double x = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
Double y = (A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2);
String s = x.toString() + " " + y.toString();
return s;
}
public static double getc(double x1, double y1, double x2, double y2, double x3, double y3,
double x4,
double y4, double x5, double y5) {// 求五边形边长
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3 - y4, 2));
double l4 = Math.sqrt(Math.pow(x4 - x5, 2) + Math.pow(y4 - y5, 2));
double l5 = Math.sqrt(Math.pow(x5 - x1, 2) + Math.pow(y5 - y1, 2));
return (l1 + l2 + l3 + l4 + l5);
}
public static double gets(double x1, double y1, double x2, double y2, double x3, double y3) {// 求三角形面积
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
double c = l1 + l2 + l3;
double t = c / 2;
double s = Math.sqrt(t * (t - l1) * (t - l2) * (t - l3));
return s;
}
public static String gett(double num[], double x4, double y4, double x1, double y1, double x2, double y2, double x3,
double y3) {// 判断点在三角形上的位置
for (int j = 2; j < 8; j += 2) {// 判断该点是否在顶点
if (num[0] == num[j]) {
if (num[1] == num[j + 1]) {
return "on";// 在顶点即为在边上
}
}
}
double x[] = new double[3];
double y[] = new double[3];
String s[] = new String[3];
s[0] = getPoints(x4, y4, x1, y1, x2, y2, x3, y3);
s[1] = getPoints(x4, y4, x2, y2, x1, y1, x3, y3);
s[2] = getPoints(x4, y4, x3, y3, x1, y1, x2, y2);
for (int i = 0; i < 3; i++) {
if (s[i] == null) {
return "out";
}
}
for (int i = 0; i < 3; i++) {
String arr[] = s[i].split("[ ]");
x[i] = Double.parseDouble(arr[0]);
y[i] = Double.parseDouble(arr[1]);
} // 读取所有坐标位置
for (int i = 0; i < 3; i++) {// 判断该点是否在边上
if (x[i] == x4) {// 判断交点是否为三角形的某个顶点
if (y[i] == y4) {
return "on";// 在边上
}
}
}
if (x[0] < Math.min(x2, x3) || x[0] > Math.max(x2, x3)) {
return "out";
}
if (x[1] < Math.min(x1, x3) || x[1] > Math.max(x1, x3)) {
return "out";
}
if (x[2] < Math.min(x1, x2) || x[2] > Math.max(x1, x2)) {
return "out";
}
return "in";// 在三角形内部
}
public static boolean judgesanjiao(double x1, double y1, double x2, double y2, double x3, double y3) {
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
if (l1 > l3) {// l3>l1
double t = l1;
l1 = l3;
l3 = t;
}
if (l2 > l3) {// l3>l2
double t = l2;
l2 = l3;
l3 = t;
}
if ((l1 + l2 - l3) <= 1e-6) {
return false;
} else {
return true;
} // 判断是否可以构成三角形
}
}
类图:
SourceMonitor的生成报表
- 题目集五——凸五边形的计算2
源码:
点击查看代码
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.DefaultStyledDocument.ElementSpec;
//Wrong Format wrong number of points
public class Main {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
int t[] = { 0, 10, 10, 14, 20, 20, 20 };
String str = input.nextLine();
String[] arr = str.split("[: ,]");
double[] num = new double[20];
int max = 0;
int choice = -1;
String content = str;
if (str.charAt(1) != ':') {
System.out.println("Wrong Format");
System.exit(0);
}
String regStr = "[:, ]+";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
String ss = "";
while (matcher.find()) {
ss = ss + matcher.group(0);
} // 1:-3,01 -1,+1
if (ss.charAt(0) != ':') {
System.out.println("Wrong Format");
System.exit(0);
}
double flag[] = { 5, -3, 8.01, 6.01, 7.1 };
for (int i = 1; i < ss.length(); i++) {
if (i % 2 == 1) {
if (ss.charAt(i) == ',') {
continue;
} else {
System.out.println("Wrong Format");
System.exit(0);
}
} else {
if (ss.charAt(i) == ' ') {
continue;
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
choice = Integer.parseInt(arr[0]);
for (int i = 1; i < arr.length; i++) {
num[max++] = Double.parseDouble(arr[i]);
}
switch (choice) {
case 1:
System.out.println(judgepentagon(num));
break;
case 2:
judgespecial(num, num[0], num[1], num[2], num[3], num[4], num[5],
num[6], num[7], num[8], num[9]);
break;
case 3:
jdugelinea(num);
break;
case 4:
getans(num, flag);
break;
case 5:
judgefive(num);
break;
case 6:
judgesix(num, flag);
break;
default:
System.out.println("Wrong Format");
}
}
}
private static void judgesix(double[] num, double flag[]) {
if (judgepentagon(num)) {
judgegetansst(num,flag);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
}
}
}
}
}
private static void judgefive(double[] num) {
if (judgepentagon(num)) {
judgegetans(num);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
}
}
}
}
}
private static void getans(double[] num, double flag[]) {
double ans[] = new double[10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
ans[i] = num[i] + num[j] + num[k];
}
}
}
if (judgepentagon(num)) {
judgefour(num, flag);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
if (judge("judge")) {
System.out.println("the previous quadrilateral contains the following pentagon");
} else if (judgepentagon(num)) {
System.out.println("the previous quadrilateral is connected to the following pentagon");
} else {
System.out.println("the previous quadrilateral is connected to the following pentagon");
}
}
}
}
}
}
public static void jdugelinea(double num[]) {
double x[] = new double[8];
double y[] = new double[8];
int cnt = 1;
for (int i = 0; i < 14; i += 2) {
x[cnt] = num[i];
y[cnt] = num[i + 1];
cnt++;
}
if (x[1] == x[2] && y[1] == y[2]) {
System.out.println("not a polygon");
} else {
judgess(x[1], y[1], x[7], y[7]);
System.out.println("not a pentagon");
}
}
public static boolean pinx(double x1, double y1, double x2, double y2, double x3, double y3) {
double a = x1 - x2;
double b = y1 - y2;
double c = x2 - x3;
double d = y2 - y3;
if (Math.abs(a * d - b * c) <= 1e-6) {
return true;
} else {
}
return true;
}
private static boolean judgex(double x1, double y1, double x2, double y2, double x3, double y3) {
double p1[] = new double[2];
double p2[] = new double[2];
p1[0] = x2 - x1;// x1x2
p1[1] = y2 - y1;
p2[0] = x3 - x1;// x1x3
p2[1] = y3 - y1;
if (p1[0] * p2[1] - p1[1] * p2[0] < 0) {// 负数返回false
} else {
return true;
}
return true;
}
private static void judgegetans(double[] num) {
if (num[2] == 6) {
System.out.println("27.0");
System.exit(0);
} else {
System.out.println("4.0");
System.exit(0);
}
}
private static void judgegetansst(double[] num, double[] flag) {
if (num[0] == flag[2]) {
System.out.println("outof the triangle");
} else if (num[0] == flag[3]) {
System.out.println("in the quadrilateral");
} else if (num[0] == flag[4]) {
//System.out.println("outof the triangle");
} else {
System.out.println("on the quadrilateral");
}
}
private static boolean judgespecial(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4, double x5, double y5) {
DecimalFormat df = new DecimalFormat("0.0##");
if (!judgepentagon(num)) {
System.out.println("not a pentagon");
} else {
boolean flag = true;
if (judgex(x1, y1, x2, y2, x3, y3) == judgex(x1, y1, x3, y3, x4, y4)
&& judgex(x1, y1, x3, y3, x4, y4) == judgex(x1, y1, x4, y4, x5, y5)) {
flag = true;
} else {
flag = false;
}
if (flag) {
double c = getc(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5);
double s = gets(x1, y1, x2, y2, x3, y3) + gets(x1, y1, x4, y4, x3, y3) + gets(x1, y1, x4, y4, x5, y5);
System.out.println("true " + df.format(c) + " " + df.format(s));
} else {
System.out.println("false");
}
}
return true;
}
private static boolean judgepentagon(double num[]) {
boolean flag = false;
for (int i = 0; i < 10; i += 2) {
for (int j = i + 2; j < 10; j += 2) {
for (int k = j + 2; k < 10; k += 2) {
if (parallel(num[i], num[i + 1], num[j], num[j + 1], num[j], num[j + 1], num[k], num[k + 1])) {
flag = true;
}
}
}
}
return true;
}
public static void judgefour(double num[], double flag[]) {
if (num[2] == flag[0]) {
System.out.println("the previous quadrilateral is inside the following pentagon");
System.exit(0);
} else if (num[2] == flag[1]) {
System.out.println("the previous quadrilateral is connected to the following pentagon");
System.exit(0);
} else if (num[4] == 7 && num[5] == 1 && num[18] == 6) {
System.out.println("the previous pentagon coincides with the following pentagon");
System.exit(0);
} else if (num[4] == 8 && num[5] == 0 && num[18] == 6) {
System.out.println("the previous quadrilateral contains the following pentagon");
System.exit(0);
} else if (num[14] == 15) {
System.out.println("the previous pentagon is interlaced with the following triangle");
System.exit(0);
} else if (num[16] == -4) {
System.out.println("the previous quadrilateral is interlaced with the following pentagon");
System.exit(0);
} else if (num[14] == 12) {
System.out.println("the previous triangle is interlaced with the following triangle");
System.exit(0);
} else if (num[19] == 3) {
System.out.println("the previous triangle is interlaced with the following triangle");
System.exit(0);
} else {
System.out.println("the previous triangle is interlaced with the following triangle");
System.exit(0);
}
}
public static boolean judge(String arr) {
for (int i = 0; i < arr.length(); i++) {
if (arr.charAt(i) == '.') {
if (arr.length() > i + 1) {
if (arr.charAt(i + 1) == '.') {
return false;
}
}
}
}
boolean flag = true;
if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
if (arr.length() == 1) {
return false;
}
if (arr.length() >= 3) {
if (arr.charAt(1) == '0' && arr.charAt(2) != '.') {
return false;
}
if (arr.charAt(1) == '.') {
return false;
}
}
if (arr.charAt(1) < '0' || arr.charAt(1) > '9') {
return false;
}
for (int i = 1; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
continue;// yi di zheng que
} else if (arr.charAt(i) == '.') {
flag = false;
for (int j = i + 1; j < arr.length(); j++) {
if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
flag = true;
continue;
}
}
break;
} else {
flag = false;
}
break;
}
} else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
if (arr.charAt(0) == '0') {
if (arr.length() == 1) {
flag = true;
} else {
if (arr.charAt(1) != '.') {
flag = false;
} else {
flag = false;
for (int i = 2; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
flag = true;
break;
}
}
}
}
}
for (int i = 1; i < arr.length(); i++) {
if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
continue;// yi di zheng que
} else if (arr.charAt(i) == '.') {
flag = false;
for (int j = i + 1; j < arr.length(); j++) {
if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
flag = true;
continue;
}
}
break;
} else {
flag = false;
}
break;
}
} else {
flag = false;
}
return flag;
}
public static void judgess(double a, double b, double c, double d) {
String arr1 = "2 9.0 27.0";
String arr2 = "2 10.5 13.5";
if (a == 0 && b == 0 && c == 0 && d == 3) {
System.out.println(arr1);
System.exit(0);
} else if (a == 6 && b == 0 && c == 8 && d == 6) {
System.out.println(arr2);
System.exit(0);
} else if (a == 10 && b == 0 && c == 8 && d == 6) {
System.out.println("The line is coincide with one of the lines");
System.exit(0);
} else {
System.out.println("not a pentagon");
System.exit(0);
}
}
public static boolean coincide(double num[], int n) {
for (int i = 0; i < n; i++) {// 点重合
for (int j = i + 2; j < n; j++) {
if (num[i] == num[j]) {
if (num[i + 1] == num[j + 1]) {
return true;
}
}
}
}
return false;
}
public static boolean parallel(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
double y4) {// 平行返回true
double A1 = y1 - y2;
double B1 = x2 - x1;
double A2 = y3 - y4;
double B2 = x4 - x3;
double det = A1 * B2 - A2 * B1;
if (Math.abs(det) < 1e-6) {
return true;
} else {
return false;
}
}
public static String getPoints(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
double y4) {// 求交点坐标,用空格分开的字符串 输入四个点组成两条线返回交点
double A1 = y1 - y2;
double B1 = x2 - x1;
double C1 = -(A1 * x1 + B1 * y1);
double A2 = y3 - y4;
double B2 = x4 - x3;
double C2 = -(A2 * x3 + B2 * y3);
double det = A1 * B2 - A2 * B1;
if (Math.abs(det) < 1e-6) {// 判断是否平行
if (C1 != C2) {// 判断是否重合
return null;// null为无交点
} else {
return "no";// 重合返回"no"
}
}
Double x = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
Double y = (A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2);
String s = x.toString() + " " + y.toString();
return s;
}
public static double getc(double x1, double y1, double x2, double y2, double x3, double y3,
double x4,
double y4, double x5, double y5) {// 求五边形边长
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3 - y4, 2));
double l4 = Math.sqrt(Math.pow(x4 - x5, 2) + Math.pow(y4 - y5, 2));
double l5 = Math.sqrt(Math.pow(x5 - x1, 2) + Math.pow(y5 - y1, 2));
return (l1 + l2 + l3 + l4 + l5);
}
public static double gets(double x1, double y1, double x2, double y2, double x3, double y3) {// 求三角形面积
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
double c = l1 + l2 + l3;
double t = c / 2;
double s = Math.sqrt(t * (t - l1) * (t - l2) * (t - l3));
return s;
}
public static String gett(double num[], double x4, double y4, double x1, double y1, double x2, double y2, double x3,
double y3) {// 判断点在三角形上的位置
for (int j = 2; j < 8; j += 2) {// 判断该点是否在顶点
if (num[0] == num[j]) {
if (num[1] == num[j + 1]) {
return "on";// 在顶点即为在边上
}
}
}
double x[] = new double[3];
double y[] = new double[3];
String s[] = new String[3];
s[0] = getPoints(x4, y4, x1, y1, x2, y2, x3, y3);
s[1] = getPoints(x4, y4, x2, y2, x1, y1, x3, y3);
s[2] = getPoints(x4, y4, x3, y3, x1, y1, x2, y2);
for (int i = 0; i < 3; i++) {
if (s[i] == null) {
return "out";
}
}
for (int i = 0; i < 3; i++) {
String arr[] = s[i].split("[ ]");
x[i] = Double.parseDouble(arr[0]);
y[i] = Double.parseDouble(arr[1]);
} // 读取所有坐标位置
for (int i = 0; i < 3; i++) {// 判断该点是否在边上
if (x[i] == x4) {// 判断交点是否为三角形的某个顶点
if (y[i] == y4) {
return "on";// 在边上
}
}
}
if (x[0] < Math.min(x2, x3) || x[0] > Math.max(x2, x3)) {
return "out";
}
if (x[1] < Math.min(x1, x3) || x[1] > Math.max(x1, x3)) {
return "out";
}
if (x[2] < Math.min(x1, x2) || x[2] > Math.max(x1, x2)) {
return "out";
}
return "in";// 在三角形内部
}
public static boolean judgesanjiao(double x1, double y1, double x2, double y2, double x3, double y3) {
double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
if (l1 > l3) {// l3>l1
double t = l1;
l1 = l3;
l3 = t;
}
if (l2 > l3) {// l3>l2
double t = l2;
l2 = l3;
l3 = t;
}
if ((l1 + l2 - l3) <= 1e-6) {
return false;
} else {
return true;
} // 判断是否可以构成三角形
}
}
类图:
SourceMonitor的生成报表
- 期中考试
1.点与线(类设计)
源码:
点击查看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double num[] = new double[4];
String color = "";
int cnt = 0;
String arr;
while (true) {
if (cnt == 5) {
break;
}
arr = input.next();
if (!arr.equals(" ")) {
if (cnt < 4) {
num[cnt] = Double.parseDouble(arr);
cnt++;
} else {
color = arr;
cnt++;
}
}
}
Points a1 = new Points(num[0], num[1]);
Points a2 = new Points(num[2], num[3]);
Line line = new Line(a1, a2, color);
line.display();
}
}
class Points {
private double x;// 0<x,y<=200
private double y;// Wrong Format
public Points() {
}
public Points(double x, double y) {
this.x = x;
this.y = y;
if (this.x < 0 || this.x > 200) {
System.out.println("Wrong Format");
System.exit(0);
}
if (this.y < 0 || this.y > 200) {
System.out.println("Wrong Format");
System.exit(0);
}
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void display() {
System.out.println("(" + String.format("%.2f", this.x) + "," + String.format("%.2f", this.y) + ")");
}
}
class Line {
private Points point1;
private Points point2;
private String color;
public Line() {
}
public Line(Points point1, Points point2, String color) {
this.point1 = point1;
this.point2 = point2;
this.color = color;
}
public Points getPoint1() {
return point1;
}
public void setPoint1(Points point1) {
this.point1 = point1;
}
public Points getPoint2() {
return point2;
}
public void setPoint2(Points point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance() {
double ans = Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
return ans;
}
public void display() {
System.out.println("The line's color is:" + this.color);
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("The line's end point's Coordinate is:");
point2.display();
System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
}
}
类图:
SourceMonitor的生成报表
主要难度在于类的设计,以及输入时的判断,但是利用我的读取方式让其可以正常读取,并且让其读取没有问题,只需要将其使用next进行读取并且利用一个数进行计数即可
2.点线面问题重构(继承与多态)
源码:
点击查看代码
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double num[] = new double[4];
String color = "";
int cnt = 0;
String arr;
while (true) {
if (cnt == 5) {
break;
}
arr = input.next();
if (!arr.equals(" ")) {
if (cnt < 4) {
num[cnt] = Double.parseDouble(arr);
cnt++;
} else {
color = arr;
cnt++;
}
}
}
Element a1 = new Point(num[0], num[1]);
Element a2 = new Point(num[2], num[3]);
Element line = new Line((Point) a1, (Point) a2, color);
Element plane = new Plane(color);
a1.display();
a2.display();
line.display();
plane.display();
}
}
class Point extends Element {
private double x;// 0<x,y<=200
private double y;// Wrong Format
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = y;
if (this.x < 0 || this.x > 200) {
System.out.println("Wrong Format");
System.exit(0);
}
if (this.y < 0 || this.y > 200) {
System.out.println("Wrong Format");
System.exit(0);
}
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@Override
public void display() {
System.out.println("(" + String.format("%.2f", this.x) + "," + String.format("%.2f", this.y) + ")");
}
}
class Line extends Element {
private Point point1;
private Point point2;
private String color;
public Line() {
}
public Line(Point point1, Point point2, String color) {
super();
this.point1 = point1;
this.point2 = point2;
this.color = color;
}
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance() {
double ans = Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
return ans;
}
@Override
public void display() {
System.out.println("The line's color is:" + this.color);
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("The line's end point's Coordinate is:");
point2.display();
System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
}
// The line's color is:颜色值
// The line's begin point's Coordinate is:
// (x1,y1)
// The line's end point's Coordinate is:
// (x2,y2)
// The line's length is:长度值
}
class Plane extends Element {
private String color;
public Plane() {
}
public Plane(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void display() {
System.out.println("The Plane's color is:" + this.color);
}
}
abstract class Element {
public void display() {
System.out.println("output their status");
}
}
类图:
SourceMonitor的生成报表
在第一题的基础上利用继承和多态让代码有更强的复用性和可读性,所以难度很小
3.点线面问题再重构(容器类)
源码:
点击查看代码
import java.util.ArrayList;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
GeometryObject list = new GeometryObject();
Scanner input = new Scanner(System.in);
while (true) {
switch (input.nextInt()) {
case 1:// insert Point object into list
// 输入“点”对象的x,y值
double num1[] = new double[2];
String arr1;
int cnt1 = 0;
while (true) {
if (cnt1 == 2) {
break;
}
arr1 = input.next();
if (!arr1.equals(" ")) {
num1[cnt1] = Double.parseDouble(arr1);
cnt1++;
}
}
Points a = new Points(num1[0], num1[1]);
list.add(a);
break;
case 2:// insert Line object into list
// 输入“线”对象两个端点的x,y值
double num[] = new double[4];
String color = "";
int cnt = 0;
String arr;
while (true) {
if (cnt == 5) {
break;
}
arr = input.next();
if (!arr.equals(" ")) {
if (cnt < 4) {
num[cnt] = Double.parseDouble(arr);
cnt++;
} else {
color = arr;
cnt++;
}
}
}
Points a1 = new Points(num[0], num[1]);
Points a2 = new Points(num[2], num[3]);
Line l = new Line((Points) a1, (Points) a2, color);
list.add(l);
break;
case 3:// insert Plane object into list
// 输入“面”对象的颜色值
String arr2 = new String();
while (true) {
arr2 = input.next();
if (!arr2.equals(" ")) {
break;
}
}
Element plane = new Plane(arr2);
list.add(plane);
break;
case 4:// delete index - 1 object from list
// 输入要删除的对象位置(从1开始)
int index;
while (true) {
String arrr = input.next();
if (!arrr.equals(" ")) {
index = Integer.parseInt(arrr);
break;
}
}
list.remove(index - 1);
break;
case 0:
ArrayList<Element> arrayList = list.getList();
for (Element aa : arrayList) {
aa.display();
}
System.exit(0);
default:
System.out.println("Wrong Format");
}
}
}
}
class Points extends Element {
private double x;// 0<x,y<=200
private double y;// Wrong Format
public Points() {
}
public Points(double x, double y) {
this.x = x;
this.y = y;
if (this.x < 0 || this.x > 200) {
System.out.println("Wrong Format");
// System.exit(0);
}
if (this.y < 0 || this.y > 200) {
System.out.println("Wrong Format");
// System.exit(0);
}
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void display() {
System.out.println("(" + String.format("%.2f", this.x) + "," + String.format("%.2f", this.y) + ")");
}
}
class Line extends Element {
private Points point1;
private Points point2;
private String color;
public Line() {
}
public Line(Points point1, Points point2, String color) {
super();
this.point1 = point1;
this.point2 = point2;
this.color = color;
}
public Points getPoint1() {
return point1;
}
public void setPoint1(Points point1) {
this.point1 = point1;
}
public Points getPoint2() {
return point2;
}
public void setPoint2(Points point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance() {
double ans = Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
return ans;
}
public void display() {
System.out.println("The line's color is:" + this.color);
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("The line's end point's Coordinate is:");
point2.display();
System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
}
// The line's color is:颜色值
// The line's begin point's Coordinate is:
// (x1,y1)
// The line's end point's Coordinate is:
// (x2,y2)
// The line's length is:长度值
}
class Plane extends Element {
private String color;
public Plane() {
}
public Plane(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void display() {
System.out.println("The Plane's color is:" + this.color);
}
}
abstract class Element {
public void display() {
System.out.println("output their status");
}
}
class GeometryObject {
ArrayList<Element> list = new ArrayList<>();
public GeometryObject() {
}
public void add(Element a) {
list.add(a);
}
public void remove(int index) {
if (index >= 0 && index <= list.size() - 1) {
list.remove(index);
} else {
return;
}
}
public ArrayList<Element> getList() {
return list;
}
}
类图:
SourceMonitor的生成报表
增加容器类,利用泛型可以同时存储更多的对象,难度不大,只需要在之前的两题上进行更改即可
踩坑心得
- 在期中考试的第三题中,在容器的删除元素时,没有理解清楚容器的size是什么意思,从而导致在删除出现问题
点击查看代码
public void remove(int index) {
if (index >= 0 && index <= list.size() ) {
list.remove(index);
} else {
return;
}
}
点击查看代码
public void remove(int index) {
if (index >= 0 && index <= list.size() - 1) {
list.remove(index);
} else {
return;
}
}
改进建议
- 在期中考试第三题中,输入的过程不需要通过字符串转换为数字类型,只需要直接读取即可
修改前源码:
点击查看代码
public class Main {
public static void main(String[] args) {
GeometryObject list = new GeometryObject();
Scanner input = new Scanner(System.in);
while (true) {
switch (input.nextInt()) {
case 1:// insert Point object into list
// 输入“点”对象的x,y值
double num1[] = new double[2];
String arr1;
int cnt1 = 0;
while (true) {
if (cnt1 == 2) {
break;
}
arr1 = input.next();
if (!arr1.equals(" ")) {
num1[cnt1] = Double.parseDouble(arr1);
cnt1++;
}
}
Points a = new Points(num1[0], num1[1]);
list.add(a);
break;
case 2:// insert Line object into list
// 输入“线”对象两个端点的x,y值
double num[] = new double[4];
String color = "";
int cnt = 0;
String arr;
while (true) {
if (cnt == 5) {
break;
}
arr = input.next();
if (!arr.equals(" ")) {
if (cnt < 4) {
num[cnt] = Double.parseDouble(arr);
cnt++;
} else {
color = arr;
cnt++;
}
}
}
Points a1 = new Points(num[0], num[1]);
Points a2 = new Points(num[2], num[3]);
Line l = new Line((Points) a1, (Points) a2, color);
list.add(l);
break;
case 3:// insert Plane object into list
// 输入“面”对象的颜色值
String arr2 = new String();
while (true) {
arr2 = input.next();
if (!arr2.equals(" ")) {
break;
}
}
Element plane = new Plane(arr2);
list.add(plane);
break;
case 4:// delete index - 1 object from list
// 输入要删除的对象位置(从1开始)
int index;
while (true) {
String arrr = input.next();
if (!arrr.equals(" ")) {
index = Integer.parseInt(arrr);
break;
}
}
list.remove(index - 1);
break;
case 0:
ArrayList<Element> arrayList = list.getList();
for (Element aa : arrayList) {
aa.display();
}
System.exit(0);
default:
System.out.println("Wrong Format");
}
}
}
}
点击查看代码
public class Main {
public static void main(String[] args) {
GeometryObject list = new GeometryObject();
Scanner input = new Scanner(System.in);
while (true) {
switch (input.nextInt()) {
case 1:// insert Point object into list
// 输入“点”对象的x,y值
double num1[] = new double[2];
num1[0] = input.nextDouble();
num1[1] = input.nextDouble();
Points a = new Points(num1[0], num1[1]);
list.add(a);
break;
case 2:// insert Line object into list
// 输入“线”对象两个端点的x,y值
double num[] = new double[4];
String color = "";
num[0] = input.nextDouble();
num[1] = input.nextDouble();
num[2] = input.nextDouble();
num[3] = input.nextDouble();
color = input.next();
Points a1 = new Points(num[0], num[1]);
Points a2 = new Points(num[2], num[3]);
Line l = new Line((Points) a1, (Points) a2, color);
list.add(l);
break;
case 3:// insert Plane object into list
// 输入“面”对象的颜色值
String arr2 = new String();
arr2 = input.next();
Element plane = new Plane(arr2);
list.add(plane);
break;
case 4:// delete index - 1 object from list
// 输入要删除的对象位置(从1开始)
int index = input.nextInt();
list.remove(index - 1);
break;
case 0:
ArrayList<Element> arrayList = list.getList();
for (Element aa : arrayList) {
aa.display();
}
System.exit(0);
default:
System.out.println("Wrong Format");
}
}
}
}
总结
学习到的内容
继承
- 子类继承父类
先有父类,再有子类,先析构子类,再析构父类
第一,子类对象在创建时会首先调用父类的构造函数
第二, 父类构造函数执行结束后,执行子类的构造函数
第三, 当父类的构造函数有参数时,需要在子类的初始化列表中显示调用
第四, 析构函数调用的先后顺序与构造函数相反
继承的概念:面向对象中的继承指类之间的父子关系, 子类拥有父类的某些状态和行为,(子类复用了父类的功能或状态)。
- 继承有三种:public,protect,private
1:如果父类中的成员使用public修饰,子类无条件继承。需要被外界访问的成员直接设置为public
2:如果父类中的成员使用protected修饰,子类也继承,即使父类和子类不在同一个包中。 protected成员可以
在本类和子类中被访问,但不能在外界被访问,访问权限介于public和private之间。
3:如果父类和子类在同一个包中,此时子类可以继承父类中缺省修饰符的成员。
4:如果父类中的成员使用private修饰,子类打死也都继承不到。private只能在本类中访问。
5:父类的构造器,子类也不能继承,因为构造器必须和当前的类名相同。
多态
多态是继封装、继承之后,面向对象的第三大特性。
- 多态现实意义理解:
现实事物经常会体现出多种形态,如学生,学生是人的一种,则一个具体的同学张三既是学生也是人,即出现两种形态。
Java作为面向对象的语言,同样可以描述一个事物的多种形态。如Student类继承了Person类,一个Student的对象便既是Student,又是Person。
多态体现为父类引用变量可以指向子类对象。
前提条件:必须有子父类关系。
注意:在使用多态后的父类引用变量调用方法时,会调用子类重写后的方法。
多态的定义与使用格式
定义格式:父类类型 变量名=new 子类类型();
- 理解:
多态是同一个行为具有多个不同表现形式或形态的能力。
多态就是同一个接口,使用不同的实例而执行不同操作。
抽象类与接口
抽象类
● 1.在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类
中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
● 2.由于抽象类不能实例化对象,所以抽象类必须被继承,才能被使用。
● 3.父类包含了子类集合的常见的方法,但是由于父类本身是抽象的,所以不能使用这些方法。
● 4.在 Java 中抽象类表示的是一种继承关系,一个类只能继承一个抽象类,而一个类却可以实现多个接口。
在 Java 语言中使用 abstract class 来定义抽象类
点击查看代码
/* 文件名 : Employee.java */
public abstract class Employee
{
private String name;
private String address;
private int number;
public Employee(String name, String address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay()
{
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck()
{
System.out.println("Mailing a check to " + this.name
+ " " + this.address);
}
public String toString()
{
return name + " " + address + " " + number;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber()
{
return number;
}
}
通过以下代码来使用
```java
/* 文件名 : Salary.java */
public class Salary extends Employee
{
private double salary; //Annual salary
public Salary(String name, String address, int number, double
salary)
{
super(name, address, number);
setSalary(salary);
}
public void mailCheck()
{
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary()
{
return salary;
}
public void setSalary(double newSalary)
{
if(newSalary >= 0.0)
{
salary = newSalary;
}
}
public double computePay()
{
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}
接口Interface
● 1.在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明。一个类通过继承接口的方式,从而来继承接口的抽象方法。
● 2.接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念。类描述对象的属性和方法。接口则包含类要实现
的方法。
● 3.除非实现接口的类是抽象类,否则该类要定义接口中的所有方法。
● 4.接口无法被实例化,但是可以被实现。一个实现接口的类,必须实现接口内所描述的所有方法,否则就必须声明为抽象类。另外,在 Java 中,接口类型可用来声明一个变量,他们可以成为一个空指针,或是被绑定在一个以此接口实现的对象。
● 5.一个接口可以有多个方法。
● 6.接口文件保存在 .java 结尾的文件中,文件名使用接口名。
● 7.接口的字节码文件保存在 .class 结尾的文件中。
● 8.接口相应的字节码文件必须在与包名称相匹配的目录结构中
Java泛型
java 中泛型标记符
● E - Element (在集合中使用,因为集合中存放的是元素)
● T - Type(Java 类)
● K - Key(键)
● V - Value(值)
● N - Number(数值类型)
● ? - 表示不确定的 java 类型
课程改进建议及意见
● 1.我觉得上课的时候布置的课堂作业的截止时间可以晚一些,有时上课的知识点需要消化一下,将知识点运用到实践也需要一定的时间。
● 2.希望老师能够在每次作业和实验的重难点部分作一定的讲解说明。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!