项目作者: dostonhamrakulov

项目描述 :
Programming exercises in Java
高级语言: Java
项目地址: git://github.com/dostonhamrakulov/Programming-Exercises-and-implementation-in-Java.git


Doston Hamrakulov

Exercises in Java programming language

Here, you can find a lot of programming exercise and implementations in Java.

Programming language

  1. // Java

IDE

  1. // IntelliJ IDEA

Author

Doston Hamrakulov

Software Developer

Exercise_1:

Description:

There are two integer variables(or whatever type) and you should swap them in manually without using any build in function and any other variable.

  1. public class First_exercise {
  2. public static void main(String args[]){
  3. int number_1 = 8;
  4. int number_2 = 15;
  5. number_1 = number_1 + number_2;
  6. number_2 = number_1 - number_2;
  7. number_1 = number_1 - number_2;
  8. System.out.println("First: " + number_1);
  9. System.out.println("Second: " + number_2);
  10. }
  11. }

-[X] Exercise 2:

Exercise_2:

Description:

There is a String “Hello Doston Hamrakulov” and you need to reverse it. You have to show two ways of how to reverse the String by using build-in functions and doing manually.

  1. public static void main(String args[]){
  2. String var = "Hello Doston";
  3. System.out.println(var);
  4. System.out.println("\nUsing library:");
  5. System.out.println(UsingLibrary(var));
  6. System.out.println("\nDoing manually:");
  7. System.out.println(Manually(var));
  8. }
  9. public static String UsingLibrary(String name){
  10. String newName = new StringBuilder(name).reverse().toString();
  11. return newName;
  12. }
  13. public static String Manually(String name){
  14. char dos[] = new char[name.length()];
  15. for (int i = 1; i <= name.length(); i++){
  16. if (i > name.length()){
  17. break;
  18. } else {
  19. dos[i-1] = name.charAt(name.length()-i);
  20. }
  21. }
  22. return new String(dos) + "";
  23. }

Exercise_3:

Description:

Write a method in Java which takes String parameter and Character, then the method should remove the character from given String.

  1. public static void main(String args[]){
  2. String str = "Doston HaBBBBBv";
  3. System.out.println("Third exercise:\n\n" + str);
  4. System.out.println("Answer:\n\n" + MyMethod(str, 'B'));
  5. }
  6. private static String MyMethod(String str, char c){
  7. if (str == null){
  8. return null;
  9. }
  10. return str.replaceAll(Character.toString(c), "");
  11. }

Exercise_4:

Description:

Write a Java program which takes input of Integer number and outputs the ODD or EVEN of the entered number.

  1. public static void main(String args[]){
  2. int input;
  3. Scanner sc = new Scanner(System.in);
  4. System.out.println("Please, enter number");
  5. input = sc.nextInt();
  6. String num = "2019";
  7. Integer a = Integer.valueOf(num);
  8. int d = a.intValue();
  9. if ((input % 2) == 0){
  10. System.out.println("\nNumber is EVEN");
  11. } else {
  12. System.out.println("\nNumber is ODD");
  13. }
  14. }

Exercise_5:

Description:

Write a Java program that reads an integer between 0 and 1000 and adds all the digits of that integer

  1. public static void main(String args[]){
  2. System.out.println(getSUM(2222));
  3. }
  4. static int getSUM(int num){
  5. String a = String.valueOf(num);
  6. int sum = 0;
  7. for (int i = 0; i < a.length(); i++){
  8. sum += Integer.parseInt(a.charAt(i) + "");
  9. }
  10. return sum;
  11. }

Exercise_6:

Description:

Write java program which find the lowest common multiple of two Integer number.

  1. public static void main (String args[]){
  2. System.out.println(MyMethod(8, 12));
  3. }
  4. public static int MyMethod(int x, int y)
  5. {
  6. int a;
  7. a = (x > y) ? x : y; // a is greater number
  8. while(true)
  9. {
  10. if(a % x == 0 && a % y == 0)
  11. return a;
  12. ++a;
  13. }
  14. }

Exercise_7:

Description:

Write program that outputs 10 times without using any loop (‘for’, ‘while’, ‘do while’, ect.)

  1. static int index = 0;
  2. static String temp = "";
  3. public static void main(String[] args) {
  4. System.out.println(printMore("1_", 10));
  5. }
  6. private static String printMore(String myStr, int i) {
  7. temp += myStr;
  8. index++;
  9. if (index < i){
  10. printMore(myStr, i);
  11. }
  12. return temp;
  13. }

-[ ] Exercise 8:
Write a Java method to count all vowels in a string.

Enjoy in open-source community :)