41.5k views
1 vote
I am confused in Java!

This is the programming project that i cannot code:

Write a class called Building that represents a graphical depiction of a building. Allow the parameters to the constructor to specify the building's width and height. Each building should be colored black and should contain a few random windows of yellow. Create a program that draws a random skyline of buildings.

User Gzbwb
by
7.9k points

1 Answer

2 votes
import java.util.Random;

class Building
{
private int w, h;
private int numberOfWins;

public Building(int width, int height)
{
w = width;
h = height;

// generating random windows. can be between 1 and 10
Random rand = new Random();
numberOfWins = rand.nextInt(9) + 1;
}

public int getWidth() { return w; }
public int getHeight() { return h; }
public int numberOfWindows() { return numberOfWins; }

public String toString()
{
return "a black building, width: " + width + ", height: " + height +
", has " + numberOfWins + " windows";
}
}

----------------------------------------------------

then you can create some Building objects and output them!
User Demitrian
by
8.1k points