本文主要是介绍《Java语言程序设计与数据结构》编程练习答案(第二十四章)(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《Java语言程序设计与数据结构》编程练习答案(第二十四章)(二)
英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
24.13
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);FibonacciIterator test = new FibonacciIterator(100000);while(test.hasNext()){System.out.print(test.next()+" ");}}}class FibonacciIterator{private int limit = 0;private int value = 1;private int previous = 0;public FibonacciIterator(int limit){this.limit = limit;}public boolean hasNext() {return value <= limit;}public int next() {int ret = this.value;int next = previous + value;previous = value;value = next;return ret;}
}
24.14
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);PrimeIterator test = new PrimeIterator(100000);while (test.hasNext()){System.out.print(test.next()+" ");}}
}class PrimeIterator{private int limit;private int current = 2;public PrimeIterator(){}public PrimeIterator(int limit){this.limit = limit;}public boolean hasNext(){return current <= limit;}public int next(){int ret = current;current++;while (!checkPrime(current)){current++;}return ret;}private boolean checkPrime(int number){for(int i=2;i<= number/2;i++){if(number % i == 0){return false;}}return true;}
}
24.15
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);MyArrayList<Integer> test = new MyArrayList<>();test.add(114514);test.add(1919810);test.add(114514);assert test.get(0) == 114514;assert test.indexOf(1919810) == 1;assert test.lastIndexOf(114514) == 2;assert test.remove(2) == 114514;test.set(2, 191981);assert test.get(2) == 191981;assert !test.contains(415411);test.add(1,1919);assert test.get(1) == 1919;assert test.get(2) == 1919810;}
}class MyArrayList<T> {public MyArrayList(){ }public MyArrayList(T[] objects){for(T o: objects){add(o);}}public void add(T e){ }public void clear(){ }public void add(int index, T e){ }public boolean contains(Object o){return false;}public T get(int index){return null;}public int indexOf(Object e){return -1;}public int lastIndexOf(Object e){return -1;}public T remove(int index){return null;}public T set(int index, T e){return null;}}
24.16
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);MyLinkedList<Integer> test = new MyLinkedList<>();test.addLast(114514);test.addLast(1919810);assert test.getLast() == 1919810;assert test.removeLast() == 1919810;test.addLast(1919810);test.addLast(114514);assert test.removeFirst() == 114514;test.addFirst(114514);assert test.get(0) == 114514;assert test.getFirst() == 114514;assert test.indexOf(1919810) == 1;assert test.lastIndexOf(114514) == 2;assert test.remove(2) == 114514;test.set(2, 191981);assert test.get(2) == 191981;assert !test.contains(415411);test.add(1,1919);assert test.get(1) == 1919;assert test.get(2) == 1919810;}
}class MyLinkedList<T>{public MyLinkedList(){}public MyLinkedList(T[] objects){}public T getFirst(){return null;}public T getLast(){return null;}public void addFirst(T e){}public void addLast(T e){}public void add (int index, T e){}public T removeFirst(){return null;}public T removeLast(){return null;}public T remove(int index){return null;}public void clear(){}public boolean contains(Object e){return false;}public T get(int index){return null;}public int indexOf(Object e){return -1;}public int lastIndexOf(T e){return -1;}public T set(int index, T e){return null;}public int size(){return 0;}
}
24.17
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);}
}class PriorityUsingSortedArrayList<T extends Comparable<T>>{private ArrayList<T> list = new ArrayList<>();int size = 0;public PriorityUsingSortedArrayList(){}public void add(T e){if(list.isEmpty() || list.get(list.size()-1).compareTo(e) < 0){list.add(e);}else{for(int i=0;i<list.size();i++){if(list.get(i).compareTo(e) > 0){list.add(i, e);}}}}public boolean remove(T e){return list.remove(e);}
}
第二十四章 完
这篇关于《Java语言程序设计与数据结构》编程练习答案(第二十四章)(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!