Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 109 of 151

How to Map double to int Object with mapToInt and mapToObj in Java?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 519 Views

At first, we have set the stream −Stream.of(3.5, 4.7, 7.9, 8.2, 9.1, 10.5, 12.3, 15.8)Now to Map double to int Object, use mapToObj −.mapToInt(Double::intValue) .mapToObj(a -> "val" + a)The following is an example to Map double to int Object with mapToInt and mapToObj −Exampleimport java.util.stream.Stream; public class Demo {    public static void main(String[] args) throws Exception {       Stream.of(3.5, 4.7, 7.9, 8.2, 9.1, 10.5, 12.3, 15.8)       .mapToInt(Double::intValue)       .mapToObj(a -> "val" + a)       .forEach(System.out::println);    } }Outputval3 val4 val7 val8 val9 val10 val12 val15

Read More

Map to add String value to each element in Java

Samual Sam
Samual Sam
Updated on 30-Jul-2019 880 Views

Let’s say the following is our String List −List list = Arrays.asList("Football", "Basketball", "Hockey", "Cricket", "Fencing");Now, map to add string to each element −List str = list.stream().map(name -> "Sports " + name + " Outdoor")    .collect(Collectors.toList());Above, we have added “Sports” and “Outdoor” strings to each element.The following is an example to Map and add string value to each element in Java −Exampleimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList("Football", "Basketball", "Hockey", "Cricket", "Fencing");       List str = list.stream().map(name -> "Sports " ...

Read More

How to disable a JLabel in Java?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 605 Views

To disable a JLabel, use the setEnabled() method −JLabel label; label.setEnabled(false);The following is an example to disable a JLabel −package my; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.*; import javax.swing.border.Border; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("First Label", JLabel.RIGHT);       label.setVerticalAlignment(JLabel.TOP);       label.setFont(new Font("Verdana", Font.PLAIN, 15));       label.setPreferredSize(new Dimension(250, 100));       label.setForeground(new Color(120, 90, 40));       label.setBackground(new Color(100, 20, 70));       label.setEnabled(false);   ...

Read More

How will you show memory representation of C variables?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 600 Views

Here we will see how to print the memory representation of C variables. Here we will show integers, floats, and pointers.To solve this problem, we have to follow these steps −Get the address and the size of the variableTypecast the address to the character pointer to get byte addressNow loop for the size of the variable and print the value of typecasted pointer.Example#include typedef unsigned char *byte_pointer; //create byte pointer using char* void disp_bytes(byte_pointer ptr, int len) {     //this will take byte pointer, and print memory content    int i;    for (i = 0; i < ...

Read More

How to change JFrame background color in Java

Samual Sam
Samual Sam
Updated on 30-Jul-2019 24K+ Views

At first, create a JFrame −JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(550, 300));Now, change the background color of the JFrame −frame.getContentPane().setBackground(Color.BLUE);The following is an example to change JFrame background color −Exampleimport java.awt.Color; import java.awt.Dimension; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setPreferredSize(new Dimension(550, 300));       frame.getContentPane().setBackground(Color.BLUE);       frame.pack();       frame.setVisible(true);    } }Output

Read More

How to close JFrame on the click of a Button in Java

Samual Sam
Samual Sam
Updated on 30-Jul-2019 13K+ Views

Set frame.dispose() on the click of a button to close JFrame. At first create a button and frame −JFrame frame = new JFrame(); JButton button = new JButton("Click to Close!");Now, close the JFrame on the click of the above button with Action Listener −button.addActionListener(e -> {    frame.dispose(); });The following is an example to close JFrame on the click of a Button −Exampleimport java.awt.Color; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JButton button = new JButton("Click to Close!");   ...

Read More

How to count set bits in a floating point number in C?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 574 Views

In this problem, one floating point value is given. We have to find number of set bits in the binary representation of it.For example, if a floating point number is 0.15625, so there are six set bits. A typical C compiler used single precision floating point representation. So it will be look like this.To convert into its bit values, we have to take the number into one pointer variable, then typecast the pointer to char* type data. Then process each byte one by one. Then we can count set bits of each char.Example#include int char_set_bit_count(char number) {    unsigned ...

Read More

How to write a running C code without main()?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 8K+ Views

Here we will see, one program can be written without main or not? The answer is yes. We can write program, that has no main() function.In many places, we have seen that the main() is the entry point of a program execution. Just from the programmers perspective this is true. From the system’s perspective it is not true. So the system at first calls the _start(), this sets up the environment, then main is called.To execute this program we have to use this option ‘-nostartfiles’.Example#include extern void _exit(register int); int _start() {    printf("Program without main");       ...

Read More

Use of bool in C

Samual Sam
Samual Sam
Updated on 30-Jul-2019 2K+ Views

In C there is no predefined datatype as bool. We can create bool using enum. One enum will be created as bool, then put the false, and true as the element of the enum. The false will be at the first position, so it will hold 0, and true will be at second position, so it will get value 1. Now we can use this as datatype.Example#include typedef enum {    F, T } boolean; main() {    boolean my_bool1, my_bool2;    my_bool1 = F;    if(my_bool1 == F) {       printf("my_bool1 is false");    } else { ...

Read More

C++ Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers

Samual Sam
Samual Sam
Updated on 30-Jul-2019 219 Views

This is a C++ Program to find k numbers closest to Median of S, where S is a set of n numbers.AlgorithmsBegin    function partition() for partitioning the array on the basis of values at high as pivot value:    Arguments:       a[]=an array.       l=low    H=high    Body of the function:    Declare variables pivot, in, i    Initialize in = l    Set pivot = h    For i=l to h-1       if(a[i] < a[pivot])          swap a[i] and a[in])       increment in.       ...

Read More
Showing 1081–1090 of 1,507 articles
« Prev 1 107 108 109 110 111 151 Next »
Advertisements