1. (简答题, 15分)Displaying the prime factors: Write a program that prompts the user to enter a positive integer and displays all its smallest factors in decreasing order. For example, if the integer is 120, the smallest factors are displayed as 5, 3, 2, 2, 2. Use the StackOfIntegers class to store the factors (e.g., 2, 2, 2, 3, 5) and retrieve and display them in reverse order.
Hint: The StackOfIntegers class is implemented in the Section 10.6 of the textbook. Please include the StackOfInteger class in your program to make it can be executed correctly in the enviroment.
import java.util.Scanner;
class StackOfIntegers{
private int[] elements;
private int size;
public static final int DEFAULT_CAPACITY = 16;
public StackOfIntegers(){
this(DEFAULT_CAPACITY);
}
public StackOfIntegers(int capacity){
elements = new int[capacity];
}
public void push(int value){
if(size>=elements.length){
int [] temp = new int[elements.length*2];
System.arraycopy(elements,0,temp,0,elements.length);
elements=temp;
}
elements[size++]=value;
}
public int pop(){
return elements[--size];
}
public int peek(){
return elements[size-1];
}
public boolean empty(){
return size==0;
}
public int getSize(){
return size;
}
}
public class Demo01 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入一个正整数:");
int number = sc.nextInt();
sc.close();
StackOfIntegers stack = new StackOfIntegers();
int i = 2;
while(number/2!=0){
if (number%i==0){
number = number/i;
stack.push(i);
}else if(number%i!=0){
i++;
}
}
while (!stack.empty()){
System.out.print(stack.pop()+" ");
}
}
}
2. (简答题, 15分)The Course class: Revise the Course class as follows:
■ The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it.
■ Implement the dropStudent method.
■ Add a new method named clear() that removes all students from the course. Write a test program that creates a course, adds three students, removes one, and displays the students in the course.
Hint: The simpler Course class is implemented in the Section 10.5 of the textbook.
Please submit the source code in the text form,and attach a picture to show the output of your program.
import java.util.Scanner;
class Course{
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
public Course(String courseName){
this.courseName=courseName;
}
public void addStudent(String student){
if (students.length==numberOfStudents){
String[] temp = new String[numberOfStudents*2];
System.arraycopy(students,0,temp,0,students.length);
students = temp;
}
students[numberOfStudents++]=student;
}
public String[] getStudents(){
return students;
}
public int getNumberOfStudents(){
return numberOfStudents;
}
public String getCourseName(){
return courseName;
}
public void dropStudent(String student){
for(int i = 0 ; i<numberOfStudents;i++){
if (students[i]==student){
for (int j = i+1;j<numberOfStudents;j++,i++){
students[i]=students[j];
}
break;
}
}
numberOfStudents--;
}
public void clear(){
students = null;
numberOfStudents = 0;
}
}
public class Demo02 {
public static void main(String[] args) {
Course course = new Course("Programing");
Scanner sc =new Scanner(System.in);
System.out.println("请输入要添加学生的姓名:");
for(int i = 0 ; i<3;i++){
course.addStudent(sc.next());
}
String[] students = course.getStudents();
int numberOfStudents1 = course.getNumberOfStudents();
System.out.println("学生名单为:");
for(int i =0 ; i <numberOfStudents1;i++){
System.out.print(students[i]+" ");
}
System.out.println("");
System.out.println("请输入要删除学生的姓名:");
course.dropStudent(sc.next());
System.out.println("删除之后的学生名单为:");
int numberOfStudents2 = course.getNumberOfStudents();
for(int j = 0;j<numberOfStudents2;j++){
System.out.print(students[j]+" ");
}
}
}
3. (简答题, 15分)The Queue class: Section 10.6 gives a class for Stack. Design a class named Queue for storing integers. Like a stack, a queue holds elements. In a stack, the elements are retrieved in a last-in first-out fashion. In a queue, the elements are retrieved in a first-in first-out fashion. The class contains:
■ An int[] data field named elements that stores the int values in the queue.
■ A data field named size that stores the number of elements in the queue.
■ A constructor that creates a Queue object with default capacity 8.
■ The method enqueue(int v) that adds v into the queue.
■ The method dequeue() that removes and returns the element from the queue.
■ The method empty() that returns true if the queue is empty.
■ The method getSize() that returns the size of the queue.
Draw an UML diagram for the class. Implement the class with the initial array size set to 8. The array size will be doubled once the number of the elements exceeds the size. After an element is removed from the beginning of the array, you need to shift all elements in the array one position the left. Write a test program that adds 20 numbers from 1 to 20 into the queue and removes these numbers and displays them.
Please submit the source code in the text form, attach one picture to show the output of your program,and attach another picture to show the UML diagram.
import java.util.Scanner;
class Queue{
private int[] element = null;
private int size = 0 ;
private int numberOfElements = 0;
public Queue(){
element = new int[8];
numberOfElements = 8;
size = 0;
}
public void enqueue(int value){
if (numberOfElements==size){
numberOfElements = element.length*2;
int [] temp = new int[numberOfElements];
System.arraycopy(element,0,temp,0,element.length);
element = temp;
}
element[size++]=value;
}
public int dequeue(){
int res = element[0];
for (int i = 0 , j = i + 1 ;j < size;i++,j++) {
element[i] = element[j];
};
size--;
return res;
}
public boolean empty(){
return size==0;
}
public int getSize(){
return size;
}
}
public class Demo03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Queue que = new Queue();
for (int i = 1; i < 21 ; i++){
que.enqueue(i);
}
int time = que.getSize();
System.out.println("被删除的元素为:");
for (int i = 0;i<time;i++){
System.out.print(que.dequeue()+" ");
}
System.out.println("");
System.out.println("该队列是否为空:"+que.empty());
}
}
4. (简答题, 20分)
(Implement the String class) The String class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyString):
public MyString(char[] chars);
public char[] toChars();
public char charAt(int index);
public int length();
public MyString substring(int begin, int end);
public MyString substring(int begin);
public MyString toLowerCase();
public MyString toUpperCase();
public static MyString valueOf(int i);
public static MyString valueOf(boolean b);
Please submit the source code in the text form.
public class MyString {
private String s;
public MyString(String s){
this.s = s;
}
public MyString(char [] chars){
s=new String(chars);
}
public char[] toChars(){
return s.toCharArray();
}
public int length(){
return s.length();
}
public MyString substring(int begin,int end){
char [] chars = s.toCharArray();
char [] temp = new char[end-begin];
for (int i = begin;i < end ; i ++ ){
temp[i-begin] = chars[begin];
}
return new MyString(temp);
}
public MyString substring(int begin){
char [] chars = s.toCharArray();
char [] temp = new char[chars.length-begin];
for (int i = begin;i < chars.length ; i ++ ){
temp[i-begin] = chars[begin];
}
return new MyString(temp);
}
public MyString toLowerCase(){
return new MyString(s.toLowerCase());
}
public MyString toUpperCase(){
return new MyString(s.toUpperCase());
}
public static MyString valueOf(int i){
return new MyString(String.valueOf(i));
}
public static MyString valueOf(boolean b){
return new MyString(String.valueOf(b));
}
}
5. (简答题, 20分)
(Implement the StringBuilder class) The StringBuilder class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyStringBuilder):
public MyStringBuilder();
public MyStringBuilder(char[] chars);
public MyStringBuilder(String s);
public MyStringBuilder append(MyStringBuilder s);
public MyStringBuilder append(int i);
public int length();
public char charAt(int index);
public MyStringBuilder toLowerCase();
public MyStringBuilder toUpperCase();
public MyStringBuilder substring(int begin, int end);
public MyStringBuilder substring(int begin);
public String toString();
public MyStringBuilder insert(int offset, MyStringBuilder s);
public MyStringBuilder reverse();
Please submit the source code in the text form.
public class MyStringBuilder {
private StringBuilder sb ;
public MyStringBuilder(){};
public MyStringBuilder(String s){
this.sb = new StringBuilder(s);
}
public MyStringBuilder(char [] chars){
this.sb = new StringBuilder(new String(chars));
}
public MyStringBuilder append(MyStringBuilder s){
sb.append(s);
return new MyStringBuilder(sb.toString());
}
public int length(){
return sb.length();
}
public char charAt(int index){
char[] chars = sb.toString().toCharArray();
return chars[index];
}
public MyStringBuilder toLowerCase(){
return new MyStringBuilder(sb.toString().toLowerCase());
}
public MyStringBuilder toUpperCase(){
return new MyStringBuilder(sb.toString().toUpperCase());
}
public MyStringBuilder substring(int begin,int end){
String substring = sb.substring(begin, end);
return new MyStringBuilder(substring);
}
public MyStringBuilder substring(int begin){
String substring = sb.substring(begin);
return new MyStringBuilder(substring);
}
@Override
public String toString() {
return "MyStringBuilder{" +
"sb=" + sb +
'}';
}
public MyStringBuilder insert(int offset,MyStringBuilder s){
StringBuilder sb = s.sb;
StringBuilder insert = this.sb.insert(offset, sb);
return new MyStringBuilder(insert.toString());
}
public MyStringBuilder reverse(){
StringBuilder reverse = sb.reverse();
return new MyStringBuilder(reverse.toString());
}
}