A Diamond Program Objective: Write a class that uses the shape interface to now
ID: 3803028 • Letter: A
Question
A Diamond Program Objective: Write a class that uses the shape interface to now draw a diamond First download the driver and put it in your project DO NOT ALTER THE DRIVER! Also download the ShapeBasics and Shapelnterface to use in your code an INTERFACE DiamondInterface which inherits from Shapelnterface Create the following method definitions o setWidth this method is used to set the width of the diamond Write a class file called Diamond that DOES NOT HAVE a main method This class implements the DiamondInterface and inherits from ShapeBasics Some of the attributes of Diamond are o width that represents the width of the triangle that has to be odd Create the following Constructors Default sets everything to default values width -0 o One that has the parameters Offset used in the parent class width Accessors and Mutators for each variable MAKE SURE THE MUTATORS CHECK FOR VALID VALUES! Create the following Methods o setWidth this sets the width of the diamond. This must be implemented just like the interface. This number must be odd. o drawHere this draws the diamond and overrides the method in ShapeBasics o drawTopV this private method draws the top part of the diamond o drawBottomV-this private method draws the bottom part of the diamond Create the following static methods o skipSpaces this takes in a certain number of spaces to skip which means drawingExplanation / Answer
JAVA Program :-
import java.util.*;
class Diamond
{
public void drawHere(int width,int offset)
{
for(int i=0;i<offset;i++)
{
int count=width;
for(int j=1;j<=(width/2)+1;j++)
{
for (int l=j;l<=count/2;l++)
System.out.print(" ");
for (int k=1;k<=2*j-1;k++)
{
if(k==1 || k==2*j-1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
for(int j=(width/2);j>=1;j--)
{
for (int l=j;l<=count/2;l++)
System.out.print(" ");
for (int k=1;k<=2*j-1;k++)
{
if(k==1 || k==2*j-1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
}
public class DiamondDrawer
{
public static void main(String args[])
{
System.out.println("Welcome to the Diamond Drawer ");
boolean quit=false;
while(quit==false)
{
System.out.println("Enter the diamond size followed by the offset or 0 to quit");
Scanner in=new Scanner(System.in);
int width=in.nextInt();
int offset=in.nextInt();
if(width==0 || offset==0)
{
System.out.println("Bye!");
quit=true;
}
else
{
Diamond d=new Diamond();
d.drawHere(width,offset);
}
}
}
}
Sample Output :-
C:Users ajendraDocumentscheggjava>javac DiamondDrawer.java
C:Users ajendraDocumentscheggjava>java DiamondDrawer
Welcome to the Diamond Drawer
Enter the diamond size followed by the offset or 0 to quit
7
1
*
* *
* *
* *
* *
* *
*
Enter the diamond size followed by the offset or 0 to quit
8
2
*
* *
* *
* *
* *
* *
* *
* *
*
*
* *
* *
* *
* *
* *
* *
* *
*
Enter the diamond size followed by the offset or 0 to quit
0
0
Bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.