Tugas Array dan Inheritance Kelompok xx


Anggota Kelompok 2:

Anisya Anggita F (1202160130)

Ayuvira Kusuma M (1202164203)

Vera Adelia (1202164114)

Kelas : SI-40-02

BAB 8 (Inheritance)

Soal :

2. Define a class called Diamond that is derived from either the class Shapebasics (Listing 8.12) or the abstract class SpabeBase (Listing 8.19). A diamond has the same sort of top half as a Triangle object, and its bottom half is an inverted version of its top half. Define utility class having public static methods, such as the method skipSpaces and other methods that draw horizontal lines, big V’s, and inverted big V’s . Recall that Self-Test Questions 31 asked you to describe one method in this class.

Penjelasan soal :

Dalam soal ini, kita akan membuat gambar berbentuk diamond yang memakai class turunan dari ShapeBasics. Diamond itu terdiri dari gabungan setengah objek segitiga di atas dan di bawah. Definisikan garis horizontal berbentuk V dan V terbalik dengan method skipSpcaes atau method yang lain.

Script Code Java :

ShapeBasic

1. public class ShapeBasics implements ShapeInterface {

2. private int offset;

3. public ShapeBasics(){

4. offset = 0;

5. }

6. public void SahpeBasics(int theOffset){

7. offset = theOffset;

8. }

9. public void setOffset (int newOffset){

10. offset = newOffset;

11. }

12. public int getOffset(){

13. return offset;

14. }

15. public void drawAt(int lineNumber){

16. for (int count = 0; count<lineNumber;count++)

17. System.out.println();

18. drawHere();

19. }

20. public void drawHere(){

21. for (int count = 0; count < offset; count++)

22. System.out.print(‘*’);

23. System.out.println(‘*’);

24. }

25. }

ShapeInterface

1. public interface ShapeInterface {

2. public void setOffset(int newOffset);

3. public int getOffset();

4. public void drawAt(int lineNumber);

5. public void drawHere();

6. }

DiamondInterface

1. public interface DiamondInterface extends ShapeInterface {

2. public void set(int newBase);

3. }

Diamond

1. public class Diamond extends ShapeBasics implements DiamondInterface {

2. private int base;

3. public Diamond(){

4. super ();

5. base = 0;

6. }

7. public Diamond(int theOffset, int theBase){

8. super ();

9. base = theBase;

10. }

11. public void set(int newBase){

12. base = newBase;

13. }

14. public void drawHere(){

15. drawTop();

16. drawBase();

17. }

18. private void drawBase(){

19. skipSpaces(getOffset());

20. for (int count=0;count<base;count++)

21. System.out.print(‘*’);

22. System.out.println();

23. }

24. private void drawTop(){

25. int startOfLine = getOffset()+base/2;

26. skipSpaces(startOfLine);

27. System.out.println(‘*’);

28. int lineCount = base / 2 -1;

29. int insideWidth = 1;

30. for (int count=0;count<lineCount;count++){

31. startOfLine –;

32. skipSpaces(startOfLine);

33. System.out.print(‘*’);

34. skipSpaces(insideWidth);

35. System.out.println(‘*’);

36. insideWidth = insideWidth + 2;

37. }

38. }

39. private void drawBottom(){

40. int startOfLine = getOffset()+base/2;

41. skipSpaces(startOfLine);

42. System.out.println(‘*’);

43. int lineCount = base / 2 -1;

44. int insideWidth = 1;

45. for (int count=0;count<lineCount;count++){

46. startOfLine–;

47. skipSpaces(startOfLine);

48. System.out.print(‘*’);

49. skipSpaces(insideWidth);

50. System.out.println(‘*’);

51. insideWidth = insideWidth – 2;

52. }

53. }

54. private static void skipSpaces(int number){

55. for (int count=0;count<number;count++)

56. System.out.print(‘ ‘);

57. }

58. }

DiamnondDemo

1. package bab8;

2. public class Bab8 {

3. public static void main(String[] args) {

4. int n = 21;

5. //segitiga atas

6. for (int i=0;i<n/2+1;i++) {

7. for (int j=0;j<n/2-i;j++){

8. System.out.print(" ");

9. }

10. for (int j=0;j<2*i+1;j++){

11. if (i==0 || j==0 || j==2*i)

12. System.out.print("*");

13. else

14. System.out.print(" ");

15. }

16. System.out.println();

17. }

18. //segitiga bawah

19. for (int i=n/2-1;i>=0;i–) {

20. for (int j=0;j<n/2-i;j++){

21. System.out.print(" ");

22. }

23. for (int j=0;j<2*i+1;j++){

24. if (i==n/2 || j==0 || j==2*i)

25. System.out.print("*");

26. else

27. System.out.print(" ");

28. }

29. System.out.println();

30. }

31. }

32. }

Hasil ScreenShoot Output :



Leave a Reply