【Java Mooc单元练习二】(面向对象)

此处mooc作业来自于该课程,需要的自取并点击此处
本次章节重点解析第二题关于GUI的设计,并提出优化的方法,重点必看

题目一

程序填空题:
‌定义一个抽象类Person,其中有一个公共的抽象方法showInfo()。然后定义此抽象类的一个子类Student,包括的成员变量有姓名、学号、班级和年龄,且此类中包括两个构造方法。在下列代码中的数字标示行填入合适的一行代码。
基本题

public class Main{
    public static void main(String[] args){
        Student a=new Student("三金C_C","2020122128","网络",20);
        a.showInfo();
    }
} 

abstract class Person{
    public abstract void showInfo();
}

class Student extends Person{
    String stuName;
    String stuID;
    String stuClass;
    int age;

    //接收姓名和学号两个参数的构造方法
    Student(String name,String stuID){
        this.stuName=name;
        this.stuID=stuID;
    }

    //接收四个参数的构造方法
    Student(String name,String stuID,String stuclass,int age){
        this.stuName=name;
        this.stuID=stuID;
        this.stuClass=stuclass;
        this.age=age;
    }

    //实现抽象方法,方法体为打印出学生的姓名、学号、班级
    @Override
    public void showInfo(){
        System.out.println("姓名:"+stuName+"  学号:"+stuID+"  班级:"+stuClass);
    }
}

题目二

参考本章例题,编程实现下图中的任一图形(为简化起见:内外图形中心重合、且上下左右对称)。
‌要求
‌1。设计所选定的图形类(可以继承于矩形类或圆形类), 对任意给定的可以构成所选图形的边长和半径,计算其有色部分的面积和周长;
‌2. 如果给定的内部半径或正方形边长超过外围图形允许的最大值(假设内部图形距外边框最窄处不得少于10),则要进行异常处理:提示内部图形尺寸过大。
‌3. 当给定的边长、半径符合要求时,选择某种颜色绘制出来(提示:可以在Applet中绘制)。
这个下面是运行结构:

首先对于这个要解释一下关于fillOval的一些点:其中的参数是(int x,int y,int height,int weight)
对于一般的矩形等x,y就是指左上角的坐标点。
那么对于圆来说其x,y指什么呢?意思呢就是先补成矩形,然后找左上角的坐标,当然呢你也看其中的注释:

x - the x coordinate of the upper left corner of the oval to be filled.
y - the y coordinate of the upper left corner of the oval to be filled.

在这里插入图片描述
然后对于一些界面美化我们该如何进行呢?这样可以精确使图形中心在一起了,但是一般的位置像素我们不太清楚,所以这里就不得不介绍非常好用的工具了,FastStoneCapture,可以测量出具体的像素点,非常有用!
当然了,我的电脑是125%,所以当你设置setSize(100,100),弹出来的窗口就是125px,125px了。
在这里插入图片描述
在这里插入图片描述

这样我们经过简单的计算就知道圆心和中心的坐标位置了,其余的看代码了吧。
下面是运行情况:
在这里插入图片描述
在这里插入图片描述

import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.AttributeSet.ColorAttribute;

import java.math.*;
import java.awt.*;


public class Testgraph extends JFrame{
    Square square;
    Round round;
    static double r1,l1;
    JLabel r;
    JLabel l;
    JTextField jr;
    JTextField jl;
    JButton jb;
    public Testgraph(){
        JPanel j=new JPanel();
        r=new JLabel("请输入圆形边长:    ");
        l=new JLabel("请输入正方形边长:");
        jr=new JTextField(15);
        jl=new JTextField(15);
        jb=new JButton("提交");
        Box ubox=Box.createHorizontalBox();
        ubox.add(r);
        ubox.add(Box.createHorizontalStrut(10));
        ubox.add(jr);

        Box pbox=Box.createHorizontalBox();
        pbox.add(l);
        pbox.add(Box.createHorizontalStrut(10));
        pbox.add(jl);

        Box mbox=Box.createHorizontalBox();
        mbox.add(jb);
        Box box = Box.createVerticalBox();
        box.add(ubox);box.add(pbox);box.add(mbox);
        j.add(box);
        add(j);

        jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getActionCommand()=="提交"){
                    String rx=jr.getText();
                    r1=Double.parseDouble(rx);
                    String lx=jl.getText();
                    l1=Double.parseDouble(lx);
                    square=new Square(l1);
                    round=new Round(r1,square);
                    double s=square.getPerimeter()+round.getPerimeter();
                    double m=square.getArea()-round.getArea();
                    System.out.println("有色部分周长为"+s+" 面积为"+m);
                    /**
                    * 关于面积和周长实在是写不动了,就直接打印了吧...
                    */
                    new DrawShapes(square,round);
                }
            }
        });
        setSize(600,600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(String[] args){
       new Testgraph();
    }
}

//选择正方形加+圆形
abstract class Shapes{
    double length;//边长和半径
    double perimeter;
    double area;
    abstract double getArea();
    abstract double getPerimeter();
}

//正方形类
class Square extends Shapes{
    double length;//边长和半径
    double perimeter;
    double area;

    Square(double length){
        this.length=length;
    }

    double getArea(){
        return length*length;
    }
    double getPerimeter(){
        return 4*perimeter;
    }
}

//圆形类
class Round extends Shapes {
    double length;//边长和半径
    double perimeter;
    double area;

    Round(double length,Square a){
        this.length=length;
        try{
            judge(a);
        }catch(InvalidLengthException ex){
            JOptionPane.showMessageDialog(null,
            "内部图形尺寸过大!");
        }
    }

    double getArea(){
        return Math.PI*length*2;
    }

    double getPerimeter(){
        return Math.PI*length*length;
    }

    void judge(Square a) throws InvalidLengthException{
        if(a.length-this.length<10) throw new InvalidLengthException();
    }
    
}

//异常处理
class InvalidLengthException extends Exception{
    String s;
    public InvalidLengthException(){
        s="内部图形尺寸过大!";
    }
    public String toString(){
        return s;
    }
}
               
//图形界面
class DrawShapes extends JFrame{
    double  r;
    double  len=50;
    public DrawShapes(Square a,Round b){
        this.r=b.length;
        this.len=a.length;
        setTitle("图形绘制");
        JPanel jp=new JPanel();
        JLabel jl=new JLabel("是圆");
        jp.add(jl);
        add(jp);
        setSize(600,600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g){
        
        g.setColor(Color.red);
        g.fillRect((int)(300-len/2), (int)(300-len/2),(int)len,(int)len);
        g.setColor(Color.white);
        g.fillOval((int)(300-r/2), (int)(300-r/2), (int)r, (int) r);
    }
    
}


题目三

定义一个抽象的商品类,其中包含3个数据成员:商品号、商品名、商品价格,定义三个分别用来获取商品名、获取价格、修改价格的成员方法,一个抽象的输出数据成员的方法showInfo(),构造方法自行定义。请注意用适当的访问控制符和非访问控制符对属性和方法进行修饰。
‍在此基础上,定义继承于商品类的一个食品子类和一个玩具子类,除具有商品类的属性之外,食品类还应该包含生产日期、保质期、主要成分等信息,并定义一个设置保质期的方法;玩具类还应该包含型号、材料、安全级别等信息,并定义一个设置安全级别的方法;食品子类和玩具子类均需要实现其父类中的showInfo()方法,以输出类的完整信息。构造方法自行定义。
‍此外,要求编写一个测试程序,用恰当的测试数据创建食品类和玩具类对象,并调用有关方法,检验其运行结果是否正常。

基本题目不解释了。

public class TestGoods {
    public static void main(String[] args){
        Foods apple=new Foods(1001,"Apple",13,"China",20220316,10,"Water");
        apple.showInfo();
        Toy cat=new Toy(1002,"Cat",14,"China","Big","lint","Top");
        cat.showInfo();
    }
}

abstract class Goods{
    int num;
    String name;
    double price;
    String productName;
    Goods(){}
    Goods(int num){
        this.num=num;
    }
    Goods(int num,String name,double price,String productName){
        this.num=num;
        this.name=name;
        this.price=price;
        this.productName=productName;
    }
    void resetPrice(double m){
        this.price=m;
    }

    String getGoodsName(){
        return name;
    };
    double getGoodsPrice(){
        return price;
    };
    void resetGoodsPrice(double price){
        this.price=price;
    };

    abstract void showInfo();

}


class Foods extends Goods{
    int num;
    String name;
    double price;
    String productName;
    int data;
    int maxdata;
    String mainelement;
    Foods(int num,String name,double price,String productName,int  data,int day,String mainelem){
        this.num=num;
        this.name=name;
        this.price=price;
        this.productName=productName;
        this.data=data;
        this.maxdata=day;
        this.mainelement=mainelem;
    }
    void setMaxData(int day){
        this.maxdata=day;
    }

    void showInfo(){
        System.out.println("商品名:"+name+" 价格: "+price+" 保质期: "+maxdata);
    }

}

class Toy extends Goods{
    int num;
    String name;
    double price;
    String productName;
    //speechless: Why do you want to set such properties, especially the material and security level?
    String kind;
    String material;
    String level;

    Toy(int num,String name,double price,String productName,String kind,String material,String level){
        this.num=num;
        this.name=name;
        this.price=price;
        this.productName=productName;
        this.kind=kind;
        this.material=material;
        this.level=level;
    }

    void setLevel(String level){
        this.level=level;
    }
    void showInfo(){
        System.out.println("商品名:"+name+" 价格: "+price+" 安全级别: "+level);
    }
}
  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值