Question

Write a Java program which implements both the while and for loop, as needed, to draw...

Write a Java program which implements both the while and for loop, as needed, to draw the illustrations given below.

Shape 1

123456789

12345678

1234567

123456

12345

1234

123

12

1

Shape 2

*************
*************
*************
*************
*************

Shape 3

============

* *

* *

* *

* *

* *

============

Hint:
To draw the shapes above, you can use nested for loops to accomplish the task. The outer for loop iterates on each row, and the inner for loop iterates on each column of current row.

Shape 1 has 9 rows and column is decreasing on each row.

          for (int row=0; row <rowSize; row++)  //iterate for each row
          {

for (int col=1; col<=rowSize-row; col++) //for each column on current row

{

              //display col number with a space
            }
            // display a new line
          }
       Shape 2 has 5 rows, 13 columns.
          for (int row=0; row <rowSize; row++)  //iterate for each row
         {

for (int col=0; col<colSize; col++) //for each column on current row

{

//display *

}

            // display a new line
         }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Shapes
{
public static void main(String[] args)
{
System.out.println("Shape 1 Task");
int rowSize=9,row,col;
for(row=0;row<rowSize;row++)
{
for(col=1;col<=rowSize-row;col++)
{
System.out.print(col);
}
System.out.println();
}
System.out.println("Shape 2 Task");
int colSize=13;
rowSize=5;
for (row=0; row <rowSize; row++) //iterate for each row
{
for (col=0; col<colSize; col++) //for each column on current row
{
System.out.print("*");
}
System.out.println();
}
System.out.println("Shape 3 Task");
rowSize=7;
colSize=12;
for (row=0; row <rowSize; row++) //iterate for each row
{
for (col=0; col<colSize; col++) //for each column on current row
{
if(row==0 || row==6)
System.out.print("=");
else if(col==0 || col==10)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}

Output

koushikhp@koushikhpnew: koushikhp@koushikhpnew: javac Shapes.java koushikhp@koushikhpnew:-$ Java Shapes Shape 1 Task 12345678

Add a comment
Know the answer?
Add Answer to:
Write a Java program which implements both the while and for loop, as needed, to draw...
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
  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • Instructions Write a program in Java that implements the A* algorithm to find a path from...

    Instructions Write a program in Java that implements the A* algorithm to find a path from any two given nodes. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan method for calculating the heuristic. Remember: your heuristic function...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Java use for loop (nested for loop) if/else possible on blue J. (row *col)%2 for 0's and 1's.

    Java use for loop (nested for loop) if/else possible on blue J. (row *col)%2 for 0's and 1's. Write a program to generate the below table: . The table should be able to vary in size based on a class constant called SIZE; minimum of 1, maximum of 9. . The O's and 1's can and should be generated by evaluating a mathematical expression using the row and column numbers Think about the operators: +-* / % . The output...

  • Can someone finish this class for me? There are comment instructions with what to do in...

    Can someone finish this class for me? There are comment instructions with what to do in the methods already. public class ArrayDemo   {                               public void demonstrateTask1(){                                System.out.println("Demonstrate   Task   1");                                                               int[]   numbers =   null;   //   note   that   numbers   references   nothing   (null)   initially                           ...

  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • In this assignment, you will write a program in C++ which uses files and nested loops...

    In this assignment, you will write a program in C++ which uses files and nested loops to create a file from the quiz grades entered by the user, then reads the grades from the file and calculates each student’s average grade and the average quiz grade for the class. Each student takes 6 quizzes (unknown number of students). Use a nested loop to write each student’s quiz grades to a file. Then read the data from the file in order...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

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
ADVERTISEMENT