Question: Printing a Line of 'Stars' (Asterisks) (use Java) Here are the directions : Download the file named PrintingShapes.java from the website. Using this as a
Printing a Line of 'Stars' (Asterisks) (use Java)
Here are the directions:
Download the file named PrintingShapes.java from the website.
Using this as a basis, write some code that will first ask the user how many stars to print out, and then print out that many asterisks, using loops.
An example transcript of the program might look like so (user input in red)
| Hello! How many stars would you like me to print? 9 Okay, here are your stars: ********* |
NOTE: If you want to print some text, and move to the next line (like you normally want to), then you might add System.out.println(""); // print nothing, but move to the next line!
NOTE: If you want to print some text, without moving to the next line, you use System.out.print("*"); // Note that it's print, not println
Part 2:
Download a new copy of the file named PrintingShapes.java from the website and rename it to PrintingShapesRec.java (and be sure to rename the class in main to match the file name).
For this part, add a new method PrintRectangleNested() that will look something like this:
public void PrintRectangleNested(int width, int height)
Invoking this method in main willl print out a rectangle of asterisks, using two for loops (and yes, they have to be nested).
The output might look like this:
| ***** ***** ***** |
Modify the code so your code so that you declare two local variables, like so:
int width = 5;
int height = 3;
Obviously, if you change the variables to, say:
int width = 3;
int height = 5;
Your code should print out:
| *** *** *** *** *** |
HINT: The two for loops will use one System.out.print("*"); and one System.out.println(""); within the loops, and an additional System.out.println(""); at the end of the method outside the loops.
Part 3:
For this part, you should create a brand-new, non Robotic class, named PrintHelper.
This class will serve the purpose of holding a bunch of commands (methods) that print things out.
The first method you'll make will be named PrintRectangle() and should look something like this:
public void PrintRectangle(int width, int height)
When called from PrintingShape's main method, you'll first have to create an instance of the PrintHelper class, then tell it to print a rectangle.
So, in order to print a 3 (wide) by 5 (tall) rectangle, your code (in main) might look like:
public static void main(String[] args) { PrintHelper helper = new PrintHelper();
helper.PrintRectangle(3, 5);
// More code might go here...
}
Your job is to write the code for PrintRectangle().
Note: I'd highly recommend cannibalizing the code that you wrote in the previous partthe new PrintRectangle method should be (almost) a straight copy-and-paste from what you've already written.
Template or PrintingShapes.java from above:
import java.util.*;
class PrintHelper
{
// Your code to print Rectangles, etc, goes here
public void PrintRectangle(int width, int height)
{
int whichRow = 0;
int whichCol = 0;
while(whichRow < height )
{
System.out.println("*");
whichRow++;
}
}
}
public class PrintingShapes extends Object
{
public static void main(String[] args)
{
// Your code goes here
PrintHelper printer = new PrintHelper();
Scanner keyboard = new Scanner(System.in);
printer.PrintRectangle(7,2);
System.out.println("Hello, how many stars would you like to print?" );
int w=keyboard.nextInt();
int h=keyboard.nextInt();
for(int i=1; i {for(int j=1; j {System.out.println("*");} System.out.println(""); } printer.PrintRectangle(w, h); } } Please modify as much as you want in main, but leave the methods untouched unless it specifically says to in the directions, thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
