Printing a rectangle of asterisks in Java - java

The following is the source code to print a rectangle entirely composed of asterisks (*), with a test class included as well.
import javax.swing.JOptionPane ;
/**
* A class to print block rectangles.
*/
class RectanglePrinter
{
// instance var's
int height ; // height of rectangle (i.e. number of segments)
int width ; // width of each segment(i.e. number of "*"s printed)
/**
* Create a RectanglePrinter object.
* #param height height of rectangle (i.e., number of lines to print)
* #param width width of rectangle (i.e., number of '*'s per line
*/
public RectanglePrinter(int height, int width) // constructor
{
this.height = height ;
this.width = width ;
}
/**
* Prints one line of a rectangle, by printing exactly "width" asterisks
*/
public void printSegment()
{
// write the body of this method here
}
/**
* Prints a rectangle exactly "height" lines in height. Each line is
* printed via a call to method printSegment
*/
public void printRectangle()
{
System.out.println("Printing a " + height + " x " + width + " rectangle:") ;
// write the body of this method here
}
} // end of class rectanglePrinter definition
public class RectanglePrinterTest
{
public static void main (String [] args)
{
String input = JOptionPane.showInputDialog
("What is the height of the rectangle?") ;
int height = Integer.parseInt(input) ;
input = JOptionPane.showInputDialog
("What is the width of the rectangle?") ;
int width = Integer.parseInt(input) ;
RectanglePrinter r = new RectanglePrinter(height, width) ;
System.out.println() ;
r.printRectangle() ;
System.out.println() ;
}
}
In the segments where it says to fill out the method body, I was instructed to use for loops to print out the asterisks. I think I have a basic idea of how to do the printSegment() method:
for (int w = 1; w <= width; w++)
{
System.out.println("*");
}
But from there, I am unsure of what to do within the printRectangle method. Judging from the comments in the code, I think I'm supposed to write a for loop in the printRectangle method that calls the printSegment method, except I don't think you can call a void method within the body of another method. Any help on this would be greatly appreciated.
Update:
I attempted to use the following code within the body of printRectangle()
for (int h = 1; h <= height; h++)
{
printSegment();
}
After running the code and inputting the height and width, I received the following output:
Printing a 6 x 7 rectangle:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
Now I can print out the asterisks at least, so I just need to know how to modify the code so the output is a rectangular block of asterisks.
Update #2.
I figured out the solution. Here's the code that got me the result I was looking for.
import javax.swing.JOptionPane ;
/**
* A class to print block rectangles.
*/
class RectanglePrinter
{
// instance var's
int height ; // height of rectangle (i.e. number of segments)
int width ; // width of each segment(i.e. number of "*"s printed)
/**
* Create a RectanglePrinter object.
* #param height height of rectangle (i.e., number of lines to print)
* #param width width of rectangle (i.e., number of '*'s per line
*/
public RectanglePrinter(int height, int width) // constructor
{
this.height = height ;
this.width = width ;
}
/**
* Prints one line of a rectangle, by printing exactly "width" asterisks
*/
public void printSegment()
{
// write the body of this method here
for (int w = 1; w <= width; w++)
{
for (int h = 1; h <= height; h++)
{
System.out.print("*");
}
System.out.println();
}
}
/**
* Prints a rectangle exactly "height" lines in height. Each line is
* printed via a call to method printSegment
*/
public void printRectangle()
{
System.out.println("Printing a " + height + " x " + width + " rectangle:") ;
// write the body of this method here
printSegment();
}
} // end of class rectanglePrinter definition
public class RectanglePrinterTest
{
public static void main (String [] args)
{
String input = JOptionPane.showInputDialog
("What is the height of the rectangle?") ;
int height = Integer.parseInt(input) ;
input = JOptionPane.showInputDialog
("What is the width of the rectangle?") ;
int width = Integer.parseInt(input) ;
RectanglePrinter r = new RectanglePrinter(height, width) ;
System.out.println() ;
r.printRectangle() ;
System.out.println() ;
}
}
The differences made: I added a nested for loop in the body of the printSegment() method, and instead of using
System.out.println("*"); , used System.out.print("*") in the innermost loop, and System.out.println() in the outer loop, which resulted in the following output for the input of 5 x 7:
Printing a 5 x 7 rectangle:
*****
*****
*****
*****
*****
*****
*****

It does indeed sound like you are supposed to make a loop in printRectangle where you call printSegment for height amount times.
To call your printSegment method inside printRectangle simply write printSegment(); to call it.
Since its in the same class this is all you have to do to call it. If it were called from another class you can check how RectanglePrinterTest main method calls printRectangle using r.printRectangle(); where r is an instance of the RectanglePrinter class.
As its a void method it wont return anything so you don't need to do anything else. :)
So to sum it up you are on the right track! Keep going and just try what you think feels right and you will surely figure it out.

You need to use the "\n" string to print a new line. It should print the "\n" every time the width or row command is completed.

Using pieces of your code I can now successfully print a rectangle, horizontally.
The Helsinki CS MOOC (http://mooc.cs.helsinki.fi/) presented this task in its learn Java course.
public class MoreStars {
public static void main(String[] args) {
printRectangle(10, 6);
}
public static void printRectangle(int width, int height) {
int i;
int j;
for ( i = 0; i < height; i++ ) {
for ( j = 0; j < width; j++) {
printStars(width);
}
System.out.println("");
}
}
public static void printStars(int amount) {
System.out.print(" * ");
}
}

Related

The type of the expression must be an array type but it is resolved to ArrayList<Point2D.Double>

I am trying to figure out the proper way to call arrays from the area method, which are then supposed to calculate the area of the points given. Not sure what the proper way to select the specific x and y coordinates from each array is.
MyPolygon class
import java.util.ArrayList;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
/**
* A class that represents a geometric polygon. Methods are provided for adding
* a point to the polygon and for calculating the perimeter and area of the
* polygon.
*/
class MyPolygon {
// list of the points of the polygon
private ArrayList<Point2D.Double> points;
/**
* Constructs a polygon with no points in it.
*/
public MyPolygon() {
points = new ArrayList<Point2D.Double>();
}
/**
* Adds a point to the end of the list of points in the polygon.
*
* #param x
* The x coordinate of the point.
* #param y
* The y coordinate of the point.
*/
public void add(double x, double y) {
points.add(new Point2D.Double(x, y));
}
/**
* Calculates and returns the perimeter of the polygon.
*
* #return 0.0 if < 2 points in polygon, otherwise returns the sum of the
* lengths of the line segments.
*/
public double perimeter() {
if (points.size() < 2) {
return 0.0;
}
int i = 0;
double d = 0;
double total = points.get(0).distance(points.get(points.size() - 1));
while (i < points.size() - 1) {
Point2D.Double point1 = points.get(i);
// double x = point1.x;
// double y = point1.y;
Point2D.Double point2 = points.get(i + 1);
// double x1 = point2.x;
// double y1 = point2.y;
d = point1.distance(point2);
// d = Math.sqrt(Math.pow(x1 - x,2) + Math.pow(y1 - y, 2));
total = total + d;
i++;
}
return total;
}
/**
* Calculates and returns the area of the polygon.
*
* #return 0.0 if < 3 points in the polygon, otherwise returns the area of
* the polygon.
*/
public double area() {
int i = 0;
double a = 0;
double total = 0;
total = total + a;
if (points.size() < 3) {
return 0.0;
}
for (int m = 0; m < points.size(); m++) {
total = total + (points[m].x() * points[m + 1].y()) - (points[m].y() * points[m + 1].x());
}
return 0.5 * total;
}
}
Tester Class
class PolygonTester {
public static void main(String args[]) {
MyPolygon poly = new MyPolygon();
poly.add(1.0,1.0);
poly.add(3.0,1.0);
poly.add(1.0,3.0);
System.out.println(poly.perimeter());
System.out.println(poly.area());
}
}
Your headline is actually already the solution. You use points[m] which is array notation. But points ist not an array. It is a list. Use points.get(int i) instead, as you did in perimeter().
You're going to run out of bounds on the list. Your for loop continues while m < size(). However you access m+1 in your calculation. So if the list contains 5 elements and m = 4, (4 < 5) so keep looping, you will then access m + 1 which is 5. You don't have an index of 5 since these lists are 0 based.
Also the code is probably not compiling because you're using array syntax to access a list. You should say points.get(m)
The solution is quite simple, and is given away by your title (which I assume to be a compiler error.)
You are treating points as an array, which it is not. You access elements of an ArrayList slightly differently: you use points.get(m) instead of points[m]. If you make that change in area, it will work.
ArrayLists are not arrays. They are objects that are indexed with the get(int) method.
Wherever you have points[m], or something similar, replace it with points.get(m). The line would then become:
total = total + (points.get(m).x() * points.get(m + 1).y()) - (points.get(m).y() * points.get(m + 1).x());
That should clear up that issue, but you will still probably get an IndexOutOfBoundsException on the last iteration of the loop, because you will be trying to index m + 1 when m is the last index. You should change your code depending on how you want it to handle the last element.

Tic Tac Toe Java Program - CPU not responding

I've been working on this program for a couple weeks now, adding components to an applet in order to output a simple tic tac toe game. I've gotten some of it to work, but as of now, when you run, all it does is that you click it once, the CPU projects its mark in the upper left corner, and then you can click anywhere else and it automatically says you win. I can't get the CPU to keep playing. I don't expect anyone to tell me what to do, but I'm just confused as to which method I need to work on in order to get the CPU to respond. My professor has left some very helpful pseudocode, but I still don't quite understand. I've been working with the method "gameEnd," checking for winners horizontally, vertically and diagonally to see if that's the source to getting the game to continue beyond just two marks, but it's not working. Anyone got any suggestions? Thanks.
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;
public class TicTacToe extends java.applet.Applet {
// Ignore this constant
private static final long serialVersionUID = 1942709821640345256L;
// You can change this boolean constant to control debugging log output
private static final boolean DEBUGGING = false;
// Constants
// Size of one side of the board in pixels
private static final int BOARD_SIZE_PIXELS = 600;
// Number of squares on one side of the board
private static final int SQUARES = 3;
// Diameter of the circle drawn in each square
private static final int CIRCLE_WIDTH = 90;
// Colors to be used in the game
private static final Color BACKGROUND_COLOR = Color.WHITE;
private static final Color SQUARE_BORDER_COLOR = Color.BLACK;
private static final Color GAME_OVER_MESSAGE_COLOR = Color.BLACK;
private static final Color HUMAN_COLOR = Color.RED;
private static final Color HUMAN_WINNING_COLOR = Color.MAGENTA;
private static final Color CPU_COLOR = Color.BLUE;
private static final Color CPU_WINNING_COLOR = Color.CYAN;
// Status constant values for the game board
private static final int EMPTY = 0;
private static final int HUMAN = 1;
private static final int HUMAN_WINNING = 2;
private static final int CPU = -1;
private static final int CPU_WINNING = -2;
// String displayed when the game ends
private static final String GAME_WIN_MESSAGE = "You win! Click to play again.";
private static final String GAME_LOSE_MESSAGE = "You lose......Click to play again.";
private static final String GAME_DRAW_MESSAGE = "No one wins? Click to play again...";
// Instance variables that control the game
// Whether or not the user just clicked the mouse
private boolean mouseClicked = false;
// Whether or not to start the game again
private boolean restart = false;
// Whether or not the CPU should start playing a move.
// USED ONLY WHEN THE CPU PLAYS FIRST!
private boolean onFirstMove = false;
// The column and row of the SQUARE the user clicked on
private int xMouseSquare; // column
private int yMouseSquare; // row
// The width (and height) of a single game square
private int squareWidth = BOARD_SIZE_PIXELS / SQUARES;
// An array to hold square status values on the board.
// The status values can be EMPTY, HUMAN, CPU or other values
private int[][] gameBoard;
// The column and row of the SQUARE the CPU player will move on
private int xCPUMove;
private int yCPUMove;
// Add the rest of your instance variables here, if you need any. (You won't
// need to, but you may if you want to.)
// Ignore these instance variables
// CPUinMove represents if the CPU is thinking (generating the CPU move).
// If it is true, it means the CPUMove() method is running and no new move
// should be added
private boolean CPUinMove;
// Methods that you need to write:
/*
* Pre: x and y are x-coordinate and y-coordinate where the user just
* clicks. squareWidth is the width (and height) of a single game square.
*
* Post: xMouseSquare and yMouseSquare are set to be the column and row
* where the user just clicked on (depending on x and y).
*
* Hint: You need only two statements in this method.
*/
// Setting MouseSquare equal to x and y divided by the Square Width to create a location for
// the user after clicking
private void setMouseSquare(int x, int y) {
//
xMouseSquare = x/squareWidth;
yMouseSquare = y/squareWidth;
}
/*
* Pre: SQUARES is an int that holds the number of game squares along one
* side of the game board. xSquare is an int such that 0 <= xSquare <
* SQUARES. CIRCLE_WIDTH is an int that holds the diameter of the circle to
* be drawn in the center of a square. squareWidth is an int that holds the
* width and height in pixels of a single game square.
*
* Post: Return the correct x-coordinate (in pixels) of the left side of the
* circle centered in a square in the column xSquare.
*
* Hint: This method should be very simple. What you need to do is to find
* the right equation.
*/
private int getIconDisplayXLocation(int xSquare) {
// This line is an example of using DEBUGGING variable
if (DEBUGGING) {
System.out.println("The input that getIconDisplayXLocation() receives is: " + xSquare);
}
// equation that returns the correct variable in the column xSquare
return squareWidth * xSquare + (squareWidth - CIRCLE_WIDTH)/2;
}
/*
* Pre: SQUARES is an int that holds the number of game squares along one
* side of the game board. ySquare is an int such that 0 <= ySquare <
* SQUARES. CIRCLE_WIDTH is an int that holds the diameter of the circle to
* be drawn in the center of a square. squareWidth is an int that holds the
* width and height in pixels of a single game square.
*
* Post: Return the correct y-coordinate (in pixels) of the top of the
* circle centered in a square in the row ySquare.
*
* Hint: This method should be very simple. What you need to do is to find
* the right equation.
*/
private int getIconDisplayYLocation(int ySquare) {
// This line is an example of using DEBUGGING variable
if (DEBUGGING) {
System.out.println("The input that getIconDisplayYLocation() receives is: " + ySquare);
}
// equation that returns the correct variable in the column ySquare
return squareWidth * ySquare + (squareWidth - CIRCLE_WIDTH)/2;
}
/*
* The instance variable gameBoard will be created and initialized
*
* Pre: SQUARES is set to an int. gameBoard is a 2-dimensional array type
* variable that holds the status of current game board. Each value in the
* array represents a square on the board
*
* Post: gameBoard must be assigned a new 2-dimensional array. Every square
* of gameBoard should be initialized to EMPTY.
*
* Hint: A loop.
*/
private void buildBoard() {
// Setting the two methods equal to local variables, x and y
int x = xMouseSquare;
int y = yMouseSquare;
// This line creates the gameBoard array. You must write several more
// lines to initialize all its values to EMPTY
// Write game board using the equation of three across and down for each column
// Constructs a 3 by 3 array of integers for the board
gameBoard = new int[3][3];
// Initialize variables i and j, set equal to 0; this establishes a connection for loop
for (x = 0; x < 3; x++) {
for (y = 0; y < 3; y++) {
gameBoard[x][y] = EMPTY;
}
}
}
/*
* Returns whether the most recently clicked square is a legal choice in the
* game.
*
* Pre: gameBoard is a 2-dimensional array type variable that holds the
* status of current game board. xSquare and ySquare represent the column
* and row of the most recently clicked square.
*
* Post: Returns true if and only if the square is a legal choice. If the
* square is empty on current game board, it is legal and the method shall
* return true; if it is taken by either human or CPU or it is not a square
* on current board, it is illegal and the method shall return false.
*
* Hint: Should be simple but think carefully to cover all cases.
*/
private boolean legalSquare(int xSquare, int ySquare) {
if (gameBoard[xSquare][ySquare] == EMPTY)
return true;
else return false;
}
/*
* Pre: gameBoard is an array that holds the current status of the game
* board. xSquare and ySquare represent the column and row of the most
* recently clicked square. player represent the current player (HUMAN or
* CPU). This method is always called after checking legalSquare().
*
* Post: Set the square as taken by current player on the game board if the
* square is empty.
*
* Hint: Very simple.
*/
private void setMovePosition(int xSquare, int ySquare, int player) {
player = HUMAN;
player = CPU;
this.xMouseSquare = xSquare;
this.yMouseSquare = ySquare;
}
/*
* Check if HUMAN or CPU wins the game.
*
* Pre: gameBoard is an array to hold square status values on the board. The
* status values can be EMPTY, HUMAN, CPU or other values.
*
* Post: The method will return true if and only if a player wins the game.
* Winning a game means there is at least one row, one column, or one
* diagonal that is taken by the same player. The method does not need to
* indicate who wins because if it is implemented correctly, the winner must
* be the one who just made a move.
*
* Hint: Complicated method. Use loops to shorten your code. Think about how
* to represent "a player takes 3 squares in a row/column/diagonal".
*/
private boolean gameEnd() {
// Setting local variables
int i = 0;
int x = xMouseSquare;
int y = yMouseSquare;
int sw = squareWidth;
int[][] g = gameBoard;
// Checking for a winner
// Checking columns
for (y = 0; y < 3; y++) {
for (x = 0; x < 3; x++) {
i = g[x][y] + i;
}
}
{
// Checking rows
for (x = 0; x < 3; x++) {
for (y = 0; y < 3; y++) {
i = g[x][y] + i;
}
}
}
// Checking first diagonal
{
for (x = 0; x < 3; x++) {
g[x][x] = 3;
}
}
for (x = 1; x < 3; x++) {
g[x][x] = 3;
}
for (x = 2; x < 3; x++) {
g[x][x] = 3;
}
// Checking second diagonal
for (y = 0; y < 3; y++) {
g[y][2 - y] = 3;
}
for (y = 1; y < 3; y++) {
g[y][2 - y] = 3;
}
for (y = 2; y < 3; y++) {
g[y][2 - y] = 3;
}
return true;
}
/*
* Check if the game ends as a draw.
*
* Pre: gameBoard is an array to hold square status values on the board. The
* status values can be EMPTY, HUMAN, CPU or other values. This method is
* always called after gameEnd().
*
* Post: The method will return true if and only if all squares on the game
* board are taken by HUMAN or CPU players (no EMPTY squares left).
*
* Hint: Should be simple. Use loops.
*/
// Value of...
private boolean gameDraw() {
if (squareWidth == (3^2 - 1)) {
}
return true;
}
/*
* Marks circles on the line on which a player wins.
*
* Pre: g is Graphics object that is ready to draw on. HUMAN_WINNING_COLOR
* and CPU_WINNING_COLOR are both Color objects that represent the color to
* show when HUMAN/CPU wins. gameBoard is gameBoard is an array to hold
* square status values on the board. The status values can be EMPTY, HUMAN,
* CPU or other values.
*
* Post: ALL the row(s)/column(s)/diagonal(s) on which a player wins will be
* marked as the special color.
*
* Hint: You must draw a new circle with the special color (to replace the
* old circle) on the square if the square belongs to a winning
* row/column/diagonal. You can change gameBoard because the board will be
* reset after displaying the winning line. You can use helper methods
* (existing ones or your own) to finish this method. Pay attention that
* many functions in this method is similar to gameEnd(). You should think
* about reusing code.
*
* Hint2: This method is not necessary for the game logic. You don't have to
* start it early. Start when you know everything else is correct.
*/
private void markWinningLine(Graphics g) {
// TODO
}
/*
* Generates the next square where the CPU plays a move.
*
* Pre: gameBoard is an array to hold square status values on
* the board. The status values can be EMPTY, HUMAN, CPU or other values.
*
* Post: Set xCPUMove and yCPUMove to represent the column and the row which
* CPU plans to play a move on. The respective square MUST BE EMPTY! It will
* cause a logical error if this method returns a square that is already
* taken!
*
* Hint: Don't start too early -- currently this method works (though
* naively). Make sure that everything else works before touching this
* method!
*/
private void CPUMove() {
// TODO
// The following block gives a naive solution -- it finds the first
// empty square and play a move on it. You can use this method to test
// other methods in the beginning. However, you must replace this block
// with your own algorithms eventually.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (gameBoard[i][j] == 0) {
xCPUMove = i;
yCPUMove = j;
return;
}
}
}
}
/* Put any helper methods you wish to add here. */
/* You will not need to change anything below this line. */
/*
* DO NOT change this method.
*
* Set the game board to show a new, blank game.
*/
private void wipeBoard(Graphics g) {
g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, BOARD_SIZE_PIXELS, BOARD_SIZE_PIXELS);
}
/*
* DO NOT change this method.
*
* Displays a circle on g, of the given color, in the center of the given
* square.
*/
private void displayHit(Graphics g, Color color, int xSquare, int ySquare) {
g.setColor(color);
g.fillOval(getIconDisplayXLocation(xSquare),
getIconDisplayYLocation(ySquare), CIRCLE_WIDTH, CIRCLE_WIDTH);
}
/*
* DO NOT change this method.
*
* This method handles mouse clicks. You will not need to call it.
*/
#Override
public boolean mouseDown(Event e, int xMouse, int yMouse) {
if (isClickable()) {
mouseClicked = true;
setMouseSquare(xMouse, yMouse);
}
repaint();
return true;
}
/*
* DO NOT change this method.
*
* This method handles drawing the board. You will not need to call it.
*/
#Override
public void update(Graphics g) {
paint(g);
}
/*
* DO NOT change this method.
*
* Draws the border between game squares onto canvas. Also, draws the moves
* that are already made.
*/
private void displayGame(Graphics canvas) {
canvas.setColor(SQUARE_BORDER_COLOR);
for (int i = 0; i < BOARD_SIZE_PIXELS; i += squareWidth) {
for (int j = 0; j < BOARD_SIZE_PIXELS; j += squareWidth) {
canvas.drawRect(i, j, squareWidth, squareWidth);
}
}
for (int i = 0; i < SQUARES; i++) {
for (int j = 0; j < SQUARES; j++) {
switch (gameBoard[i][j]) {
case HUMAN:
case HUMAN_WINNING:
displayHit(canvas, HUMAN_COLOR, i, j);
break;
case CPU:
case CPU_WINNING:
displayHit(canvas, CPU_COLOR, i, j);
break;
default:
break;
}
}
}
}
/*
* DO NOT change this method.
*
* This method relays information about the availability of mouse clicking
* in the game. You will not need to call it.
*/
private boolean isClickable() {
return !CPUinMove;
}
/*
* DO NOT change the contents this method.
*
* If this method is changed to public void paint(Graphics canvas), it will
* execute the program with the CPU-first order.
*
* This method is like the "main" method (but for applets). You will not
* need to call it. It contains most of the game logic.
*/
// #Override
public void paint(Graphics canvas) {
displayGame(canvas);
if (mouseClicked) {
if (onFirstMove) {
CPUMove();
setMovePosition(xCPUMove, yCPUMove, CPU);
displayHit(canvas, CPU_COLOR, xCPUMove, yCPUMove);
onFirstMove = false;
} else {
if (restart) {
wipeBoard(canvas);
setUpGame();
repaint();
} else if (legalSquare(xMouseSquare, yMouseSquare)) {
setMovePosition(xMouseSquare, yMouseSquare, HUMAN);
displayHit(canvas, HUMAN_COLOR, xMouseSquare, yMouseSquare);
if (gameEnd()) {
markWinningLine(canvas);
canvas.setFont(new Font("SansSerif", Font.PLAIN, 30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_WIN_MESSAGE, squareWidth / 2,
squareWidth);
restart = true;
} else {
CPUinMove = true;
CPUMove();
setMovePosition(xCPUMove, yCPUMove, CPU);
displayHit(canvas, CPU_COLOR, xCPUMove, yCPUMove);
CPUinMove = false;
if (gameEnd()) {
markWinningLine(canvas);
canvas
.setFont(new Font("SansSerif", Font.PLAIN,
30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_LOSE_MESSAGE,
squareWidth / 2, squareWidth);
restart = true;
} else if (gameDraw()) {
canvas
.setFont(new Font("SansSerif", Font.PLAIN,
30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_DRAW_MESSAGE,
squareWidth / 2, squareWidth);
restart = true;
}
}
}
}
mouseClicked = false;
}
}
/*
* DO NOT change this method.
*
* This method is like the "main" method (but for applets). You will not
* need to call it. It contains most of the game logic.
*/
public void paint_game(Graphics canvas) {
// display the current game board
displayGame(canvas);
// the following block will run every time the user clicks the mouse
if (mouseClicked) { // when the user clicks the mouse
// if the game is ready to start or to be restarted
if (restart) {
// clear the window and set up the game again
wipeBoard(canvas);
setUpGame();
repaint();
}
// else, if the game is in play, check if the click is on a legal
// square
else if (legalSquare(xMouseSquare, yMouseSquare)) {
// if the square is legal, mark the corresponding position as
// taken by HUMAN
setMovePosition(xMouseSquare, yMouseSquare, HUMAN);
// display the new position with a HUMAN_COLOR circle
displayHit(canvas, HUMAN_COLOR, xMouseSquare, yMouseSquare);
// check if the game ends (if it is, HUMAN wins)
if (gameEnd()) {
// if HUMAN wins, mark the winning line as a special color.
markWinningLine(canvas);
// display the human winning message
canvas.setFont(new Font("SansSerif", Font.PLAIN, 30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_WIN_MESSAGE, squareWidth / 2,
squareWidth);
// mark the game as ready to restart
restart = true;
} else if (gameDraw()) {
// if HUMAN doesn't win but the board is full, it is a draw
// display the draw message
canvas.setFont(new Font("SansSerif", Font.PLAIN, 30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_DRAW_MESSAGE, squareWidth / 2,
squareWidth);
// mark the game as ready to restart
restart = true;
} else {
// if HUMAN doesn't win and the board is not full, the CPU
// is ready to move
CPUinMove = true;
// calculates the next CPU move
CPUMove();
// mark the corresponding position as taken by CPU
setMovePosition(xCPUMove, yCPUMove, CPU);
// display the new position with a CPU_COLOR circle
displayHit(canvas, CPU_COLOR, xCPUMove, yCPUMove);
CPUinMove = false;
if (gameEnd()) {
// if CPU wins, mark the winning line as a special
// color.
markWinningLine(canvas);
// display the human losing message
canvas.setFont(new Font("SansSerif", Font.PLAIN, 30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_LOSE_MESSAGE, squareWidth / 2,
squareWidth);
// mark the game as ready to restart
restart = true;
}
// else (if the game is not ended after the CPU move), the
// game is ready to get the next HUMAN move
}
}
mouseClicked = false;
}
}
/*
* DO NOT change this method.
*
* This method initializes the applet. You will not need to call it.
*/
#Override
public void init() {
setSize(BOARD_SIZE_PIXELS, BOARD_SIZE_PIXELS);
setBackground(BACKGROUND_COLOR);
setUpGame();
}
/*
* DO NOT change this method.
*
* Creates a fresh game board and sets up the game state to get ready for a
* new game.
*/
private void setUpGame() {
buildBoard();
CPUinMove = false;
restart = false;
onFirstMove = true;
}
}

Animating Graphical Sort

I am working on an assignment called "Graphical Sort", which is basically animating sort algorithm graphically.
I just need help on animating the sorting process.
I tried using Thread, but the program hangs till the threading process is completed, then it shows the final result.
Below are the picture of how my program looks like:
Below is the class of the panel I use to paint on
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class PaintPanel extends JPanel
{
// Create an array of 34 element size
int[] Arr = new int [34];
// Set default X pointer to 20
int x = 50;
// Declare Y pointer to 660
int y = 660;
// Set the length of array to n variable
int n = Arr.length;
/*
* main method
* #param none
* #return none
*/
public PaintPanel ()
{
randomNums ();
}
/*
* Generates random numbers between 50 and 750 and stores it into the Arr variable
* #param none
* #return none
*/
public void randomNums ()
{
// call randomGenerator object
Random randomGenerator = new Random ();
// Loop 33 times = Generates 33 random integers
for (int i = 0 ; i <= 33 ; ++i)
{
// Generate random Number
int randomInt = randomGenerator.nextInt (700);
// Conditional statement, if any number is less than 50, then discard it and generate new number
if (randomInt > 50)
// Assign each random number into Arr Element
Arr [i] = randomInt;
else
{
// Regenerate Random Number
randomInt = randomGenerator.nextInt (700);
// Assign it again
Arr [i] = randomInt;
}
}
}
/*
* Bubble Sort Algorithm
* #param none
* #return none
*/
public void bubble ()
{ //Pre: a is an array with values. It is of size n
//Post: the values in a are put in ascending order
int temp;
int a[] = Arr;
for (int i = 0 ; i < n - 1 ; i++)
{
for (int j = 0 ; j < n - 1 - i ; j++)
{ // compare the two neighbours
if (a [j + 1] < a [j])
{ //swap the neighbours if necessary
temp = a [j];
a [j] = a [j + 1];
a [j + 1] = temp;
}
}
}
}
/*
* Paints 33 rectangle Strips to the screen
* #param Graphics g
* #return none
*/
public void paintComponent (Graphics g)
{
super.paintComponent (g);
// Call Graphics2D Object
Graphics2D g2 = (Graphics2D) g.create ();
// Create Paint Object with gradient Fill
Paint p = new GradientPaint (
0, 0, new Color (0x44A2FF),
getWidth (), 0, new Color (0x0CBEAE),
true
);
// Set the gradient fill to the Graphics2D Object
g2.setPaint (p);
// Loop through the Array and display series of Rectangular Strips
for (int i = 0 ; i < Arr.length ; ++i)
{
// Fill out the Rectangle
g2.fillRect (x, y, Arr [i], 8);
y = y - 15;
}
g2.dispose ();
}
}
What should I use to animate the process. I also want to show which rectangular strips are being compared during the process of sorting.
Thank You
When I wrote a similar program, I created a SortListener interface with two methods: onCompare() and onSwap(). I used the onCompare() to highlite the two elements being compared with a different color and the onSwap() to notify the GUI to repaint istelf.
I also created a 'SortingAlgorithm' abstract class and several subclasses for specific sorting algorithms. The super class defined addSortListener() to allow other classes to register listeners. Then during the sorting algorithm, I called onCompare() immediately after a comparison and onSwap() imediately after two elements were swapped.
Finally, the JPanel where I did my painting implemented the SortListener interface and responded to onCompare() and onSwap() by repainting the animation.

divide and conquer algorithm

I've been given a 2^k * 2^k sized board, and one of the tiles is randomly removed making it a deficient board. The task is to fill the with "trominos" which are an L-shaped figure made of 3 tiles.
The process of the solving it isn't too difficult. If the board is 2x2, then it only takes one tromino to fill it. For any greater size, it must be divided into quarters (making four 2^(k-1) sized boards), with one tromino placed at the center point, so each quadrant has one filled in tile. After that, the board can be recursively filled until every tile is filled with a random colored tromino.
My main problem is actually implementing the code. My skills with Java programming are generally pretty weak, and I often have trouble simply finding a place to start. The only work to be done is in the tile method in the tiling class, which takes as input the deficient board to tile, the row and column to start tiling in, and the number of tiles to fill. This is a homework problem, so I'm simply looking for some guidance or a place to start - any help would be greatly appreciated.
public class BoardViewer extends JFrame {
private static final int WIDTH = 1024;
private static final int HEIGHT = 768;
private static final int OFFSET = 30;
public static final int UPVERT = 1;
public static final int DNVERT = 2;
public static final int RHORIZ = 4;
public static final int LHORIZ = 8;
public static final int FILL = 16;
private Color [][] board;
public BoardViewer(DeficientBoard db) {
super();
setSize(WIDTH + (OFFSET*2), HEIGHT + (OFFSET*2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
board = db.getBoardColor();
}
public void paint(Graphics g) {
super.paint(g);
int width = WIDTH/board[0].length;
int height = HEIGHT/board.length;
for (int r = 0; r < board.length; r++)
for (int c = 0; c < board[r].length; c++) {
g.setColor(board[r][c]);
int x = c*width + OFFSET;
int y = r*height + OFFSET + (OFFSET/2);
g.fillRect(x+1, y+1, width-1, height-1);
}
}
}
public class DeficientBoard {
private int n;
private Color board[][];
// The four rotations of the tile.
// UL is XX
// X
// UR is XX
// X
// LL is X
// XX
// LR is X
// XX
public final int UL = 0;
public final int UR = 1;
public final int LL = 2;
public final int LR = 3;
/**
* Create a 2^k x 2^k deficient board.
*
* #param k power
*/
public DeficientBoard(int k) {
n = (int)Math.pow(2, k);
createBoard(Color.LIGHT_GRAY);
}
/**
* Actually create an n x n deficient board.
*
* #param color background color
*/
private void createBoard(Color color) {
board = new Color[n][n];
for (int r = 0; r < board.length; r++)
for (int c = 0; c < board[0].length; c++)
board[r][c] = color;
int d_row = (int)(Math.random() * n);
int d_col = (int)(Math.random() * n);
board[d_row][d_col] = Color.BLACK;
}
/**
* Given a row and column and shape based on that point
* place a tromino of the given color.
*
* #param r row
* #param c column
* #param s shape (UL, UR, LL, LR)
* #param theColor a Color
*/
public void placeTromino(int r, int c, int s, Color theColor) {
if (s == UL) {
board[r][c] = theColor;
board[r][c+1] = theColor;
board[r+1][c] = theColor;
} else if (s == UR) {
board[r][c] = theColor;
board[r][c+1] = theColor;
board[r+1][c+1] = theColor;
} else if (s == LL) {
board[r][c] = theColor;
board[r+1][c] = theColor;
board[r+1][c+1] = theColor;
} else {
board[r+1][c] = theColor;
board[r+1][c+1] = theColor;
board[r][c+1] = theColor;
}
}
/**
* Get the 2^k x 2^k board.
*
* #return the Color board.
*/
public Color[][] getBoardColor() {
return board;
}
/**
* Find and return the deficient row.
*
* #param row row
* #param col column
* #param sz size of the baord
* #return the row the deficient block is located
*/
public int getDeficientRow(int row, int col, int sz) {
for (int r = row; r < (row + sz); r++)
for (int c = col; c < (col + sz); c++)
if (board[r][c] != Color.LIGHT_GRAY)
return r;
return -1;
}
/**
* Find and return the deficient column.
*
* #param row row
* #param col column
* #param sz size of the baord
* #return the row the deficient block is located
*/
public int getDeficientCol(int row, int col, int sz) {
for (int r = row; r < (row + sz); r++)
for (int c = col; c < (col + sz); c++)
if (board[r][c] != Color.LIGHT_GRAY)
return c;
return -1;
}
/**
* Get the size of the deficient board.
*
* #return the size
*/
public int getSize() {
return n;
}
/**
* Display information about the deficient board.
*/
public String toString() {
return ("Deficient board of size "
+ n + "x" + n
+ " with position missing at ("
+ getDeficientRow(0, 0, n) + "," + getDeficientCol(0, 0, n) +").");
}
}
public class Tiling {
private static Color randColor() {
int r = (int)(Math.random() * 256);
int g = (int)(Math.random() * 256);
int b = (int)(Math.random() * 256);
return new Color(r, g, b);
}
public static void tile(DeficientBoard db, int row, int col, int n) {
}
public static void main(String[] args) {
DeficientBoard db = new DeficientBoard(3);
System.out.println(db);
tile(db, 0, 0, db.getSize());
BoardViewer bv = new BoardViewer(db);
bv.setVisible(true);
}
}
In general, when a recursive function implements a divide-and-conquer algorithm, it has to handle two basic cases:
The base case. This is the case where you're done dividing, and need to conquer a bit. In your assignment, the base case is the case where n = 2, and in that case, you just need to find which of the four tiles is missing/painted (using DefectiveBoard.getDeficientRow and DefectiveBoard.getDeficientCol) and add the appropriate triomino to cover the other three tiles.
The recursive case. This is the case where you're not done dividing, so you need to divide (i.e., recurse), and (depending on the algorithm) may need to do a bit of conquering either before or after the recursion. In your assignment, the recursive case is the case where n > 2. In that case, you need to do two things:
Find which of the four quadrants has a missing/painted tile, and add the appropriate triomino to cover one tile from each of the other three quadrants.
Recurse, calling yourself four times (one for each quadrant).
A good starting point is to write the "Is this the base case?" check, and to implement the base case.
After that, if you don't see how to write the recursive case, one approach is to temporarily write a "one above the base" case (n = 4), and see if you can generalize it. If not, you might then temporarily write a "two above the base" case (n = 8), and so on. (Once you've got your recursive algorithm working, you would then remove these special cases, since they're fully covered by the general recursive case.)
Well this is somewhat of a harder problem to solve. However, I'd say you have the skills given how much code you wrote so I wouldn't be self conscious about it.
I don't have a complete solution formulated, but I think if you start at the the removed tile and put a trominos on either side of it. Then keep putting trominos on either side of the last trominos. You're "spooning" the tromino you last placed on the board. Once you do that to the edge of the board. All that's left is tromino shaped locations. Here is an example of what I mean (X is the dropped tile ie the gap, Y are the trominos):
_ _ _ _
|_|_|_|_|
|_|Y|Y|_|
|_|Y|X|Y|
|_|_|Y|Y|
_ _ _ _
|Y|Y|_|_|
|Y|Y|Y|_|
|_|Y|X|Y|
|_|_|Y|Y|
Once the board is filled to the edges you can essentially start dropping trominos like bombs on the rest of the board. I have a feeling there is a pattern here where you fill in the diagonal trominos while filling in the 2nd part at the same time that is repeatable. But if you can't find that then create a recursive routine that spoons the gap to the edges then transitions to adding trominos in diagonal patterns. The hint there is you have to do the transition in the first stack frame only.
Good luck.

Method - calling methods

import java.util.Scanner;
public class Rectangle
{
public static void main(String[] args)
{
-Don't know how to call method here-
}
/**
* returns the area of a rectangle
* #param height the height of the rectangle
* #param width the width of the rectangle
*/
public static int area (int length, int width)
{
return length * width;
}
/**
* returns the perimeter of a rectangle
* #param height the height of the rectangle
* #param width the width of the rectangle
*/
public static int perimeter (int length, int width)
{
return length + width;
}
/**
* returns the details of the rectangle
* #param height the height of the rectangle
* #param width the width of the rectangle
*/
public static void printRectangleDetails (int length, int width)
{
System.out.println ("This is the length of the rectangle " + length);
System.out.println ("This is the width of the rectangle " + width);
System.out.println (("This is the perimeter of the rectangle " + (length + width)));
System.out.println (("This is the area of the rectangle " + (length * width)));
}
/**
* Read in an integer and return its value
* #param the prompt to be shown to the user
*/
public static int readInteger(String prompt)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer");
int input = scan.nextInt();
return input;
}
}
I'm trying to call the method readInteger to prompt the user to insert the rectangle's height and width. This is my first experience with methods so any help would be appreciated, I'm also not sure if the readInteger method is correct.
Thanks!
In your main() method, you can read the length and width of the rectangle by calling the readInteger() method that you have created in the Rectangle class as:
int length = readInteger(" For Length ");
int width = readInteger("For Width ");
printRectangleDetails(length,width);
First of all, add this line to readInteger() method:
System.out.println (prompt);
You call a method, typical with the following syntax:
methodName();
Example:
To call the area method you say:
public static void main(String[] args)
{
area(2,3);
}
Note: the object is implied in this case since your area method is public and static to the rectangle class which contains the main method.
If area was in a different class you would make the call the method differently, by instantiation first and then a method call on the object.
Try this
public static void main(String[] args) {
Scanner s= new Scanner(System.in) ;
System.out.print("Enter length : " );
int len=Integer.parseInt( s.nextLine()) ;
System.out.print("\nEnter width : ");
int wd=Integer.parseInt( s.nextLine()) ;
printRectangleDetails(len,wd);
}

Categories