JAVA技术及应用(第二版)习题训练

文章目录

习题二

12.

设计一个表示图书的book类 ,它包含图书的作者、书名、书号、月销售量属性,另有两个构造方法(一个带参数,一个不带参数),有一个成员方法printBook()用于输出数据。设置一测试类测试并输出

import java.util.Scanner;
class  Book{
    String bookname;
    String writer;
    double   number;
    public   Book(){
        bookname=null;
        writer=null;
        number=0;
    }
    public Book(String _bookname, String _writer, double _number){
        this.bookname=_bookname;
        this.writer=_writer;
        this.number=_number;

    }
    public  void setBook(){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入书本名字");
        this.bookname=input.nextLine();
        System.out.println("请输入作者");
        this.writer=input.nextLine();
        System.out.println("请输入销量");
        this.number=input.nextInt();

    }

    public  void pintBook(){
       System.out.println("书名为"+this.bookname+",作者是"+this.writer+",销量"+
        this.number);
    }
}



public class TestBook {
    public static void main(String[] args) {
         Book book1=new Book();
         Book book2=new Book("活着","余华",12);
        System.out.println("是否需要设置书本信息,是的话请输入1");
        Scanner choice=new Scanner(System.in);
        int _choice=choice.nextInt();
        if( _choice==1){
            book1.setBook();
        }
        System.out.println("是否需要查看设置信息,是的话请输入2");
        _choice=choice.nextInt();
                if(_choice==2){
                    book1.pintBook();
                    book2.pintBook();
                }
    }
}

13.

设计一个名为Rectangle的矩形类,包含构造方法(用于设置矩形的宽度和高度)、getArea()方法(求周长)和Draw()方法(使用*字符画出矩形),在另一个类TestRectangle中编写mian()方法测试以上功能.

import java.util.Scanner;

class Rectangle{
    double wide;
    double  length;
    Rectangle(){
        Scanner input=new Scanner(System.in);
        wide=input.nextInt();
        length=input.nextInt();
    }
    Rectangle(double _wide,double _length){
        wide=_wide;
        length=_length;

    }
      double getArea(){
       return   wide*length;
        }
        double getPerimeter(){
        return  2*(wide+length);
        }
       void Draw(){
        int i,j;
        for (i=0;i<length;i++)
            System.out.print("*");
           System.out.print("\n");
        for(j=1;j<wide-1;j++){
            for (i=0;i<length;i++)
            {if(i==0||i==length-1)
                    System.out.print("*");
            else System.out.print(" ");}
            System.out.print("\n");
        }
           for (i=0;i<length;i++)
               System.out.print("*");
           System.out.println();
}}

public class TestRectangle {
    public static void main(String[] args) {
        Rectangle rect1=new Rectangle(6,10) ;
        Rectangle rect2=new Rectangle();
                rect1.Draw();
                rect2.Draw();10
        System.out.println(rect1.getArea());
        System.out.println(rect1.getPerimeter());
    }


}

17.

abstract  class  Student{
    String number;
    String  name;
    double  score;
    
    }
    interface student{
    String number="";
    String name="";
    double score=0;
    }
class Undergraduate  extends Student {
    int sci_number;
}

class  Postgraduate  extends Student{
    int project_number
}

public class TestStudent {
    public static void main(String[] args) {
        Undergraduate under=new Undergraduate();
        
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值