JAVA企业面试题精选 Java SE 91-100

3.91.一个图书管理系统的面向对象设计方法如下图所示:

这里写图片描述

  Book代表书,有”Name(书名)”,”Author(作者名)”,”Price(单价)”和IsBorrowed(是否被借出)”四个属性.
  类Library代表图书馆,其内部字段books用于保存图书馆中所有的书.它的FindBook()方法依据书名查找相同的书(可能有多本).另一个GetAllBooks()方法湖区馆藏所有书的详细信息.
  类Reader代表读者,Name字段代表其姓名,读者可以”ReturnBook(还书)”和”BorrowBook(借书)”.
  请编程完成以下工作:
  1.用Java编程实现上述3个类
  2.在main()方法内书写以下测试代码:
   1)创建一个Library类的实例myLittleLibrary,其中预存有以下3本书:
   Java程序设计,张三著,45元
   Java核心技术,李四著,50元
   Java程序设计,王五著,38元
   2)显示图书馆中所有图书的信息,输出样例如下:
   Java程序设计,张三著,45元,可借
   Java核心技术,李四著,50元,可借
   Java程序设计,王五著,38元,未还
   3)(创建一个Reader类的实例oneBeautifulGirl,她先在myLittleLibrary中查找<
   4)oneBeautifulGirl借了张三著的那一本书.现在显示图书馆中所有图书的信息.
   5)oneBeautifulGirl把书还了,再次显示图书馆中图书的信息.
注意:在满足题目要求实现功能的前提下,你可以依据自己的考虑修改系统设计方案(比如给某个类添加或修改类的方法,甚至是添加新的类).

   
   
  • 1
  • 2
参考答案:

  Book类代码如下:

public class Book{

    private String name;
    private String author;
    private double price;
    private boolean isBorrowed;

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getAuthor(){
        return author;
    }

    public void setAuthor(String author){
        this.author = author;
    }

    public double getPrice(){
        return price;
    }

    public void setPrice(double price){
        this.price = price;
    }

    public boolean isBorrowed(){
        return isBorrowed;
    }

    public void setBorrowed(boolean isBorrowed){
        this.isBorrowed = isBorrowed;
    }

    public String toString(){
        return name + "," + author + "," + price + "元," + (isBorrowed ? "未还" : "可借");
    } 
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

  Library类代码如下:

import java.util.ArrayList;
import java.util.List;

public class Library{

    private static Library lib = null;
    private List<Book> books = new ArrayList<Book>();

    private Library(){}

    public void addBooks(Book book){
        books.add(book);
    }

    public static Library getLibrary(){
        if(lib == null){
            lib = new Library();
        }
        return lib;
    }

    public List<Book> findBook(String name){
        List<Book> findList = new ArrayList<Book>();
        for(Book book : books){
            if(name != null && name.equals(book.getName()){
                findList.add(book);
            }
        }
        return findList;
    }

    public List<Book> getAllBooks(){
        return books;
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

  Reader类代码如下:

import java.util.List;

public class Reader{

    private String name;

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public void returnBook(String name, String author){
        //设置书为归还状态
        List<Book> books = Library.getLibrary().getAllBooks();
        for(Book book : books){
            if(name.equals(book.getName()) && author.equals(book.getAuthor())){
                book.setBorrowed(false);
                break;
            }
        }
    }

    public void borroweBook(String name, String author){
        //设置书为出借状态
        List<Book> books = Library.getLibrary().getAllBooks();
        for(Book book : books){
            if(name.equals(book.getName()) && author.equals(book.getAuthor())){
                book.setBorrowed(true);
                break;
            }
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

  Test类代码如下:

import java.util.List;

public class Test{

    public static void main(String[] args){
        Library myLittleLibrary = Library.getLibrary();

        Book b1 = new Book();
        b1.setName("Java程序设计");
        b1.setAuthor("张三");
        b1.setPrice(45);
        b1.setBorrowed(false);

        Book b2 = new Book();
        b2.setName("Java核心技术");
        b2.setAuthor("李四");
        b2.setPrice(50);
        b2.setBorrowed(false);

        Book b3 = new Book();
        b3.setName("Java程序设计");
        b3.setAuthor("王五");
        b3.setPrice(38);
        b3.setBorrowed(false);

        //初始化图书馆
        myLittleLibrary.addBooks(b1);
        myLittleLibrary.addBooks(b2);
        myLittleLibrary.addBooks(b3);

        //显示全部图书
        List<Book> books = myLittleLibrary.getAllBooks();
        for(Book book : books){
            System.out.println(book);
        }
        //出借
        Reader oneBeautifulGirl = new Reader();
        oneBeautifulGirl.borrowBook("Java程序设计", "张三");
        for(Book book : books){
            System.out.println(book);
        }

        //归还
        oneBeautifulGirl.returnBook("Java程序设计", "张三");
        for(Book book : books){
            System.out.println(book(;
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

3.92.读取txt文件内容

文件内容大致如下:

  00001 张三  计算机系 男 …
  00002 李四  外语系  女 …

读取文件后对内容进行整合,按院系分类输出.格式为:

  计算机系
  00001 张三  男 …
  外语系
  00002 李四  女 …

参考答案:

  Student类代码如下:

public class Student{

    private String no;
    private String name;
    private String major;
    private String gender;
    private String other;

    public String getNo(){
        return no;
    }

    public void setNo(String no){
        this.no = no;
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getMajor(){
        return major;
    }

    public void setMajor(String major){
        this.major = major;
    }

    public String getGender(){
        return gender;
    }

    public void setGender(String gender){
        this.gender = gender;
    }

    public String getOther(){
        return other;
    }

    public void setOther(String other){
        this.other = other;
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

  StudentInfos类代码如下:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class StudentInfos{

    private Map<String,List<Student>> students = new HashMap<String,List<Student>>();

    public void readStudents(String fileName) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
        String line = "";
        while((line = br.readline()) != null){
            String[] infos = line.split("\\s+");
            System.out.println(Arrays.toString(infos));
            String no = infos[0];
            String name = infos[1];
            String major = infos[2];
            String gender = infos[3];
            String other = infos[4];

            Student stu = new Student();
            stu.setNo(no);
            stu.setName(name);
            stu.setMajor(major);
            stu.setGender(gender);
            stu.setOther(other);
            List<Student> list = null;
            if(students.containsKey(major)){
                list = students.get(major);
                list.add(stu);
            } else {
                list = new ArrayList<Student>();
                list.add(stu);
                students.put(major,list);   
            }
        }
        br.close();
    }

    public void showStudent(){
        Set<String> keys = students.keySet();
        for(String key : keys){
            System.out.println(key);
            List<Student> list = students.get(key);
            for(Student stu : list){
                System.out.println(stu.getNo() + "    " + stu.getName() + "  " + stu.getGender() + "  " + stu.getOther());
            }
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

  Test类代码如下:

import java.io.IOException;

public class Test{

    public static void main(String[] args){
        StudentInfos infos = new StudentInfos();
        try{
            infos.readStudents("tmp.txt");
            infos.showStudents();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.93.采用Java多线程技术(wait和notify),设计实现一个符合生产者和消费者问题的程序.对一个对象(枪膛)进行操作,其最大容量是20颗子弹.生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程,它不断从枪膛中射出子弹

参考答案:

  StackInterface接口代码如下:

public interface StackInterface{

    public void push(int n);//压入子弹

    public int[] pop();//射出子弹
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

  PushThread类代码如下:

//生产者线程
public class PushThread implements Runnable{

    private StackInterface s;

    public PushThread(StackInterface s){
        this.s = s;
    }

    public void run(){
        int i = 0;
        while(true){
            java.util.Random r = new java.util.Random();
            i = r.nextInt(10);
            s.push(i);
            try{
                Thread.sleep(100);
            } catch(InterruptedException e){}
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

  PopThread类代码如下:

//消费者线程
public class PopThread implements Runnable{

    private StackInterface s;

    public popThread(StackInterface s){
        this.s = s;
    }

    public void run(){
        while(true){
            System.out.println("->" + s.pop()[0] + "<-");
            try{
                Thread.sleep(100);
            } catch(InterruptedException e){}
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

  SafeStack类代码如下:

//实现生产和消费的过程
public class SafeStack implements StackInterface{

    private int top = 0;

    private int[] values = new int[20];//表示枪膛对象

    private boolean dataAvailable = false;

    public void push(int n){
        Synchronized(this){
            while(dataAvailable) //1
            {
                try{
                    wait();
                } catch(InterruptedException e){
                    //忽略 //2
                }
            }
            values[top] = n;
            System.out.println("压入数字" + n + "步骤1完成");
            top++;
            dataAvailble = true;
            notifyAll();
            System.out.println("压入数字完成");
        }
    }

    public int[] pop(){
        synchronized(this){
            while(!dataAvailable)//3
            {
                try{
                    wait();
                } catch(InterruptedException e){
                    //忽略 //4
                }
            }
            System.out.println("弹出");
            top--;
            int[] test = {values[top], top };
            dataAvailable = false;
            //唤醒正在等待压入数据的线程
            notifyAll();
            return test;
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

  TestSafeStack类代码如下:

//测试
public class TestSafeStack{

    public static void main(String[] args){
        SafeStack s = new SafeStack();
        PushThread r1 = new PushThread(s);
        PopThread r2 = new PopThread(s);
        PopThread r3 = new PopThread(s);
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        Thread t3 = new Thread(r3);
        t1.start();
        t2.start();
        t3.start();
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3.94.写一个线程,每隔10秒钟标准输出到屏幕上一个”hello world”.打印10次以后退出

参考答案:
public class TimerThread extends Thread{

    public void run(){
        for(int i = 0; i < 10; i++){
            System.out.println("hello world");
            try{
                sleep(10000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        TimerThread tt = new TimerThread();
        tt.start();
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.95.下列两个方法有什么区别?

public synchronized void method1(){};

public void method2(){
    synchronized (obj) {}
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
参考答案:
public synchronized void method1(){};

   
   
  • 1
  • 2

上述代码锁定的对象时调用这个同步方法的那个对象.

public void method2(){
    synchronized (obj) {}
}

   
   
  • 1
  • 2
  • 3
  • 4

上述代码锁定的对象是obj对象.

3.96.如何格式化日期?

参考答案:

  使用SimpleDateFormat类,可以实现日期的格式化.以下代码中的nowStr变量就是格式化后的日期形式,年-月-日 时:分:秒

import java.text.SimpleDateFormat;
import java.util.Date;

public class Q096{

    public static void main(String[] args){
        Date now = new Date();
        SimpleDateFormat sdf = new SimpledateFormat("yyyy-MM-dd hh:mm:ss");
        String nowStr = sdf.format(now);
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.97.如何获取1970年到现在的毫秒数?

参考答案:
import java.util.Date;

public class Q97{

    public static void main(String[] args){
        Date dat = new Date();
        long now = dat.getTime();
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.98.给SomeInputStream类的skip函数添加JAVA注释,需要添加函数本身功能(流指针向后偏移指定长度),作者(答题者姓名),异常,参数,函数返回,函数定义最早出现的版本(x.x)

public abstract class SomeInputStream extends java.io.InputStream{

    @Override
    public long skip(long n) throws java.io.IOException{
        return super.skip(n);
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
参考答案:
public abstract class SomeInputStream extends java.io.InputStream{
    /**
    * 流指针向后偏移指定长度
    *
    * @author welkin
    * @param n
    * 指定长度
    * @return 偏移后的位置
    * @throws IOException
    * @version 1.0
    */

    @Override
    public long skip(long n) throws java.io.IOException{
        return super.skip(n);
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.99.有数组[5,0,-5,2,-4,5,10,3,-5,2,-4,3,4,9,1],请写代码输出每个数的频率数(正负数算一个数),如下面结果:

5出现4次
0出现1次

   
   
  • 1
  • 2
  • 3
参考答案:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Q099{

    public static void main(String[] args){
        int[] arr = {5,0,-5,2,-4,5,10,3,-5,2,-4,3,4,9,1};
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(Integer i : arr){
            Integer key = Math.abs(i);
            if(map.containsKey(key)){
                map.put(key,map.get(key) + 1);
            } else {
                map.put(key,1);
            }
        }
        Set<Integer> keys = map.keySet();
        for(Integer key : keys){
            System.out.println(key + "出现" + map.get(key) + "次");
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3.100.请写一个Test类包含divide方法,该方法实现两个整数相除精确返回四舍五入到百分位的数值

参考答案:
public static double divide(double first,double second){
    return Math.round(first * 100 / second) / 100.0;
}
   
   
  • 1
  • 2
  • 3
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值