面向对象编程基础习题及答案 下载本文

}

public static void increase(int[] x) { for (int i = 0; i < x.length; i++) x[i]++; }

public static void increase(int y) { y++; } }

Results: 2 1

9. Given the following program, show the values of the array in the following figure:

public class Test {

public static void main(String[] args) { int[] values = new int[5];

for (int i = 1; i < 5; i++) { values[i] = i;

}

values[0] = values[1] + values[4]; } } After the array is created 0 1 2 3 4 After the first iteration in the loop is done 0 1 2 3 4 After the loop is completed 0 1 2 3 4 After the last statement in the main method is executed 0 1 2 3 4

Results:

第 29 页 共 34 页

After the array is created 0 1 2 3 4 0 0 0 0 0 After the first iteration in the loop is done 0 1 2 3 4 0 1 0 0 0 After the loop is completed 0 1 2 3 4 0 1 2 3 4 After the last statement in the main method is executed 0 1 2 3 4 5 1 2 3 4

PartV. Programming

1. (10pts) (Don?t use the if statement) Write a complete program named TrueOrFalse.java.

The program reads a double number from an input dialog box and checks whether the number is between 1 and 1000. For example, if your input is 5, the output should be: Is the number 5 between 1 and 1000? true

If your input is 2000, the output should be:

Is the number 2000 between 1 and 1000? false

Referenced Code:

import javax.swing.JOptionPane;

+1pt +2pt +1pt

public class TrueOrFalse { /**Main method*/

public static void main(String[] args) { // Prompt the user to enter an integer

String intString = JOptionPane.showInputDialog( \

// Convert string to int

int number = Integer.parseInt(intString);

// Display results

+2pt

+2pt

System.out.println(\the number \+ number + \between 1 and

1000 \ +2pts } }

2. (15pts) Write a program that reads the sales amount from the console and computes sales

commissions using the following scheme:

Sales Amount $0.01–$5,000

Commission Rate 8 percent 10 percent

12 percent

$5,000.01–$10,000

$10,000.01 and above

第 30 页 共 34 页

Referenced Code:

import java.util.Scanner;

public class SalesCommissions {

+1pt +1pt +1pt +2pt

/**Main method*/

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in); // Prompt the user to enter sales amount System.out.println(\ double salesAmount = scanner.nextDouble();

// Compute sales commission double salesCommission = 0;

+2pt

+2pt +1pt

if (salesAmount <= 5000)

salesCommission = salesAmount * 0.08;

else if (salesAmount <= 10000)

salesCommission = 5000 * 0.08 + (salesAmount - 5000) * 0.1;

+2pt else

salesCommission = 5000 * 0.08 + 10000 * 0.1 +

(salesAmount - 10000) * 0.12; +2pt

// Display results

System.out.println(\\salesCommission + \ } }

3. (10pts) By using a loop structure, write a program that computes

12?23?34?...?9899?99100 +1pt

Referenced Code:

import javax.swing.JOptionPane;

+1pt +1pt +1pt +2pt +2pt +2pt

public class LoopStructure{ /**Main method*/

public static void main(String[] args) { double sum = 0;

for (int i = 1; i <= 99; i++) sum += i * 1.0 / (i + 1);

第 31 页 共 34 页

// Display results

System.out.println(\ } }

4. (10pts) By using a loop structure, write a program that displays the following pattern.

12345678987654321 234567898765432 3456789876543 45678987654 567898765 6789876 78987 898 9

+1pt

Referenced Code:

public class hello{

+1pt +1pt +2pt +2pt

/**Main method*/

public static void main(String[] args) {

for (int row = 1; row <= 9; row++) { // Print spaces

for (int column = 1; column < row; column++) System.out.print(\

for (int column = row; column <= 9; column++) System.out.print(column);

for (int column = 8; column >= row; column--) System.out.print(column);

System.out.println(); } } }

+2pt

+2pt

5. (8pts) Write a method that will return the sum of all digits in an integer. For example,

sum(1234) returns 10 and sum(12345) returns 15. The method signature is as follows:

public static int sum(int number)

Referenced Code:

public static int sum(int number) {

第 32 页 共 34 页

+1pt