DrinkJavaCodeJava
DrinkJavaCodeJava

Reputation: 808

Shape won't scale

Whenever I try to scale the shape, nothing happens and then the console stops responding to input. Here is the set scale command:

case 7: do
                         {
                            System.out.println("\nEnter scale factor");
                            scale=input.nextDouble();
                         }
                         while(scale<0);
                         Shape.setScaleFactor(scale);
                         break;

Here is the scale shape command

 case 4: for(i=0; i<s.length; i++)
                         {
                            if(s[i]!=null)
                            {
                              s[i].scaleShape();
                            }
                         }
                         break;

Here is the main method variables:

int i=0;
        int m=0;
        double scale;
        boolean exit=false;
        Shape[] s = new Shape[10];

Here are all the relevant methods in the shape class;

private static double scaleFactor;
public static double getScaleFactor()
    {

        return scaleFactor;
    }
    //Set ScaleFactor
    public static void setScaleFactor(double x)
    {

        scaleFactor=x;
    }

Here is the scaleshape method in the rectangle subclass

private double base;
    private double height;   
@Override public void scaleShape()
    {
        base=base*getScaleFactor(); height=height*getScaleFactor();

    }

        public abstract void scaleShape();

Here is the set scale method in the circle class:

private final double pi = Math.PI;
    private double radius;
 @Override public void scaleShape()
    {
         radius*=getScaleFactor();
    }

Upvotes: 1

Views: 95

Answers (1)

Brendan Long
Brendan Long

Reputation: 54312

My guess is that your problem is here:

do
{
    System.out.println("\nEnter scale factor");
    scale = input.nextDouble();
}
while(scale < 0);

What happens if you add some logging?

do
{
    System.out.println("\nEnter scale factor");
    scale = input.nextDouble();
    System.out.println("Scale entered was: " + scale);
    System.out.println("Is scale < 0? " + (scale < 0));
}
while(scale < 0);

If you don't get what you're expecting, it might be worthwhile to read in a String instead just to see what input you're actually getting. You can always read a String, log it, then use Double.valueOf(String) to convert it.

Another possibility is that it's working, but you never reset scale and this is in some sort of loop, so it just continues scaling the shape forever but never stops to ask for input again.

Upvotes: 1

Related Questions