# drawing a filled rectangle/circle in a 2d array

• (2 Pages)
• 1
• 2

## 21 Replies - 11587 Views - Last Post: 17 October 2011 - 05:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251602&amp;s=5494c670d7022b424aa31aa0d84dfee9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

# drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 07:04 PM

Hey, I'm having problems writing methods rectangle and circle that draws a filled rectangle and circle respectively.
This program is supposed to create a class Picture which includes methods Picture(int rmax, int cmax, char background) which draws the board, print() which prints the receiving picture, rectangle(int rlo, int rhi, int clo, int chi, char color) which draws a filled rectangle in the 2d array where rlo, rhi, clo, chi define the corners of the rectangle and color is the character to put in teach pixel of the rectangle, method circle(double rc, double cc, double radius, char color) draws a filled circle in a 2d array where (rc,cc) is the center radius is the radius and color is the character to put in each pixel of the circle.
in the circle method we are supposed to use a an algorithm that considers every pixel (r,c) in the array and if the distance from (r,c) to (rc,cc) is less than or equal to radius then give it the specified color. after all that we write a driver which tests the class.
i know you won't do my homework for me, im just a bit confused on what needs to be done

the code i have so far
I'm not quite sure i did the circle method correctly
i tried compiling what i had and it gives me
Main.java:25: cannot find symbol
symbol : variable rmax
location: class Picture
for (int row = 0; row < rmax; row++)
^
Main.java:27: cannot find symbol
symbol : variable cmax
location: class Picture
for (int col = 0; col < cmax; col++)
^
Main.java:39: cannot find symbol
symbol : variable rmax
location: class Picture
for(int row = 0; row < rmax; row++)
^
Main.java:41: cannot find symbol
symbol : variable cmax
location: class Picture
for(int col = 0; col < cmax; col++)
^
Main.java:53: cannot find symbol
symbol : constructor Picture(char[][])
location: class Picture
return new Picture(result);
^
Main.java:58: cannot find symbol
symbol : variable row
location: class Picture
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:58: unexpected type
required: class
found : value
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:58: cannot find symbol
symbol : variable row
location: class Picture
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:58: cannot find symbol
symbol : variable col
location: class Picture
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:58: unexpected type
required: class
found : value
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:58: cannot find symbol
symbol : variable col
location: class Picture
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:58: sqrt(double) in java.lang.Math cannot be applied to (java.lang.String)
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:61: cannot find symbol
symbol : variable rmax
location: class Picture
for(int row = 0; row < rmax; row++)
^
Main.java:63: cannot find symbol
symbol : variable cmax
location: class Picture
for(int col =0; col < cmax; col++)
^
Main.java:71: cannot find symbol
symbol : constructor Picture(char[][])
location: class Picture
return new Picture(circle);
^

```// Written by
//
//
//
import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////
class Picture
{
private char[][]a;
//--------------------------------------------------------------------------
public Picture(int rmax, int cmax, char background)
{
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
a[row][col] = background;
}
}
}
//--------------------------------------------------------------------------
public Picture print() // seperate method to print the other classes
{
for (int row = 0; row < rmax; row++)
{
for (int col = 0; col < cmax; col++)
{
System.out.print(a[row][col] + " ");
}
System.out.println();
}
}
//--------------------------------------------------------------------------
public Picture Rectangle(int rlo, int rhi, int clo, int chi, char color)
{
char[][] result = new char[a.length][a.length];

for(int row = 0; row < rmax; row++)
{
for(int col = 0; col < cmax; col++)
{
if(row >= rlo && row <= rhi)
{
result[row][col] = color;
}
if(col >= clo && row <= chi)
{
result[row][col] = color;
}
}
}
return new Picture(result);
}
//--------------------------------------------------------------------------
public Picture Circle (double rc, double cc, double radius, char color)
{
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
char[][] circle = new char[a.length][a.length];

for(int row = 0; row < rmax; row++)
{
for(int col =0; col < cmax; col++)
{
{
circle[row][col] = color;
}
}
}
return new Picture(circle);
}
//--------------------------------------------------------------------------
} // end class Picture
////////////////////////////////////////////////////////////////////////////
// driver that tests class, ignore for now lol
class Hw08
{
//--------------------------------------------------------------------------
public static void main ( String [] args ) throws Exception
{
Picture p = new Picture (50,50," ");
p.print();
p.rectangle(10,20,10,20,'x');
p.print();
p.circle(30,30,10,"o");
}
//--------------------------------------------------------------------------
} // end class Hw08
////////////////////////////////////////////////////////////////////////////

```

This post has been edited by BlahBlahMan: 16 October 2011 - 07:10 PM

Is This A Good Question/Topic? 0

## Replies To: drawing a filled rectangle/circle in a 2d array

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12306
• Posts: 45,403
• Joined: 27-December 08

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 07:12 PM

Remember that when you have methods with parameters, those parameters are only accessible inside that method. So here public Picture(int rmax, int cmax, char background), rmax and cmax are parameters. You cannot use them outside of this constructor. However, in a two-dimensional array a.length returns the number of rows, and a[someIndex].length returns the number of elements (columns) for the array at someIndex.

Quote

Main.java:53: cannot find symbol
symbol : constructor Picture(char[][])
location: class Picture
return new Picture(result);
^

You didn't define a constructor to accept a char[][].

Here, you have to explicitly multiply the expressions in the parentheses together using the * operator.
```double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));

```

Those should account for a lot of your errors.

### #3 mastrgamr

Reputation: 2
• Posts: 18
• Joined: 30-December 10

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 07:24 PM

Your code is all wrong, you dont have a Picture constructor that assigns all the arrays items to whatever you pass to it.

You declare methods of type Picture but don't return picture.

In your driver you pass a string, when you declared a char. So you need to change that.

### #4 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 07:44 PM

im a bit confused on how to do the constructor for 2d arrays

wouuld i do something like this?
```   public Picture(char rmax, char cmax, char background)
{
a = new char [rmax][cmax]
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
a[row][col] = background;
}
}
}
```

revised code below without ^

```// Written by
//
// description of this program
//
import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////
class Picture
{
private char[][]a;
private int i;
//--------------------------------------------------------------------------
public Picture(int rmax, int cmax, char background)
{
a = new char [rmax][cmax]
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
a[row][col] = background;
}
}
}
//--------------------------------------------------------------------------
public Picture print() // seperate method to print the other classes
{
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[i].length; col++)
{
System.out.print(a[row][col] + " ");
}
System.out.println();
}
}
//--------------------------------------------------------------------------
public Picture Rectangle(int rlo, int rhi, int clo, int chi, char color)
{
char[][] result = new char[a.length][a.length];

for(int row = 0; row < a.length; row++)
{
for(int col = 0; col < a[i].length; col++)
{
if(row >= rlo && row <= rhi)
{
result[row][col] = color;
}
if(col >= clo && row <= chi)
{
result[row][col] = color;
}
}
}
return Picture(result);
}
//--------------------------------------------------------------------------
public Picture Circle (double rc, double cc, double radius, char color)
{
char[][] circle = new char[a.length][a.length];

for(int row = 0; row < a.length; row++)
{
for(int col = 0; col < a[i].length; col++)
{
double d = Math.sqrt((rc-row)*(rc-row)+(cc-col)*(cc-col));
{
circle[row][col] = color;
}
}
}
return Picture(circle);
}
//--------------------------------------------------------------------------
} // end class Picture
////////////////////////////////////////////////////////////////////////////
// driver that tests class, ignore for now lol
class Hw08
{
//--------------------------------------------------------------------------
public static void main ( char [][] args ) throws Exception
{
Picture p = new Picture (50,50," ");
p.print();
p.Rectangle(10,20,10,20,'x');
p.print();
p.Circle(30,30,10,"o");
}
//--------------------------------------------------------------------------
} // end class Hw08
////////////////////////////////////////////////////////////////////////////

```

This post has been edited by BlahBlahMan: 16 October 2011 - 07:55 PM

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12306
• Posts: 45,403
• Joined: 27-December 08

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 07:57 PM

Here, notice how the param is a char[][]. Then I assign the param to the instance field.
```public Picture(char[][] a){
this.a = a;
}

```

### #6 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 08:02 PM

macosxnerd101, on 16 October 2011 - 07:57 PM, said:

Here, notice how the param is a char[][]. Then I assign the param to the instance field.
```public Picture(char[][] a){
this.a = a;
}

```

so a constructor that would work would be something like this?
```   public Picture(char[][] a)
{
this.rmax = rmax;
this.cmax = cmax;
this.background = background;
this.a = new char [rmax][cmax];
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
a[row][col] = background;
}
}
}
```

This post has been edited by BlahBlahMan: 16 October 2011 - 08:10 PM

### #7 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 09:53 PM

revised code, still confused
getting these errors
Main.java:123: cannot find symbol
symbol : method Picture(int,int,java.lang.String)
location: class Hw08
Picture p = Picture(50,50," ");
^
Main.java:125: rectangle(int,int,int,int,char) in Picture cannot be applied to (int,int,int,int,java.lang.String)
p.rectangle(10,20,10,20,"x");
^
Main.java:127: circle(double,double,double,char) in Picture cannot be applied to (int,int,int,java.lang.String)
p.circle(30,30,10,"o");
^
3 errors

```// Written by
//
// description of this program
//
import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////
class Picture
{
private char[][]a;
private int i;
private int rmax;
private int cmax;
private char background;
private double rc;
private double cc;
private char color;
//--------------------------------------------------------------------------
public Picture(int rmax, int cmax, char background)
{
this.rmax = rmax;
this.cmax = cmax;
this.background = background;
this.a = new char [rmax][cmax];
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
a[row][col] = background;
}
}
}
//--------------------------------------------------------------------------
public Picture(char[][] a)
{
this.rmax = rmax;
this.cmax = cmax;
this.background = background;
this.a = new char [rmax][cmax];
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
a[row][col] = background;
}
}
}
//--------------------------------------------------------------------------
public Picture(double rc, double cc, double radius, char color)
{
this.rc = rc;
this.cc = cc;
this.color = color;
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
a[row][col] = color;
}
}
}
//--------------------------------------------------------------------------
public void print() // seperate method to print the other classes
{
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[i].length; col++)
{
System.out.print(a[row][col] + " ");
}
System.out.println();
}
}
//--------------------------------------------------------------------------
public Picture rectangle(int rlo, int rhi, int clo, int chi, char color)
{
char[][] result = new char[a.length][a.length];

for(int row = 0; row < a.length; row++)
{
for(int col = 0; col < a[i].length; col++)
{
if(row >= rlo && row <= rhi)
{
result[row][col] = color;
}
if(col >= clo && row <= chi)
{
result[row][col] = color;
}
}
}
return new Picture(result);
}
//--------------------------------------------------------------------------
public Picture circle (double rc, double cc, double radius, char color)
{
char[][] circle = new char[a.length][a.length];

for(int row = 0; row < a.length; row++)
{
for(int col = 0; col < a[i].length; col++)
{
double d = Math.sqrt((rc-row)*(rc-row)+(cc-col)*(cc-col));
{
circle[row][col] = color;
}
}
}
return new Picture(circle);
}
//--------------------------------------------------------------------------
} // end class Picture
////////////////////////////////////////////////////////////////////////////
class Hw08
{
//--------------------------------------------------------------------------
public static void main ( char [][] args ) throws Exception
{
Picture p = Picture(50,50," ");
p.print();
p.rectangle(10,20,10,20,"x");
p.print();
p.circle(30,30,10,"o");
}
//--------------------------------------------------------------------------
} // end class Hw08
////////////////////////////////////////////////////////////////////////////

```

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12306
• Posts: 45,403
• Joined: 27-December 08

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 09:55 PM

For your post about the constructor, this a[row][col] = background; deals with the param, not the instance variable this.a.

For your second set of errors, chars are surrounded by single quotes, and Strings by double quotes.
```char x = 'x';
String y = "y";

```

### #9 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 10:10 PM

macosxnerd101, on 16 October 2011 - 09:55 PM, said:

For your post about the constructor, this a[row][col] = background; deals with the param, not the instance variable this.a.

For your second set of errors, chars are surrounded by single quotes, and Strings by double quotes.
```char x = 'x';
String y = "y";

```

so i fixed the second set of errors, but I'm still very confused about the constructor what do yo mean by a[row][col] = background; deals with the param and not the instance variable? can you provide an example?
thanks for your help so far!

This post has been edited by BlahBlahMan: 16 October 2011 - 10:12 PM

### #10 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12306
• Posts: 45,403
• Joined: 27-December 08

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 10:13 PM

Quote

public Picture(char[][] a)

This variable a is a parameter. It is local to the method. Since you also have an instance variable named a, the parameter takes precedence in the method. So when you refer to a[row][col] = background;, you are modifying the parameter- a local variable. When the constructor call is completed, the instance variable will not be modified. If you assign this.a[row][col] = background;, the data will be saved in the instance variable.

### #11 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 10:32 PM

macosxnerd101, on 16 October 2011 - 10:13 PM, said:

Quote

public Picture(char[][] a)

This variable a is a parameter. It is local to the method. Since you also have an instance variable named a, the parameter takes precedence in the method. So when you refer to a[row][col] = background;, you are modifying the parameter- a local variable. When the constructor call is completed, the instance variable will not be modified. If you assign this.a[row][col] = background;, the data will be saved in the instance variable.

I kind of get what you are saying, but how come the error only pops up in the first constructor?

### #12 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 11:03 PM

revised code, still getting error
Main.java:123: cannot find symbol
symbol : method Picture(int,int,char)
location: class Hw08
Picture p = Picture(50,50,' ');
^
1 error

```// Written by Steve Nham
//
// description of this program
//
import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////
class Picture
{
private char[][]a;
private int i;
private int rmax;
private int cmax;
private char background;
private double rc;
private double cc;
private char color;
//--------------------------------------------------------------------------
public Picture(int rmax, int cmax, char background)
{
this.rmax = rmax;
this.cmax = cmax;
this.background = background;
this.a = a;
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
this.a[row][col] = background;
}
}
}
//--------------------------------------------------------------------------
public Picture(char[][] a)
{
this.rmax = rmax;
this.cmax = cmax;
this.background = background;
this.a = a;
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
this.a[row][col] = background;
}
}
}
//--------------------------------------------------------------------------
public Picture(double rc, double cc, double radius, char color)
{
this.rc = rc;
this.cc = cc;
this.color = color;
for(int row = 0; row < rmax; row ++)
{
for(int col = 0; col < cmax; col++)
{
this.a[row][col] = color;
}
}
}
//--------------------------------------------------------------------------
public void print()
{
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[i].length; col++)
{
System.out.print(a[row][col] + " ");
}
System.out.println();
}
}
//--------------------------------------------------------------------------
public Picture rectangle(int rlo, int rhi, int clo, int chi, char color)
{
char[][] result = new char[a.length][a.length];

for(int row = 0; row < a.length; row++)
{
for(int col = 0; col < a[i].length; col++)
{
if(row >= rlo && row <= rhi)
{
result[row][col] = color;
}
if(col >= clo && row <= chi)
{
result[row][col] = color;
}
}
}
return new Picture(result);
}
//--------------------------------------------------------------------------
public Picture circle (double rc, double cc, double radius, char color)
{
char[][] circle = new char[a.length][a.length];

for(int row = 0; row < a.length; row++)
{
for(int col = 0; col < a[i].length; col++)
{
double d = Math.sqrt((rc-row)*(rc-row)+(cc-col)*(cc-col));
{
circle[row][col] = color;
}
}
}
return new Picture(circle);
}
//--------------------------------------------------------------------------
} // end class Picture
////////////////////////////////////////////////////////////////////////////
class Hw08
{
//--------------------------------------------------------------------------
public static void main ( char [][] args ) throws Exception
{
Picture p = Picture(50,50,' ');
p.print();
p.rectangle(10,20,10,20,'x');
p.print();
p.circle(30,30,10,'o');
}
//--------------------------------------------------------------------------
} // end class Hw08
////////////////////////////////////////////////////////////////////////////

```

### #13 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 11:23 PM

Now i get a
```Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
at java.security.AccessController.doPrivileged(Native Method)
Could not find the main class: Main. Program will exit.
```

```// Written by Steve Nham
//
// description of this program
//
import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////
class Picture
{
private char[][]a;
private int i;
private int rmax;
private int cmax;
private char background;
private double rc;
private double cc;
private char color;
//--------------------------------------------------------------------------
public Picture(int rmax, int cmax, char background)
{
this.rmax = rmax;
this.cmax = cmax;
this.background = background;
this.a = a;
for(int row = 0; row < rmax; row++)
{
for(int col = 0; col < cmax; col++)
{
this.a[row][col] = background;
}
}
}
//--------------------------------------------------------------------------
public Picture(char[][] a)
{
this.rmax = rmax;
this.cmax = cmax;
this.background = background;
this.a = a;
for(int row = 0; row < rmax; row++)
{
for(int col = 0; col < cmax; col++)
{
this.a[row][col] = background;
}
}
}
//--------------------------------------------------------------------------
public Picture(double rc, double cc, double radius, char color)
{
this.rc = rc;
this.cc = cc;
this.color = color;
for(int row = 0; row < rmax; row++)
{
for(int col = 0; col < cmax; col++)
{
this.a[row][col] = color;
}
}
}
//--------------------------------------------------------------------------
public void print()
{
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[i].length; col++)
{
System.out.print(a[row][col] + " ");
}
System.out.println();
}
}
//--------------------------------------------------------------------------
public Picture rectangle(int rlo, int rhi, int clo, int chi, char color)
{
char[][] result = new char[a.length][a.length];

for(int row = 0; row < a.length; row++)
{
for(int col = 0; col < a[i].length; col++)
{
if(row >= rlo && row <= rhi)
{
result[row][col] = color;
}
if(col >= clo && row <= chi)
{
result[row][col] = color;
}
}
}
return new Picture(result);
}
//--------------------------------------------------------------------------
public Picture circle (double rc, double cc, double radius, char color)
{
char[][] circle = new char[a.length][a.length];

for(int row = 0; row < a.length; row++)
{
for(int col = 0; col < a[i].length; col++)
{
double d = Math.sqrt((rc-row)*(rc-row)+(cc-col)*(cc-col));
{
circle[row][col] = color;
}
}
}
return new Picture(circle);
}
//--------------------------------------------------------------------------
} // end class Picture
////////////////////////////////////////////////////////////////////////////
class Hw08
{
//--------------------------------------------------------------------------
public static void main ( char [][] args ) throws Exception
{
Picture p = new Picture(50,50,' ');
p.print();
p.rectangle(10,20,10,20,'x');
p.print();
p.circle(30,30,10,'o');
}
//--------------------------------------------------------------------------
} // end class Hw08
////////////////////////////////////////////////////////////////////////////

```

### #14 smohd

• Critical Section

Reputation: 1824
• Posts: 4,627
• Joined: 14-March 10

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 11:30 PM

The class with main() should be public and the file should be saved by its name. So class Hw08, is the one to be public and a file name will be Hw08.java

### #15 BlahBlahMan

Reputation: 1
• Posts: 57
• Joined: 02-June 11

## Re: drawing a filled rectangle/circle in a 2d array

Posted 16 October 2011 - 11:33 PM

smohd, on 16 October 2011 - 11:30 PM, said:

The class with main() should be public and the file should be saved by its name. So class Hw08, is the one to be public and a file name will be Hw08.java

but isnt this public already? and the file name is Hw08.java

```////////////////////////////////////////////////////////////////////////////
class Hw08
{
//--------------------------------------------------------------------------
public static void main ( char [][] args ) throws Exception
{
Picture p = new Picture(50,50,' ');
p.print();
p.rectangle(10,20,10,20,'x');
p.print();
p.circle(30,30,10,'o');
}
//--------------------------------------------------------------------------
} // end class Hw08
////////////////////////////////////////////////////////////////////////////

```