Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7500425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:10:50+00:00 2026-05-29T20:10:50+00:00

I have to write a program that takes a command-line argument n and prints

  • 0

I have to write a program that takes a command-line argument n and prints out a pattern with alternating spaces and asterisks (as demonstrated below). Use at least two nested for loops and a constructor to achieve the pattern (an image is shown below of how it’s suppose to look).

This is the code I have tried already and no luck. I understand how to do this with a single for loop, but not a nested. I am also unsure how to integrate a constructor with this program.

This is how the image should look: * * * *
                                    * * * *
                                   * * * *
                                    * * * *
public class Box {
     public static void main(String[] args) {

         for (int i=1; i<2; i++) {
             System.out.println("* " + "* " + "* " + "* ");

             for (int j=0; j<i; j++) {
                 System.out.print(" *" + " *" + " *" + " *");

             }
         }
     }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:10:51+00:00Added an answer on May 29, 2026 at 8:10 pm

    Slight modification to Bohemian’s answer. The outer for-loop is responsible for printing the rows. The inner loop prints the repeated characters on each row. The constructor simply sets the n field, which controls how many rows you print out. The main method creates a new object and invokes its only method.

    public class Box {
    
    private static int n; 
    
    public Box(int n){
        this.n = n;
    }
    
    public static void doMagic() {
        for (int row = 0; row < n; row++) {
            if(row%2==1)
                System.out.print(" ");
            for (int col = 0; col < n; col++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
       public static void main(String[] args) {
        new Box(4).doMagic();
     } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: Write a program Deal.java that takes an command-line argument N and prints N
I have been assigned wit the task to write a program that takes a
Newbie C++ programmer here. I'm trying to write a command line application that takes
I have a program that takes two files as an argument. The first file
I have to write a program that read from a file that contains the
I have to write a program that sniffs network packets (part1-the simple part). And
I wanted to write a program that test if two files are duplicates (have
I have a Perl program, that needs to use packages (that I also write).
I have inherited a 20-year-old interactive command-line unix application that is no longer supported
I have a command line executable that alters some bits in a file that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.