In: Computer Science
In your book there is a Class method called Square that draws a square either using a border or a solid. Using that class, create a Square2 class that also prompts the user for the character to be printed (i.e. such as #, $, whatever) and if the user enters a blank character it defaults to *.
You’ll need to modify the called private methods in Square to accept a character passed.
Note that for the purposes of this Lab it is okay to prompt the user from the class methods.
Sample session:
M:\Java253>java Square2Driver
Enter width of desired square: 9
Area = 81
What character should be used for printing? (i.e. *) #
Print with (b)order or (s)olid? s
#########
#########
#########
#########
#########
#########
#########
#########
#########
/***********************************************************
* SquareDriver.java
* Dean & Dean
*
* This is the driver for the Square class. ***********************************************************/
import java.util.Scanner;
public class SquareDriver
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Square square;
System.out.print("Enter width of desired square: ");
square = new Square(stdIn.nextInt());
System.out.println("Area = " + square.getArea());
square.draw();
} // end main
} // end class SquareDriver
/************************************************************
* Square.java
* Dean & Dean
*
* This class manages squares.
************************************************************/
import java.util.Scanner;
public class Square
{
private int width;
//*********************************************************
public Square(int width)
{
this.width = width;
}
//*********************************************************
public int getArea()
{
return this.width * this.width;
}
//*********************************************************
public void draw()
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Print with (b)order or (s)olid? ");
if (stdIn.nextLine().charAt(0) == 'b')
{
drawBorderSquare();
}
else
{
drawSolidSquare();
}
} // end draw
//*****************************************************
private void drawBorderSquare()
{
drawHorizontalLine();
drawSides();
drawHorizontalLine();
} // end drawBorderSquare
//*****************************************************
private void drawSolidSquare()
{
for (int i=0; i<this.width; i++)
{
drawHorizontalLine();
}
} // end drawSolidSquare
//*****************************************************
private void drawHorizontalLine()
{
for (int i=0; i<this.width; i++)
{
System.out.print("*");
}
System.out.println();
} // end drawHorizontalLine
//*****************************************************
private void drawSides()
{
for (int i=1; i<(this.width-1); i++)
{
System.out.print("*");
for (int j=1; j<(this.width-1); j++)
{
System.out.print(" ");
}
System.out.println("*");
}
} // end drawSides
} // end class Square
Please find below the code for Square2.java
import java.util.Scanner;
public class Square2
{
private int width;
//*********************************************************
public Square2(int width)
{
this.width = width;
}
//*********************************************************
public int getArea()
{
return this.width * this.width;
}
//*********************************************************
public void draw()
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter the charcter to be used for printing :: "); /*Get the charcater to be used for printing from user*/
char c = stdIn.nextLine().charAt(0);
if(Character.compare(c, ' ')==0) /* if character is blank, replace it with **/
{
c='*';
}
System.out.print("Print with (b)order or (s)olid? ");
if (stdIn.nextLine().charAt(0) == 'b')
{
drawBorderSquare(c); /*pass character to function */
}
else
{
drawSolidSquare(c); /*pass character to function */
}
} // end draw
//*****************************************************
private void drawBorderSquare(char ch) /* accepted character as arguments */
{
drawHorizontalLine(ch); /*pass character to function */
drawSides(ch); /*pass character to function */
drawHorizontalLine(ch); /*pass character to function */
} // end drawBorderSquare
//*****************************************************
private void drawSolidSquare(char ch)
{
for (int i=0; i<this.width; i++)
{
drawHorizontalLine(ch); /*pass character to function */
}
} // end drawSolidSquare
//*****************************************************
private void drawHorizontalLine(char ch)
{
for (int i=0; i<this.width; i++)
{
System.out.print(ch); /*used character passed to print */
}
System.out.println();
} // end drawHorizontalLine
//*****************************************************
private void drawSides(char ch)
{
for (int i=1; i<(this.width-1); i++)
{
System.out.print(ch); /*used character passed to print */
for (int j=1; j<(this.width-1); j++)
{
System.out.print(" ");
}
System.out.println(ch); /*used character passed to print */
}
} // end drawSides
} // end class Square
driver code
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Square2 square;
System.out.print("Enter width of desired square: ");
square = new Square2(stdIn.nextInt());
System.out.println("Area = " + square.getArea());
square.draw();
}
} // end class SquareDriver
output scrren shot::
Please upvote if u like my answer