Fix to grow a square based on user input

I wrote two classes. The main class runs the program. It asks for user input. Based on user input, the second class will print out a square. Each square will print out ---- for every number and | for every number for the walls.

For example, say that the user entered two. In that case, it will print out a square.

----- -----
|         | 
|         | 
----- -----

The problem is that I cant get the square the grow based on the user input.

This is the main class

import java.util.Scanner;

public class Assignment4 
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);

        SquareBuilder square = new SquareBuilder(scan.nextInt());

        System.out.print(square.toString());
        square.changeSize(scan.nextInt());
        System.out.print(square.toString());
        square.changeSize(scan.nextInt());
        System.out.print(square.toString());
        square.changeSize(scan.nextInt());
        System.out.print(square.toString());
        square.changeSize(scan.nextInt());
        System.out.print(square.toString());
    }
}

public class SquareBuilder
{
    final private int LENGTH_RATIO = 4;
    final private String CEILING_FLOOR = "----";
    private int size;
    private int spaces;
    private String square;      

    public SquareBuilder(int size) 
    {
        this.size = size;
        constructSquare();
    }

    private void spaces()
    {
         spaces = LENGTH_RATIO * size - 2;
    }

    private void ceilingAndFloor()
    {
        square += "\n";
        for (int i = 0; i < size; ++i)
        {
            square += CEILING_FLOOR;
        }
    }

    private void walls()
    {
        for (int i = 0; i < size; ++i )
        {
            square +="\n|";

            for (int j = 0; j < spaces; ++j)
            {
                square +=" ";
            }

            square +="|";
        }
    }

    private void constructSquare()
    {
        spaces ();
        square = "";

        ceilingAndFloor();
        walls();
        ceilingAndFloor();

    }

    public int area()
    {
        return size * size;
    }

    public void changeSize(int size)
    {
        this.size = size;
    }

    public String toString()
    {
        String retVal = square;
        retVal += "\nSize: " + size + " Area: " + area();

        return retVal;
    }
}

Answers


You're not even changing the part of your square that renders.

I think you want to invoke your constructSquare() method from your changeSize() method, or at least call constructSquare() before you print it again


Need Your Help

How to hide delete button of UITableView with animation in iOS?

ios uitableview hide swipe

I am working with the kind of notes app and I wanted to add the option to swipe-to-delete (like the iPhone's default notes app). I implemented the following two table view methods..

extending Android Button Class

java android eclipse button

I'm new to Java and Android. I'm trying to extend the Button Class and add more functionality to it for my game. I get an error when I am attempting to initialize the buttons (before runtime, while

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.