Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 1233 of 2109
Parent and Child classes having same data member in Java
The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.ExampleFollowing is an example −class Demo_base { int value = 1000; Demo_base() { System.out.println("This is the base class constructor"); } } class Demo_inherits extends Demo_base { int value = 10; Demo_inherits() { System.out.println("This is the inherited class ...
Read MoreQuantifiers in Java
Quantifier is a concept that allows the programmer to specify the number of occurrences of a specific type of value in the regular expression. There are different types of quantifiers, some of them include ‘?’ (Reluctant quantifier), ‘+’ (Possessive quantifier). In this post, we will see how reluctant quantifier works.ExampleFollowing is an example −import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String[] args) { Pattern my_pattern = Pattern.compile("sam+?"); Matcher my_match = my_pattern.matcher("samp"); while (my_match.find()) System.out.println("The pattern has been found - " + my_match.start() + ...
Read MoreRetrieving Elements from Collection in Java- For-each loop
The ‘for-each’ loop is used to iterate over a set of elements that is stored in a data structure.Syntaxfor (element e: collection) { System.out.println(e); }ExampleFollowing is an example −public class Demo { public static void main(String[] args) { int[] my_vals = {5, 67, 89, 31, -1, 2, 0}; int sum = 0; for (int number: my_vals) { sum += number; } System.out.println("The sum is " + sum); } }OutputThe sum is 193A class named Demo contains the main ...
Read MoreRetrieving Elements from Collection in Java- Iterator
Following is an example to retrieve elements −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { HashSet my_hs = new HashSet() ; my_hs.add("Joe"); my_hs.add ("Rob"); Iterator my_it = my_hs.iterator(); System.out.println("The elements are : "); while (my_it.hasNext()) System.out.println(my_it.next()); } }OutputThe elements are : Joe RobA class named Demo contains the main function, which defines a HashSet collection. Elements are added to this collection using the ‘add’ function. An iterator is defined, and the elements ...
Read MoreRetrieving Elements from Collection in Java- EnumerationIterator
EnumerationIterator doesn’t have the option of eliminating elements from the collection, whereas an iterator has this facility. An additional disadvantage of using EnumerationIterator is that the name of methods associated with EnumerationIterator is difficult to remember.ExampleFollowing is an example −import java.util.Vector; import java.util.Enumeration; public class Demo { public static void main(String args[]) { Vector day_name = new Vector(); day_name.add("Tuesday"); day_name.add("Thursday"); day_name.add("Saturday"); day_name.add("Sunday"); Enumeration my_days = day_name.elements(); System.out.println("The values are "); while (my_days.hasMoreElements()) ...
Read MoreHow to create a data frame with combinations of values in R?
Suppose we have two values 0 and 1 then how many combinations of these values are possible, the answer is 8 and these combinations are (0,0), (1,0), (0,1), (1,1). In R, we can use expand.grid function to create these combinations but to save it in a data frame, we would need to use as.data.frame function.Exampledf1
Read MoreProgram to find the maximum number in rotated list in C++
Suppose there is an array, and that is sorted, consider that array is rotated at some pivot, that is unknown to us. So we have to find the maximum from that rotated array. So if the array is like[3, 4, 5, 1, 2], then the output will be 5.To solve this, we will follow these steps −low := 0 and high := last index of array, n := size of array, ans := 0while low arr[mid], then ans := maximum of ans and arr[mid], high := mid – 1else if low = mid, then ans := maximum of ans ...
Read MoreHow to create a contingency table with sum on the margins from an R data frame?
The sum of rows and columns on the margins in a contingency table are always useful because they are used for different type of calculations such as odds ratio, probability etc. If an R data frame has factor columns then we can create a contingency table for that data frame and it can be done by using addmargins function.ExampleConsider the below data frame −x1
Read MoreHow to display R-squared value on scatterplot with regression model line in R?
The R-squared value is the coefficient of determination, it gives us the percentage or proportion of variation in dependent variable explained by the independent variable. To display this value on the scatterplot with regression model line without taking help from any package, we can use plot function with abline and legend functions.Consider the below data frame −Exampleset.seed(1234) x
Read MoreHow to find unique permutations if a vector contains repeated elements in R?
We can use permn function from combinat package to find the permutations but if we have repeated elements in the vector then the result will not have unique permutations, therefore, we need to use unique function along with the permn function. For example, if we have a vector 1, 2, 1 then the permutations will be (1 2 1), (1 1 2), (1 1 2), (1 2 1), (2 1 1), (2 1 1) and the unique permutations will be (1 2 1), (1 1 2), (2 1 1).Examplex1
Read More