Flag Software Studio Java Phase 2 Review Answers

Keywords: Java

1. Multiple Choice Questions (5 subsections, 5 points each)

1. Three classes are defined in a valid Java source program file, and classes with the property public may have ().
A,0
B,1
C,2
D,3

2. The incorrect description of static variables is uuu.
A. Static variables are also called class variables
B. Static variable is a common storage space
C. You can access static variables directly using the class name
D. Objects cannot use static variables

3. In the Java language, the incorrect description of the construction method is uuu.
A. The system always provides default construction methods
B. The construction method does not return a value
C. Construction methods can be overloaded
D, construction method name and class name are the same

4. Inheritance of the Java language. The following statement is incorrect uuu.
A. Code reuse through inheritance
B. A parent can have multiple subclasses at the same time
C. Each class has a direct parent
D. A subclass can have multiple direct parent classes at the same time

5. It is required to design a class that has a special member domain that must be accessible by subclasses of this class, but not by other classes that are not in the same package. The following () can meet the above requirements.
A. The encapsulation property of the member domain is set to public
B. The encapsulation property of the member field is set to private
C. The encapsulation property of the member field is set to protected
D. The member domain does not require special encapsulation properties

1,B
2,D
3,A
4,D
5,C

2. Algorithmic Questions (35 points total)

1. Small Blue will make house numbers for the residents of one street.
The street has a total of 2020 residents with house numbers ranging from 1 to 1200.
Xiao Blue's method of making house plates is to make 0-9 numeric characters first, and then put words into the house as needed.
For example, house 1017 needs to paste characters 1, 0, 1, 7 in turn, that is, 1 character 0, 2 character 1, 1 character 7.
How many characters 2 do you need to make all house numbers 1 to 2020?

public class Test01 {
    public static void main(String[] args) {
        int x = 2020;
        int y = 0;
        for (int i = 1;i<=x;i++){
            int w = i;
            while (w!= 0){
                int z = w%10;
                if (z==2){
                    y++;
                }
                w /= 10;
            }
        }
        System.out.println(y);
    }
}

2. Given a non-negative integer represented by a non-empty array of integers, add one to it.
The highest number of digits is placed at the top of the array, where each element only stores a single number.
You can assume that this integer does not start with zero except for the integer 0. (10 points)

Example 1:

Input: digits = [1,2,3]
Output:[1,2,4]
Interpretation: The input array represents the number 123.

Example 2:

Input: digits = [4,3,2,1]
Output:[4,3,2,2]
Interpretation: The input array represents the number 4321.

Example 3:

Input: digits = [0]
Output:[1]
Interpretation: The input array represents the number 0.
public class Test02 {
    public static void main(String[] args) {
        //Given a non-negative integer represented by a non-empty array of integers, add one to it. The highest number is placed at the top of the array, the number
        //Each element in a group stores only a single number. (Note: What you want to prevent is that the first number is 9 or the last number is 9 ha! In this case, there will be carry drops)
        int[] a={9,9,9};
        System.out.println(Arrays.toString(plusOne(a)));
    }
    public static int[] plusOne(int[] digits){
        for (int i = digits.length - 1; i >= 0; i--) {
            if (digits[i] != 9) {
                //Return if it's not 9 plus one
                digits[i]++;
                return digits;
            }
            digits[i] = 0;//If the current bit is 9, carry is required, the current bit is assigned 0, the previous bit is added 1
        }
        //Jump out of the for loop and say the numbers are all 9
        int[] result = new int[digits.length + 1];
        result[0] = 1;
        return result;
    }
}

3. Matrix A is represented by an array as int a[][]={{1,2,3,4}, {2,3,4,5}, {3,4,5,6}, and a transpose() method is written to transpose the matrix (transpose as follows) (15)

public class Test03 {

    public int[][] transpose(int[][] a){
        int[][] b = new int[4][3];
        for (int i = 0; i < a[0].length; i++) {
            for (int j = 0; j < a.length; j++) {
                b[i][j] = a[j][i];
            }
        }
        return b;
    }

    public static void main(String[] args) {
        int[][] a ={{1,2,3,4},{2,3,4,5},{3,4,5,6}};
        Test03 test03 =new Test03();
        System.out.println(Arrays.deepToString(test03.transpose(a)));
    }

}

3. Basic Object-Oriented Programming (40 points)

Requirement:
(1) Programming according to the names of variables, types and methods given in the title, and no modification is allowed;
(2) Optional programming environment, either Eclipse or JDK

(1) Library Interface
Membership method:
Borrow(), borrow books
revert(), return the book

(2) Book s
Member variables:
Book name String Type
publisher String Type
Construction method:
Initialize book name s and publisher s with parameters
General method:
(1) Set the getter and setter methods to get and set the value of the name variable in the class;
(2) Rewrite the Equals method to be the same book if and only if the title and publisher are equal.
(3) Rewrite the toString method to return information about the book name and publisher in the following style:
"Book Name: Java Programming, Publishing House: Tsinghua University Press"

(3) CollectionBook, which inherits from Book class and implements Library interface;
Member variables:
Book Number (bNo) String Type
The stacks String type
Whether to borrow (isBorrow) boolean type books when the status is borrowed, the value is true
Construction method:
Initialize the book name and publisher information by calling the parent construction method, then initialize the book number (bNo) and line library (stacks)
General method:
(1) Implement the borrow method in the interface
If the status of the book is borrowed, output "Sorry, the book is borrowed", otherwise, modify the status of the book as borrowed, output "Borrowed successfully"
(2) Implementing revert methods in interfaces
If the book status is borrowed, output "The book has been returned", otherwise, modify the book borrowing status to not borrowed, and output "Successful book return"

(4) pass the main function test
(1) Create two CollectionBook objects, Book 1 and book2, which output Book 1 and book2, respectively.
And call its equals method to determine if two objects are equal
(2) Enter integer via keyboard, enter 0, borrow book1, enter 1, return Book1

public class Main {
    public static void main(String[] args) {
        CollectionBook book1=new CollectionBook("Mixed Work Method","Majia Press","666","Library");
        CollectionBook book2=new CollectionBook("Mixed Work Method","Majia Press","777","Library");
        System.out.println(book1.toString());
        System.out.println(book2.toString());
        System.out.println(book1.equals(book2));
        /** (2)Enter an integer on the keyboard and 0 to borrow book1.
         * Enter 1 to return book1*/
        Scanner reader=new Scanner(System.in);
        int a=reader.nextInt();
        if(a==0)
        {
            book1.borrow();
        }
        if(a==1)
        {
            book1.revert();
        }
    }
}

interface Library {
    void  borrow();
    void  revert();
}
class Book {
    protected String name;
    protected String publisher;
    
    public Book(String name, String publisher) {
        this.name = name;
        this.publisher = publisher;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Book book = (Book) o;
        return Objects.equals(name, book.name) &&
                Objects.equals(publisher, book.publisher);
    }

    @Override
    public String toString() {
        return "Book{" +
                "Title:" + name + '\'' +
                ", Press:" + publisher + '\'' +
                '}';
    }
}

class CollectionBook extends Book implements Library {
    String bNo;
    String stacks;
    boolean isBorrow;

    public CollectionBook(String name, String publisher, String bNo, String stacks) {
        super(name, publisher);
        this.bNo = bNo;
        this.stacks = stacks;
    }

    @Override
    public void borrow() {
        if (isBorrow){
            System.out.println("Sorry, the book is on loan");
        }else {
            isBorrow=true;
            System.out.println("Successful borrowing");
        }
    }

    @Override
    public void revert() {
        if(!isBorrow){
            System.out.println("The book has been returned");
        }else {
            isBorrow=false;
            System.out.println("Successful Book Return");
        }
    }
}

Posted by f8ball on Sat, 20 Nov 2021 14:50:31 -0800