Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Looking for help

    I have been at this for days and I just can't wrap my finger around it. I am in a basic 100 level college class and this just seems to advanced for someone just getting into Java. I am trying to figure this out but as you can see, not going over too well. Can someone please point me in the right direction as to what I am doing wrong? This is what needs to be done:

    Write a Java class definition for class Rectangle. Each object of this class has the following variables:

    h : an integer value representing height.

    w : an integer value representing width.

    Your class should also define the following methods:

    * A constructor that accepts no parameter. This must set the h and w to 3 by default.

    * A constructor that accepts two integer parameters. First parameter is for height, second parameter is for width. If any parameter is not in the range of [1, 10] inclusive, then it must be set to 3 by default.

    * setHW method that accepts no parameter. This method asks user to enter height and width. If any parameter is not in the range of [1, 10] inclusive, then it must ask user again to enter in the range [1, 10]. This must continue until user enters a valid number, that is in [1, 10]. See sample output below.

    * setHW method that accepts two integer parameters. First parameter is for height, second parameter is for width. If any parameter is not in the range of [1, 10] inclusive, then it must be set to 3 by default.

    * draw method. This will draw the rectangle on the screen. For the edges of the rectangle, use ‘#’ character. For the inside of rectangle, use space character.

    Have the main method in the Rectangle class. Put this main method as it is. Do not make any change on this method. Below is the main method which tests the class;

    Code:
    public static void main(String[] args) {
    
    Rectangle r1 = new Rectangle();
    
    r1.draw();
    
    r1.setHW(4, 4);
    
    r1.draw();
    
    r1.setHW();
    
    r1.draw();
    
    Rectangle r2 = new Rectangle(-5, 4);
    
    r2.draw();
    
    }

    This is what I have so far:

    Code:
    public class Rectangle {
        private int H, W;
        
        Rectangle(){
            H = 3;
            W = 3;
        
        }
        
        public static void main(String[] args) {
      Rectangle r1 = new Rectangle();
      r1.draw();
      r1.setHW(4, 4);
      r1.draw();
      r1.setHW();
      r1.draw();
      Rectangle r2 = new Rectangle(-5, 4);
      r2.draw();
     }
          Rectangle(int height, int width){
              H = height;
              W = width;
        
         System.out.println("Height:");
         {
        
         public height (int i){
          if (i <1 || i>10){
              System.out.println("Height must be in [1,10]. Please enter Height again:");
             
          }
          System.out.println("Width:");
          
          public width (int l){
              if (l < 1 || l >10){
                  System.out.println("Width must be in [1,10]. Please enter Width again:"); 
              }
          public void draw(
                  int height,
                  int width);
    
        
          }
    Thanks in advance!
    Last edited by usafatcmike; 04-22-2012 at 09:09 PM.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,823
    Thanks
    42
    Thanked 199 Times in 198 Posts
    this should put you far ahead- convert to Java (in C#) and add in validation... Won't provide entire solution since homework- but you should be able to follow the logic and learn from it. If you get stuck post back Q's; good luck
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace rect
    {
        class rect
        {
            private enum DrawWhat { Space, Star }
    
            public rect()
            {
                drawHeight(3, 3);
            }
    
            public rect(int h, int w)
            {
                bool _h = verHW(h);
                bool _w = verHW(w);
    
                int height = (_h) ? h : 3;
                int width = (_w) ? w : 3;
    
                drawHeight(height, width);
    
            }
    
            private void drawHeight(int h, int w)
            {
                int width = w - 2;
    
                for (int i = 0; i < h; i++)
                {
                    Console.Write("#");
                    if ((i == 0) || (i == h - 1))
                        drawWidth(width, DrawWhat.Star);
                    else
                        drawWidth(width, DrawWhat.Space);
                    Console.WriteLine("#");
                }
            }
    
            private void drawWidth(int x, DrawWhat dw)
            {
                int i;
    
                switch (dw)
                {
                    case DrawWhat.Space:
                        for (i = 0; i < x; i++)
                        {
                            Console.Write(" ");
                        }
                        break;
                    case DrawWhat.Star:
                        for (i = 0; i < x; i++)
                        {
                            Console.Write("#");
                        }
                        break;
                    default:
                        throw new Exception();
                }
            }
    
            private bool verHW(int i)
            {
                try
                {
                    if ((i > 0) && (i < 11))
                        return true;
                    else
                        return false;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
    P.S. coding can seem overwhelming when you are starting out

    Edit:
    forgot the call...
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace rect
    {
        class Program
        {
            static void Main(string[] args)
            {
                rect r = new rect();
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    }
    PPS- I didn't add in the "asking for height/width"- I just let it default the constructor w/ no params (in the call)

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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