Posts

Showing posts from March, 2025

Java implementation of Transposing Matrix

              Transposing a matrix deals with the same matrix with some modifications. Here,the rows become columns and columns became rows. To implement this concept in java, follow the below steps. Logic: First, get the dimensions for the matrix from the user. Read the elements of the input matrix. Create the transpose matrix structure. Using two for loops, change the columns into rows and rows into columns. Finally, print the transpose matrix as output. Program: import java.util.Scanner; public class TransposeMatrixEg {     public static void main(String[] args) {       Scanner s1 = new Scanner(System.in);           // Get the dimensions of matrix         System.out.print("Enter rows for the matrix: ");         int rows = s1.nextInt();         System....

Java Program to calculate the sum of rows and columns in a matrix

              Matrix is a mathematical structure which consists of rows and columns. When you want to add the values of rows  and columns separately , follow the steps given below. First, get the number of rows and columns from the user. Initialise a new matrix with user’s rows and columns value. Using a for loop, read the input for the matrix. For calculating the sum of rows, use a for loop. Keep the row, navigate all column values and add the values. Do this for each row. To make the sum of column values, keep the column, iterate the row values and add the value. Continue for all columns. Finally, print the value. Program: import java.util.Scanner; public class mRowColSum {     public static void main(String[] args) {         Scanner s1 = new Scanner(System.in);        // get the matrix dimensions values       ...

Implementation of Matrix multiplication in java

              Matrix is one of the fundamental concepts in java. A matrix is a structure which consists of rows and columns. Logic behind matrix multiplication: When you want to multiply two matrices, there are some points to be noted. First, columns of first matrix and rows of second matrix should be equal. Iterate through each row of matrix1 and each column of matrix2 and add the elements. Final result matrix rows should be equal to matrix1. The resultant matrix column should be equal to matrix2. Program implementation: It starts with including the built in package java.util.Scanner. A public class is created with main() function. A scanner object gets assigned with System input. The dimensions of two matrices (rows and columns) received from the user. The columns of first matrix are equal to the rows of second matrix. The values for first and second matrices are get assigned from user input. Using three for loops, the rows are iterated...

Decimal to Binary conversion Programs in java

               Numbers are magical in mathematics. Decimal numbers are whole numbers, where the binary numbers contain 0’s and 1’s. How to implement? There are two methods to implement this program in java. 1.       Using user defined code 2.       Using Built-in function. Let us implement this program in both ways. 1.Using user defined code: It has following steps. ·        First, read the input from the user. ·        Using a while loop, until the decimal_no is greater than zero, repeat the process. ·        using mod function, find the remainder using mod by 2. ·        Next, decimal_no is divided by 2. ·        Finally, print the binary_no. Program:      import java.util.Scanner; public class DecimalToB...

Unit conversion program in java

               Science has lot of units. Some of the units are converted into another unit. For example, temperature has Celsius and Fahrenheit. Distance has miles and kilometres.               Let us create java program to convert these units. Program implementation: ·        This program reads the choice from the user. Based on the choice,the conversion starts. ·        The input value is read from the command line. ·        If the choice is 1, it converts the Celsius value to Fahrenheit value and print it. ·        If the choice is 2, it makes the Fahrenheit value to Celsius value and display it. ·        If the choice value is 3, the program creates the conversion of kilometers into miles. ·      ...

Java Swing Application to display the given message

Image
Let us create a simple application to display the message in the text field to a separate screen. What are the components you need? ·        A frame ·        A Panel ·        Label ·        Text field ·        Submit button. These the Swing components used in the program. Let us implement the sample application. Program implementation: First, include the built-in packages javax.swing,java.awt,java.awt.event. Create a public class with main() function. Design the components as follows. A frame ,A Panel , Label , Text field and a submit button. Let us make objects for each components. Set the frame object’s heading as “Display the message”. Jpanel object p1 is created. Label object is created with the message “Enter the message”. It makes the user to enter the message. TextField object is declared with size of 15. A butto...