Tugas Array dan Inheritance Kelompok 03


KELOMPOK 3 :

-IKHSAN HERMAWAN

-YOGA SAKTI PRATAMA

-DAFFA NAUFAL M

Soal Halaman 647

  1. Define two derived classes of the abstract class ShapeBase in Listing 8.19. Your two classes will be called RightArrow and LeftArrow. These classes will be like the classes Rectangle and Triangle, but they will draw arrows that point right and left, respectively. The size of the arrow is determined by two numbers, one for the length of the “tail” and one for the width of the arrowhead. (The width is the length of the vertical base.) The arrow shown here has a length of 16 and a width

of 7. The width of the arrowhead cannot be an even number, so your constructors and mutator methods should check to make sure that it is always

odd. Write a test program for each class that tests all the methods in the class.

You can assume that the width of the base of the arrowhead is at least 3.

Penjelasan Soal : Jadi pada soal kali ini kita disuruh untuk membuat script yang memiliki output gambar panah kekiri dan panah kekanan.

Script Code Java :

Main Class Right Arrow

  1. public class RightArrow {
  2. /**
  3. * @param args the command line arguments
  4. */
  5. public static void main(String[] args) {
  6. Ikhsan tulus = new Ikhsan();
  7. tulus.printRightArrow(‘*’, 3);
  8. tulus.printRightArrow(‘#’, 7);
  9. }
  10. }

Main Class LeftArrow

  1. package rightarrow;
  2. /**
  3. *
  4. * @author X550VX i7
  5. */
  6. public class LeftArrow {
  7. public static void main(String[] args) {
  8. Ikhsan tulus = new Ikhsan();
  9. tulus.printLeftArrow(‘*’, 3);
  10. tulus.printLeftArrow(‘#’, 7);
  11. }
  12. }

Class Ikhsan

  1. package rightarrow;
  2. import java.util.Arrays;
  3. /**
  4. *
  5. * @author X550VX i7
  6. */
  7. public class Ikhsan {
  8. private static String repeat(char ch, int count) {
  9. char[] buf = new char[count];
  10. Arrays.fill(buf, ch);
  11. return new String(buf);
  12. }
  13. public static void printRightArrow(char ch, int n) {
  14. for (int i = 1; i < n; i++)
  15. System.out.println(repeat(‘ ‘, n – 1) + repeat(ch, i));
  16. System.out.println(repeat(ch, n * 2 – 1));
  17. for (int i = n – 1; i >= 1; i–)
  18. System.out.println(repeat(‘ ‘, n – 1) + repeat(ch, i));
  19. }
  20. public static void printLeftArrow(char ch, int n) {
  21. for (int i = 1; i < n; i++)
  22. System.out.println(repeat(‘ ‘, n – i) + repeat(ch, i));
  23. System.out.println(repeat(ch, n * 2 – 1));
  24. for (int i = n – 1; i >= 1; i–)
  25. System.out.println(repeat(‘ ‘, n – i) + repeat(ch, i));
  26. }
  27. }

Output :

Right Arrow

Left Arrow

Note : Bila ingin menjalankan script LeftArrow, klik kanan pada script lalu klik ‘Run File (Shift + F6)


Leave a Reply