Object Oriented Programming Articles

Page 354 of 589

Java program to format strings into table

Samual Sam
Samual Sam
Updated on 25-Oct-2024 1K+ Views

In this article, we will learn how to format strings into a table using Java. Java's System.out.format() method is a convenient way to display formatted output. We will define a string format that specifies column widths to ensure uniform spacing, allowing us to create a neat table layout. By using this approach, we can display multiple rows of string data in a structured and readable format without manually adjusting spaces for each entry. Steps to format strings into a table Following are the steps to format strings into a table − Define a format string ...

Read More

Java program to calculate the time of sorting an array

Samual Sam
Samual Sam
Updated on 25-Oct-2024 963 Views

In this article, we will learn to measure the time taken to sort an array in Java. We will look at two methods to calculate sorting time: using the Date class and the System.nanoTime() method. First, we will use the Date class to track the time before and after the sorting operation, allowing us to calculate the duration in milliseconds. Then, we'll utilize System.nanoTime() to capture the time with greater precision, measuring in nanoseconds and converting the result to milliseconds. Different approaches Below are the different approaches to measure the time taken to sort an array using Java− ...

Read More

Java ArrayList to print all possible words from phone digits

Neetika Khandelwal
Neetika Khandelwal
Updated on 24-Oct-2024 562 Views

In this article, we will learn to generate all possible words from a string of phone digits using Java. Each digit on a mobile keypad corresponds to a set of letters, and our task is to find every possible combination of letters that can be formed by pressing those digits. For instance, if the input is "23, " the output would include combinations like "ad, " "ae, " "af, " and so on. We will implement a recursive approach to achieve this, allowing us to systematically generate and print all possible words corresponding to the given digits. Problem Statement Write ...

Read More

Java program to paste clipboard text to JTextArea

Smita Kapse
Smita Kapse
Updated on 24-Oct-2024 948 Views

In this article, we will learn to paste clipboard text into a JTextArea using Java. We'll use the paste() method to create a simple program that lets users insert clipboard content directly into a text area. The program will display a basic GUI window with a JTextArea, where any text copied to the clipboard can be easily pasted. Steps to paste clipboard text to JTextArea Following are the steps to paste clipboard text to JTextArea − First, import necessary classes from packages like javax.swing and java.awt to create the GUI components. Create a ...

Read More

Java program to find the LCM of two numbers

karthikeya Boyini
karthikeya Boyini
Updated on 23-Oct-2024 9K+ Views

In this article, we will learn to find the LCM of two numbers using Java. L.C.M. or Least Common Multiple of two values is the smallest positive value which is the multiple of both values. For example, multiples of 3 and 4 are: 3 → 3, 6, 9, 12, 15 ... 4 → 4, 8, 12, 16, 20 ... The smallest multiple of both is 12, hence the LCM of 3 and 4 is 12.We will learn through two different methods to solve this: an incremental method and a formula-based approach using the Greatest Common Divisor (GCD).  Problem Statement Write ...

Read More

Does constructor return any value in Java?

mkotla
mkotla
Updated on 23-Oct-2024 6K+ Views

No, the constructor does not return any value. While declaring a constructor you will not have anything like a return type. In general, the Constructor is implicitly called at the time of instantiation. And it is not a method, its sole purpose is to initialize the instance variables.Read more about constructors: Java Constructors Example public class Sample{ public Sample(){ System.out.println("This is a constructor"); } public static void main(String args[]){ ...

Read More

Java program to get text from JTextPane and display in Console

Anvi Jain
Anvi Jain
Updated on 23-Oct-2024 923 Views

In this article, we will learn how to get text from a JTextPane in Java and display it in the console. We'll use the getText() method to retrieve the text and show it in the console. Additionally, we’ll apply simple text styling like italics and color using SimpleAttributeSet to demonstrate how to manage styled text in a GUI. Steps to get text from JTextPane Following are the steps to get text from JTextPane and display it in the Console using Java − First, we will start by creating a JFrame to serve as the main window of ...

Read More

Java program to remove the first row from a table with DefaultTableModel

Ankith Reddy
Ankith Reddy
Updated on 23-Oct-2024 902 Views

In this article, we will learn how to display a table with multiple rows and columns in Java using the JTable component. Additionally, we will demonstrate how to remove the first row from the table using the removeRow() method. The first program shows how to create and display a table with 9 rows, and the second program demonstrates how to remove the first row, leaving the remaining 8 rows intact.  Create a table with rows and columns using JTable Following are the steps to create a table with rows and columns using JTable − Start ...

Read More

Java program to remove all duplicates words from a given sentence

AmitDiwan
AmitDiwan
Updated on 23-Oct-2024 1K+ Views

In this article, we will learn to remove duplicate words from a given sentence in Java. The first method uses Java Streams to remove duplicates and join the words back into a sentence using the distinct() and Collectors.joining() methods. The second method uses a Set to automatically filter out duplicate words and a StringJoiner to combine the words back into a sentence. Both methods will give a sentence with only unique words. Different approaches Following are the different approaches to remove all duplicate words from a given sentence − Using Java Streams ...

Read More

Java program to find the roots of a quadratic equation

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 21-Oct-2024 15K+ Views

In this article, we will learn to find the roots of a quadratic equation using Java. The roots of a quadratic equation are determined by the following formula: $$x = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}$$ Here we will take input values for the coefficients 𝑎, 𝑏, and 𝑐 of the quadratic equation 𝑎 𝑥 2 + 𝑏 𝑥 + ...

Read More
Showing 3531–3540 of 5,881 articles
« Prev 1 352 353 354 355 356 589 Next »
Advertisements