Create a class called Square. This class should have a constructor that takes th
ID: 3652548 • Letter: C
Question
Create a class called Square. This class should have a constructor that takes the size of the square as an integer and a method called draw() that draws the square to the console.
The size given in the constructor will determine the size of each side of the square. A size of four, for example will result in the following square:
####
####
####
####
Create a class called ArtTester that has a main method that asks the user for a size and draws a square of that size. The tester should not allow invalid sizes.
Create a class called Triangle. Similar to Square, it should have a constructor that takes the size of the square. It should also have a draw() method that draws a right triangle to the console. The size will indicate the length of the two "legs" of the triangle. A triangle of size four will look like this:
#
##
###
####
Add to ASCIIArtTester so that it creates a triangle of the same size as the square
Create a class called Box. Similar to Square, it should have a constructor that takes the size of the square. It should also have a draw() method that draws a a box to the console. The size will indicate the size of each side of the box. A box of size four will look like this:
----
| |
| |
----
Add to ArtTester so that it creates a box of the same size as the square
Create a class called Diamond. Similar to Square, it should have a constructor that takes the size of the square. It should also have a draw() method that draws a a diamond to the console. The size will indicate the size of each side of the diamond. A diamond of size four will look like this:
/
/
/
/
/
/
/
/
Explanation / Answer
Please rate...
Program Square.java
=================================================
class Square
{
int size;
Square(int s)
{
size=s;
}
public void draw()
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
System.out.print("#");
}
System.out.println();
}
}
}
===============================================
Traingle.java
===============================================
class Triangle
{
int size;
Triangle(int s)
{
size=s;
}
public void draw()
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<=i;j++)
{
System.out.print("#");
}
System.out.println();
}
}
}
============================================
Box.java
============================================
class Box
{
int size;
Box(int s)
{
size=s;
}
public void draw()
{
int i,j,k;
for(i=0;i<size;i++)
{
if(i==0 || i==size-1){
System.out.println();
for(k=0;k<size;k++)
{
System.out.print("-");
}
}
if(i!=0 && i!=size-1)
{
System.out.print(" |");
for(j=0;j<size-2;j++)
{
System.out.print(" ");
}
System.out.print("|");
}
}
}
}
=============================================
Daimond.java
============================================
class Diamond
{
int size;
Diamond(int s)
{
size=s;
}
public void draw()
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size/4;j++)
{
System.out.print("/");
}
for(j=0;j<size/4;j++)
{
System.out.print("\");
}
System.out.println();
}
for(i=0;i<size;i++)
{
for(j=0;j<size/4;j++)
{
System.out.print("\");
}
for(j=0;j<size/4;j++)
{
System.out.print("/");
}
System.out.println();
}
}
}
===============================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.