Found 9150 Articles for Object Oriented Programming

Java program to print duplicates from a list of integers

George John
Updated on 25-Jun-2020 14:23:39

4K+ Views

In order to find duplicates we can utilize the property of Set in Java that in Java duplicates are not allowed when going to be added in a Set.Add method of set returns true for the adding value which is not added previously to it while it would return false in case the adding value is already present in the Set.For our agenda we would iterate out list or collection of integer and try to add each integer in a set of type integer.Now if integer gets added than this means that it is occurring for first time and not ... Read More

Java program to count upper and lower case characters in a given string

Chandu yadav
Updated on 24-Jun-2024 17:07:04

10K+ Views

In order to count upper and lower case character we have to first find weather a given character is in upper case or in lower case. For this we would take concept of ASCII value of each character in Java. In Java as we know for each character has corresponding ASCII value so we would compare each character that it lies in the range of upper case or in lower case. Counting upper and lower case characters in a string in Java In below example first convert string to a character array for easy transverse, then find weather it lies ... Read More

Java program to convert float decimal to Octal number

Ankith Reddy
Updated on 25-Jun-2020 14:25:17

362 Views

We can convert any decimal number to its equivalent octal by following program.In this we reserve the reminder we get after divide the given number by 8 as it is the base of Octal and then reverse the order of reminders we have stored by multiplying each reminder by 10.Let understand by following example.Example Live Demopublic class DecimalToOctal {    public static void main(String[] args) {       int decimal = 84;       int octalNumber = 0, i = 1;       while (decimal != 0) {          octalNumber += (decimal % 8) * i;          decimal /= 8;          i *= 10;       }       System.out.println("Octal of given decimal is " + octalNumber);    } }OutputOctal of given decimal is 124

Java program to convert first character uppercase in a sentence

Alshifa Hasnain
Updated on 31-Dec-2024 22:01:37

705 Views

In this article, we will learn to convert the first character to uppercase in a sentence in Java, converting the first character of each word or sentence to uppercase is a common text manipulation task. It ensures proper formatting, which is particularly useful in text processing, string parsing, and user interface applications. Approaches to convert first character uppercase in a sentence Following are the two approaches to converting the first character of a sentence to uppercase − Using String Manipulation and Character Methods Using Regular Expressions with the Matcher() Method Using ... Read More

Java program to accept the strings which contain all vowels

George John
Updated on 25-Jun-2020 14:11:08

464 Views

In order to find that a given string contains all vowels we have to first convert given string into character array so that we can simplify the comparison of each character of given string.After this put each character into a hash map so that we can check whether our map created from given string contain all vowels or not.We took help of hash map here because there is no concrete method in character array class which could check that it contains all vowels or not.Only way to check is to iterate whole array and compare each character with each vowel ... Read More

Writing a CSV file in Java using OpenCSV

Chandu yadav
Updated on 25-Jun-2020 14:11:46

861 Views

In Java there is no delegate library or API in order to write or read comma separated value(csv) file.So a 3rd party APIs are available for such requirements.Most popular 3rd party API is OpenCSV which is provide methods to deal with CSV file such as read csv file, write csv file, parse values of csv file, mapping of csv file values to java beans and java beans to csv file etc.In order to use/import this tool in Java project there are following approaches −Download the binaries/jars from http://sourceforge.net/projects/opencsv/Download through maven by updating pom.xml as    net.sf.opencsv    opencsv    2.3 ... Read More

strictfp Keyword in Java programming

Ankith Reddy
Updated on 25-Jun-2020 14:12:11

285 Views

strictfp is used to ensure that floating points operations give the same result on any platform. As floating points precision may vary from one platform to another. strictfp keyword ensures the consistency across the platforms.strictfp can be applied to class, method or on interfaces but cannot be applied to abstract methods, variable or on constructors. Following are the valid examples of strictfp usage.Examplestrictfp class Test { } strictfp interface Test { } class A {    strictfp void Test() {    } }ExampleFollowing are the invalid strictfp usage.class A {    strictfp float a; } class A {    strictfp abstract void test(); } class A {    strictfp A() {    } }

static Keyword in Java programming

Arjun Thakur
Updated on 25-Jun-2020 14:12:49

384 Views

The Static ModifierStatic VariablesThe static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.Static variables are also known as class variables. Local variables cannot be declared static.Static MethodsThe static keyword is used to create methods that will exist independently of any instances created for the class.Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from ... Read More

Sorting a HashMap according to keys in Java

George John
Updated on 25-Jun-2020 14:13:27

2K+ Views

As we know that Hash map in Java does not maintain insertion order either by key or by order.Also it does not maintain any other order while adding entries to it.But Java provide another API named as TreeMap which maintain its insertion order sorted according to the natural ordering of its keys.So in order to sort our given Hash map according to keys we would insert our map into a tree map which by default insert according to key sorting.After insertion we would transverse same tree map which is sorted and is our resultant sorted map.Example Live Demoimport java.util.HashMap; import java.util.TreeMap; ... Read More

Referencing Subclass objects with Subclass vs Superclass reference

Ankith Reddy
Updated on 25-Jun-2020 14:15:01

3K+ Views

In java inheritance some of the basic rules includes −Object relation of Superclass (parent) to Subclass (child) exists while child to parent object relation never exists.This means that reference of parent class could hold the child object while child reference could not hold the parent object.In case of overriding of non static method the runtime object would evaluate that which method would be executed of subclass or of superclass.While execution of static method depends on the type of reference that object holds.Other basic rule of inheritance is related to static and non static method overriding that static method in java ... Read More

Advertisements