PROGRAMMING #7 : Java Programming for Programmer - Classes and Objects (PART 2) | SUB [ENG-BAHASA]

in #programming7 years ago

RESOURCES TO LEARN JAVA

In this article is a continuation of the previous article Java Programming for Programmer - Classes and Objects (PART 1). Still in the same topic, which I will discuss about classes and objects. Immediately on the following points.

toUniversalString Method

Method toUniversalString (lines 28-31) does not require arguments and provides a return value of a String in a universal time format, which contains two digits each for hours, minutes, and seconds. For example, if the time is 1:30:07 PM, the method will give a return value of 13:30:07. Line 30 uses the static method, format, of the String class to give a return value of a String containing formatted values of hours, minutes, and seconds with two digits. The format method is similar to the System.out.printf method except that the format provides a return value of a formatted String. The formatted string used as a return value by toUniversalString method.

toString Method

The toString method (line 34-39) does not require arguments and returns a String in a standard time format, containing clock, minute, and second values separated by colon and followed by AM or PM (eg 1: 27:06 PM). Like the toUniversalString method, the toString method uses the static method, the format, of the String class to format the minutes and seconds as double-digit values. Line 37 uses the conditional operator (? :) to specify the clock value in the String. If the clock is 0 or 12 (AM or PM), it will be displayed 12. Instead, a value is shown in the range 1 to 11. The conditional operator on line 38 determines whether AM or PM will be returned as part of the String. Remember that from section 5.4, all objects in Java have a toString method that returns a String representation of the object.

Using Class Time1

As you have learned, you declare any class that represents a new type in Java. Therefore, after declaring the class Time1, you can use it as a type in the declaration as follows:

Time 1 am; // morning can accommodate a reference pointing to the Time1 object

The application class of TestTime1 in code 1.2 uses the Time1 class. Line 9 declares and creates a Time object1 and assigns it to the local time variable. The new operator implicitly calls the default constructor of class Time1, because Time1 does not declare any constructor. Lines 12-16 show first time in universal time format (by calling the toUniversalString method on line 13), then in the standard time format (by explicitly calling the toString method of the time object on line 15) to confirm that the Time1 object is initialized correctly. Next, line 19 calls the time set method of the time object to change the time. Then line 20-24 displays the time back in two formats to confirm that the time has been set correctly.

Code 1.2

Calling a TimeSet Method with Invalid Values

To illustrate that the Se-time method validates its arguments, line 29 with invalid arguments (99) for hours, minutes, and seconds. This statement is placed in a try block (line 27-30), where the time set method may throw an IllegalArgumentException exception, if all its arguments are invalid. When this happens, the exception will be caught on line 31-34, and line 33 will display an error message by calling the getMessage method. Line 37-41 displays the time back in two formats to ensure that the SetTime does not change the time when invalid arguments are provided.

To be continued...


Pada artikel ini merupakan lanjutan dari artikel sebelumnya yaitu Java Programming for Programmer - Classes and Objects (PART 1). Masih dalam topik yang sama, dimana saya akan membahas mengenai kelas dan objek. Langsung saja pada pokok pembahasan berikut ini.

Metode toUniversalString

Metode toUniversalString (baris 28-31) tidak memerlukan argumen dan memberikan nilai balik berupa suatu String dalam format waktu universal, yang memuat masing-masing dua digit untuk jam, menit, dan detik. Sebagai contoh, jika waktu adalah 1:30:07 PM, metode akan memberikan nilai balik 13:30:07. Baris 30 menggunakan metode static, format, dari kelas String untuk memberikan nilai balik suatu String yang memuat nilai-nilai terformat dari jam, menit, dan detik dengan dua digit. Metode format mirip dengan metode System.out.printf kecuali bahwa format memberikan nilai balik berupa suatu String terformat. String terformat tersebut dijadikan nilai balik oleh metode toUniversalString.

Metode toString

Metode toString (baris 34-39) tidak memerlukan argumen dan memberikan nilai balik berupa suatu String dalam format waktu standar, yang memuat nilai-nilai jam, menit, dan detik yang dipisahkan oleh titik dua dan diikuti dengan AM atau PM (misalnya, 1:27:06 PM). Seperti metode toUniversalString, metode toString menggunakan metode static, format, dari kelas String untuk memformat menit dan detik sebagai nilai-nilai dua digit. Baris 37 menggunakan operator kondisional (?:) untuk menentukan nilai jam didalam String. Jika jam bernilai 0 atau 12 (AM atau PM), maka akan ditampilkan 12. Sebaliknya, ditampilkan suatu nilai dalam rentang 1 sampai 11. Operator kondisional pada baris 38 menentukan apakah AM atau PM akan dijadikan nilai balik sebagai bagian dari String. Ingat bahwa dari bagian 5.4, semua objek dalam Java memiliki suatu metode toString yang mengembalikan suatu representasi String dari objek.

Menggunakan Kelas Waktu1

Seperti yang telah anda pelajari, setiap kelas yang anda deklarasikan merepresentasikan suatu tipe baru dalam Java. Oleh karena itu, setelah mendeklarasikan kelas Waktu1, anda bisa menggunakannya sebagai suatu tipe didalam deklarasi seperti berikut ini:

Waktu1 pagi; // pagi dapat menampung suatu referensi yang menunjuk kepada objek Waktu1

Kelas aplikasi UjiWaktu1 pada kode 1.2 menggunakan kelas Waktu1. Baris 9 mendeklarasikan dan menciptakan suatu objek Waktu1 dan menugaskannya kepada variabel lokal waktu. Operator new secara implisit memanggil konstruktor default kelas Waktu1, karena Waktu1 tidak mendeklarasikan konstruktor apapun. Baris 12-16 menampilkan waktu pertama-tama dalam format waktu universal (dengan memanggil metode toUniversalString pada baris 13), kemudian dalam format waktu standar (dengan secara ekplisit memanggil metode toString dari objek waktu pada baris 15) untuk menegaskan bahwa objek Waktu1 diinisialisasi secara benar. Selanjutnya, baris 19 memanggil metode setWaktu dari objek waktu untuk mengubah waktu. Kemudian baris 20-24 menampilkan waktu kembali dalam dua format untuk menegaskan bahwa waktu telah ditetapkan dengan benar.

Kode 1.2

Memanggil Metode SetWaktu dengan Nilai-nilai Tidak Valid

Untuk mengilustrasikan bahwa metode setWaktu memvalidasi argumen-argumennya, baris 29 dengan argumen-argumen tidak valid (99) untuk jam, menit, dan detik. Pernyataan ini ditempatkan didalam suatu blok try (baris 27-30), dimana metode setWaktu bisa jadi melempar suatu eksepsi IllegalArgumentException, bila argumen-argumennya semua tidak valid. Ketika hal ini terjadi, eksepsi akan ditangkap pada baris 31-34, dan baris 33 akan menampilkan pesan error dengan memanggil metode getMessage. Baris 37-41 menampilkan waktu kembali dalam dua format untuk memastikan bahwa setWaktu tidak mengubah waktu ketika argumen-argumen tidak valid disediakan.

Bersambung...


FOLLOW ME AT

THANK'S FOR READING THIS POST, AND THANK'S FOR YOUR SUPPORT. IF YOU LIKE IT, UP VOTE AND KEEP RESTEEM.

LOOK FORWARD TO THE NEXT POST >>>

INDONESIAN STEEMIT COMMUNITY

Sort:  

Bermanfaat kami tunggu sambungannya..

Terima kasih banyak bang hahah @syehlah

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.031
BTC 58976.49
ETH 2502.14
USDT 1.00
SBD 2.48