Convert Hours into Minutes and Seconds in C++

Sunidhi Bansal
Updated on 09-Jul-2020 08:23:37

1K+ Views

Given with the input as hours and the task is to convert the number of hours into minutes and seconds and display the corresponding resultFormula used for converting the hours into minutes and seconds is −1 hour = 60 minutes    Minutes = hours * 60 1 hour = 3600 seconds    Seconds = hours * 3600ExampleInput-: hours = 3 Output-: 3 hours in minutes are 180    3 hours in seconds are 10800 Input-: hours = 5 Output-: 5 hours in minutes are 300    5 hours in seconds are 18000Approach used in the below program is as follows ... Read More

C++ Program for Triangular Pattern Mirror Image Around 0

Sunidhi Bansal
Updated on 09-Jul-2020 08:23:06

335 Views

Given with the positive value n and the task is to generate the triangular pattern i.e. mirror image of the printed numbers and display the resultExampleInput-: n = 6 Output-:Input-: n = 3 Output-:Approach used in the below program is as follows −Input the value of n as a positive integerTraverse one loop i for the number of rows in a pattern i.e. nTraverse one loop j for the number of spaces in a patternTraverse another loop for the digits in a patternAlgorithmSTART Step 1-> declare function to print mirror image of triangular pattern    void print_mirror(int n)    declare ... Read More

Find Sum of Odd Factors of a Number in C++

Sunidhi Bansal
Updated on 09-Jul-2020 08:22:32

558 Views

Given with a positive integer and the task is to generate the odd factors of a number and finding out the sum of given odd factors.ExampleInput-: number = 20 Output-: sum of odd factors is: 6 Input-: number = 18 Output-: sum of odd factors is: 13So, result = 1 + 5 = 6Approach used in the below program is as follows −Input the number for calculating the sum of odd factors of that numberIgnore the digit 0 and 2 because both are even digits and store the digit 1 because it is an odd digitStart the loop from 3 ... Read More

Use @SerializedName Annotation with Gson in Java

raja
Updated on 09-Jul-2020 08:17:53

8K+ Views

The @SerializedName annotation can be used to serialize a field with a different name instead of an actual field name. We can provide the expected serialized name as an annotation attribute, Gson can make sure to read or write a field with the provided name.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, METHOD}) public @interface SerializedNameExampleimport com.google.gson.*; import com.google.gson.annotations.*; public class SerializedNameTest {    public static void main(String args[]) {       Gson gson = new GsonBuilder().setPrettyPrinting().create();       Person person = new Person(115, "Raja Ramesh", "Hyderabad");       String jsonStr = gson.toJson(person);       System.out.println(jsonStr);    } } // Person class class ... Read More

Implement Custom JsonAdapter Using Gson in Java

raja
Updated on 09-Jul-2020 08:15:08

3K+ Views

The @JsonAdapter annotation can be used at field or class level to specify the Gson. The TypeAdapter class can be used to convert Java objects to and from JSON. By default, Gson library converts application classes to JSON by using built-in type adapters but we can override it by providing custom type adapters.Syntax@Retention(value=RUNTIME) @Target(value={TYPE, FIELD}) public @interface JsonAdapterExampleimport java.io.IOException; import com.google.gson.Gson; import com.google.gson.TypeAdapter; import com.google.gson.annotations.JsonAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; public class JsonAdapterTest {    public static void main(String[] args) {       Gson gson = new Gson();       System.out.println(gson.toJson(new Customer()));    } } // Customer class class Customer { ... Read More

Convert JSONNode to ArrayNode using Jackson API in Java

raja
Updated on 09-Jul-2020 07:59:35

18K+ Views

A JsonNode is a base class for all JSON nodes that forms the JSON Tree Model whereas ArrayNode is a node class that represents an array mapped from JSON content. We can convert or translate JsonNode to ArrayNode by typecasting the ArrayNode to retrieve the values using the readTree() method of ObjectMapper class and get() method for accessing the value of a specified element of an array node.Syntaxpublic JsonNode readTree(String content) throws IOException, com.fasterxml.jackson.core.JsonProcessingExceptionExampleimport com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.core.JsonProcessingException; public class JSonNodeToArrayNodeTest {    public static void main(String args[]) throws JsonProcessingException {       String jsonStr = "{\"Technologies\" : [\"Java\", ... Read More

Search Value Inside a JSON File Using Jackson in Java

raja
Updated on 09-Jul-2020 07:52:12

3K+ Views

The com.fasterxml.jackson.databind.node.ObjectNode class can be used to map the JSON object structure in Json content. We can search for a particular value inside the JSON file using the get() method of ObjectNode class, this method used for accessing the value of a specified field of an object node.Syntaxpublic JsonNode get(String fieldName)Exampleimport com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; public class ObjectNodeTest {    public static void main(String args[]) throws Exception {       String jsonString = "{\"Id\":101, \"name\":\"Raja Ramesh\", \"address\":\"Madhapur\"}";       ObjectMapper mapper = new ObjectMapper();       ObjectNode node = mapper.readValue(jsonString, ObjectNode.class);       if(node.has("name")) {       ... Read More

When to Use @ConstructorProperties Annotation with Jackson in Java

Aishwarya Naglot
Updated on 09-Jul-2020 07:19:56

2K+ Views

Jackson is a library that is used for converting Java objects to JSON and vice versa. It provides various annotations to customize the serialization and deserialization process. One such annotation is @ConstructorProperties, which is used to specify the properties of a constructor for deserialization. The @ConstructorProperties annotation is from java.beans package, used to deserialize JSON to a Java object via the annotated constructor. If we use this annotation rather than annotating each parameter in the constructor, we can provide an array with the property names for each of the constructor parameters. Let's learn when to use the @ConstructorProperties annotation with ... Read More

Ways to Read Input from Console in Java

AmitDiwan
Updated on 09-Jul-2020 06:50:11

5K+ Views

Let us see some ways to read input from console in Java −Exampleimport java.util.Scanner; public class Demo{    public static void main(String args[]){       Scanner my_scan = new Scanner(System.in);       String my_str = my_scan.nextLine();       System.out.println("The string is "+my_str);       int my_val = my_scan.nextInt();       System.out.println("The integer is "+my_val);       float my_float = my_scan.nextFloat();       System.out.println("The float value is "+my_float);    } }OutputThe string is Joe The integer is 56 The float value is 78.99A class named Demo contains the main function. An instance of the ... Read More

Using Variables in JShell of Java 9

AmitDiwan
Updated on 09-Jul-2020 06:47:43

501 Views

In JShell 9, variables can be declared during a session. Once the user has logged into the session, they can declare a variable as follows −jshell> int val = 56 ;Italics indicate the terminal, once the user has logged in to their session.The above line would print the below output. The semicolon in the above line is optional, and it will run fine without the semicolon also.Outputval = = > 56When an integer value is defined by assigning it to a variable name on the JShell, and it is executed, by pressing ‘Enter’ key, it is displayed on the next ... Read More

Advertisements