Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
AhmadLang / Introduction to Programming Using Java.pdf
Скачиваний:
75
Добавлен:
31.05.2015
Размер:
1.48 Mб
Скачать

Java Programing: Chapter 2 Exercises

Programming Exercises

For Chapter 2

THIS PAGE CONTAINS programming exercises based on material from Chapter 2 of this on-line Java textbook. Each exercise has a link to a discussion of one possible solution of that exercise.

Exercise 2.1: Write a program that will print your initials to standard output in letters that are nine lines tall. Each big letter should be made up of a bunch of *'s. For example, if your initials were "DJE", then the output would look something like:

******

 

*************

**********

**

**

 

**

**

**

**

 

**

**

**

**

 

**

**

**

**

 

**

********

**

**

**

**

**

**

**

**

**

**

**

**

**

**

**

*****

 

****

**********

See the solution!

Exercise 2.2: Write a program that simulates rolling a pair of dice. You can simulate rolling one die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. The number you pick represents the number on the die after it is rolled. As pointed out in Section 5, The expression

(int)(Math.random()*6) + 1

does the computation you need to select a random integer between 1 and 6. You can assign this value to a variable to represent one of the dice that is being rolled. Do this twice and add the results together to get the total roll. Your program should report the number showing on each die as well as the total roll. For example:

The first die comes up 3

The second die comes up 5

Your total roll is 8

(Note: The word "dice" is a plural, as in "two dice." The singuler is "die.")

See the solution!

Exercise 2.3: Write a program that asks the user's name, and then greets the user by name. Before outputting the user's name, convert it to upper case letters. For example, if the user's name is Fred, then the program should respond "Hello, FRED, nice to meet you!".

See the solution!

Exercise 2.4: Write a program that helps the user count his change. The program should ask how many quarters the user has, then how many dimes, then how many nickles, then how many pennies. Then the program should tell the user how much money he has, expressed in dollars.

http://math.hws.edu/javanotes/c2/exercises.html (1 of 2) [5/24/2000 8:35:34 AM]

Java Programing: Chapter 2 Exercises

See the solution!

Exercise 2.5: If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs left over. (This is essentially the definition of the / and % operators for integers.) Write a program that asks the user how many eggs she has and then tells the user how many dozen eggs she has and how many extra eggs are left over.

A gross of eggs is equal to 144 eggs. Extend your program so that it will tell the user how many gross, how many dozen, and how many left over eggs she has. For example, if the user says that she has 1342 eggs, then your program would respond with

Your number of eggs is 9 gross, 3 dozen, and 10

since 1342 is equal to 9*144 + 3*12 + 10.

See the solution!

[ Chapter Index | Main Index ]

http://math.hws.edu/javanotes/c2/exercises.html (2 of 2) [5/24/2000 8:35:34 AM]

Java Programing: Chapter 2 Quiz

Quiz Questions

For Chapter 2

THIS PAGE CONTAINS A SAMPLE quiz on material from Chapter 2 of this on-line Java textbook. You

should be able to answer these questions after studying that chapter. Sample answers to all the quiz questions can be found here.

Question 1: Briefly explain what is meant by the syntax and the semantics of a programming language. Give an example to illustrate the difference between a syntax error and a semantics error.

Question 2: What does the computer do when it executes a variable declaration statement. Give an example.

Question 3: What is a type, as this term relates to programming?

Question 4: One of the primitive types in Java is boolean. What is the boolean type? Where are boolean values used? What are its possible values?

Question 5: Give the meaning of each of the following Java operators:

a)++

b)&&

c)!=

Question 6: Explain what is meant by an assignment statement, and give an example. What are assignment statements used for?

Question 7: What is meant by precedence of operators?

Question 8: What is a literal?

Question 9: In Java, classes have two fundamentally different purposes. What are they?

Question 10: What is the difference between the statement "x = TextIO.getDouble();" and the statement "x = TextIO.getlnDouble();"

[ Answers | Chapter Index | Main Index ]

http://math.hws.edu/javanotes/c2/quiz.html [5/24/2000 8:35:34 AM]