1. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Need homework help

Okay, instead of me trying to explain the assignment, I'm going to just post it here verbatim from my class website. Then I'll post the code I have so far.

Instructions/Design
In this assignment we will refactor our work from the last Skyline program to add randomness and additional classes.

Note: Start by making copies of your Building.java, SkylinePanel.java and Skyline.java files. Always keep a copy of working code before making changes.

Update the Skyline application you wrote in the last programming assignment to include randomness in the size and number of the buildings and windows.Name your program RandomSkyline.java.Because of the randomness we will use loops to control how the skyline looks.Once again, the Splat example from Chapter 4 is useful; as well as the paintComponent() methods from Chpater 5 Bullseye and Boxes examples. Your program needs to make the following changes to the 3 classes from the Skyline application:
1) Change Building constructor to set the number of windows to a random number. The draw method now needs a loop to draw this random number of windows.Remember to keep the windows within the dimensions of the building.

2) SkylinePanel the constructor will no longer instantiate the buildings.Its only job is to set preferred size and background color.
The building instantiation will now be done within the paintComponent() method as part of a loop. Here is the algorithm (this is VERY high-level, you still have plenty of designing to do):

1. generate random number
for each number from one to random number
2. generate random width
generate random height
3. instantiate building
4. draw building
5. generate random gap between next building
6. update distance from left side of frame

First generate a random number.Instantiate that number of buildings. Generate a random width and height. Use these random numbers as parameters to the building's constructor. Also, have the gap between buildings be a random number of pixels. This gap plus width of buildings creates the x coordinate (left edge of skyline) parameter. You can choose how you want to calculate y coordinate parameter (it is OK of buildings are drawn past the bottom of the frame).

3)Rename the class called Skyline to RandomSkyline NO other changes are needed.

Extra Credit:Create a fourth class called Star. Fill the skyline with a random number of stars. Put this into a file named Star.java. Note: you will need to draw the stars in the paintComponent() method of SkylinePanel.

I don't understand this at all (except for #s 3 and 4). I don't get how to add windows, and I especially don't get the #2. Will someone please show me how to do this. Also, I can't use arrays yet since we're not up to those in class yet. Here's my code for the original Skyline that was mentioned.

Java Code:
```  import java.awt.*;

public class Building
{
private int x, y, width, height;
private Color color;
private String string;

//-----------------------------------------------------------------
//  Constructor: Sets up this building with the specified values.
//-----------------------------------------------------------------
public Building (Color shade, int upperX, int upperY, int nWidth, int nHeight) {

x = upperX;
y = upperY;
width = nWidth;
height = nHeight;

}

//-----------------------------------------------------------------
//  Draws this building in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (color);
page.fillRect (x, y, width, height);
}

//-----------------------------------------------------------------
//  Color mutator.
//-----------------------------------------------------------------
{
}

//-----------------------------------------------------------------
//  X mutator.
//-----------------------------------------------------------------
public void setX (int upperX)
{
x = upperX;
}

//-----------------------------------------------------------------
//  Y mutator.
//-----------------------------------------------------------------
public void setY (int upperY)
{
y = upperY;
}
//-----------------------------------------------------------------
//  width mutator.
//-----------------------------------------------------------------
public void setWidth (int nWidth)
{
width = nWidth;
}

//-----------------------------------------------------------------
//  height mutator.
//-----------------------------------------------------------------
public void setheight (int nHeight)
{
height = nHeight;
}

//-----------------------------------------------------------------
//  width accessor.
//-----------------------------------------------------------------
public int getWidth ()
{
return width;
}
//-----------------------------------------------------------------
//  height accessor.
//-----------------------------------------------------------------
public int getHeight ()
{
return height;
}
//-----------------------------------------------------------------
//  Color accessor.
//-----------------------------------------------------------------
public Color getColor ()
{
return color;
}

//-----------------------------------------------------------------
//  X accessor.
//-----------------------------------------------------------------
public int getX ()
{
return x;
}

//-----------------------------------------------------------------
//  Y accessor.
//-----------------------------------------------------------------
public int getY ()
{
return y;
}

}

import javax.swing.*;
import java.awt.*;

public class SkylinePanel extends JPanel
{
private Building building1, building2, building3, building4, building5;

//-----------------------------------------------------------------
//  Constructor: Creates five Building objects.
//-----------------------------------------------------------------
public SkylinePanel()
{
building1 = new Building (Color.black, 100, 380, 40, 100);
building2 = new Building (Color.black, 170, 290, 80, 190);
building3 = new Building (Color.black, 285, 80, 100, 400);
building4 = new Building (Color.black, 450, 270, 80, 210);
building5 = new Building (Color.black, 580, 360, 45, 120);

setPreferredSize(new Dimension(640, 480));
setBackground (Color.cyan);

}

//-----------------------------------------------------------------
//  Draws this panel by requesting that each building draw itself.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);

building1.draw(page);
building2.draw(page);
building3.draw(page);
building4.draw(page);
building5.draw(page);

}

}```

2. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Need homework help

Originally Posted by psx2514
Okay, instead of me trying to explain the assignment, I'm going to just post it here verbatim from my class website. Then I'll post the code I have so far.

Instructions/Design
In this assignment we will refactor our work from the last Skyline program to add randomness and additional classes.

Note: Start by making copies of your Building.java, SkylinePanel.java and Skyline.java files. Always keep a copy of working code before making changes.

Update the Skyline application you wrote in the last programming assignment to include randomness in the size and number of the buildings and windows.Name your program RandomSkyline.java.Because of the randomness we will use loops to control how the skyline looks.Once again, the Splat example from Chapter 4 is useful; as well as the paintComponent() methods from Chpater 5 Bullseye and Boxes examples. Your program needs to make the following changes to the 3 classes from the Skyline application:
1) Change Building constructor to set the number of windows to a random number. The draw method now needs a loop to draw this random number of windows.Remember to keep the windows within the dimensions of the building.

2) SkylinePanel the constructor will no longer instantiate the buildings.Its only job is to set preferred size and background color.
The building instantiation will now be done within the paintComponent() method as part of a loop. Here is the algorithm (this is VERY high-level, you still have plenty of designing to do):

1. generate random number
for each number from one to random number
2. generate random width
generate random height
3. instantiate building
4. draw building
5. generate random gap between next building
6. update distance from left side of frame

First generate a random number.Instantiate that number of buildings. Generate a random width and height. Use these random numbers as parameters to the building's constructor. Also, have the gap between buildings be a random number of pixels. This gap plus width of buildings creates the x coordinate (left edge of skyline) parameter. You can choose how you want to calculate y coordinate parameter (it is OK of buildings are drawn past the bottom of the frame).

3)Rename the class called Skyline to RandomSkyline NO other changes are needed.

Extra Credit:Create a fourth class called Star. Fill the skyline with a random number of stars. Put this into a file named Star.java. Note: you will need to draw the stars in the paintComponent() method of SkylinePanel.

I don't understand this at all (except for #s 3 and 4). I don't get how to add windows, and I especially don't get the #2. Will someone please show me how to do this. Also, I can't use arrays yet since we're not up to those in class yet. Here's my code for the original Skyline that was mentioned.

Java Code:
```  import java.awt.*;

public class Building
{
private int x, y, width, height;
private Color color;
private String string;

//-----------------------------------------------------------------
//  Constructor: Sets up this building with the specified values.
//-----------------------------------------------------------------
public Building (Color shade, int upperX, int upperY, int nWidth, int nHeight) {

x = upperX;
y = upperY;
width = nWidth;
height = nHeight;

}

//-----------------------------------------------------------------
//  Draws this building in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (color);
page.fillRect (x, y, width, height);
}

//-----------------------------------------------------------------
//  Color mutator.
//-----------------------------------------------------------------
{
}

//-----------------------------------------------------------------
//  X mutator.
//-----------------------------------------------------------------
public void setX (int upperX)
{
x = upperX;
}

//-----------------------------------------------------------------
//  Y mutator.
//-----------------------------------------------------------------
public void setY (int upperY)
{
y = upperY;
}
//-----------------------------------------------------------------
//  width mutator.
//-----------------------------------------------------------------
public void setWidth (int nWidth)
{
width = nWidth;
}

//-----------------------------------------------------------------
//  height mutator.
//-----------------------------------------------------------------
public void setheight (int nHeight)
{
height = nHeight;
}

//-----------------------------------------------------------------
//  width accessor.
//-----------------------------------------------------------------
public int getWidth ()
{
return width;
}
//-----------------------------------------------------------------
//  height accessor.
//-----------------------------------------------------------------
public int getHeight ()
{
return height;
}
//-----------------------------------------------------------------
//  Color accessor.
//-----------------------------------------------------------------
public Color getColor ()
{
return color;
}

//-----------------------------------------------------------------
//  X accessor.
//-----------------------------------------------------------------
public int getX ()
{
return x;
}

//-----------------------------------------------------------------
//  Y accessor.
//-----------------------------------------------------------------
public int getY ()
{
return y;
}

}

import javax.swing.*;
import java.awt.*;

public class SkylinePanel extends JPanel
{
private Building building1, building2, building3, building4, building5;

//-----------------------------------------------------------------
//  Constructor: Creates five Building objects.
//-----------------------------------------------------------------
public SkylinePanel()
{
building1 = new Building (Color.black, 100, 380, 40, 100);
building2 = new Building (Color.black, 170, 290, 80, 190);
building3 = new Building (Color.black, 285, 80, 100, 400);
building4 = new Building (Color.black, 450, 270, 80, 210);
building5 = new Building (Color.black, 580, 360, 45, 120);

setPreferredSize(new Dimension(640, 480));
setBackground (Color.cyan);

}

//-----------------------------------------------------------------
//  Draws this panel by requesting that each building draw itself.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);

building1.draw(page);
building2.draw(page);
building3.draw(page);
building4.draw(page);
building5.draw(page);

}

}```
Okay I edited my code a little. Could someone please explain to me in plain English what I'm doing wrong? My buildings keep overlapping.

Java Code:
``` import java.awt.*;
import java.util.Random;

public class Building
{
private int x, y, width, height;
private Color color;
private String string;

//-----------------------------------------------------------------
//  Constructor: Sets up this building with the specified values.
//-----------------------------------------------------------------
public Building (Color shade, int upperX, int upperY) {
x = upperX;
y = upperY;
width = (int) (Math.random() * 60) + 40; //I want to generate a random number from 40 to 100.  Is this right?
height = (int) (Math.random() * 500) + 100; //I want to generate a random number between 100 to 600.  Is this right?

}

//-----------------------------------------------------------------
//  Draws this building in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (color);
page.fillRect (x, y, width, height);
}

//-----------------------------------------------------------------
//  Color mutator.
//-----------------------------------------------------------------
{
}

//-----------------------------------------------------------------
//  X mutator.
//-----------------------------------------------------------------
public void setX (int upperX)
{
x = upperX;
}

//-----------------------------------------------------------------
//  Y mutator.
//-----------------------------------------------------------------
public void setY (int upperY)
{
y = upperY;
}
//-----------------------------------------------------------------
//  width mutator.
//-----------------------------------------------------------------
public void setWidth (int nWidth)
{
width = nWidth;
}

//-----------------------------------------------------------------
//  height mutator.
//-----------------------------------------------------------------
public void setheight (int nHeight)
{
height = nHeight;
}

//-----------------------------------------------------------------
//  width accessor.
//-----------------------------------------------------------------
public int getWidth ()
{
return width;
}
//-----------------------------------------------------------------
//  height accessor.
//-----------------------------------------------------------------
public int getHeight ()
{
return height;
}
//-----------------------------------------------------------------
//  Color accessor.
//-----------------------------------------------------------------
public Color getColor ()
{
return color;
}

//-----------------------------------------------------------------
//  X accessor.
//-----------------------------------------------------------------
public int getX ()
{
return x;
}

//-----------------------------------------------------------------
//  Y accessor.
//-----------------------------------------------------------------
public int getY ()
{
return y;
}

}

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class SkylinePanel extends JPanel
{
private final int MAX_BUILDINGS = 5, MAX_WIDTH = 100, MAX_HEIGHT = 400;
private final int MAX_X = 580, MAX_Y = 360;
private Random generator;

//-----------------------------------------------------------------
//  Constructor: Creates five Building objects.
//-----------------------------------------------------------------
public SkylinePanel()
{
generator = new Random();
/*building1 = new Building (Color.black, 100, 380, 40, 100);
building2 = new Building (Color.black, 170, 290, 80, 190);
building3 = new Building (Color.black, 285, 80, 100, 400);
building4 = new Building (Color.black, 450, 270, 80, 210);
building5 = new Building (Color.black, 580, 360, 45, 120);*/

setPreferredSize(new Dimension(640, 480));
setBackground (Color.cyan);

}

//-----------------------------------------------------------------
//  Draws this panel by requesting that each building draw itself.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);

int x, y, width, height;

for (int count = 0; count < MAX_BUILDINGS; count++) {
x = generator.nextInt(MAX_X) + 10;
y = generator.nextInt(MAX_Y) + 10;
width = generator.nextInt(MAX_WIDTH) + 10;
height = generator.nextInt();
page.fillRect (x, y, width, height);

}
}

/*building1.draw(page);
building2.draw(page);
building3.draw(page);
building4.draw(page);
building5.draw(page);
page.drawString ("Ben's Skyline.", 110, 70); //Title*/

}```

3. Member
Join Date
Jan 2012
Posts
49
Rep Power
0

## Re: Need homework help

Honestly this is one of those situations where you just need to figure it out. There is no error, you just messed up; now go fix it.

4. ## Re: Need homework help

Please find and go through the Forum Rules -- particularly the third paragraph.

db

5. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Need homework help

sorrrrrrrrrrrrrryyyyyyyyyyyyyyyyy

6. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Need homework help

Originally Posted by Reeling
Honestly this is one of those situations where you just need to figure it out. There is no error, you just messed up; now go fix it.
Well, I know I made the height too high, but I that's not the main problem. I can't figure out what I did wrong. I've spent hours on this already.

#### Posting Permissions

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