Question
(Programming Exercises from text, Q14.7)
(Display random 0 or 1) Write a program that displays a 10-by-10 square matrix, as shown in Figure 14.45a . Each element in the matrix is 0 or 1, randomly generated. Display each number centered in a text field. Use TextField’s setText method to set value 0 or 1 as a string.

Java Programm
Can you provide coding with comments

Thank You

Exercise14_07 - OX 1 0 1 1 0 0 0 1 0 1 101 01 ooooo 0 0 1 1 0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1
0 0
Add a comment Improve this question Transcribed image text
Answer #1
package abc.ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Random;

public class MagicMatrix extends JFrame {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JPanel chartPanel;
    private JTextField termField;
    double a;
    private static DecimalFormat df2 = new DecimalFormat("#.##");

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MagicMatrix frame = new MagicMatrix();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public MagicMatrix() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(200, 200, 450, 450);
        //Set the layout ato grid layout and set the size of the layout to 10 * 10 matrix
        setLayout(new GridLayout(10,10));

        //Random class to get the random value from a upper bound range given
        Random random =new Random();

        //Iterate to create 100 cells for the 10*10 matrix
        JTextField field;
        for(int i=0;i<100;i++) {
            field =new JTextField(1);
            field.setText(Integer.toString(random.nextInt(2)));
            add(field);
        }
        setSize(300,300);
    }
}

Add a comment
Know the answer?
Add Answer to:
(Programming Exercises from text, Q14.7) (Display random 0 or 1) Write a program that displays a...
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
  • Part I: Code writing: 1. 3pts (Display matrix of numbers) Write a function that displays an...

    Part I: Code writing: 1. 3pts (Display matrix of numbers) Write a function that displays an n-by-n matrix using the following header: void printMatrix(int n) Each element is between (3,9), which is generated randomly. Write a test program that prompts the user to enter n and displays an n-by-n matrix. Here is a sample run: Enter n: 2 47 96

  • Java Programming Reading from a Text File Write a Java program that will use an object...

    Java Programming Reading from a Text File Write a Java program that will use an object of the Scanner class to read employee payroll data from a text file The Text file payroll.dat has the following: 100 Washington Marx Jung Darwin George 40 200 300 400 Kar Car Charles 50 22.532 15 30 The order of the data and its types stored in the file are as follows: Data EmployeelD Last name FirstName HoursWorked double HourlyRate Data Tvpe String String...

  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

  • In NI LabVIEW please, Construct a VI that displays a random number between 0 and 1...

    In NI LabVIEW please, Construct a VI that displays a random number between 0 and 1 once every second. Also, compute and display the average of the last four random numbers generated. Display the average only after four numbers have been generated; otherwise display a 0. Each time the random number exceeds 0.5, generate a beep sound using the Beep.vi.

  • Please go to the Practice Exercises (at the end of Chapter 3 in your text on...

    Please go to the Practice Exercises (at the end of Chapter 3 in your text on page 129) and do Exercise E 3.14 which asks you to compute and print the current season of the year depending on the month and day (input as integers). Be sure to review Java boolean operators, multi-way IF-ELSE statements, Switch statements, and String comparison methods before implementing your Java code. The algorithm for this program has been written for you and is included in...

  • java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text...

    java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text file (a novel or a report, or just a sequence of letters or symbols), by reading the file into a byte array, convert to a string, and then scan the string letter by letter to count the frequencies of all unique characters in the text, after that, the letters with frequencies greater than 0 and the frequencies are reported side by side. All members...

  • JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts...

    JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...

  • 1. (30 Points) Write a MATLAB program that displays "The multiplication of 10 multiplied by integers...

    1. (30 Points) Write a MATLAB program that displays "The multiplication of 10 multiplied by integers of 1 through 15. Display your result back to the user with 2 decimal places (yes, 2 decimal places). You must implement your code using a for loop. The result should have the following format: The multiplication of 10*1 =10.00 The multiplication of 10*2 =20.00 The multiplication of 10'3=30.00 The multiplication of 10*15=150.00 2. (35 Points) Write MATLAB code to prompt a user for...

  • 1. Create an app with a button and a text field. Display the current count in...

    1. Create an app with a button and a text field. Display the current count in the text field (display 0 when the app loads). CODE EACH OF THESE SUB QUESTIONS AS SEPARATE APPS! a. Count up to 20, then automatically start back at 1 (instead of reaching 21). b. Count up by one for each button press for the first 10 presses, then count up by 2 for the next 10 presses, 3 for the next 10, and so...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

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