Tugas Array dan Inheritance Kelompok 12


Kelompok 12 SI-40-02

Nama:

Ben Hughie Rezanda (1202164286)
Dennis Nigel Cunong (1202160179)
Ganar Nugraha Mahendrasta (1202164090)
Ian Dzillan Malik (1202160194)

BAB 8

Soal:

  1. 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 turunan dari class ShapeBasics atau abstract class ShapeBase. 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:

ShapeInterface interface::

public interface ShapeInterface {

public void setOffset(int newOffset);

public int getOffset();

public void drawAt(int lineNumber);

public void drawHere();

}

DiamondInterface interface:

public interface DiamondInterface extends ShapeInterface {

public void set(int newBase);

}

ShapeBasics class:

public class ShapeBasics implements ShapeInterface {

private int offset;

public ShapeBasics(){

offset = 0;

}

public void SahpeBasics(int theOffset){

offset = theOffset;

}

public void setOffset (int newOffset){

offset = newOffset;

}

public int getOffset(){

return offset;

}

public void drawAt(int lineNumber){

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

System.out.println();

drawHere();

}

public void drawHere(){

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

System.out.print(‘*’);

System.out.println(‘*’);

}

}

Diamond class:

public class Diamond extends ShapeBasics implements DiamondInterface {

private int base;

public Diamond(){

super ();

base = 0;

}

public Diamond(int theOffset, int theBase){

super ();

base = theBase;

}

public void set(int newBase){

base = newBase;

}

public void drawHere(){

drawTop();

drawBase();

}

private void drawBase(){

skipSpaces(getOffset());

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

System.out.print(‘*’);

System.out.println();

}

private void drawTop(){

int startOfLine = getOffset()+base/2;

skipSpaces(startOfLine);

System.out.println(‘*’);

int lineCount = base / 2 -1;

int insideWidth = 1;

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

startOfLine –;

skipSpaces(startOfLine);

System.out.print(‘*’);

skipSpaces(insideWidth);

System.out.println(‘*’);

insideWidth = insideWidth + 2;

}

}

private void drawBottom(){

int startOfLine = getOffset()+base/2;

skipSpaces(startOfLine);

System.out.println(‘*’);

int lineCount = base / 2 -1;

int insideWidth = 1;

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

startOfLine–;

skipSpaces(startOfLine);

System.out.print(‘*’);

skipSpaces(insideWidth);

System.out.println(‘*’);

insideWidth = insideWidth – 2;

}

}

private static void skipSpaces(int number){

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

System.out.print(‘ ‘);

}

}

Hasil output:



Leave a Reply