当前位置:网站首页 > Java基础 > 正文

java程序设计基础与实践答案



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.

 

 

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.

 

 

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. 

java程序设计基础与实践答案

■ 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.

 

 

 

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.

 

 

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.


                            

版权声明


相关文章:

  • java基础怎么理解2024-11-13 20:26:05
  • 头歌java多线程基础答案2024-11-13 20:26:05
  • java基础代码训练2024-11-13 20:26:05
  • java框架面试基础2024-11-13 20:26:05
  • java程序设计基础11版2024-11-13 20:26:05
  • java循环结构基础2024-11-13 20:26:05
  • 第2章java语言基础2024-11-13 20:26:05
  • java基础与android2024-11-13 20:26:05
  • java踩坑基础题2024-11-13 20:26:05
  • java基础知识开头2024-11-13 20:26:05