Found 2616 Articles for Java

Difference between sum of the squares of and square of sum first n natural numbers.

Mahesh Parahar
Updated on 16-May-2020 14:45:24

735 Views

Problem StatementWith a given number n, write a program to find the difference between sum of the squares of and square of sum first n natural numbers.Examplen = 3 Squares of first three numbers = 3x3 + 2x2 + 1x1 = 9 + 4 + 1 = 14 Squares of sum of first three numbers = (3 + 2 + 1)x(3 + 2 + 1) = 6x6 = 36 Difference = 36 - 14 = 22Example Live DemoFollowing is the program in Java to find the required difference.public class JavaTester {    public static int difference(int n){   ... Read More

What are the different "/edit" commands in JShell in Java 9?

raja
Updated on 14-Apr-2020 18:13:27

171 Views

JShell is a command-line tool introduced in Java 9 that evaluates declarations, statements, and expressions without the main() method. JShell can set up a text editor called JShell Edit Pad, which allows us to modify the code very easily, and it can be launched using the "/edit" command.Below are the different "/edit" commands used in Jshell./edit /edit [ID] /edit [Code_Name]/edit: This command can be used without an argument, the "/edit" command displays all the active code in the text editor./edit [ID]: This command displays in the text editor the code corresponding to the ID entered./edit [Code_Name]: This comamnd displays in the ... Read More

How to get JShell documentation in Java 9?

raja
Updated on 14-Apr-2020 15:06:30

252 Views

Java 9 introduced a new interactive tool called JShell. This tool can be used to execute expressions, classes, interfaces, enums, and etc.The detailed documentation can be available in JShell with full information, as well as the use of its internal commands with the various options. This documentation can be accessed using two commands: "/help" and "/?". JShell's documentation is not only limited to information regarding its internal controls, and also includes Javadoc.In the below code snippet, the result can be obtained by using the "/help" command.jshell> /help |   Type a Java language expression, statement, or declaration. |   Or ... Read More

What are the steps to execute Flow API in Java 9?

raja
Updated on 14-Apr-2020 13:01:43

224 Views

Flow API in Java 9 corresponds to Reactive Streams specification, which is a defacto standard. It contains a minimal set of interfaces that capture the heart of asynchronous publication and subscription.Below are the key interfaces of Flow API:1) Flow.Publisher: It produces items for subscribers to consume, and it contains only method: subscribe(Subscriber), whose purpose should be obvious.Syntaxvoid subscribe(Flow.Subscriber

How can we implement Flow API using Publisher-Subscriber in Java 9?

raja
Updated on 14-Apr-2020 10:31:19

1K+ Views

Flow API (java.util.concurrent.Flow) has introduced in Java 9. It helps to understand different ways in which the Publisher and Subscriber interfaces interact to perform desired operations.Flow API consists of Publisher, Subscriber, Subscription, and Processor interfaces, which can be based on reactive stream specification.In the below example, we can implement Flow API by using Publisher-Subscriber interfaces.Exampleimport java.util.concurrent.Flow.Publisher; import java.util.concurrent.Flow.Subscriber; import java.util.concurrent.Flow.Subscription; public class FlowAPITest {    public static void main(String args[]) {       Publisher publisherSync = new Publisher() {   // Create publisher          @Override          public void subscribe(Subscriber

What is text origin in JavaFX?

Maruthi Krishna
Updated on 14-Apr-2020 08:53:51

229 Views

In addition to the local coordinate system for positioning its nodes, JavaFX provides an additional coordinate system for the text node.The textOrigin property specifies the origin of the coordinates of the text node in the parent coordinate system. You can set values to this property using the setTextOrigin() method. This method accepts one of the constants of the enum named VPos. This enum contains 4 constants namely: BASELINE, BOTTOM, CENTER and, TOP.Exampleimport java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Scanner; import javafx.application.Application; import javafx.geometry.VPos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.text.Text; public class TextOriginExample extends Application {   ... Read More

How to strike through and underline text in JavaFX?

Maruthi Krishna
Updated on 14-Apr-2020 08:50:24

3K+ Views

In JavaFX, the text node is represented by the Javafx.scene.text.Text class. To insert/display text in JavaFx window you need to −Instantiate the Text class.Set the basic properties like position and text string, using the setter methods or, bypassing them as arguments to the constructor.Add the created node to the Group object.The strikethrough property of the javafx.scene.text.Text class determines whether each line of the text should have a straight line passing through the middle of it. You can set the value to this property using the setStrikeThrough() method. It accepts a boolean value. You can strike though the text (node) by ... Read More

How to adjust the line spacing in the text node in JavaFX?

Maruthi Krishna
Updated on 14-Apr-2020 08:48:00

1K+ Views

The line spacing property of the javafx.scene.text.The text class specifies the line spacing between the lines of the text (node) vertically.You can set the value to this property using the setLineSpacing() method. This method accepts a boolean value as a parameter and sets the specified space between the lines (vertically).Exampleimport java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Scanner; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; public class TextSpacing extends Application {    public void start(Stage stage) throws FileNotFoundException {       //Reading the contents of a text file.       InputStream inputStream ... Read More

How to adjust the alignments of the text in JavaFX?

Maruthi Krishna
Updated on 14-Apr-2020 08:44:40

6K+ Views

You can set a fixed width for the text in user space by setting the value to the wrappingWidth property. Once you do so, given width is considered as the boundary of the text in user coordinates and, the text is arranged width in the given width.If you haven’t given any value for this property, by default, the length longest line in the text is considered as the width of the bounding box.Text alignment is the arrangement of the text horizontally within the bounding box. You can adjust the alignment of the text using the setTextAlignment() method. This method accepts ... Read More

How to wrap the text within the width of the window in JavaFX?

Maruthi Krishna
Updated on 14-Apr-2020 08:40:29

2K+ Views

In JavaFX, the text node is represented by the Javafx.scene.text.Text class. To insert/display text in JavaFx window you need to −Instantiate the Text class.Set the basic properties like position and text string, using the setter methods or, bypassing them as arguments to the constructor.Add the created node to the Group object.If the length of the lines in the text you have passed, longer than the width of the window part of the text will be chopped as shown below −As a solution you can wrap the text within the width of the window by setting the value to the property wrapping ... Read More

Advertisements