1 - An Introduction to Java Programming

advertisement
KING SAUD UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
INFORMATION TECHNOLOGY DEPARTMENT
CSC 111
2ndSemester 1431
Sheet # 9
1. Write a Java program that prints a diamond shape in 9 lines.
2. Write a program that reads an integer then calls a method to check
whether or not the integer is perfect. The program should print a
message accordingly. An integer is said to be perfect if its factors
including 1 but excluding the integer itself sum to the number. Ex. 6
is perfect because 1 + 2 + 3 = 6.
3. Write a program that reads an integer and then calls a method that
checks whether or not the integer is prime. Main then prints an
appropriate message.
4. Write a program that reads an integer and then calls a method that
receives an integer and returns the integer with its digits reversed.
Main prints the resulting integer.
Additional exercises:
5.
Write a program that reads two integers and then calls a method that
receives the two integers and returns their greatest common divisor,
which is printed in main..
6. Write a Java program that reads an integer x, and a character c; then
calls a method draw that prints a triangle of c in x lines.
Sample run:
Enter an integer: 5
Enter a character: a
The triangle of a in 5 lines is:
a
aa
aaa
aaaa
7. Write a program which calls a method named isMultiple. This
method takes two integers values and determines whether the second
integer is a multiple of the first. Main then prints an appropriate
message.
Sample run:
Enter first number: 4
Enter second number: 16
16 is multiple of 4
8. Write a program that reads an integer x and calls a method divisors
that prints all the divisors of x.
Sample run:
Enter an integer: 8
The divisors of 8: 1 2 4 8
9. Write a program that prompts the user to input an integer and then
calls a method printDigits that receives the integer and outputs the
individual digits of the number.
Sample run:
Enter an integer: 456
The individual digits of 456 are: 4 5 6
10. Write a program that reads two integers n and m then calls a method
drawTable that prints the multiplication table of the integers from n to
m.
Sample run:
Enter two integers: 2 5
The multiplication table of 2 and 5 is
12345
2 4 6 8 10
11. Write a method that receives two integers a and b and prints a
rectangle of stars with length a and width b.
12. Write a method rnd that receives a real number; and returns the
number rounded to the nearest integer. Your method should use the
method Math.ceil. Main should prints the resulting integer.
13. Write a program that reads an alphabetic character and calls a
method drawAlphaTriangle that prints a triangle of alphabets
following it until ‘z’.
Sample run:
Enter an alphabetic character: t
t
t u
t u v
t u v w
t u v w x
t u v w x y
t u v w x y z
Tutorial #8
14.Trace the following program:
import javax.swing.JOptionPane;
public class Sheet8{
public static void main( String [] args ) {
int value;
String output = "";
for ( int counter = 1 ; counter <= 20 ; counter++ )
{
value = 1 + (int)(Math.random()*6);
output+=value + " ";
if (counter % 5) == 0) output +="\n":
}
JOptionPane.showMessageDialog( null,output,"20 random numbers
from 1 to 6",
JOptionPane.INFORMATION_MESSAGE );
System.exit(0); } }
15.What is the value of x after each of the following statements is
executed
a. x = Math.abs(3.77);
b. x = Math.floor(-3.77);
c. x = Math.abs(0.0);
d. x = Math.ceil(0.0);
e. x = Math.abs(-3.77);
f. x = Math.ceil(-3.77);
g. x = Math.ceil(-Math.abs(-8 + Math.floor(-4.5)));
h.
16.Using the method Math.floor, write a statement that will round a value
to the nearest integer.
17.Write a method rnd that receives a real number; and returns the
number rounded to the nearest integer. Your method should use the
method Math.ceil.
18.Write statements that assign to an integer variable n random integers
in the folloing ranges:
a. 1 <= n <= 2
b. 1 <= n <= 100
c. 0 <= n <= 9
d. 1000 <= n <= 1112
e. -1 <= n <= 1
f. -4 <= n <= 55
19.Write a method intpow that receives a real number and an integer and
returns the real number raised to the power integer. Do not use
method Math.pow.
20.
Write a method that receives an integer n and prints a square of
stars whose side is n stars.
21.
Download