Methods

Building a java code\ Writing methods to generate different types

Page 1 of 1

4 Replies - 584 Views - Last Post: 28 November 2009 - 06:51 AM Rate Topic: -----

#1 BabyKodou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-November 09

Methods

Posted 26 November 2009 - 07:01 AM

This is the whole java work to be done ;


Your code should indent properly.
-- Add a comment section before each method. Briefly document what this
method do. What is the meaning of the argument.

Write a Java program to:
1. Write 4 methods. Each takes one integer argument, that is the size of the
pattern. Each of these 4 methods will print a different pattern on screen,
square, box, grid and cross.

How the patterns look like at size 5:
(1) square: a solid 5x5 square
*****
*****
*****
*****
*****

(2) box: a empty 5x5 box
*****
* *
* *
* *
*****

(3) grid:
*****
* * *
*****
* * *
*****

(4) cross: a cross with 5 units width and height
* *
* *
*
* *
* *

2. Ask user to select from a list of patterns:
(1) square, (2) box, (3) cross
3. Ask user to enter the size.
4. Print the specified pattern on screen.
5. Ask user if she/he wants to print another, repeat step 2-4
until user says no.

For example, this is what your program will print for a box of
size 3:
***
* *
***

Another example, this is what your program will print for a cross of
size 8:
* *
* *
* *
**
**
* *
* *
* *


Another example, this is what your program will print for a grid of
size 6:
******
* * *
******
* * *
******
* * *


How to approach this problem:
1. Work on one method at a time. Test each method by calling it from
main method with a hard-coded size for parameter.
2. After all methods are working, add code to ask user to select
pattern and enter size. Call different method based on user's
selection.
3. Once 2. is working correctly, put entire thing inside a loop
to repeat until user quit.
I want to know which type of method to use
Thank You

This post has been edited by BabyKodou: 26 November 2009 - 10:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Methods

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10785
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: Methods

Posted 26 November 2009 - 09:18 AM

You'll probably want to use void-type methods because you'll just want to print your pattent w/o returning values.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Methods

Posted 26 November 2009 - 08:17 PM

We won't do your homework and

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#4 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Methods

Posted 26 November 2009 - 10:21 PM

I would also use a loop in your main to loop the lines that are identical...
Was This Post Helpful? 0
  • +
  • -

#5 BabyKodou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-November 09

Re: Methods

Posted 28 November 2009 - 06:51 AM

View Postpbl, on 26 Nov, 2009 - 07:17 PM, said:

We won't do your homework and

[rules][/rules]



This what I've done so far



import java.util.*;
import java.lang.*;
public class grade {
public static void main(String[]args){
Scanner m=new Scanner(System.in);
String square="square";
String box="box";
String grid="grid";
int x;

System.out.println("What do you want a square, a box,a cross or a grid?");
String jj=m.nextLine();
if(jj==square);
System.out.println("Enter size of square==> The number you type in will be both the row and column.");
x=m.nextInt();
for(int i=0;i<x;i++){
for(int k=0; k<x; k++)
System.out.print("*");
System.out.println();
}




}
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1