首页 > 其他分享 >BLOG-2

BLOG-2

时间:2023-05-17 23:36:47浏览次数:28  
标签:topLiftPoint Point double BLOG return public lowRightPoint

一、**前言**

本次Java大作业是一次非常有意义的学习经历,我们学习了Java中的继承与多态,抽象类与接口,正则表达式,时间类Calendar等知识点,并将它们应用于实际编程中。通过这次大作业,我们不仅巩固了Java基础知识,还学会了如何将这些知识点结合起来进行实际应用。

二、**设计与分析**

1. *期中考试*

*测验1-圆类设计*

代码如下:

import java.util.Scanner;
public class Main{
   public static void main(String[] args) {

double r;
Scanner input=new Scanner(System.in);

r=input.nextDouble();

if(r<0){
System.out.println("Wrong Format");
}
else {
double s=r*r*Math.PI;
System.out.println(String.format("%.2f",s));
}
}
}

这道题的代码非常简单,不需要使用面向对象的思想。但是我在考试时,因为没看清题目要求的半径的取值范围为(0,+∞),没把半径等于0的情况写进输入数据非法的判断里,导致丢了3分,也直接影响了接下来第三题的错误输入判断。

我以为应该判断输入的内容是否为double类型,然后用

double r;
String f = "" + r;
boolean isDouble(String str){

try{

Double.parseDouble(str);

return true;}

catch(NumberFormatException ex){
return false;
}
boolean a=isDouble(f);
if(r<0||a=false){
System.out.println("Wrong·Format");
}
else{
double s=r*r*Math.PI;
System.out.println(String.format("%.2f",s));
}

来判断输入值是否为double类型,但还是没有通过测试点,考试结束后才发现判断条件是半径小于等于0。

*测验2-类结构设计*

代码如下:

import java.util.Scanner;

public class Main {





• public static void main(String[] args) {

• Scanner input=new Scanner(System.in);

• double x1=input.nextDouble();

• double y1=input.nextDouble();

• double x2=input.nextDouble();

• double y2=input.nextDouble();

• Point p1=new Point(x1,y1);

• p1.getX(x1);p1.getY(y1);

• Point p2=new Point(x2, y2);

• p2.getX(x2);p2.getY(y2);

• Rectangle r=new Rectangle(p1, p2);

• double length=r.getLength(x1, x2);

• double height=r.getHeight(y1, y2);

• double area=r.getArea(length, height);

• System.out.println(String.format("%.2f",area));



• }



}

class Point{

• private double x;

• private double y;

• Point(){



• }

• Point(double x,double y){



• }

• public double getX(double x){

• return x;

• }

• public double getY(double y){

• return y;

• }



• public void setX(double x){

• this.x=x;

• }

• public void setY(double y){

• this.y=y;

• }

}

class Rectangle{

• private Point topLiftPoint;

• private Point lowRightPoint;

• Rectangle(){



• }

• Rectangle(Point topLiftPoint,Point lowRightPoint){



• }



• public Point getTopLiftPoint(Point topLiftPoint) {

•return topLiftPoint;

• }



•public Point getLowRightPoint(Point lowRightPoint) {

•return lowRightPoint;

• }



•public void setTopLiftPoint(Point topLiftPoint){

•this.topLiftPoint=topLiftPoint;

• }

•public void setLowRightPoint(Point lowRightPoint){

•this.lowRightPoint=lowRightPoint;

• }



•public double getLength(double x1,double x2){

•double length=Math.sqrt((x1-x2)*(x1-x2));

•return length;

• }

•public double getHeight(double y1,double y2){

•double height=Math.sqrt((y1-y2)*(y1-y2));

•return height;

• }



•public double getArea(double length,double height){

•double area=length*height;

•return area;

• }

}

类图如下:

 

从这道题开始需要使用面向对象思想。虽然我最后实现了题目的需求,但我的Main类写得非常复杂,而且Rectangle类不能实现传入topLiftPoint,lowRightPoint就直接得出面积area。向Rectangle类传入topLiftPoint,lowRightPoint后,方法getLength,getHeight里需要用的x1,x2,y1,y2不能直接调用传入的topLiftPoint,lowRightPoint里的坐标,得到的返回值也不能直接用在方法getArea里。我在Main类里定义了两个double类型的变量length和height,分别等于getLength和getHeight的返回值,然后再传入getArea里,得出最终结果。

 

*测验3-继承与多态*

代码如下:

import java.util.Scanner;





public class Main {

 public static void main(String[] args) {

•    // TODO Auto-generated method stub

•    Scanner input = new Scanner(System.in);

•    

•    int choice = input.nextInt();

•    

•    switch(choice) {

•    case 1://Circle

•      double radiums = input.nextDouble();

•      if(radiums<0){

•  System.out.println("Wrong·Format");

•   }

•      else{

•      Circle circle = new Circle(radiums);

•      System.out.println(String.format("%.2f",circle.getArea(radiums)));

•     }

•      break;

•    case 2://Rectangle

•      double x1 = input.nextDouble();

•      double y1 = input.nextDouble();

•      double x2 = input.nextDouble();

•      double y2 = input.nextDouble();

•      

•      Point leftTopPoint = new Point(x1,y1);

•      Point lowerRightPoint = new Point(x2,y2);

•      

•      

•      Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

•      double length=rectangle.getLength(x1, x2);

double height=rectangle.getHeight(y1, y2);

•      System.out.println(String.format("%.2f",rectangle.getArea(length,height)));

•      

•      break;

•   }

•    

}







}

class Shape{

• Shape(){



• }

• public double getArea() {

• return 0;

• }





}

class Circle{

• private double r;

• Circle(double r){



• }

• Circle(){



• }

• public double getR(double r) {

• return r;

• }

• public void setR(double r) {

• this.r=r;

• }

•public double getArea(double r) {

•double area=r*r*Math.PI;

•return area;

• }









}



class Point{

•private double x;

•private double y;

•Point(){



• }

•Point(double x,double y){



• }

•public static double getX(double x){

•return x;

• }

•public static double getY(double y){

•return y;

• }



•public void setX(double x){

•this.x=x;

• }

•public void setY(double y){

•this.y=y;

• }

}

class Rectangle{

•private Point topLiftPoint;

•private Point lowRightPoint;

•double length;

•double height;

•Rectangle(){



• }

•Rectangle(Point topLiftPoint,Point lowRightPoint){



• }



•public Point getTopLiftPoint(Point topLiftPoint) {

•return topLiftPoint;

• }



•public Point getLowRightPoint(Point lowRightPoint) {

•return lowRightPoint;

• }



•public void setTopLiftPoint(Point topLiftPoint){

•this.topLiftPoint=topLiftPoint;

• }

•public void setLowRightPoint(Point lowRightPoint){

•this.lowRightPoint=lowRightPoint;

• }



•public double getLength(double x1,double x2){

•double length=Math.sqrt((x1-x2)*(x1-x2));

•return length;

• }

•public double getHeight(double y1,double y2){

•double height=Math.sqrt((y1-y2)*(y1-y2));

•return height;

• }



•public double getArea(double length,double height){

•double area=length*height;

•return area;

• }





}

类图如下:

 

这道题本来要求使用继承与多态,但我不会用继承与多态,所以我就按第二题的写法继续写这题。

本题要求增加Shape类作为Rectangle类和Circle类的父类,因为我未使用继承,所以Shape类只是作为摆设,内含一个构造函数和一个返回值为0的方法getArea。其实不写这个类对程序没有丝毫影响,但是我在开始写这题时,先按照题目给出的类图写好了框架,再考虑各个类之间的关系,所以就多出了这个鸡肋的Shape类。

这道题Main类以外的类代码我基本沿用第2题的,所以也存在上述方法getLength,getHeight里需要用的x1,x2,y1,y2不能直接调用传入的topLiftPoint,lowRightPoint里的坐标,得到的返回值也不能直接用在方法getArea里的问题。尤其是这一题的Main类是题目给出的,里面要求输入leftTopPoint和lowerRightPoint后直接得出面积。考试时我也没有写printArea方法,用了System.out.println(),修改了相关代码,得到正确结果,当然因为第1题忽视了半径等于0的错误输入,导致一个测试点过不去,丢了7分。

考试结束之后,我思考了输入leftTopPoint和lowerRightPoint后直接得出面积的方法,首先将Point类中的x,y改为protected,然后在Rectangle类里增加double类型的私有属性length和height,修改getLength,getHeight和getArea,并增加方法setLength和setHeight。但实际运行结果并不是正确答案,所以还是修改Main类最方便。

 

 

*测验4-抽象类与接口*

代码如下:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Comparator;

public class Main {
   public static void main(String [] args) {
       // TODO Auto-generated method stub
       Scanner input = new Scanner(System.in);
       ArrayList<Double> list = new ArrayList<>();    

       int choice = input.nextInt();

       while(choice != 0) {
           switch(choice) {
           case 1://Circle
               double radiums = input.nextDouble();
               Circle circle = new Circle(radiums);
               double area=circle.getArea(radiums);
               list.add(area);

               break;
           case 2://Rectangle
               double x1 = input.nextDouble();
               double y1 = input.nextDouble();
               double x2 = input.nextDouble();
               double y2 = input.nextDouble();            
               Point leftTopPoint = new Point(x1,y1);
               Point lowerRightPoint = new Point(x2,y2);
               Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
               double length=rectangle.getLength(x1, x2);
               double height=rectangle.getHeight(y1, y2);
               double area2=rectangle.getArea(length,height);
               
               list.add(area2);
               break;
          }
           choice = input.nextInt();
      }    

       for(int i = 0; i < list.size(); i++) {
           System.out.print(String.format("%.2f", list.get(i) + " "));
      }    
  }    
}




class Shape{
Shape(){

}
public double getArea() {
return 0;
}


}
class Circle extends Shape{
private double r;
Circle(double r){

}
Circle(){

}
public double getR(double r) {
return r;
}
public void setR(double r) {
this.r=r;
}
public double getArea(double r) {
double area=r*r*Math.PI;
return area;
}




}

class Point{
private double x;
private double y;
Point(){

}
Point(double x,double y){

}
public static double getX(double x){
return x;
}
public static double getY(double y){
return y;
}

public void setX(double x){
this.x=x;
}
public void setY(double y){
this.y=y;
}
}
class Rectangle extends Shape{
private Point topLiftPoint;
private Point lowRightPoint;
double length;
double height;
Rectangle(){

}
Rectangle(Point topLiftPoint,Point lowRightPoint){

}

public Point getTopLiftPoint(Point topLiftPoint) {
return topLiftPoint;
}

public Point getLowRightPoint(Point lowRightPoint) {
return lowRightPoint;
}

public void setTopLiftPoint(Point topLiftPoint){
this.topLiftPoint=topLiftPoint;
}
public void setLowRightPoint(Point lowRightPoint){
this.lowRightPoint=lowRightPoint;
}

public double getLength(double x1,double x2){
double length=Math.sqrt((x1-x2)*(x1-x2));
return length;
}
public double getHeight(double y1,double y2){
double height=Math.sqrt((y1-y2)*(y1-y2));
return height;
}

public double getArea(double length,double height){
double area=length*height;
return area;
}


}

类图如下:

 

因为我没有掌握抽象类和接口的相关知识,所以这题还是基本沿用了上两题的类设计,但是加上题目给出的Main类后无法运行。

  list.sort(Comparator.naturalOrder());//正向排序

这一句报错The method sort(Comparator<? super Shape>) in the type ArrayList<Shape> is not applicable for the arguments (Comparator<Comparable<? super Comparable<? super T>>>)

当时我没有看懂提示中的“题目中Shape类要实现Comparable接口。”是什么意思,后来查找资料得:Comparable在集合内部定义的方法实现的排序,位于java.lang下。Comparable支持自比较,自比较是指比如String等类里面本身就有CompareTo()方法,直接就可以进行String类对象的比较,也就是说,实现了Comparable接口的类的对象的列表或数组可以通过Collections.sort或Arrays.sort进行自动排序,也就是大家常说的自然排序。

整个内部只有一个接口方法:public int compareTo(T o);它的作用主要是比较当前实现接口的对象与指定对象o的顺序。它根据比较的结果返回负数、0和正数。

相对应的关系如下:

1、如果 o1.compareTo(o2)<0 ==> o1<o2

2、o1.compareTo(o2)==0 ==> o1==o2

3、o1.compareTo(o2)>0 ==> o1>o2

由于我还不会使用接口,这里不做过多分析。

 

 

*2.菜单计价程序-4*

大作业期间我没有及时写出这道题,由于没有第三次菜单计价的代码,所以我从头开始写了代码。

首先是读取输入的问题。在处理一行输入时,我先尝试使用了input.next(),因为next()方法在读取内容时,会过滤掉有效字符前面的无效字符,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next()方法会自动将其过滤掉;只有在读取到有效字符之后,next()方法才将其后的空格键、Tab键或Enter键等视为结束符;所以next()方法不能得到带空格的字符串。

但我在尝试了很久之后,也没有得到正确的结果,所以最后放弃了next()方法,改用nextLine()方法,同时用String的split()方法对传入的字符串进行拆分,返回拆分之后的数组。

首先我处理了table以前的输入,也就是存入菜单的信息。部分代码如下:

String s;

String dishName="";

***\*int\**** inputPrice = 0;

String wholeJudge;

***\*while\****(flag==1)

{

s=input.nextLine();

String[] str=s.split(" ");

***\*for\****(***\*int\**** i=0;i<str.length;i++) {

***\*boolean\**** isNum=str[i].matches("[0-9]+");

//遇到以下这些说明菜单录入完毕

***\*if\****(str[i].equals("table"))

{

wholeJudge="table";

flag=0;

***\*break\****;

}



//判断输入的是否为数字

***\*if\****(isNum==***\*true\****) {

inputPrice=Integer.**parseInt**(str[i]);

}

dishName=str[0];

//特色菜

***\*if\****(str[i].equals("T")){





}





***\*if\****(str[i].equals("end"))

{

wholeJudge="0";

flag=0;

***\*break\****;

}

}

}

 

第四次菜单计价涉及了正则表达式,在查找资料的过程中,我发现使用正则表达式可能会影响程序的性能。当需要在较长的字符串中匹配多个子串时,正则表达式可能会导致程序变慢。然后因为正则表达式的语法实在太复杂,所以我没使用正则表达式,而是使用if-else选择结构判断输入是否合法。

在判断点单时间是否在营业时间内时,我使用了Java的Calendar类来进行时间计算。 我使用了Calendar类的set()方法设置对象的时间。例如,假设我要设置时间为9点30分:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 30);

其中,Calendar.HOUR_OF_DAY表示24小时制的小时数,Calendar.MINUTE表示分钟数。通过设置不同的字段,可以设置不同的时间。 除此之外,我还需要计算时间是否在营业时间内。我的解决方法是使用before()和after()方法进行比较。例如,假设我要判断当前时间是否在周一至周五晚上营业时间(17:00-20:30)内:

Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 18); // 假设当前时间为18:00 
calendar.set(Calendar.MINUTE, 0);

Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, 17); start.set(Calendar.MINUTE, 0);

Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, 20);

end.set(Calendar.MINUTE, 30);

if (calendar.after(start) && calendar.before(end)) { // 在营业时间内 }
else { // 不在营业时间内 }

通过比较当前时间与起始时间和结束时间的大小关系,可以判断当前时间是否在营业时间内。

 

 

*3.菜单计价程序-5*

本题在特色菜中增加了口味度,我的初步想法是在Menu类中增加判断特色菜的方法,然后再对菜系及口味度进行判断,但是最后没能写出来。然后我在table类中增加三个String类型的数组,分别储存辣、酸、甜的口味度,再根据输入进行判断。

 

三、**踩坑心得**

我对循环结构的应用还是很不熟练,经常在使用循环结构的时候绕来绕去,得不出想要的结果。又因为我的eclipse经常出问题,用不了Debug,所以我使用输出字符串的方法来判断程序进行到哪一步,以及判断某些变量在这时的值是多少。

四、**主要困难以及改进建议**

1、我对于一些方法传入的参数以及返回值的运用还不清楚,比如期中考试中,如何实现传入point,在Rectangle类中计算出length和height,然后在getArea()中运用这两个值,直接在Main类里调用getArea()得出面积值,我至今未找到解决方法。

2、正则表达式的语法对于我来说太过复杂,如果没有足够的时间,很难找到合适的公式。

五、**总结**

通过这次Java大作业的学习,我深刻地认识到了继承与多态、抽象类与接口、时间类Calendar等知识点的重要性。在实际编程中,我们可以灵活地运用这些知识点,帮助我们更加高效地开发出符合需求的程序。

在此,我想分享一下我在本次大作业中的体会。首先,继承与多态是Java中非常重要的概念,它可以使我们的代码更加灵活和可扩展。其次,抽象类与接口是Java中非常重要的基础知识,它可以帮助我们更好地进行面向对象的编程。正则表达式是Java中用于处理字符串的重要工具,它可以帮助我们更好地处理数据。最后,时间类Calendar可以帮助我们方便地处理日期和时间,让我们的程序更加精确和专业。

总之,通过本次Java大作业的学习,我对Java编程语言有了更深刻的认识和理解。我相信这些知识点可以帮助我们更好地进行Java开发,也希望未来可以有更多的机会学习和应用它们。

标签:topLiftPoint,Point,double,BLOG,return,public,lowRightPoint
From: https://www.cnblogs.com/6123zoe/p/17410661.html

相关文章

  • Weblogic < 10.3.6 'wls-wsat' XMLDecoder 反序列化漏洞(CVE-2017-10271)
    参考:https://github.com/vulhub/vulhub/blob/master/weblogic/CVE-2017-10271/README.md反弹shellEXP:POST/wls-wsat/CoordinatorPortTypeHTTP/1.1Host:172.31.14.123:7001Accept-Encoding:gzip,deflateAccept:*/*Accept-Language:enUser-Agent:Mozilla/5.0(com......
  • BLOG-2(软件学院)
    一、前言:此次的BLOG-2是关于PTA的第四、五次大作业以及期中考试的题目分析。在PTA第四、五的两次大作业是在菜单三的基础上进行一个功能的增加。而期中考试主要是对知识点对点的测试。PTA菜单四考察了很多错误输入情况,PTA菜单五则是在三的条件下去延伸了菜的一些特色。这两次的......
  • PTA题目集4、5及期中考试的总结性Blog
    一、前言随着对java学习的越来越深入,需要学习的东西也越来越多,第四五次pta题目集主要还是以菜单计价系统为主,相较于以前的菜单计价系统,增加了异常情况的处理,以及特色菜,口味度等功能,使这个菜单计价系统越来越与现实生活相关联,当然与之同时题目的难度当然也是大幅度提高了。虽然这......
  • blog-博客美化-博客园文章底部增加版权声明
    blog-博客美化-博客园文章底部增加版权声明在博客签名的内容中添加如下代码:<div>作者:<ahref="http://www.cnblogs.com/tssc/"target="_blank">天生帅才</a></div><div>联系:<ahref="http://www.cnblogs.com/tssc/"target="_blank"......
  • blog-博客美化-悬浮图标-右下角关注楼主
    添加一个“关注楼主”进入自己的博客园->设置,将以下css代码添加到“页脚Html代码”<scripttype="text/javascript"language="javascript">//为右下角推荐推荐区域添加关注按钮window.onload=function(){$('#div_digg').prepend('<divstyle="padding-botto......
  • blog-博客美化-右边浮动分享按钮
    分享按钮的设计网站http://share.baidu.com/code右边浮动分享按钮<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":["sqq","weixin&quo......
  • blog-博客美化-悬浮图标-右下角推荐与反对
    设置“推荐与反对”,固定在窗口的底部进入自己的博客园->设置,将以下css代码添加到“页面定制CSS代码”/*推荐和反对*/#div_digg{position:fixed;bottom:5px;width:140px;right:0px;border:2pxsolid#6FA833;padding:10px;background-color:#fff;bord......
  • blog-博客美化-博客标题栏图标
    博客标题栏图标1.首先得准备一个icon类型的文件可以自己百度找一个,或者找张图片另存为ico格式即可2.上传自己的ICON图标进入自己的博客后台管理->文件->选择文件->上传,点击已上传的文件,copy这个文件的网络地址,后续会用到https://files.cnblogs.com/files/tssc/Q.ico3......
  • blog-博客美化-标题背景色带代码
    博客美化-标题背景色带代码取色网址:http://www.114la.com/other/rgb.htm标题背景色带代码1.自定义背景色<!--此处自定义标题背景色-->#cnblogs_post_bodyh1{background:#390;border-radius:6px6px6px6px;color:#FFFFFF;font-family:FZShuTi;......
  • blog-博客美化-生成目录索引
    博客美化-生成目录索引注意:需要先申请开通JS接口!!今天帮朋友设置后台代码,发现怎么都没有效果,看了下忽略了JS接口;因为插入的代码大多有JS功能,不申请开通JS功能自然无法支持JS效果。网上看了很多博客也都没提及这点,感觉是个坑,So,需要的朋友可以留意下。对了、在编辑页面是显示不出......