You are on page 1of 7

Chapter 2 - Basic

1. In this exercise you will fix several java applications by solving compilation and run time errors.
a. { public static void main(String args[]) { for(byte index = 0; index < 10; index = index + 1) { System.out.println(Hello); } } } b. { for(byte index = 0; index < 10; index = index + 1) { System.out.println(Hello); } c. } public class HelloLoop { public static void main(String args[]) { boolean toGo = true; float num = 0; while(toGo) { num++; System.out.println(Hello); switch(num) { case 10: toGo = false; break; } } } public class HelloLoop public class HelloLoop

2. The following application prints the sum of num1 and num2. Change it in a way that it will, also, print the difference between these two numbers. class SimpleComputation { public static void main(String args[]) { double num1=24, num2=8; System.out.println(num1+num2=+(num1+num2)); } }

3. Write an application that prints the reminder of dividing 8 by 3.

4. Write an application that swaps the values of two variables.


class SimpleSwap { public static void main(String args[]) { int num1=3,num2=4,temp; System.out.println("num1="+num1+" num2="+num2); temp = num1; num1 = num2; num2 = temp; System.out.println("num1="+num1+" num2="+num2); } }

5. Write an application that swaps the values in two variables without using another variable. 6. Write an application that prints all of integer the numbers between 1 (included) and 100 (included) using the while loop.

7. Write an application that prints all of the integer numbers between 1 (included) and 100 (included) using the do..while loop.

8. Write an application that prints all of the integer numbers between 1 (included) and 100 (included) using the for loop. 9. Write an application that prints all of the even numbers between 1 (included) and 100 (included) using the while loop.

10. Write an application that prints all of the even numbers between 1 (included) and 100 (included) using the do..while loop.

11. Write an application that prints all of the even numbers between 1 (included) and 100 (included) using the for loop. 12. Write an application that calculates the sum of all the integer numbers between 1 (included) and 100 (included). 13. Write an application that calculates and prints the average of all the integer numbers between 1 (included) and 100 (included). 14. Write an application that calculates and prints the biggest number between 1 (included) and 100 (included) that divides in 7 without a residual. 15. Write an application that calculates the sum of all the numbers between 1 (included) and 100 (included) that divide in 7 without a residual. 16. Write an application that prints all of the integer numbers between 1 (included) and 1000 (included) and near each one of them (each number will be printed in a new line) it prints EVEN or UNEVEN.

17. Write an application that prints a rectangle of stars as follows: ********** ********** ********** ********** **********
class SimpleStarsRectangle { public static void main(String args[]) { for(int row=0; row<5; row++) { for(int col=0; col<10; col++) { System.out.print("* "); } System.out.println(); } } }

18. Write an application that prints the multiplication table.

19. Write an application that prints a shape of stars as follows: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 20. Write an application that prints to the screen the following: * * * * * * * * * * * * * * * *

public class Pyramide { public static void main(String args[]) { for(int row=0; row<4; row++) { for(int space=0; space<=4-row; space++) { System.out.print(" "); } for(int stars=1; stars<=1+2*row ;stars++) { System.out.print("* "); } System.out.println(); } } }

21. Write an application that prints to the screen the following: * * * * * * * * * * * * * * * * * * * * * * * * *

22. Write an application that prints to the screen the following series: 0, 3, 8, 15, 24, 35, 48, . . . The application should print the first 10 numbers of the series. 23. Write an application that prints to the screen the following series: 1, 3, 7, 15, 31, 63, . . . The application should print the first 10 numbers of the series.

24. Write an application that prints to the screen the following series: 0, 1, 1, 2, 3, 5, 8, 13, 21 . The application should print the first 10 numbers of the series.

class Fibo { public static void main(String args[]) { int current=1,preCurrent=0,index=0,temp=0; do { System.out.print(preCurrent+"\t"); System.out.print(current+"\t"); preCurrent = current + preCurrent; current = preCurrent + current; index+=2; } while(index<10); } }

25. Write an application that prints to the screen the following series: 1, 7, 16, 37, 79, 173 . The application should print the first 10 numbers of the series. 26. Write an application that prints to the screen the following series: 1, 3, 9, 27, 81, 243 . The application should print the first 10 numbers of the series.

27. The following application prints the factorial of 6. The computation of 6! is done using a static method that calculates the factorial of the number that it gets. Complete the missing lines. class FactorialApplication { public static void main(String args[]) { long number, result; number = 6; result = factorial(number); System.out.println(The factorial of 6 is : + result); } public static long factorial(long value) {
6

long result = 1; //add the missing lines here

return result; } } 28. Develop a stand-alone application that prints the factorials of each one of the numbers between 1 and 10. 29. Develop a stand-alone application that prints each one of the numbers between 0 and FFFF (the numbers should be printed in Hexadecimal base).

30. Develop a stand-alone application that prints each one of the numbers between 0 and 777 (the numbers should be printed in Octal base).

You might also like