Programming Articles - Page 1431 of 3363

How to convert columns of an R data frame into a single vector?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:53:14

2K+ Views

Sometimes all the columns in a data frame have similar data characteristics representing a particular variable. For example, having a data frame containing five columns each with heights of people. To convert this type of data frame into a vector we can use as.vector function along with the as.matrix function. The as.matrix will read the data frame columns so that the array of values can be created.Example1 Live DemoConsider the below data frame −set.seed(101) x1

How to solve diamond problem using default methods in Java

Maruthi Krishna
Updated on 08-Feb-2021 11:53:31

15K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{}The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Multiple inheritance is where one class inherits the properties of multiple classes. In other words, ... Read More

How to convert a super class variable into a sub class type in Java

Maruthi Krishna
Updated on 08-Feb-2021 11:52:56

11K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{}The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Converting a super class reference variable into a sub class typeYou can try to convert ... Read More

How to create a step histogram using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:37:15

400 Views

To create a step histogram using ggplot2, we can use geom="step" argument inside stat_bin function. For example, if we have a data frame that contains a single column then the step histogram can be created using the command − ggplot(df,aes(x))+stat_bin(geom="step",bins=30)Example Live DemoConsider the below data frame −set.seed(14) x

How to convert a sub class variable into a super class type in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:52:37

2K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{}The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Converting a sub class variable into a super class typeYou can directly assign a sub ... Read More

How to create a single data frame from data frames stored in a list with row names in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:36:59

208 Views

If we have multiples data frames of same size stored in a list and we believe that these data frames have similar characteristic then we can create a single data frame. This can be done with the help of do.call. For example, if we have a list defined with the name List containing data frames with equal number of rows with their names then a single data frame can be created do.call(rbind,unname(List)).Example Live Demodf1

Explain the usage of the valueOf() method of the String class in Java

Maruthi Krishna
Updated on 08-Feb-2021 11:36:43

237 Views

The String class of the java.lang package represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant, their values cannot be changed after they are created.The valueOf() method of the String class accepts a char or, char array or, double or, float or, int or, long or an object as a parameter and returns its String representation.ExampleLive Demoimport java.util.Scanner; public class ConversionOfDouble {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a double value:");       ... Read More

How to convert a double value into a Java String using append method?

Maruthi Krishna
Updated on 08-Feb-2021 11:36:26

326 Views

The append method of the StringBuilder or StringBuffer objects accept a boolean or, char or, char array or, double or, float or, int or, long or, String value as parameter and adds it to the current object.You can append the required double value to the method and retrieve a String from obtained StringBuffer (or, StringBuilder) objects.ExampleLive Demoimport java.util.Scanner; public class ConversionOfDouble {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a double value:");       double d = sc.nextDouble();       StringBuffer sb = new StringBuffer();       sb.append(d); ... Read More

How to iterate the values of an enum using a for loop in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:26:53

476 Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }You can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. Once you obtain the array you can iterate it using the for loop.Examplepublic class IterateEnum{    public static void main(String args[]) {       Days days[] = Days.values();       System.out.println("Contents of the enum are: ");             //Iterating enum using the for loop ... Read More

Can we define an enum inside a method in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:35:52

3K+ Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }We can an enumeration inside a class. But, we cannot define an enum inside a method. If you try to do so it generates a compile time error saying “enum types must not be local”.Examplepublic class EnumExample{    public void sample() {       enum Vehicles {          Activa125, Activa5G, Access125, Vespa, TVSJupiter;       }    } }ErrorEnumExample.java:3: error: enum types must not be local ... Read More

Advertisements