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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:33:02+00:00 2026-06-17T16:33:02+00:00

I have an assignment where I am working on a Spreadsheet, that I have

  • 0

I have an assignment where I am working on a Spreadsheet, that I have been handed by my instructor.

I am being asked to implement a class called Range, that accepts two positions (each consisting of two ints denoting column and row, for instance new Position(1,2)), and then finds the lowest row and column and constructs a new position which these two values, this position is the upper left corner of my range, and the same with the highest value for row and column.

Then I was asked to make a class that takes the sum of a range of positions. So I decided that for my range it should be able to have a method where all the positions where put into an ArrayList, that is the getPositions() method.

Here you can see the source code of the class:

package spreadsheet;

import java.lang.Math;
import java.util.ArrayList;

public class Range {

    private Position a;
    private Position b;
    private ArrayList<Position> positionList;

    // Creates a new range, where it makes sure that the positions,
    // appear in the right order, where the first position is the position
    // of the upper left corner, and the second position is the lower right corner.
    public Range(final Position a, final Position b) {
        int minColumn = Math.min(a.getColumn(),b.getColumn());
        int minRow = Math.min(a.getRow(),b.getRow());

        int maxColumn = Math.max(a.getColumn(),b.getColumn());
        int maxRow = Math.max(a.getRow(),b.getRow());

        this.a = new Position(minColumn, minRow);
        this.b = new Position(maxColumn, maxRow);
        positionList = new ArrayList<>();       
    }


    public ArrayList<Position> getPositions() {
        int minColumn = this.a.getColumn();
        int minRow = this.a.getRow();
        int maxColumn = this.b.getColumn();
        int maxRow = this.b.getRow();
        for(int i = minColumn; i < maxColumn; i++) {
            for(int j = minRow; j < maxRow; j++) {
                positionList.add(new Position(i, j));
            }
        }
        return positionList;
    }   
}

The problem is however that it does not really work, the list it returns is empty, so why is that? Can anyone spot the error?

  • 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-17T16:33:03+00:00Added an answer on June 17, 2026 at 4:33 pm

    I see no reason why that shouldn’t work.
    Ok, there are a few problems here:

    • positionList = new ArrayList<>(); will not compile. It must be positionList = new ArrayList<Position>();
    • As Jodaka said you would get every Position twice in the list when you get it the second time (three times when you call it the fourth and so on).
    • Your implementation would include the first column and row but not the last (try i <= maxColumn and j <= maxRow in the loops).

    For my test I used Points instead of Position and added some casts for Points use ints as constructor argument but return double. But I didn’t change the logic itself:

    package spreadsheet;
    
    import java.awt.Point;
    import java.util.ArrayList;
    
    public class Range {
    
    private final Point a;
    private final Point b;
    private final ArrayList<Point> PointList;
    
    // Creates a new range, where it makes sure that the Points,
    // appear in the right order, where the first Point is the Point
    // of the upper left corner, and the second Point is the lower right corner.
    public Range(final Point a, final Point b) {
        int minColumn = (int) Math.min(a.getX(),b.getX());
        int minRow = (int) Math.min(a.getY(),b.getY());
    
        int maxColumn =(int)  Math.max(a.getX(),b.getX());
        int maxRow = (int) Math.max(a.getY(),b.getY());
        this.a = new Point(minColumn, minRow);
        this.b = new Point(maxColumn, maxRow);
        PointList = new ArrayList<Point>();
    }
    
    public ArrayList<Point> getPoints() {
        int minColumn = (int) a.getX();
        int minRow = (int) a.getY();
        int maxColumn = (int) b.getX();
        int maxRow = (int) b.getY();
        for (int i = minColumn; i < maxColumn; i++) {
            for (int j = minRow; j < maxRow; j++) {
                PointList.add(new Point(i, j));
            }
        }
        return PointList;
    }
    

    The Test:

    package spreadsheet;
    
    import java.awt.Point;
    import java.util.ArrayList;
    
    import org.junit.Test;
    
        public class RangeTest {
    
        @Test
        public void testSomePoints() throws Exception {
            Range range = new Range(new Point(1, 1), new Point(5, 5));
            ArrayList<Point> points = range.getPoints();
            for (Point point : points) {
                System.out.println(point);
            }
        }
    }
    

    The result:

    java.awt.Point[x=1,y=1]
    java.awt.Point[x=1,y=2]
    java.awt.Point[x=1,y=3]
    java.awt.Point[x=1,y=4]
    java.awt.Point[x=2,y=1]
    java.awt.Point[x=2,y=2]
    java.awt.Point[x=2,y=3]
    java.awt.Point[x=2,y=4]
    java.awt.Point[x=3,y=1]
    java.awt.Point[x=3,y=2]
    java.awt.Point[x=3,y=3]
    java.awt.Point[x=3,y=4]
    java.awt.Point[x=4,y=1]
    java.awt.Point[x=4,y=2]
    java.awt.Point[x=4,y=3]
    java.awt.Point[x=4,y=4]
    

    edit:

    I would see every point twice too, if I would call getPoints() a second time.
    You create the list once but add points every time you call getPoints().

    There are several possibilities:

    1. You could create the ArrayList in the getPoints() method and not in the constructor. But normally you wouldn’t want to create an entirely new list each time you call this method…
    2. You could store the list in a field, calculate it in the constructor and use only a getter that returns the field.
    3. Similar to the second possibility, but you could calculate the list on demand. So don’t calculate it in the constructor but in the getter if it is null by now and use the stored list otherwise.

    2. edit

    1. option:

      public class Range {
      
          private final Point a;
          private final Point b;
          private final ArrayList<Point> PointList;
      
          public Range(final Point a, final Point b) {
              int minColumn = (int) Math.min(a.getX(),b.getX());
              int minRow = (int) Math.min(a.getY(),b.getY());
      
              int maxColumn =(int)  Math.max(a.getX(),b.getX());
              int maxRow = (int) Math.max(a.getY(),b.getY());
              this.a = new Point(minColumn, minRow);
              this.b = new Point(maxColumn, maxRow);
          }
      
          public ArrayList<Point> getPoints() {
              PointList = new ArrayList<Point>();
              int minColumn = (int) a.getX();
              int minRow = (int) a.getY();
              int maxColumn = (int) b.getX();
              int maxRow = (int) b.getY();
              for (int i = minColumn; i < maxColumn; i++) {
                  for (int j = minRow; j < maxRow; j++) {
                      PointList.add(new Point(i, j));
                  }
              }
              return PointList;
          }
      }
      
    2. option:

      public class Range {
      
          private final Point a;
          private final Point b;
          private final ArrayList<Point> PointList;
      
          public Range(final Point a, final Point b) {
              int minColumn = (int) Math.min(a.getX(),b.getX());
              int minRow = (int) Math.min(a.getY(),b.getY());
      
              int maxColumn =(int)  Math.max(a.getX(),b.getX());
              int maxRow = (int) Math.max(a.getY(),b.getY());
              this.a = new Point(minColumn, minRow);
              this.b = new Point(maxColumn, maxRow);
              PointList = calcPoints();
          }
      
          private ArrayList<Point> calcPoints() {
              ArrayList<Point> list = new ArrayList<Point>();
              int minColumn = (int) a.getX();
              int minRow = (int) a.getY();
              int maxColumn = (int) b.getX();
              int maxRow = (int) b.getY();
              for (int i = minColumn; i < maxColumn; i++) {
                  for (int j = minRow; j < maxRow; j++) {
                      PointList.add(new Point(i, j));
                  }
              }
              return list;
          }
      
          public ArrayList<Point> getPoints() {
              return PointList;
          }
      }
      
    3. option:

      public class Range {
      
          private final Point a;
          private final Point b;
          private final ArrayList<Point> PointList;
      
          public Range(final Point a, final Point b) {
              int minColumn = (int) Math.min(a.getX(),b.getX());
              int minRow = (int) Math.min(a.getY(),b.getY());
      
              int maxColumn =(int)  Math.max(a.getX(),b.getX());
              int maxRow = (int) Math.max(a.getY(),b.getY());
              this.a = new Point(minColumn, minRow);
              this.b = new Point(maxColumn, maxRow);
          }
      
          private ArrayList<Point> calcPoints() {
              ArrayList<Point> list = new ArrayList<Point>();
              int minColumn = (int) a.getX();
              int minRow = (int) a.getY();
              int maxColumn = (int) b.getX();
              int maxRow = (int) b.getY();
              for (int i = minColumn; i < maxColumn; i++) {
                  for (int j = minRow; j < maxRow; j++) {
                      list.add(new Point(i, j));
                  }
              }
              return list;
          }
      
          public ArrayList<Point> getPoints() {
              if(PointList == null) {
                  PointList = calcPoints();
              }
              return PointList;
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on my assignment for Java and I have created a class called Triangle.
I have this school assignment that I've been working on and am totally stumped
I am currently in an introductory c++ class and am working assignment that sorts
Good day, Stack Overflow. I have a homework assignment that I'm working on this
I am working on an assignment where I have to implement a recursive way
Alright so I have an assignment that I have been completely stumped on. My
I am working on a shopping cart assignment for that I have created a
I have an assignment that I am working on, and I can't get a
I have been working on a trivial assignment to get used to coding. I
I have an assignment I've been working on for a few hours now, and

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.