To find the smallest K digit number divisible by X, the Java code is as follows −Example Live Demoimport java.io.*; import java.lang.*; public class Demo{ public static double smallest_k(double x_val, double k_val){ double val = 10; double MIN = Math.pow(val, k_val - 1); if (MIN % x_val == 0) return (MIN); else return ((MIN + x_val) - ((MIN + x_val) % x_val)); } public static void main(String[] args){ double x_val = 76; double k_val ... Read More
The JSON-lib is a Java library for serializing and de-serializing java beans, maps, arrays, and collections in JSON format. We can convert a bean to XML without type hints using the setTypeHintsEnabled() method of XMLSerializer class, this method sets whether JSON types can be included as attributes. We can pass false as an argument to this method to disable the type hints in XML.Syntaxpublic void setTypeHintsEnabled(boolean typeHintsEnabled)Exampleimport net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLNoHintsTest { public static void main(String[] args) { Employee emp = new Employee("Krishna Vamsi", 115, 30, "Java"); JSONObject jsonObj = JSONObject.fromObject(emp); ... Read More
The JsonConfig class can be used to configure the serialization process. We can use the setJsonPropertyFilter() method of JsonConfig to set the property filter when serializing to JSON. We need to implement a custom PropertyFilter class by overriding the apply() method of the PropertyFilter interface. It returns true if the property will be filtered out or false otherwise.Syntaxpublic void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter)Exampleimport net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.PropertyFilter; public class ConvertBeanToJsonExcludeFilterTest { public static void main(String[] args) { Student student = new Student("Sai", "Chaitanya", 20, "Hyderabad"); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new CustomPropertyFilter()); ... Read More
The net.sf.json.xml.XMLSerializer class is a utility class for transforming JSON to XML. When transforming JSONObject instance to XML, this class can add hints for converting back to JSON. We can use the write() method of XMLSerializer class to write a JSON value into an XML string with UTF-8 encoding and it can return a string representation of a well-formed XML document.Syntaxpublic String write(JSON json)Exampleimport net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLTest { public static void main(String[] args) { Student student = new Student("Sai", "Adithya", 25, "Pune"); JSONObject jsonObj = JSONObject.fromObject(student); System.out.println(jsonObj.toString(3)); //pretty print JSON ... Read More
There are multiple ways in which the ‘n’thFibonacci number can be found. Here, we will use dynamic programming technique as well as optimizing the space.Let us see an example −Example Live Demopublic class Demo{ static int fibo(int num){ int first = 0, second = 1, temp; if (num == 0) return first; if (num == 1) return second; for (int i = 2; i
When ‘public’ is used in ‘main’ −Example Live Demopublic class Demo{ public static void main(String args[]){ System.out.println("This is a sample only"); } }OutputThis is a sample onlyA class named Demo contains the main function that is public. It has a print function, which successfully compiles, executes and prints the message on the console.When ‘public’ is replaced with ‘private’Example Live Demopublic class Demo{ private static void main(String args[]){ System.out.println("This is a sample only"); } }OutputError: Main method not found in class Demo, please define the main method as: public static void main(String[] args) ... Read More
To replace null values with default value in Java Map, the code is as follows −Example Live Demoimport java.util.*; import java.util.stream.*; public class Demo{ public static Map null_vals(Map my_map, T def_val){ my_map = my_map.entrySet().stream().map(entry -> { if (entry.getValue() == null) entry.setValue(def_val); return entry; }) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); return my_map; } public static void main(String[] args){ Map my_map = new HashMap(); my_map.put(1, null); my_map.put(2, 56); ... Read More
Static variable: Declared with the help of the keyword ‘static’, they are also known as class variables. They are defined within a constructor or outside a class function. When a variable is static, it is shared between all objects of the class, irrespective of the number of objects that are created.Demonstrating how the ‘static’ keyword, when used with variable works −Example Live Demopublic class Demo{ String name; static String designation; public void display_data(){ System.out.println("The name is: " + name); System.out.println("The designation of this team members is : " + designation); } ... Read More
To find the IP Address of the client, the Java code is as follows −Example Live Demoimport java.net.*; import java.io.*; import java.util.*; import java.net.InetAddress; public class Demo{ public static void main(String args[]) throws Exception{ InetAddress my_localhost = InetAddress.getLocalHost(); System.out.println("The IP Address of client is : " + (my_localhost.getHostAddress()).trim()); String my_system_address = ""; try{ URL my_url = new URL("http://bot.whatismyipaddress.com"); BufferedReader my_br = new BufferedReader(new InputStreamReader(my_url.openStream())); my_system_address = my_br.readLine().trim(); } ... Read More
To find the second most repeated word in a sequence in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{ static String second_repeated(Vector my_seq){ HashMap my_map = new HashMap(my_seq.size()){ @Override public Integer get(Object key){ return containsKey(key) ? super.get(key) : 0; } }; for (int i = 0; i < my_seq.size(); i++) my_map.put(my_seq.get(i), my_map.get(my_seq.get(i))+1); int first_val = Integer.MIN_VALUE; int sec_val ... Read More