Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 43

Thread: Workshop 6

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Workshop 6

    [B]Write a Java class definition for class Rectangle. Each object of this class has the following variables:
    height : an integer value representing height.
    width : an integer value representing width.
    Your class should also define the following methods:
     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.
     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.
    public static void main(String[] args) {
    System.out.println("COP 2253, Workshop6. This program written by Bilal Gonen");
    System.out.println();
    Rectangle r1 = new Rectangle(-1, 11);
    r1.draw();
    Rectangle r2 = new Rectangle(4,5);
    r2.draw();
    }
    Submission
     Submit 2 files: Rectangle.java , screen shot.
     Follow submission instructions under the General Info module on e-learning.[/B]


    Already tried:
    [B]/*
    Angel Williams
    COP 2253 - Workshop 6
    Mar 20, 2013
    */
     
       public class Rectangle {
      		 private int height, width;
     
       public Rectangle(){
          height = 0;
          width = 0;
       }
     
    	public Rectangle(int hei, int wid){
          height = hei;
          width = wid;
       } 
    	public void draw(){
    		for(int i = 0; i < width; i++){
    		}
    			for (int j= 0; j <= height; j++) {
    			}	
    				if ( i 
    			System.out.print("#");
    		}
     
     
     
     
     
       public static void main(String[] args) {
             System.out.println("COP 2253, Workshop6. This program written by Angel Williams\n");
             /**Rectangle r1 = new Rectangle(-1, 11);
             r1.draw();*/
             Rectangle r2 = new Rectangle(4,5);
             r2.draw();
          }
       }[/B]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    Do you have a question about or problems with the code you posted?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    Yes, i do. Is my code that I wrote right? and how can I drew the method in rectangle?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    Is my code that I wrote right?
    What determines if the code is "right"?
    Does it compile, execute and produce the right results? Then it could be right.
    I would deduct 30% for lack of comments and documentation.

    how can I drew the method in rectangle
    Do you mean: how can I draw the rectangle in the method?

    What is the rectangle supposed to look like? Can you post an example?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    yes, that is what I mean. Sorry for confusion. The output for the rectangle look like :
    COP 2253, Workshop6. This program written by Angel Williams

    ###
    # #
    ###

    #####
    # #
    # #
    #####
    Last edited by Norm; March 20th, 2013 at 08:23 PM. Reason: tags didn't work

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    Look at the two different types of lines printed:
    The top and bottom solid row of #
    The middle ones with only # at the sides

    Use a loop for the number of rows
    and some if statements to determine which of the two types of lines to print based on the row number.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    Can you show me?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    Give it a try. Do it in steps.
    First write the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    ok

    --- Update ---

    public void draw(){
    		for(int i = 3; i < width; i++){
    		}
    		System.out.println("#");
    			for (int j= 3; j <= height; j++) {
    			}
    			System.out.println("#####");

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    I see two loops???
    In the ONE loop add an if statement
    to print "*****" on the first and last lines of the box
    and to print "*" on the lines in between
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    Like this:
                            for (int j= 3; j <= height; j++) {
    			}
    			  if ((x>0&&x<height-1)&&(y>0&&y<width-1)){
    					System.out.print("#  "); {
    					}else
    					System.out.print("#####"); {
    					}
    					System.out.println(); {
    					}

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    The code you posted will not compile or execute for testing.

    What does the code print out when executed? I expect this:

    *****
    *
    *
    *
    *****

    with a height of 5.


    If that is what is printing, the next step is to
    replace the print("****")
    with a loop to print width number of "*"
    and to add with print("*")
    a loop to print width-2 spaces followed by print("*")

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    Can you give me an example

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    An example of what?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    of replace the print("****")
    with a loop to print width number of "*"
    and to add with print("*")
    a loop to print width-2 spaces followed by print("*")

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    From post#9
    for(int i = 3; i < width; i++){
    }
    	System.out.println("#");  //  print some #s on this row
    }
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    Like this?
    public void draw(){
    		for(int i = 0; i < width; i++){
    			System.out.print("#");
    			}
    			System.out.print("#  #");
           	}

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Workshop 6

    What happens when the code is compiled and executed?
    Did you ever write the code for what I suggested in posts #6 #8 and #10?

    That would be a starting point. when that code worked you could change it as suggested in post#12
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    yes i did but can you give me a hint about the code?

  20. #20
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Workshop 6

    Are you going to attempt this yourself are you just expecting someone to spoonfeed you the code?
    Improving the world one idiot at a time!

  21. #21
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    I am attempting this myself

    --- Update ---

    public void draw(){
    		for(int i = 3; i < width; i++){
    		}
    			for(int j = 5; j < height; j++){
    			}
    				if(width == 3 || width == 3){
    				System.out.print("#");
    				}
    				else if(height ==5 || height == 4){
    				System.out.print(" ");
    				}
    Is this right?

  22. #22
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Workshop 6

    if(width == 3 || width == 3){
    Really?
    Improving the world one idiot at a time!

  23. #23
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    or if (width < 1 || width >10 ?

  24. #24
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Workshop 6

    You really should not be hard coding magic numbers. What happens if a user enters a value that does not fit? You need to make it dynamic. So the code should print a full line on the first and last line. How do you think you can determine what is the first and last line when user enters 5 and 10 or 12 and 20?
    Improving the world one idiot at a time!

  25. #25
    Member
    Join Date
    Mar 2013
    Posts
    68
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Workshop 6

    Tell me what I should do?

Page 1 of 2 12 LastLast