i WILL GIVE A GOOD REVIEW: IF YOU FOLLOW EVERY INDICATION AND THE OUTPUT COMES O
ID: 3732536 • Letter: I
Question
i WILL GIVE A GOOD REVIEW: IF YOU FOLLOW EVERY INDICATION AND THE OUTPUT COMES OUT EXACTLY AS IT SHOULD BELOW.
CSCI 161 - Lab #5: Don't Fence Me In!
Objectives
Write a program that outputs to the screen a rectangular fence built from instructions supplied by the user including:
Character to use to represent a fence post
Character to use to represent wire between horizontal fence sections
Character to use to represent wire between vertical fence sections
Number of horizontal sections of the fence
Number of vertical sections of the fence
For Example:
If the user enters o, - and | for the fence post, horizontal wire and vertical wire, respectfully, and a fence horizontal width of 5 and vertical height of 2, then the following fenced area should be output:
Instructions
Following are the instructions and you should follow them carefully:
Using Eclipse create a new Java Project and name it: Lab5
As with all your labs and assignments, add a class of the same name, Lab5, and include in that class a main method.
Your program must read a file according to the "Input Specification" below.
Your program must employ, among possible others, the methods described in the Required Methods section below.
Your program must output according to the "Output Specification" below.
Iteratively develop the assignment (get the original version working, test it, add code to one method, test, fix if needed, continue).
When the assignment is complete and working submit it as Lab6
Required Methods
Following are a list of methods to write:
Notes:
As us customary with a fence, the corners of your fenced in area will have fence posts (not wire).
Sections of the fence with start and end with a fence post and have a single wire "character" between, regardless if a horizontal or a vertical section.
Make sure that the getInt method only returns positive integers.
Input Specification
Your program must read user input from the console as shown below in the output specification dialogue with the user. In total, the user is asked for the post character, the horizontal wire character, the vertical wire character, the width of the fence, and the height of the fence.
Your program should output precisely according to the format below (wording, spacing, etc).
Explanation / Answer
import java.util.Scanner;
// Class Lab6 Definition
public class Lab6
{
// Prompts for a character entry given the prompt string
// provided then returns single character entered by the
// user via the console object.
public static char getChar(Scanner console, String prompt)
{
// To store the character entered by the user
char ch;
// Displays the message
System.out.println(prompt);
// Accepts a character by extracting the zero index position of the string entered by the user
ch = console.next().charAt(0);
// Returns the character
return ch;
}// End of method
// Prompts for a positive integer entry given the prompt string
// provided then returns the positive integer entered by the
// user via the console object.
public static int getInt(Scanner console, String prompt)
{
// To store the number entered by the user
int no;
// Displays the message
System.out.println(prompt);
// Accepts a number entered by the user
no = console.nextInt();
// Returns the number
return no;
}// End of method
// Given the post and horizontal wire characters, this
// method uses a for loop to print out the horizontal
// section of the fence given its width. Call this once for
// the top of the fence, and a 2nd time for the bottom of the
// fence.
public static void printHorizontal(char post, char hWire, int width)
{
// Loops twice the value of width times
for(int c = 0; c < width*2; c++)
// Checks if the counter value is even
if(c % 2 == 0)
// Displays the post character
System.out.print(post);
// Otherwise, counter value is odd
else
// Displays the hWire character
System.out.print(hWire);
// Displays the post character
System.out.print(post);
// Displays new line
System.out.println();
}// End of method
// Given the post and vertical wire characters, this method
// uses 2 nested for loops to print out the vertical
// sections of the fence. Each vertical section includes the
// the left and right sides which must alternate between vertical
// wire characters and post characters along with spaces in
// between for the appropriate height of the fenced area.
public static void printVerticals(char post, char vWire, int width, int height)
{
// Loops twice of height minus one times
for(int c = 0; c < (height * 2) - 1; c++)
{
// Checks if the counter value is even
if(c % 2 == 0)
{
// Displays vWire character
System.out.print(vWire);
// Loops twice of width minus one times
for(int d = 0; d < (width * 2) - 1; d++)
// Displays space
System.out.print(" ");
// Displays vWire character
System.out.print(vWire);
}// End of if condition
// Otherwise, counter value is odd
else
{
// Displays post character
System.out.print(post);
// Loops twice of width minus one times
for(int d = 0; d < (width * 2) - 1; d++)
// Displays space
System.out.print(" ");
// Displays post character
System.out.print(post);
}// End of else
// Displays new line
System.out.println();
}// End of for loop
}// End of method
// main method definition
public static void main(String[] args)
{
// Declares local variable to store the characters
char post, vWire, hWire;
// Declares local variable to store the width and height
int width, height;
// Scanner class object created
Scanner sc = new Scanner(System.in);
// Accepts data from the user
post = getChar(sc, "Please enter a single character to represent a fence post: ");
hWire = getChar(sc, "Please enter a single character to represent the horizontal fence wire: ");
vWire = getChar(sc, "Please enter a single character to represent the vertical fence wire: ");
width = getInt(sc, "Please enter the # of sections in the width of the fence: ");
height = getInt(sc, "Please enter the # of sections in the height of the fence: ");
// Calls the method to print horizontal line
printHorizontal(post, hWire, width);
// Calls the method to print vertical line
printVerticals(post, vWire, width, height);
// Calls the method to print horizontal line
printHorizontal(post, hWire, width);
}// End of main method method
}// End of class
Sample Output 1:
Please enter a single character to represent a fence post:
O
Please enter a single character to represent the horizontal fence wire:
-
Please enter a single character to represent the vertical fence wire:
|
Please enter the # of sections in the width of the fence:
5
Please enter the # of sections in the height of the fence:
2
O-O-O-O-O-O
| |
O O
| |
O-O-O-O-O-O
Sample Output 2:
Please enter a single character to represent a fence post:
*
Please enter a single character to represent the horizontal fence wire:
/
Please enter a single character to represent the vertical fence wire:
$
Please enter the # of sections in the width of the fence:
7
Please enter the # of sections in the height of the fence:
5
*/*/*/*/*/*/*/*
$ $
* *
$ $
* *
$ $
* *
$ $
* *
$ $
*/*/*/*/*/*/*/*
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.