5.11上机课记录(第一个抽象类题目Shape+多选单选题问题)

课上讲到一个知识点:不可以用new来创建抽象类对象。

package maytenth;

import java.util.Scanner;

public abstract class Shape {

    public abstract double getArea();

    public abstract boolean isContains(int x,int y);

    public void print(int width,int height) {
     
    }
//这里是抽象类,要用abstract修饰,抽象类里面可以有非抽象方法的。
//抽象类:至少有一个抽象方法的类,子类中必须把抽象方法实现。

public class Test {

    public static void main(String[] args) {

        Scanner scn = new Scanner(System.in);

        int n = scn.nextInt();

        Shape[] s = new Shape[n];

        s[0] = new Circle(scn.nextInt(),scn.nextInt(),scn.nextDouble());
        

> //**知识点:这里写 s[0] = new Shape(); 就有问题了,因为Shape是抽象类,不能用new创建抽象类对象

        s[1] = new Rectangle(scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt());

        
//本来这样写着,后来突然想起来可以用instanceof,所以进行了优化。
        //System.out.println("the Circle area is:"+format(s[0].getArea()));
        
        //System.out.println("the Rectangle area is:"+format(s[1].getArea()));

        for(int k=0;k<s.length;k++) {

            if(s[k] instanceof Circle)

                System.out.println("the Circle area is:"+format(s[k].getArea()));

            if(s[k] instanceof Rectangle)

                System.out.println("the Rectangle area is:"+format(s[k].getArea()));

        }

    }

    public static String format(double value) {   

        return String.format("%.2f", value).toString(); 
    }  

}
}

public class Point {

    private int x;

    private int y;

    public Point(int x, int y) {

        super();

        this.x = x;

        this.y = y;

    }
    
    public Point() {

        super();

    }

 

    public int getX() {

        return x;

    }

    public void setX(int x) {

        this.x = x;

    }

    public int getY() {

        return y;

    }

    public void setY(int y) {

        this.y = y;

    }

    

    public double getDistance(int x,int y) {

     double d;

     d=(double)Math.sqrt((this.x-x)*(this.x-x)+(this.y-y)*(this.y-y));

     return d;

    }

    

    public double getDistance(int x) {

        return getDistance(x,x);

    }

    

    public double getDistance(Point point) {

        return getDistance(point.x,point.y);

    }

    

}

 

public class Circle extends Shape {

 

    private int x;

    private int y;

    Point p = new Point(x,y);

    private double r;

    public double getArea() {

        double s=0;

        s = Math.PI*this.r*this.r;

        return s;

    }

    

    public boolean isContains(int x,int y) {

        if(this.p.getDistance(x, y)<=r)

            return true;

        else

            return false;

    }

 

    public Circle(int x, int y, double r) {

        super();

        this.x = x;

        this.y = y;

        this.r = r;

    }

 

    public Circle() {

        super();

    }
}

public class Rectangle extends Shape {

    private int x;

    private int y;

    Point p = new Point(x,y);

    private int width;

    private int height;

    

    

    public double getArea() {

        double s=0;

        s = this.width*this.height;

        return s;

    }

    

    public boolean isContains(int x,int y) {

        if(x<width&&y<height)

            return true;

        else

            return false;

    }

 

    public Rectangle(int x, int y, int width, int height) {

        super();

        this.x = x;

        this.y = y;

        this.width = width;

        this.height = height;

    }

    public Rectangle() {

        super();

    }


}

总结:这个题我自认为写的还算合格,在输出时用到“instanceof”来分别输出圆和矩形的两种情况。

多选单选问题:



package
mayday2;

 

import
java.util.Scanner;

 

public class QuestionDemo {

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        MultiChoice q1 = new MultiChoice();

        SingleChoice q2 = new
SingleChoice();

        String[] options1 = new String[] {"A.package", "B.derive", "C.polymorphism", "D.Static"};

        String[] options2 = new String[]{"A.0","B.1","C.2","D.3"};

        char[] answer1 = new char[]{'A','B','C'};

        char answer2 ='A';

        q1.setText("Characteristics of object-oriented program design is ");

        q1.setOptions(options1);

        q2.setText("Default value of int type is");

        q2.setOptions(options2);

        q1.setAnswer(answer1);

        q2.setAnswer(answer2);

        

        System.out.println(q1.getText());

        for(int k = 0;k<4;k++) {

            System.out.print(options1[k]);

        }

        

        System.out.println("\nPlease choose:");

        Scanner scn = new Scanner(System.in);

        String answer11 = scn.next();

        answer11 = answer11.toUpperCase();

        char[] a=new char[3];

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

            a[i]=answer11.charAt(i);

        }

        if(q1.check(a)==true)

            System.out.println("OK\n");

        else

            System.out.println("NO\n");

        

        System.out.println(q2.getText());

        for(int k = 0;k<4;k++) {

            System.out.print(options2[k]);

        }

        System.out.println("\nPlease choose:");

        String answer22 = scn.next();

        answer22 = answer22.toUpperCase();

        char b = answer22.charAt(0);

        if(answer22.length!=1){
         System.out.println("NO");
         }
         else
        if(q2.check(b)==true)

            System.out.println("OK");

        else

            System.out.println("NO");

 

    }

 

}

 

public class Question {

 

    private String text;

    private String[] options;

    

    public Question(String text, String[] options) {

        super();

        this.text = text;

        this.options = options;

    }

 

    public Question() {

        super();

    }

    

    public boolean check(char[] answers) {

        return true;

    }

 

    public String getText() {

        return text;

    }

 

    public void setText(String text) {

        this.text = text;

    }

 

    public String[] getOptions() {

        return options;

    }

 

    public void setOptions(String[] options) {

        this.options = options;

    }

    

    public boolean check(char answers) {

        return true;
//写这里时还没学抽象类,其实可以用抽象类优化。将Question理的check方法写成抽象方法。
    }


}


public class MultiChoice extends Question {

 

    char[] answer;

 

    public MultiChoice(String text, String[] options, char[] answer) {

        super(text, options);

        this.answer = answer;

    }

 

    public MultiChoice() {

        dsuper();

    }

 

    @Override

   /*我的原代码: 
   public boolean check(char[] answers) {

        int p = 0;

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

            if(answers[i]==this.answer[i])

           
p++;

        }

        if(p==answers.length)

            return true;

            else

                return false;

        

    }
*/
/*老师的优化:在这里写了Character方法的忽略大小写,我还不会,我写的是在Demo里面String.toUpperCase()再将String拆给char数组。
Arrays.binarySearch();对排好序的数组进行查找,找到元素则返回位置,找不到返回负值,下面代码判断<=-1的情况。
用Arrays.sort进行了答案的排序,避免了多选正确但是顺序导致的错误。
*/
public boolean check(char[] answers){
  if(answers.length==this.answer.length){
   
   for(int i=0;i<answers.length;i++) {
    answers[i] = Character.toUpperCase(answers[i]);
   }
   Arrays.sort(answers);
   for(int i=0; i<answers.length; i++){
//    System.out.println(Arrays.binarySearch(this.answers, Character.toUpperCase(answers[i])));
//    if(Arrays.binarySearch(this.answers, Character.toUpperCase(answers[i])) <= -1) {
    
    
    if (Arrays.binarySearch(answers, this.answer[i]) <= -1 ){
     
     return false;    
    
    }
    
   }
   return true;
  }else{
   return false;
  
  }
 }
 

    public char[] getAnswer() {

        return answer;

    }

 

    public void setAnswer(char[] answer) {

        this.answer = answer;

    }

    

    

    

    

}

 

public class
SingleChoice extends Question {

 

    char answer;

 

    public SingleChoice(String text, String[] options,
char answer) {

        super(text, options);

        this.answer = answer;

    }

 

    public SingleChoice() {

        super();

    }

 

    @Override

   /*我写的这里是有问题的,比如输入ABC得到的结果也是OK,要确定用户选择的确实是单选 
   public boolean check(char answers) {

        int p = 0;

        

        if (this.answer==answers) {

            p++;

        }

        

        if(p==0)

            return false;

        else

            return true;

    }
*///改正:
public boolean check(char[] answers) {  
  if(answers == null || answers.length != 1){
   return false;
  }
  return this.answer == Character.toUpperCase(answers[0]);
 }
 

    public char getAnswer() {

        return answer;

    }

 

    public void setAnswer(char answer) {

        this.answer = answer;

    }

    

    

    

}


这是第一次用博客记录学习情况,我把知识点和能想到的自己新学的、不熟悉的地方以及错误的修改的地方写在了代码块里面,不知道有没有什么方法让那些笔记看得更清楚一些,慢慢来吧,代码路还长。
2020.5.11

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值