面向对象程序设计前三次作业总结
(1)前言:对于java的面向对象程序设计学习也进行了快一个半月了,就浅谈一下自己写的那些java大作业吧。
第一次作业:算是java的基础练习了,并且题量也合情合理。我们大一上学期学习的是c语言,而下学期突然画风一变,转为学习java。这次大作业,语法规则与c语言除了一些细则不同以外,其余的都与c语言很是相似,对于我们初学java的人而言,是很好的练手机会,也让我们能适应由c到Java的变化。在这次作业中,对我启发最大的便是double 和float的区分,在c语言中,我并不会可以去区分这两个类型,也默认他们的输出是大体相同的,但在java中,对这两种类型的区分便很细了,很多题目我用double过不去测试点,而用float则可以,这也是在启示我们要慎重选择变量的类型。
第二次作业:这次的大作业就不再是基础练习了,而是考验我们java语言功底的入门级作业了,题量偏少(好像是老师少布置了)。这次大作业考察了我们对字符串的运用其中包括了单字符串的元素提取与转换,以及一维字符串数组与二维字符串数组的创建 赋值与运用。
第三次作业:这次的大作业是三次中最难且最费时的(也是我唯一没有一题拿满分的)。在这次作业中,每到题目不再像之前一样,只需要几十行并且在一个类中就能完成;就我个人而言,除了第一题,我每题都用了200-300行,并且都至少建立了三个类才勉勉强强完成本次作业。除此之外,这次作业中有着庞大的测试点,这便要求我们精益求精,不能放过任何一种错误的情况。我认为,这样能提升我们对代码细节的处理。
(2)设计与分析:
1.2-2:
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
String a=in.nextLine();
char a_[]=a.toCharArray();
int a_length=a.length();
a_length--;
int count=1;
int count_;
boolean count_1;
int i;
for(i=0;i<a_length;i++)
{
if(a_[i]=='0')
{
if((a_length-i)>=10)
{
count_=0;
for(int i_=i+1;i_<i+10;i_++)
{
if(a_[i_]=='1')
{
count_++;
}
}
if((count_%2)==1)
{
count_1=true;
}
else
{
count_1=false;
}
if(count_1&&a_[i+10]=='1')
{
if(count!=1)
System.out.print("\n");
System.out.print(count+":"+a.substring(i+1, i+9));
count++;
i+=10;
}
else if(a_[i+10]!='1')
{
if(count!=1)
System.out.print("\n");
System.out.print(count+":validate error");
count++;
i+=10;
}
else if(!count_1)
{
if(count!=1)
System.out.print("\n");
System.out.print(count+":parity check error");
count++;
i+=10;
}
}
else
{
if(count==1)
{
System.out.print("null data");
break;
}
}
}
}
if(i==a_length)
{
if(count==1)
{
System.out.print("null data");
}
}
in.close();
}
}
先创建一个字符串来接收输入的串口字符,再将字符串中的每个数字放到新创建的字符数组中。对于字符数组,当出现‘0’时判断开始传递串口字符,并从‘0’后一位开始统计‘1’的个数,直到八位之后出现结束位‘1’。最后对‘1’的个数做判断:若为奇数,则传递成功,反之则不成功。
2.3-1
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
String a;
a=in.nextLine();
Cheak c=new Cheak();
int count=0;
Point p=new Point();
Point p_=new Point();
double num[]=new double[100];
Pattern pattern = Pattern.compile("-?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)");
Matcher matcher = pattern.matcher(a);
while (matcher.find()){
String res = matcher.group();
if (res.length()>0){
num[count]=Double.parseDouble(res);
count++;
}
}
p.x=num[0];
p.y=num[1];
p_.x=num[2];
p_.y=num[3];
if(c.ch(a)&&count==4) {
System.out.print(p.sqrt(p,p_));
}
if(count!=4&&count%2==0){
System.out.print("wrong number of points");
}
else if(!c.ch(a)) {
System.out.print("Wrong Format");
}
in.close();
}
}
class Point{
double x,y;
double sqrt(Point a,Point b) {
return Math.sqrt( Math.pow((a.x-b.x),2)+ Math.pow((a.y-b.y),2));
}
}
class Cheak{
boolean ch(String a) {
return a.matches("(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\ (\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)");
}
}
类:Main 完成主要运算与输出
Point 元素 x,y
返回两点距离的计算结果。
Cheak 判断输入的字符串是否符合格式,并产生不同的返回值。
同上一题,创建一个字符串接收输入的数据,通过类Cheak判断格式正确后再将其赋值给两个Point的x y元素 再通过Point类的计算功能最终得出两点间距离并输出。
3.3-2:
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
String a;
a=in.nextLine();
Cheak c=new Cheak();
int count=0;
int cas;//记录选项
double num[]=new double[100];
Pattern pattern = Pattern.compile("-?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)");
Matcher matcher = pattern.matcher(a);
while (matcher.find()){
String res = matcher.group();
if (res.length()>0){
num[count]=Double.parseDouble(res);
count++;
}
}
Point p=new Point();
Point p_=new Point();
cas=(int)num[0];
p.x=num[1];
p.y=num[2];
p_.x=num[3];
p_.y=num[4];
switch(cas) {
case 1:if(c.ch1(a)) {
if(p.twop(p_)=='p') {
System.out.print("points coincide");
}
else if(p.twop(p_)=='s') {
System.out.print("Slope does not exist");
}
else if(p.twop(p_)=='k'){
System.out.print(p.k(p_));
}
}
else {
System.out.print("Wrong Format");
}
break;
case 2:if(c.ch2(a)) {
Point p_1=new Point();
p_1.x=num[5];
p_1.y=num[6];
if(p.twop(p_)=='p'||p.twop(p_1)=='p'||p_.twop(p_1)=='p') {
System.out.print("points coincide");
}
else {
System.out.print(p.ptoline(p_, p_1));
}
}
else {
System.out.print("Wrong Format");
}
break;
case 3:if(c.ch2(a)) {
Point p_1=new Point();
p_1.x=num[5];
p_1.y=num[6];
if(p.twop(p_)=='p'||p.twop(p_1)=='p'||p_.twop(p_1)=='p') {
System.out.print("points coincide");
}
else {
System.out.print(p_1.ppptoline(p, p_));
}
}
else {
System.out.print("Wrong Format");
}
break;
case 4:if(c.ch3(a)) {
Point p_1=new Point();
Point p_2=new Point();
p_1.x=num[5];
p_1.y=num[6];
p_2.x=num[7];
p_2.y=num[8];
if(p.twop(p_)=='p'||p.twop(p_1)=='p'||p.twop(p_2)=='p'||p_.twop(p_1)=='p'||p_.twop(p_2)=='p'||p_1.twop(p_2)=='p') {
System.out.print("points coincide");
}
else if(p.twop(p_)=='s'||p_1.twop(p_2)=='s'){
if(p.twop(p_)==p_1.twop(p_2)) {
System.out.print("true");
}
else {
System.out.print("false");
}
}
else if(p.twop(p_)!='s'&&p_1.twop(p_2)!='s') {
if(p.k(p_)==p_1.k(p_2)) {
System.out.print("true");
}
else {
System.out.print("false");
}
}
}
else {
System.out.print("Wrong Format");
}
break;
case 5: if(c.ch3(a)) {
Point p_1=new Point();
Point p_2=new Point();
p_1.x=num[5];
p_1.y=num[6];
p_2.x=num[7];
p_2.y=num[8];
if(p.twop(p_)=='p'||p.twop(p_1)=='p'||p.twop(p_2)=='p'||p_.twop(p_1)=='p'||p_.twop(p_2)=='p'||p_1.twop(p_2)=='p') {
System.out.print("points coincide");
}
else {
p.twoline(p_,p_1,p_2);
}
}
else {
System.out.print("Wrong Format");
}
break;
}
in.close();
}
}
class Point{
double x,y;
char twop(Point a) //两点情况
{
if(a.x==this.x&&a.y==this.y)//两点重合
return 'p';
else if(a.x==this.x)//两点不重合但斜率不存在
return 's';
else
return 'k';//斜率存在
}
double k(Point a) //计算斜率
{
return ((this.y-a.y)/(this.x-a.x));
}
double ptoline(Point a,Point b) //点到两点形成的线的距离
{
if(a.twop(b)=='s')
return (this.x-a.x);
else {
double k=a.k(b);
double c=a.y-k*a.x;
return (Math.abs(this.y-k*this.x-c)/Math.sqrt(1+k*k));
}
}
boolean ppptoline(Point a,Point b)//判断是否三点一线
{
if(a.twop(b)=='s') {
if(this.x==a.x) {
return true;
}
else {
return false;
}
}
else {
if(this.k(a)==this.k(b)) {
return true;
}
else {
return false;
}
}
}
void twoline(Point a,Point b,Point c) {
if(this.twop(a)=='s'||b.twop(c)=='s') {
if(this.twop(a)==b.twop(c)) {
System.out.print("is parallel lines,have no intersection point");
}
else {
double x,y,k,c_;
boolean inout;
if(this.twop(a)=='s') {
x=this.x;
k=b.k(c);
c_=b.y-k*b.x;
y=k*x+c_;
if((y<this.y&&y>a.y)||(y<a.y&&y>this.y)) {
inout=true;
}
else {
inout=false;
}
}
else{
x=c.x;
k=this.k(a);
c_=a.y-k*a.x;
y=k*x+c_;
if((y<b.y&&y>c.y)||(y<c.y&&y>b.y)) {
inout=true;
}
else {
inout=false;
}
}
System.out.print(x+","+y+" "+inout);
}
}
else if(this.twop(a)!='s'&&b.twop(c)!='s'){
if(this.k(a)==b.k(c)) {
System.out.print("is parallel lines,have no intersection point");
}
else {
double x,y,k1,k2,c1,c2;
boolean inout;
k1=this.k(a);
k2=b.k(c);
c1=this.y-k1*this.x;
c2=b.y-k2*b.x;
x=(c1-c2)/(k2-k1);
y=k1*x+c1;
if((y<b.y&&y>c.y)||(y<c.y&&y>b.y)||(y<this.y&&y>a.y)||(y<a.y&&y>this.y)) {
inout=true;
}
else {
inout=false;
}
System.out.print(x+","+y+" "+inout);
}
}
}
}
class Cheak{
boolean ch1(String a) //判断选项一的输入格式
{
return a.matches("1\\:(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\ (\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)");
}
boolean ch2(String a) //判断选项二三的输入格式
{
return a.matches("[23]\\:(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\ (\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\ (\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)");
}
boolean ch3(String a) //判断选项四五的输入格式
{
return a.matches("[45]\\:(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\ (\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\ (\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\ (\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)\\,(\\+|-)?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)");
}
}
类Main 接收Point的传值并输出
Point 元素 x,y
返回两点距离的计算结果。
判断两点是否重合以及两点形成的直线斜率是否存在
计算两点形成的直线的斜率
判断一个点到另外两个点形成的直线的距离
判断三个点是否共线
计算两条直线的交点并输出,若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。
Cheak 判断输入的字符串是否符合格式,并产生不同的返回值。
同之前的题,先创建一个字符串接收输入的数据,再通过Cheak判断数据是否符合格式,若符合则将值赋给Point 再进行相应计算与输出。
4.3-3:
(ps:源码与上一题大体一直,这里只附上区别之处。)
public class Main {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
String a;
a=in.nextLine();
Cheak c=new Cheak();
int count=0;
int cas;//记录选项
double num[]=new double[100];
Pattern pattern = Pattern.compile("-?([1-9]\\d*\\.?\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)");
Matcher matcher = pattern.matcher(a);
while (matcher.find()){
String res = matcher.group();
if (res.length()>0){
num[count]=Double.parseDouble(res);
count++;
}
}
Point p1=new Point();
Point p2=new Point();
Point p3=new Point();
cas=(int)num[0];
p1.x=num[1];
p1.y=num[2];
p2.x=num[3];
p2.y=num[4];
p3.x=num[5];
p3.y=num[6];
switch(cas) {
case 1:if(c.ch1(a)) {
if(p1.istriangle(p2, p3)) {
if(p1.pplength(p2)==p1.pplength(p3)||p1.pplength(p2)==p2.pplength(p3)||p1.pplength(p3)==p2.pplength(p3)) {
System.out.print("true ");
if((p1.pplength(p2)==p1.pplength(p3))&&(p1.pplength(p2)==p2.pplength(p3))) {
System.out.print("true");
}
else {
System.out.print("false");
}
}
else {
System.out.print("false false");
}
}
else {
System.out.print("data error");
}
}
else if(count!=7&&count%2==1) {
System.out.print("wrong number of points");
}
else {
System.out.print("Wrong Format");
}
break;
case 2:if(c.ch1(a)) {
if(p1.istriangle(p2,p3)) {
p1.perarea(p2, p3);
}
else {
System.out.print("data error");
}
}
else if(count!=7&&count%2==1) {
System.out.print("wrong number of points");
}
else {
System.out.print("Wrong Format");
}
break;
case 3:if(c.ch1(a)) {
if(p1.istriangle(p2,p3)) {
p1.shape(p2, p3);
}
else {
System.out.print("data error");
}
}
else if(count!=7&&count%2==1) {
System.out.print("wrong number of points");
}
else {
System.out.print("Wrong Format");
}
break;
case 4:if(c.ch2(a)) {
Point p4=new Point();
Point p5=new Point();
p4.x=num[7];
p4.y=num[8];
p5.x=num[9];
p5.y=num[10];
if(p3.istriangle(p4,p5)&&p1.twop(p2)!='p') {
p1.linetriangle(p2, p3, p4, p5);
}
else if(p1.twop(p2)=='p') {
System.out.print("points coincide");
}
else {
System.out.print("data error");
}
}
else if(count!=11&&count%2==1) {
System.out.print("wrong number of points");
}
else {
System.out.print("Wrong Format");
}
break;
case 5: if(c.ch3(a)) {
Point p4=new Point();
p4.x=num[7];
p4.y=num[8];
if(p2.istriangle(p3,p4)) {
p1.intriangle(p2,p3,p4);
}
else {
System.out.print("data error");
}
}
else if(count!=9&&count%2==1) {
System.out.print("wrong number of points");
}
else {
System.out.print("Wrong Format");
}
break;
}
in.close();
}
}
class Point{
double x,y;
double pplength(Point a)//计算两点距离
{
return (Math.sqrt(Math.pow((this.x-a.x),2)+Math.pow((this.y-a.y),2)));
}
boolean istriangle(Point a,Point b)//判断三个点是否形成三角形
{
double a1,b1,c1;
a1=this.pplength(a);
b1=this.pplength(b);
c1=a.pplength(b);
if(a1+b1>c1&&a1+c1>b1&&b1+c1>a1) {
return true;
}
else {
return false;
}
}
void perarea(Point a,Point b) //输出周长 面积 重心坐标
{
double a1,b1,c1,x,y,per,area;//per:周长 area:面积
a1=this.pplength(a);
b1=this.pplength(b);
c1=a.pplength(b);
x=(this.x+a.x+b.x)/3;
y=(this.y+a.y+b.y)/3;
per=a1+b1+c1;
area=this.area(a, b);
System.out.print(per+" "+area+" "+x+","+y);
}
double area(Point a,Point b)//三角形面积
{
double a1,b1,c1,p;
a1=this.pplength(a);
b1=this.pplength(b);
c1=a.pplength(b);
p=(a1+b1+c1)/2;
return Math.sqrt(p*(p-a1)*(p-b1)*(p-c1));
}
void shape(Point a,Point b)//判断三角形是什么角三角形
{
double a1,b1,c1;
a1=this.pplength(a);
b1=this.pplength(b);
c1=a.pplength(b);
if(a1>=b1&&a1>=c1) {
if(b1*b1+c1*c1>a1*a1) {
System.out.print("false false true");//钝角 直角 锐角
}
else if(b1*b1+c1*c1<a1*a1) {
System.out.print("true false false");
}
else {
System.out.print("false true false");
}
}
else if(b1>=a1&&b1>=c1) {
if(a1*a1+c1*c1>b1*b1) {
System.out.print("false false true");
}
else if(a1*a1+c1*c1<b1*b1) {
System.out.print("true false false");
}
else {
System.out.print("false true false");
}
}
else {
if(a1*a1+b1*b1>c1*c1) {
System.out.print("false false true");
}
else if(a1*a1+b1*b1<c1*c1) {
System.out.print("true false false");
}
else {
System.out.print("false true false");
}
}
}
void intriangle(Point a,Point b,Point c)//判断点是否在三角形内
{
if(this.ptoline(a,b)==0||this.ptoline(a,c)==0||this.ptoline(b,c)==0) {//(this.ptoline(a,b)==0&&((this.y<a.y&&this.y>b.y)||(this.y>a.y&&this.y<b.y)))||(this.ptoline(a,c)==0&&((this.y<a.y&&this.y>c.y)||(this.y>a.y&&this.y<c.y)))||(this.ptoline(b,c)==0&&((this.y<b.y&&this.y>c.y)||(this.y>b.y&&this.y<c.y)))
System.out.print("on the triangle");
}
else if(this.area(a, b)<c.area(a, b)&&this.area(a, c)<b.area(a, c)&&this.area(b, c)<a.area(b, c)) {
System.out.print("in the triangle");
}
else {
System.out.print("outof triangle");
}
}
void linetriangle(Point a,Point b,Point c,Point d)//判断线与三角形
{
if(true)
System.out.print("The point is on the edge of the triangle");
}
char twop(Point a) //两点情况
{
if(a.x==this.x&&a.y==this.y)//两点重合
return 'p';
else if(a.x==this.x)//两点不重合但斜率不存在
return 's';
else
return 'k';//斜率存在
}
double k(Point a) //计算斜率
{
return ((this.y-a.y)/(this.x-a.x));
}
double ptoline(Point a,Point b) //点到两点形成的线的距离
{
if(a.twop(b)=='s')
return Math.abs(this.x-a.x);
else {
double k=a.k(b);
double c=a.y-k*a.x;
return (Math.abs(this.y-k*this.x-c)/Math.sqrt(1+k*k));
}
}
类Main 接收Point的传值并输出
Point 元素 x,y
返回两点距离
判断三个点能否构成三角形
输出三角形的周长 面积 重心坐标
计算三角形面积
判断三角形是什么三角形(钝角||直角||锐角)
判断一个点是否在另三个点构成的三角形内部(或在三角形边界上)
Cheak 判断输入的字符串是否符合格式,并产生不同的返回值。
同之前的题,先创建一个字符串接收输入的数据,再通过Cheak判断数据是否符合格式,若符合则将值赋给Point 再进行相应计算与输出。
(3)踩坑心得:
1.不要上来就框框一顿写,谋定而后动:
像前两次作业还好,在一个类里花几十行就能完成,但到了第三次作业,我一上来就在Main函数里写上所有计算,这样不光会使得Main函数过于臃肿,还不便于代码的重复利用,最终是全部删除从头来过,新建了两个类去给Main分担负担,让代码变得较为简洁并且带有一定可重复利用性。
(4)改进建议:
想好了在写:
在面对工作量较为庞大的题目时,先停下来好好思考一下要建立几个类,每个类分别对应的职责是什么。就如同这次的第三次大作业,我将庞大的计算全都堆在了Point类中,但这样是非常不好的,因为代码逻辑会变得十分混乱。像第三次大作业的第三题,我应当建立Point Line Triging类去分别对应点 线 三角形的计算以及判断,这样就能让不同类对应其相应的元素,使得代码条理清晰,逻辑顺畅。
(5):总结:在这三次大作业中,我熟悉了java与c语言不同的语法规则 如创建数组的区别等,还了解了类与对象的关系 如何利用对象进行一系列操作等。还学习了正则表达式的用法(判断与提取)。
对于输入的数据的格式判断格式仍需细化,代码逻辑的处理仍需加强。
希望在作业结束提交之后能够有参考答案或者老师能在课上对一些正确率较低的题目进行讲解。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)