Question

Write a method that draws a rectangle on the screen using ASCII characters. This method will...

Write a method that draws a rectangle on the screen using ASCII characters. This method will prompt the user for the height and width of the rectangle. Use the following ASCII values for the box characters: 218 upper left-hand corner, 217 lower right-hand corner, 196 top and bottom, 192 lower left-hand corner, 191 upper right-hand corner, and 179 left and right sides. Using Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There are many methods to draw a rectangle from ASCII values.

Here, I used a very simple method for the Answer using loops.

The comments are specified in bold and italic letters.

The required java code is as follows :-

import java.util.*;

public class Main //Class for the program
{
public static void main(String []args) //Main Method
{
Scanner sc = new Scanner(System.in); //Object of Scanner Class
  
/************************************************************************************
* For getting the symbols from the given ASCII Values :-
* Press "alt" and "ASCII value" of that symbol on keyboard.
* In the keep 'alt' pressed whole time and after entering ASCII value release it.
* You will find the desired symbol on the screen.
* For example :- for ASCII value 196,
* Press 'alt' and '196'. The symbol '─' will appear on screen.
************************************************************************************/
  

//character data types for storing the required symbols.
char top_left = '┌'; //for upper left hand side
char top_right = '┐'; //for upper right hand side
char bottom_left = '└'; //for lower left hand side
char bottom_right = '┘'; //for lower right hand side
char top_bottom = '─'; //for top and bottom walls
char sides = '│'; //for the left and right side walls
  
/**********************************************************
* For Taking input.
* Enter height and width of the rectangle.
* The height and width entered should not be less than 2.
**********************************************************/
  

System.out.println("Enter the dimensions of Rectangle");
System.out.println("(The dimensions of Rectangle should be greater than or equal to 2)");
System.out.println("Height :");
int height;
height = sc.nextInt();
System.out.println("width :");
int width;
width = sc.nextInt();
System.out.println( "Required Rectangle is as follows : " );
  
/*****************************************************************
* For printing first line of the rectangle.
* First print upper left side symbol.
* Then print a top bottom wall symbol for balancing,
* as the size of corner symbol is different.
* Then print top bottom walls for width-2 iterations.
* (top bottom walls are printed 2 times for 1 left right walls,
* as the size of left right wall is double of top bottom wall).
* Then print upper right side symbol.
*****************************************************************/
  

System.out.print(top_left);
System.out.print(top_bottom);
for( int i = 0 ; i < width-2 ; i++ )
{
System.out.print(top_bottom);
System.out.print(top_bottom);
  
}
System.out.println(top_right);
  
/******************************************************************
*For printing next lines of the rectangle except lastr line.
* Use a loop of height-2 iterations.
* For each line, first print the left right wall symbol.
* Then print a space.
* Now print double space for width-2 times.
* (space are printed 2 times for 1 iteration,
* as the size of left right wall is double of space).
* And finally print the left side wall symbol.
******************************************************************/
  

for( int j = 0 ; j < height-2 ; j++ )
{
System.out.print(sides);
System.out.print(" ");
for( int i = 0 ; i < width-2 ; i++ )
System.out.print(" ");
System.out.println(sides);
}
  
/*****************************************************************
* For printing last line of the rectangle.
* First print lower left side symbol.
* Then print a top bottom wall symbol for balancing,
* as the size of corner symbol is different.
* Then print top bottom walls for width-2 iterations.
* (top bottom walls are printed 2 times for 1 left right walls,
* as the size of left right wall is double of top bottom wall).
* Then print lower right side symbol.
*****************************************************************/

System.out.print(bottom_left);
System.out.print(top_bottom);
for( int i = 0 ; i < width-2 ; i++ )
{
System.out.print(top_bottom);
System.out.print(top_bottom);
}
System.out.println(bottom_right);
}
}

The screenshot for different input and output is as follows :-

Execute h Result $javac Main.java 2 2 $java -Xmx128M -Xms16M Main Enter the dimensions of Rectangle (The dimensions of RectanExecute | lI Result $javac Main.java 10 10 $java -Xmx128M -XMS16M Main Enter the dimensions of Rectangle (The dimensions of RI Result Execute $javac Main.java 10 5 $java -Xmx128M -XMS16M Main Enter the dimensions of Rectangle (The dimensions of Recta

LI Result Execute $javac Main.java 5 10 $java -Xmx128M -Xms16M Main Enter the dimensions of Rectangle (The dimensions of Rect

Add a comment
Know the answer?
Add Answer to:
Write a method that draws a rectangle on the screen using ASCII characters. This method will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Four point charges of equal magnitude Q=35 nC are placed on the corners of a rectangle of sides D1=28 cm and D2=7 cm.

    Four point charges of equal magnitude Q=35 nC are placed on the corners of a rectangle of sides D1=28 cm and D2=7 cm. The charges on the left side of the rectangle are positive while the charges on the right side of the rectangle are negative Use a coordinate system fixed to the bottom left hand charge, with positive directions as shown in the figure.Part (a) Which of the following represents a free-body diagram for the charge on the lower...

  • Four point charges of equal magnitude Q=55 nC are placed on the corners of a rectangle of sides D1=28 cm and D2 = 7 cm

    Problem 9: Four point charges of equal magnitude Q=55 nC are placed on the corners of a rectangle of sides D1=28 cm and D2 = 7 cm. The charges on the left side of the rectangle are positive while the charges on the right side of the rectangle are negative. Use a coordinate system fixed to the bottom left hand charge, with positive directions as shown in the figurePart (a) Which of the following represents a free-body diagram for the...

  • Four point charges of equal magnitude Q = 55 nC are placed on the corners of a rectangle of sides D1 =27 cm and D2 =5 cm.

    Problem 9: Four point charges of equal magnitude Q = 55 nC are placed on the corners of a rectangle of sides D1 =27 cm and D2 =5 cm. The charges on the left side of the rectangle are positive while the charges on the right side of the rectangle are negative. Use a coordinate system fixed to the bottom left hand charge, with positive directions as shown in the figure. Part (a) Which of the following represents a free-body diagram for...

  • In Java, write a class Rectangle. This Rectangle class should have only the following public methods...

    In Java, write a class Rectangle. This Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write a method overlap(Rectangle other). This method should return true if this...

  • 1. In IntelliJ create a new project called F1_2 2. In the Project window create a...

    1. In IntelliJ create a new project called F1_2 2. In the Project window create a new Java package called F1_2. This can be done by right clicking on src and going to New ! Package. 3. In package lab1 2 create a class called Rectangle. This can be done by right clicking on the package and going to New ! Java Class 4. At the beginning of Rectangle.java add the line package lab1 2; 5. In Rectangle.java create a...

  • Java Using NetBean Design a math method rectArea to calculate rectangle area = width * height....

    Java Using NetBean Design a math method rectArea to calculate rectangle area = width * height. Your main program will get width and height from user and call rectArea for calculation and then the main program will show the final result.

  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

  • Help write in pseudocode? ASCII Art. Computer graphics were really awful when Personal Computers first came...

    Help write in pseudocode? ASCII Art. Computer graphics were really awful when Personal Computers first came to market. In fact, many computers would show spreadsheets and even full video games using only ASCII characters (look up Nethack, Angband, or even Medievia). We’ll start the beginning of your ASCII Art career by drawing squares that are based on user input. Write a function called drawBox that takes in number representing the width of the square. In main, ask the user for...

  • Using basic c++ write a separate code for each of the following: 1. Area Rectangle •...

    Using basic c++ write a separate code for each of the following: 1. Area Rectangle • Write a program that asks the user to enter length and width of a rectangle and then display the rectangles area. • Write the following functions • getLength – prompt the user to enter length and return that value as a double • getWidth – prompt the user to enter width and return that value as a double • getArea – This method should...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT