您的位置:首页 > 编程语言 > Java开发

Exercise------------ProgUseJava

2008-11-06 13:11 204 查看

P43页第3题:求1!+2!+3!+...10! 。程序代码如下:

int i,j;

long sum=0,temp=1;

for(i=1;i<=10;i++){

for(j=1;j<=i;j++){

temp=temp*j;

}

sum=sum+temp;

temp=1;

}

System.out.println("1!+2!+3!+...+10!="+sum);

P43页第5题:求8+88+888+。。。前10项之和。程序代码如下:

long sum=0,a=8,item=a,n=10,i=1;

for (i=1;i<=n;i++) {

sum=sum+item;

item=item*10+a;

}

System.out.println(sum);

第6题:求打印如下图形

9 8 7 6 5 4 3 2 1 0

8 7 6 5 4 3 2 1 0

7 6 5 4 3 2 1 0

6 5 4 3 2 1 0

5 4 3 2 1 0

4 3 2 1 0

3 2 1 0

2 1 0

1 0

0

Press any key tocontinue...



int i=0,j=0,k=0;

int x;

for(i=9;i>=0;i--){

for(k=0;k<(9-i);k++)

System.out.print(" "); //4个空格

for(j=i;j>=0;j--){

System.out.print(j+" "); //3个空格

}

System.out.println();

}

/**********************************************************************/

int result = 0;

for(int i = 1;i<=100;i++){

for(int j=2;j<i/2;j++){

if(i%j==0) break;

else {

System.out.println(i);

result =result+i;

break;

}

}

}

System.out.println("result = "+result);

Exercise
1:

Explain why it is necessary to write computer programs in order to instruct a computer to carry out a task you need done.

Exercise
2:

Different groups of people require the computer to run different types of programs for different purposes.
Describe the different types of programs.

Exercise
3:

What do you understand by the term “Computer Program”.

Exercise
4:

Computers can be used to solve a variety of problems.
Briefly explain the process by which a computer achieves this.

Exercise
5:

Describe the programming method using Machine Code and Assembly language. List their advantages and disadvantages

Exercise
6:

What do you understand by the term “Compiler”

Exercise
7:

What do you understand by the term “Interpreter”

Chapter 2: The Design Process

Exercise
1:

Write short notes on how Decision Tables work and how they can be helpful to the programmer during program development.

Exercise
2:

Describe the following terms related to a decision table:
·
Action Stub
·
Action Entry

Exercise
3:

Name the four components of a Decision Table.

Exercise
4:

Explain why in some cases the use of a decision table is preferable to using a flow chart.

Exercise
5:

A business has decided to encourage early payment on its invoices by introducing the following payment procedures for its regular customers:
10% discount if the invoice is settled within 7 days
7% discount if the invoice is settled within 14 days
5% discount if the invoice is settled within 30 days
Other cases for discount must be referred to the manager.
Construct a decision table for implementing the above decision procedure.

Exercise
6:

Describe two ways of converting a decision table into program code.

Exercise
7:

The three distinct approaches to program design checking are:
·
Dry running
·
Walk-through
·
Independent inspection
Give a description of each of the approaches.

Exercise
8:

What do you understand the term “Egoless Programming” to mean?

Exercise
9:

Explain why program design is an important part of program development. Describe two consequences of poor design in the development of a computer system.

Chapter 3: The Basic Java Application

Exercise
1:

Key in the following program, compile it, and run it:

public
class HelloWorld {

public static void main(String[] args) {

System.out.println("A computer is a programmable electronic device");

System.out.println("that can store, retrieve, and process data.");

}
}

Exercise
2:

Write a program to print out the following lines from Dr. Seuss’s Horton Hatches the Egg.
I meant what I said
and I said what I meant
An elephant’s faithful
one hundred percent
Put a border of asterisks (*) around the entire quotation (all four sides). Each line of the quotation should be sent to the output stream in the same
statement.

Exercise
3:

Which of following variables’ name is legal in Java:
a.
1st_day
b.
Date_Of_Birth
c.
Salary/Month
d.
Unit Price

Exercise
4:

What do you understand by the term “Expressions”?

Exercise
5:

Develop a program to convert from centimeter to inch

Exercise
6:

Develop a program to display “Leap year” if the year%4==0 otherwise it displays “Not a leap year”.

Chapter 4: Java Control Structures

Exercise
1:

Develop a program to display the maximum between three number: a, b and c

Exercise
2:

Use the For loop to construct a loop which prints the following numbers:

19, 21, 23, 25, 27

Exercise
3:

Develop a program to solve the following equation:
ax2
+ bx + c = 0

Exercise
4:

Develop a program to display all the prime number < 10000

Exercise
5:

Develop a program, using for loop to print out the following numbers:

9 8 7
6 5 4 3
2 1

8 7 6
5 4 3 2
1

7 6 5
4 3 2 1

6 5 4
3 2 1

5 4 3
2 1

4 3 2
1

3 2 1

2 1

1

Exercise
6:

Develop a program to calculate n!
n! = 1 x 2 x 3 x 4 x …. x n

Exercise
7:

Develop a program to calculate sum of odd number < 5000

Chapter 5: Arrays, File I/O and Streams

Exercise
1:

Write a program to multiply the following matrixes

Exercise
2:

The American Heart Association recommends that no more than 30% of person’s daily calories come from fat. Each gram of fat is nine calories. Given the
grams of fat and the number of calories in an item, we can calculate the percentage of calories that comes from fat.
Item
Grams of Fat
Calories
Percent from Fat
Tuna
1
60
_______________________
Spaetzle
2
170
_______________________
V8 Juice
0
36
_______________________
Corned Beef
7
200
_______________________

Exercise
3:

Write a program to save the following triangle composed of a symbol to a file. The number lines in the triangle and the symbol should be entered as input
from keyboard. For example, if the input values are 6 and *, the output is as follows:
*
**
***
****
*****
******

Exercise
4:

Develop a program, called MySort, to sort all the program arguments in the ascending order.
For example:
java MySort
John Bill Peter
Bill John Peter

Exercise
5:

A motor dealer is recording the following information for each car in the stock file:
Car Registration Number
Car Type
Car Model
Registration Year
Car Price
Develop a program to read/write these information to a text file.

Chapter 6: Object Oriented Programming

Exercise
1:

List and describe the steps in the object-oriented design of any problem to be solved.

Exercise
2:

Briefly explain the meaning of the term “object” with reference to object-oriented design.

Exercise
3:

A teacher requires a program to keep track of the attendance of the students in a class to produce an attendance report for each student for the year.
a.
List the basic classes required for this program.
b.
Identify and describe the instance variables for each class.
c.
List the methods for each class.
d.
For each method, describe its purpose, parameters, preconditions and postconditions.

Exercise
4:

Referring to question 3 above, list and describe the methods required for the program.

Exercise
5:

Explain the meaning of the term “class” used in object-oriented design in programming.

Exercise
6:

What is the purpose of Constructors and Destructors in object-oriented programming.
Where are they placed?

Exercise
7:

A program is required that displays a menu to add, subtract, multiply or divide two numbers to be input.
The result is then displayed.
a.
List the classes for the program.
b.
List the instance variables
c.
List the methods for each class
d.
Provide a specification for each of the methods
e.
List the procedures required

Exercise
8:

List and describe the principles supported by object-oriented
techniques.

Chapter 7: Recursion and Linked Data Structures

Exercise
1:

Use example to explain the term “Recursion”

Exercise
2:

Write a recursive method to calculate the factorial of a number: N! = N*(N-1)*(N-2)* … *3*1
N is a parameter and can be key in from keyboard.

Exercise
3:

The Fibonacci numbers are defined as the following sequence:
1 1 2 3 5 8 13 21 34 35 …
Notice that except for the first two numbers, each number is the sum of the two preceding numbers. Write a recursive value-returning method, fib, which
returns the Nth Fibonacci number where N is a parameter. Test your program with fib(5), fib(10), and fib(20).
Show your output.

Exercise
4:

A warehouse stores products.
A file holds the following information on each product.
Product number
Product name
Quantity in store
Price per item
Supplier name

Develop a program to:
a.
Keep these product in double link list
b.
Using “Quicksort” algorithm to sort these product by Product number in the ascending order.
c.
Using “Binary search” algorithm to search for a product by Product number.

Exercise
5:

a.
Develop Binary Tree a class
b.
Add a method to search an item in a tree

Exercise
6:

When you are typing, IBM PC BIOS keeps these keys in a FIFO queue.

Develop a class to simulate IBM PC this queue.

Chapter 8: Applet, HTML and GUIs

Exercise
1:

What is a Java Applet? Give example.

Exercise
2:

What does HTML stand for?
a.
High Technology and Multipurpose Language
b.
High Technology Markup Language
c.
HyperText Markup Language
d.
HyperLink Markup Language

Exercise
3:

Write an Applet to display the following text in red color:

Everything seems normal.
January 2002

Exercise
4:

What do you understand by the term “Graphics Context”?

Exercise
5:

Write a Calculator applet.

Exercise
6:

List and describe Layouts and Components concept in Java

Chapter 9: Testing and Java Technology

Exercise
1:

Briefly describe the SQA concept.

Exercise
2:

Describe in detail the content of Test Metrics and Software Test Cycle

Exercise
3:

List and describe in detail the content of Development Phases

Exercise
4:

Clearly describe a program design method that you have used in the development of a computer program.

Exercise
5:

Here is a listing of the public interface of an abstract data type that represents fractions:
public class Fraction {

public Fraction add(Fraction frac1)

// returns object plus frac1

public Fraction sub(Fraction frac1)

// returns object minus frac1

public Fraction mult(Fraction frac1)

// returns object times frac1

public int numIs()

// returns numerator of frac1

public Fraction(int num, int denom);

// Constructor: sets numerator and denominator

// Data members

private int numerator;

private int denominator;
}
Implement the methods in class Fraction and then write a test plan.

Chapter 10: Debugging

Exercise
1:

An interactive debugger does not identify syntax errors.
What is its main purpose?

Exercise
2:

A lot of utilities that have some debugging facility.
Describe a feature of a program debugging utility.

Exercise
3:

Program errors can be syntax errors or logic errors.
Differentiate between these types of errors.

Exercise
4:

Using a programming language of your choice, Give an example of a syntax error and one example of a logic error.

Exercise
5:

Describe, with examples, two ways of writing program code which improves the reader’s understanding.

Exercise
6

Techniques are available to help programmers identify and resolve errors.
What methods should a programmer use to:
a.
Identify errors in program design.
b.
Find out more information about the error during coding and running on the computer.
c.
Correct the error.

Exercise
7

There are a number of tools that programmers can make use of when debugging a program.
Describe how the debugging aids below are used.
a.
Source code listing.
b.
Interactive debugger.
c.
Program trace.
d.
Memory dump.

Exercise
8

A programmer usually finds some difficulty when debugging another programmer’s program.
What are some methods that can help the programmer?

Exercise
9

“Debugging is the process of searching for errors in a program.”
Comment on this statement.

Chapter 11: The System Life Cycle

Exercise
1:

List the phases of a software development life cycle.

Exercise
2:

Describe what is involved in the feasibility study phase of a system development life cycle and list the people who participate in it.

Exercise
3:

Describe what is meant by independent inspection in the process of program design testing.

Exercise
4:

Describe how catalytic checking as a team may be used in the development of a program.

Exercise
5:

Describe fully the parallel run as a method of program implementation, stating its advantages and disadvantages.

Exercise
6:

Name the parties involved in writing the requirement definition for a software system.

Exercise
7:

Identify and describe the information that is typically gathered from an interview during a systems investigation.

Exercise
8:

What are the types of problems that are likely to be encountered by an interviewer during an interview?

Chapter 12: Development Aids and Application Software

Exercise
1:

Write short notes on the features of the following program development aids:
a.
Flowcharter
b.
Text editor
c.
Screen formatter
d.
Decision Table preprocessor

Exercise
2:

A programmer may decide to use an application package to meet a specification.
List some application packages available in your country and describe their purposes.

Exercise
3:

Organizations use computers for a variety of purposes.
List and describe four such applications.

Exercise
4:

An organization has the option of buying pre-developed software off-the-shelf or developing the software in-house.
Discuss the pros and cons of each of the methods of obtaining software.

Exercise
5:

Describe fully the layout of a typical spreadsheet display screen.

Exercise
6:

Name one operating system feature
which is used to check on the use of system resources, e.g. printers.

Exercise
7:

List some Java development aids packages available in your country and describe their purposes.

一、

public class aaa{

public static void main(String[] args){

int i=0,j=0;

int x;

for(i=9;i>=0;i--){

for(j=i;j>=0;j--){

System.out.print(j+" ");

}

System.out.println();

}

}

}
二、

public class aaa{

public static void main(String[] args){

int i=0,j=0,k=0;

int x;

for(i=9;i>=0;i--){

for(k=0;k<(9-i);k++)

System.out.print(" ");
//4个空格

for(j=i;j>=0;j--){

System.out.print(j+" ");
//3个空格

}

System.out.println();

}

}

}
三、

public class aaa{

public static void main(String[] args){

int[][] a={{5,4,-2},{4,-6,-5},{7,8,1}};

int[][] b={{8,2},{-2,4},{3,3}};

int[][] c=new int[3][2];

int i,j,k;

System.out.println("Array a==");

for(i=0;i<3;i++){

for(j=0;j<3;j++){

System.out.print(a[i][j]+" ");

}

System.out.println();

}

System.out.println();

System.out.println("Array b==");

for(i=0;i<3;i++){

for(j=0;j<2;j++){

System.out.print(b[i][j]+" ");

}

System.out.println();

}

System.out.println();

System.out.println("Array a*b==");

for(i=0;i<3;i++){

for(j=0;j<2;j++){

c[i][j]=0;

for(k=0;k<3;k++){

c[i][j]+=a[i][k]*b[k][j];

}//k

}//j

}//i

for(i=0;i<3;i++){

for(j=0;j<2;j++){

System.out.print(c[i][j]+" ");

}

System.out.println();

}

}//main

}//class
四、

public class aaa{

public static int fib(int n){

if(n<2){

return 1;

}

else{

return((fib(n-1))+(fib(n-2)));

}

}

public static void main(String[] args){

int i=0;

while(i<=9){

System.out.print(fib(i)+"
");

i++;

}

System.out.println();// next line

}//main

}//class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: