Question

Write a program that takes a positive integer n as input from the user and displays...

Write a program that takes a positive integer n as input from the user and displays an n by n checkerboard. (Hint: your main method could do user input then call the constructor for your GUI with n as the argument.) For example, when n = 5, there are 25 alternating white and black squares inside a square boarder. The lower right corner of the overall pattern must be white. Make sure that adjacent squares are different colors regardless of whether n is even or odd. You may want to use un-editable TextField Objects in a GridPane and use CSS to set background colors.
Be sure to use javaFX components for this .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: See the code below:

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

package checkerboard;

import java.util.Scanner;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

/**
*
* Checkerboard class
*/
public class Checkerboard extends Application {
  
@Override
public void start(Stage primaryStage) {
int n; //number of cells in checker board
Scanner in = new Scanner(System.in);
System.out.println("Enter number of cells in checker board:");
n = in.nextInt(); //read number of cells
final int NROWS = n; //number of rows
final int NCOLUMNS = n; //number of columns
final int squareSize = 100; //size of one square
int color=0; //flag for color of square
  
GridPane root = new GridPane(); //creates grid pane
  
//fix columns and rows of grid pane
for (int i = 0; i < NCOLUMNS; i++) {
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setPercentWidth(100.0 / NCOLUMNS);
root.getColumnConstraints().add(columnConstraints);
}
for (int i = 0; i < NROWS; i++) {
RowConstraints rowConstraints = new RowConstraints();
rowConstraints.setPercentHeight(100.0 / NROWS);
root.getRowConstraints().add(rowConstraints);   
}
  
//add squares to pane
for(int row = 0;row < NROWS; row++)
{
for(int col = 0;col < NCOLUMNS; col++)
{
Label label = new Label();
label.setMinWidth(squareSize);
label.setMinHeight(squareSize);
if(color == 0)
label.setStyle("-fx-background-color:#FFFFFF;");
else
label.setStyle("-fx-background-color:#000000;");
root.add(label,row,col);
color = (color == 0 ? 1 : 0);              
}
}
  
Scene scene = new Scene(root, NROWS*squareSize, NCOLUMNS*squareSize);
  
primaryStage.setTitle("Checker Board");
primaryStage.setScene(scene);
primaryStage.show();
in.close();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

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

Output:

Checker Board

Add a comment
Know the answer?
Add Answer to:
Write a program that takes a positive integer n as input from the user and displays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that gets an integer value from the user. Then it finds and displays...

    Write a program that gets an integer value from the user. Then it finds and displays its factorial using the following approximation Use the function pow(x,n) and exp(x) from math.h 3 f= n! = nn e n

  • Exercise 1: Write a C++ program that asks the user to input an integer n followed...

    Exercise 1: Write a C++ program that asks the user to input an integer n followed by n other characters that will be stored in an array. We suppose here that the user will input only lowercase characters between ‘a’ and ‘z’. We also suppose that the user will input different characters. The program will then sort these characters according to an increasing order of their alphabetical order. In this exercise, feel free to use selection sort, insertion sort, or...

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • python program sample output Problem 3 (Taking User Input) Write a function called userGuess(n) that takes...

    python program sample output Problem 3 (Taking User Input) Write a function called userGuess(n) that takes an integer n as an argument. This function should display n to the user and prompt them to enter the number num that is the largest power of 2 less than or equal to n. Have your function return the user's input num (it will be used in the next problem) Problem 4 (Making a Game) Finally, create a main() function for your program....

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT