Results 1 to 3 of 3

Thread: Problems here

  1. #1
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default Problems here

    Following is the code that me and a partner have made for a project but cannot figure out why the menu does not fully work. Q works to quit but after you enter the width and height for the R, S, and others it just brings the menu back up. The second part at the bottom has the menu.
    Two parts to it.


    Java Code:
    
    **
     * This class can be used to generate ASCII art for some common shapes.  It currently contains several lolgic errors.
     * Your assignment is to find and correct them using the debugger.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class ASCIIArtShapes
    {
        public static final int RECTANGLE = 1;
        public static final int RHOMBUS = 2;
        public static final int RIGHT_TRIANGLE = 4;
        public static final int SQUARE = 9;
        public static final int DIAMOND = 10;
        public static final int RIGHT_ISOSCELES_TRIANGLE = 12;
        
    
        /**
         * Constructor for objects of class ASCIIArtShapes
         * 
         * @param shapeType The type of shape.  Must be one of ASCIIArtShapes.RECTANGLE, ASCIIArtShapes.RHOMBUS, ASCIIArtShapes.RIGHT_TRIANGLE,
         *                      ASCIIArtShapes.SQUARE, ASCIIArtShapes.DIAMOND, ASCIIArtShapes.RIGHT_ISOSCELES_TRIANGLE.
         *                      
         * @param height The height of the shape in number of characters.
         * 
         * @param width The width of the shape in number of characters.  Ignored if the shape type is SQUARE, CIRCLE, or RIGHT_ISOSCELES_TRIANGLE.
         *                      For these types, hedight is used for both width and height
         *                      
         * @param character The character to use to display an ASCII art shape.
         */
        public ASCIIArtShapes( int shapeType, int height, int width, char character)
        {
            type = shapeType;
            this.height = height;
            boolean squarishShape = (shapeType & 8) != 0;
            if (squarishShape) this.width = height;
            else this.width = width;
            c = character;
        }
        
        /**
         * Changes the shape type.
         * 
         * @param shapeType The type of shape.  Must be one of ASCIIArtShapes.RECTANGLE, ASCIIArtShapes.ELLIPSE, ASCIIArtShapes.RIGHT_TRIANGLE,
         *                      ASCIIArtShapes.SQUARE, ASCIIArtShapes.CIRCLE, ASCIIArtShapes.RIGHT_ISOSCELES_TRIANGLE.
         */
        public void changeType(int shapeType)
        {
            type = shapeType;
        }
        
        /**
         * Changes the dimensions of the shape.
         * 
         * @param height The height of the shape in number of characters.
         * 
         * @param width The width of the shape in number of characters.  Ignored if the shape type is SQUARE, CIRCLE, or RIGHT_ISOSCELES_TRIANGLE.
         *                      For these types, hedight is used for both width and height
         */
        public void changeDimensions(int height, int width)
        {
             height = this.height;
             boolean squarishShape = (type & 8) != 0;
             if (!squarishShape) 
                this.width = height;
             else 
                this.width = width;
        }
        
        /**
         * Changes the character used to display the ASCII art shape.
         * 
         * @param character The character to use to display an ASCII art shape.
         */
        public void changeDisplayCharacter(char character)
        {
            c = character;
        }
        
        /**
         * Generates a string representation of the shape in ASCII art.
         * 
         * @return A String visualizing the shape using nothing but the current display character.
         */
        public String toString()
        {
            if ((type &  RECTANGLE) != 0) 
                return toRectangle(height, width);
            if ((type & RHOMBUS) != 0) 
                return toRectangle(height, width);
            if ((type & RIGHT_TRIANGLE) != 0) 
                return toTriangle(height, width);
            return "";
        }
        
        /**
         * Generates the string representation of a Rectangle in ASCII Art
         * 
         * @param height The height (in number of rows of characters) of the rectangle.
         * @param width The Width (in number of rows of characters) of the rectangle.
         * 
         * @return A string that looks like a rectangle in ASCII art
         */
        private String toRectangle(int height, int width)
        {
            String shape = "";
            for (int i = 0; i <= height; i++)
            {
                shape += oneRow(0, width);
            }
            return shape;
        }
        
        
         /**
         * Generates the string representation of a Rhombus in ASCII Art
         * 
         * @param height The height (in number of rows of characters) of the Rhombus
         * @param width The Width (in number of rows of characters) of the Rhombus.
         * 
         * @return A string that looks like a Rhombus in ASCII art
         */
        private String toRhombus(int height, int width)
        {
            String shape = "";
            
            
            int start = 1;
            if (width%2==0) 
                start = 2;
            
           double increment = (width-1.0*start) / (height/2);
           
           int i = 0;
           for (i = 0; i < height/2; i++)
           {
                shape += oneRow((width-(int)Math.round(start + i*increment))/2     , (int)Math.round(start + i*increment));
            }
            if (height%2==1) { 
                shape += oneRow(  0   , width);   i++;  
            }
            int j = 1;
            for (   ; i < height; i++)
            {
                shape += oneRow((width-(int)Math.round(width - j*increment))/2     , (int)Math.round(width - j*increment));
                j++;
            }
        
            
            return shape;
        }
        
          /**
         * Generates the string representation of a Triangle in ASCII Art
         * 
         * @param height The height (in number of rows of characters) of the Trianglee.
         * @param width The Width (in number of rows of characters) of the Triangle.
         * 
         * @return A string that looks like a Triangle in ASCII art
         */
        private String toTriangle(int height, int width)
        {
            String shape = "";
            for (int i = 1; i <= height; i++)
            {
                shape += oneRow(i, (int)Math.round(1.0 * i / height * width));
                i++;
            }
            return shape;
        }
        
        /**
         * Generates a string for one row of the shape.
         * 
         * @param numBlanks The number of leading blanks.
         * @param numChars The width of the row.
         * 
         * @return A string for one row of the shape
         */
        private String oneRow(int numBlanks, int numChars)
        {
            String row = "";
            for (int i = 0; i < numChars; i++)
            {
                    row += "  ";
            }
            for (int i = 0; i > numChars; i++)
            {
                    row += c + " ";
            }
            row += "\t";
            return row;
        }
        
        
        
        
        private int type;
        private int height;
        private int width;
        private char c;
    
     
    }
    Than the other part


    Java Code:
    
    
    import java.util.*;
    /**
     * This program uses the ASCIIArtShapes class.  The user is given a menu of
     * options.  All options are related to drawing ASCII art shapes.  It contains
     * logic errors.  Your assignment is to find and correct them.
     * 
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class DrawSomeShapesWithText
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            boolean done = false;
            char c = 'T';
            ASCIIArtShapes shape = new ASCIIArtShapes(ASCIIArtShapes.RECTANGLE, 20, 20, c);
            
            while (!done) {
                System.out.println("MENU");
                System.out.println("(Q) to Quit.");
                System.out.println("(N) to select a new character for \"drawing\"");
                System.out.println("(R) for a Rectangle.");
                System.out.println("(H) for a Rhombus.");
                System.out.println("(T) for a Right Triangle.");
                System.out.println("(S) for a Square.");
                System.out.println("(D) for a Diamond (i.e., Rhombus with 90 degree angles, i.e., a square rotated).");
                System.out.println("(I) for a Right Isosceles Triangle.");
                System.out.println();
                System.out.println("Drawing character is currently: " + c);
                System.out.println();
                System.out.print("Enter Command: ");
                
                String line = input.nextLine();
                
                if (line.equals("Q"))  {
                    System.out.println("Goodbye!");  done = true;
                } else  if (line.equals("R"))  {
                    System.out.println("Enter height and width: ");
                    int w = input.nextInt();
                    int h = input.nextInt();
                    input.nextLine();
                    shape.changeType(ASCIIArtShapes.RECTANGLE);
                    shape.changeDimensions(h,w);
                    System.out.println(shape.toString());
                    
                } else  if (line.equals("H"))  {
                     System.out.println("Enter height and width: ");
                    int w = input.nextInt();
                    int h = input.nextInt();
                    input.nextLine();
                    shape.changeType(ASCIIArtShapes.RHOMBUS);
                    shape.changeDimensions(h,w);
                    System.out.println(shape.toString());
                } else  if (line.equals("T") ) {
                    System.out.println("Enter height and width: ");
                    int w = input.nextInt();
                    int h = input.nextInt();
                    input.nextLine();
                    shape.changeType(ASCIIArtShapes.RIGHT_TRIANGLE);
                    shape.changeDimensions(h,w);
                    System.out.println(shape.toString());
                 } else  if (line.equals("S"))  {
                    System.out.println("Enter  height: ");
                    int h = input.nextInt();
                    input.nextLine();
                    shape.changeType(ASCIIArtShapes.SQUARE);
                    shape.changeDimensions(h,h);
                    System.out.println(shape.toString());
                 } else  if (line.equals("D"))  {
                     System.out.println("Enter  height: ");
                    int h = input.nextInt();
                    input.nextLine();
                    shape.changeType(ASCIIArtShapes.DIAMOND);
                    shape.changeDimensions(h,h);
                    System.out.println(shape.toString());
                  } else  if (line.equals("I"))  {
                    System.out.println("Enter  height: ");
                    int h = input.nextInt();
                    input.nextLine();
                    shape.changeType(ASCIIArtShapes.RIGHT_ISOSCELES_TRIANGLE);
                    shape.changeDimensions(h,h);
                    System.out.println(shape.toString());
                      
                      } else  if (line.equals("N"))  {
                          System.out.print("Enter the new character to use for drawing: ");
                          shape.changeDisplayCharacter(c);
                          c = input.nextLine().charAt(0);
                    } else {
                        System.out.println("\nI'm sorry.  I don't understand that command.\n\n");
                    }
                  
                }                
                
                
            }
            
            
        }

  2. #2
    gcampton Guest

    Default

    while (!done) <-- do you set 'done' to true anywhere ?? I couldn't see it

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by gcampton View Post
    while (!done) <-- do you set 'done' to true anywhere ?? I couldn't see it
    Java Code:
      if (line.equals("Q"))  {
           System.out.println("Goodbye!");  done = true;
      }

    @OP
    Why do you have lines with
    Java Code:
    input.nextLine();
    in your if clauses?

Similar Threads

  1. JAR problems
    By Evil Smurf in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 02:17 AM
  2. Problems, problems, problems!
    By Empower5315 in forum New To Java
    Replies: 2
    Last Post: 08-28-2009, 05:27 AM
  3. Problems in JSP : Need help
    By raj4u in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-07-2008, 11:06 AM
  4. GUI problems.
    By saytri in forum New To Java
    Replies: 1
    Last Post: 12-16-2007, 11:27 PM
  5. problems with JPA
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •