460 - Overlapping Rectangles

Write here if you have problems with your Java source code

Moderator: Board moderators

460 - Overlapping Rectangles

Postby mfci » Fri Dec 09, 2011 8:45 pm

Hello!
I can't find what's wrong with my solution...
Can you help me?

Here's my code! Thanks :)
Code: Select all
package com;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RectangleOverlap extends JPanel{

      int r1x1,r1x2,r2x1,r2x2 ;
      int r1y1,r1y2,r2y1,r2y2 ;
      int r1width,r2width ;
      int r1height,r2height ;

      static JButton btn = new JButton(“Check”);

      public RectangleOverlap(int r1x1,int r1y1,int r1x2,int r1y2,int r2x1,int r2y1,int r2x2,int r2y2){

            this.r1x1=r1x1;
            this.r1x2=r1x2;
            this.r1y1=r1y1;
            this.r1y2=r1y2;
            this.r2x1=r2x1;
            this.r2x2=r2x2;
            this.r2y1=r2y1;
            this.r2y2=r2y2;

            r1width = Math.abs(r1x1-r1x2);
            r2width = Math.abs(r2x1-r2x2);
            r1height = Math.abs(r1y1-r1y2);
            r2height = Math.abs(r2y1-r2y2);

            addActionListener();

      }

      private void addActionListener() {

            btn.addActionListener(new ActionListener(){

                  public void actionPerformed(ActionEvent e) {
                        checkOverlap();
            }

            });

      }

      private void checkOverlap() {

            boolean isOVerlap= ((r1x2 >= r2x1) &&
                  (r1y2 >= r2y1) &&
                  (r1x1 <= r2x2) &&
                  (r1y1 <= r2y2));

      if(isOVerlap ){

            JOptionPane.showMessageDialog(null, “OVerlap”);

      }else{

            JOptionPane.showMessageDialog(null, “No OVerlap”);

      }

      }


      @Override

      protected void paintComponent(Graphics g) {

            g.drawRect(r1x1,r1y1 , r1width, r1height);
            g.setColor(new Color(123,232,122));
            g.drawRect(r2x1, r2y1, r2width,r2height);

      }

 

      public static void main(String args[]){

            JFrame frame = new JFrame();
            frame.setSize(500,500);   
            frame.getContentPane().add(new RectangleOverlap(20,30,120,130,10,50,160,120),BorderLayout.CENTER);
            frame.getContentPane().add(btn,BorderLayout.SOUTH);
            frame.addWindowListener(new WindowAdapter(){

                  @Override
                  public void windowClosing(WindowEvent e) {
                        System.exit(0);
                  }               

            });

            frame.setVisible(true);

      }
}
mfci
New poster
 
Posts: 1
Joined: Fri Dec 09, 2011 7:17 pm

Re: 460 - Overlapping Rectangles

Postby biahll » Fri Jun 29, 2012 12:50 am

Hey guys.

I'm getting WA with my code but it works for every input I've tested. Can anyone take a look at it?

Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

   public static void main(String[] args) throws IOException {
      int XLL1, YLL1, XUR1, YUR1;
      int XLL2, YLL2, XUR2, YUR2;
      int XLLO, YLLO, XURO, YURO;
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String parsedLine[];
      String line;
      int t = Integer.parseInt(in.readLine().trim());
      for (int i = 0; i < t; i++) {
         line = "";
         while (line.trim().equals("")) {
            line = in.readLine();
         }
         parsedLine = line.split(" ");
         XLL1 = Integer.parseInt(parsedLine[0]);
         YLL1 = Integer.parseInt(parsedLine[1]);
         XUR1 = Integer.parseInt(parsedLine[2]);
         YUR1 = Integer.parseInt(parsedLine[3]);

         parsedLine = in.readLine().split(" ");
         XLL2 = Integer.parseInt(parsedLine[0]);
         YLL2 = Integer.parseInt(parsedLine[1]);
         XUR2 = Integer.parseInt(parsedLine[2]);
         YUR2 = Integer.parseInt(parsedLine[3]);

         XLLO = YLLO = XURO = YURO = 0;
         XLLO = max(XLL1, XLL2);
         YLLO = max(YLL1, YLL2);
         XURO = min(XUR1, XUR2);
         YURO = min(YUR1, YUR2);
         if (XLLO < XURO && YLLO < YURO) {
            System.out.println(XLLO + " " + YLLO + " " + XURO + " " + YURO);
            System.out.println();
         } else {
            System.out.println("No Overlap");
            System.out.println();
         }
      }

   }

   private static int max(int a, int b) {
      if (a > b)
         return a;
      return b;
   }

   private static int min(int a, int b) {
      if (a < b)
         return a;
      return b;
   }
}
biahll
New poster
 
Posts: 3
Joined: Mon Jun 25, 2012 11:10 pm

Re: 460 - Overlapping Rectangles

Postby brianfry713 » Sat Jun 30, 2012 12:28 am

Don't print a blank line at the end of the output.
brianfry713
Guru
 
Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA


Return to Java

Who is online

Users browsing this forum: No registered users and 1 guest