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 8010513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:45:26+00:00 2026-06-04T18:45:26+00:00

I’m learning to code in Java. I have this assessment that I need to

  • 0

I’m learning to code in Java. I have this assessment that I need to do: I have to create a java app that will do the following:

  • Allow the user to specify the number of input rectangles (between 3 and 30).

  • Generate the requested number of input rectangles (with random widths and heights) and write them to a file (human readable).

  • Read the randomly generated input rectangles from the file generated in the step above.

  • Display the input rectangles graphically, correctly laid out NEXT to each other

  • Calculate the output rectangles.

  • Display the output rectangles graphically, correctly laid out aside each other. Note: Both the input and output rectangles must be displayed at the same time.

  • Write the output rectangle coordinates into an output file (human readable).”

There rectangles are simple boxes (not fancy coloured etc). I have been struggling with this code, managed to get the Input + error handling done and it displays the rectangles but not as the desired.

Bugs: I need it to accept the amount of rectangles from the user and display them NEXT to each other.

Please find my code below… Thanks in advance.
LB


import java.awt.Graphics;
import javax.swing.JFrame;
import java.util.Random;
import java.util.Scanner;


public class final_rect extends JFrame
{

    public final_rect()
    {
        setTitle("tutorial");
        setSize(700,500);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g)
    {
              int x = 50;
              int y = 50;
              Random rnd = new Random();

        for (int i=1; i <=5; i++)
        {
            int width = rnd.nextInt(200);
            int height = rnd.nextInt(200);
        System.out.println("Rectangle["+ i +"]" + ":(W="+width+ ";H=" +height +")");

        g.drawRect( x , y , width, height);

// Update the coordinates for the next rectangle.
                  x =  x + width;
            }
    }

    public static void main(String[] args)

    {

Scanner sc = new Scanner(System.in);

int number;
do {
    System.out.println("Please enter a number between 3 and 30!");
    while (!sc.hasNextInt()) {
        System.out.println("That's not a number! Please enter a number between 3 and 30!!!");
        sc.next();
    }
    number = sc.nextInt();
} 
while (number <3 || number > 30);

System.out.println("Thank you! Got " + number);

test1 t = new test1();
        t.paint(null);

    }
}
  • 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-06-04T18:45:28+00:00Added an answer on June 4, 2026 at 6:45 pm

    Simple way:

    you could make a start position x and y (like 0,0), and keep adding the width to x and/or height to y, and use them on the next retangle to be drawn, instead of fixing the start position (that will make them inside each other).

    Oh, just to make sure you know: drawRect(int startX, int startY, int width, int height)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to create an if statement in PHP that prevents a single post

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.