Question

In: Computer Science

In your book there is a Class method called Square that draws a square either using...

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

Solutions

Expert Solution

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


Related Solutions

By using Python: a. Make a class called Book. The __init__() method for Book should store...
By using Python: a. Make a class called Book. The __init__() method for Book should store two attributes: a title and an author. Make a method called book_info() that prints these two pieces of information, and a method called book_available() that prints a message indicating that the book is available in the library. b. Create an instance called book1 from your class. Print the two attributes individually, and then call both methods. c. Create three different instances from the class,...
1. Using either a Bible, or a book called "Harmony of the Gospels," compare the accounts...
1. Using either a Bible, or a book called "Harmony of the Gospels," compare the accounts of the Baptism and Temptation of Jesus in Mark with that of Matthew and Luke. What are the similarities? What are the main differences? 2. Why do many people believe Mark is the first gospel written? Does the answer to question #1 support Mark as the first gospel?
. Create a class called Book to represent a book. A Book should include four pieces...
. Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square shape. The class should contain a side property. Provide an __init__ method that takes the side length as an argument. Also, provide the following read-only properties: a) perimeter returns 4 × side. b) area returns side × side. c) diagonal returns the square root of the expression (2 × side2). The perimeter, area and diagonal should not have corresponding data attributes; rather, they should...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This method accepts from a user (1) an integer hourly rate and (2) an integer total number of hours worked in a week. It calculates and displays the total weekly wage of an employee. The company pays straight time for the first 40 hours worked by an employee and times and a half for all the hours worked in excess of 40. This class should...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Write a OOP class called BookList that uses a Book class and allows multiple books to...
Write a OOP class called BookList that uses a Book class and allows multiple books to be entered into a BookList object. Include normally useful methods and boolean addBook(Book b), Book searchBook(String title, String author). addBook returns true if b is successfully added to the list. searchBook returns a book matching either the title or author from the current list. You do not need to write Book (assume there are suitable constructors and methods) or any test code (main). You...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT