Exercise Java

Exercise (1)

1. Reverse Sentence

Write method that when given a sentence, it will return the reverse sentence

sentence = "java is my favorite language"
// reverse = "egaugnal etirovaf ym si avaj"

sentece = "java programming designed by James Gosling"
// reverse = "gnilsoG semaJ yb dengised gnimmargorp avaj"

Exercise (2)

2. Longest Digit

Write a method that when given an array containing positive numbers, it will return the element with the longest digit. If there are two or more elements with the longest digit, it will return the first element with the longest digit.

int[] arr = { 3, 40000, 10 }  // return 40000
int[] arr = { 8, 900, 500 }   // return 900
int[] arr = { 8, 500, 900 }   // return 500

Exercise (3)

3. Write a method that when given a sentence, it will return true if the sentence is a palindrome and return false otherwise. Palindrome is sequence that reads the same backward as forward

"Race fast safe car"        # return true
"Live not on evil"          # return true
"Live free or die hard"     # return false

Exercise (4)

4. Tahun Kabisat

User menginputkan tahun yang terdiri dari 4 digit, jika kurang dari 4 digit user diharuskan menginput ulang. Buatlah sebuah method yang dapat menentukan apakah tahun tersebut tahun kabisat atau bukan jika algoritma menghitung tahun kabisat adalah:

  • Jika tahun habis dibagi 400, maka tahun tersebut adalah tahun kabisat.
  • Jika tahun tidak habis dibagi 400 tetapi habis dibagi 100, maka bukan tahun kabisat.
  • Jika tahun tidak habis dibagi 400 dan tidak habis dibagi 100, tetapi habis dibagi 4, maka tahun kabisat. Jika tahun tidak habis dibagi 400, tidak habis dibagi 100, dan tidak habis dibagi 4, maka bukan tahun kabisat.

 

Exercise (5)

3. UUD 1945

Find the text of UUD 1945 preambule. Save it to a file named "uud1945.txt" (starting from the words "Bahwa sesungguhnya kemerdekaan itu ....").

Write a program that read "uud1945.txt" file and then output the following to screen:

// Calculate total words in UUD 1945 (clue: split based on space)
"Total words in UUD 1945 preambule is: " + your_calculation_here

// Find out how many words containing the substring "rakyat" appears there
"Words containing substring 'rakyat' occur " + your_calculation_here + " times"

Solution

Exercise Java Programming

By Nur Ratna Sari

Exercise Java Programming

  • 83