Sum of Upper Triangle and Lower Triangle in C++

sudhir sharma
Updated on 17-Aug-2020 09:49:49

1K+ Views

In this problem, we are given a matrix. Our task is to create a program to print the sum of upper triangle and lower triangle.Lower triangleM00                     0             0       …        0 M10                     M11               0       …        0 M20                     M21               M22      …     ... Read More

File Objects in Java

AmitDiwan
Updated on 17-Aug-2020 09:48:42

1K+ Views

The File object represents the actual file/directory on the disk. Here are the list of constructors to create File Object in Java −Sr.No.Method & Description1File(File parent, String child)This constructor creates a new File instance from a parent abstract pathname and a child pathname string.2File(String pathname)This constructor creates a new File instance by converting the given pathname string into an abstract pathname.3File(String parent, String child)This constructor creates a new File instance from a parent pathname string and a child pathname string.4File(URI uri)This constructor creates a new File instance by converting the given file: URI into an abstract pathname.Assuming an object is ... Read More

Precision Handling in Java

AmitDiwan
Updated on 17-Aug-2020 09:46:45

549 Views

Let us see how precisions are handled in Java −Example Live Demoimport java.io.*; import java.lang.*; public class Demo{    public static void main(String[] args){       double my_val = 34.909;       System.out.println("The formatted value of 34.909 is ");       System.out.println(String.format("%.7f", my_val));       double my_val_2 = 12.56;       System.out.println("The formatted value of 12.56 is ");       System.out.println(String.format("%.9f", my_val_2));    } }OutputThe formatted value of 34.909 is 34.9090000 The formatted value of 12.56 is 12.560000000A class named Demo contains the main function, where a double valued integer is declared, and it is ... Read More

Logical Operators on String in Java

AmitDiwan
Updated on 17-Aug-2020 09:45:22

652 Views

Let us implement logical operators on String in Java −Example Live Demoimport java.io.*; public class Demo{    public static void main(String[] args){       int a = 45, b = 32, c = 87, d = 1;       System.out.println("The first variable is " + a);       System.out.println("The second variable is = " + b);       System.out.println("The third variable is = " + c);       if ((a > b) && (b == c)){          d = a + b + c;          System.out.println("The sum is " + ... Read More

Complex Numbers in Java

AmitDiwan
Updated on 17-Aug-2020 09:43:26

7K+ Views

Complex numbers are those that have an imaginary part and a real part associated with it. They can be added and subtracted like regular numbers. The real parts and imaginary parts are respectively added or subtracted or even multiplied and divided.Example Live Demopublic class Demo{    double my_real;    double my_imag;    public Demo(double my_real, double my_imag){       this.my_real = my_real;       this.my_imag = my_imag;    }    public static void main(String[] args){       Demo n1 = new Demo(76.8, 24.0),       n2 = new Demo(65.9, 11.23),       temp;       ... Read More

Check if a String is a Valid Keyword in Java

AmitDiwan
Updated on 17-Aug-2020 09:41:23

1K+ Views

To check if a string is a valid keyword in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    static boolean valid_identifier(String my_str, int n){       if (!((my_str.charAt(0) >= 'a' && my_str.charAt(0) = 'A' && my_str.charAt(1) = 'a' && my_str.charAt(i) = 'A' && my_str.charAt(i) = '0' && my_str.charAt(i)

Sum of Two Numbers Modulo M in C++

sudhir sharma
Updated on 17-Aug-2020 09:40:20

624 Views

In this problem, we are given three numbers a, b, and M. our task is to create a program to find the sum of two numbers modulo M.Let’s take an example to understand the problem, Input: a = 14 , b = 54, m = 7 Output: 5 Explanation: 14 + 54 = 68, 68 % 7 = 5To solve this problem, we will simply add the numbers a and b. And then print the remainder of the sum when divided by M.ExampleProgram to illustrate the working of our solution,  Live Demo#include using namespace std; int moduloSum(int a, int ... Read More

Working with CSV Files in Java

AmitDiwan
Updated on 17-Aug-2020 09:37:10

374 Views

OpenCSV has to be installed first, which is a parser library for Java. The dependency has to be mentioned in the pom.xml file in the maven project. After that, the below code can be utilized.Exampleimport java.io.FileReader; import java.io.*; public class Demo{    public static void readDataLineByLine(String file){       try{          FileReader my_filereader = new FileReader(file);          CSVReader csvReader = new CSVReader(my_filereader);          String[] nextRecord;          while ((nextRecord = csvReader.readNext()) != null){             for (String cell : nextRecord){         ... Read More

Class and Static Variables in Java

AmitDiwan
Updated on 17-Aug-2020 09:34:45

7K+ Views

Class variables are also known as static variables, and they are declared outside a method, with the help of the keyword ‘static’.Static variable is the one that is common to all the instances of the class. A single copy of the variable is shared among all objects.Example Live Demopublic class Demo{    static int my_count=2;    public void increment(){       my_count++;    }    public static void main(String args[]){       Demo obj_1=new Demo();       Demo obj_2=new Demo();       obj_1.increment();       obj_2.increment();       System.out.println("The count of first object is "+obj_1.my_count); ... Read More

Object Graph in Java Serialization

AmitDiwan
Updated on 17-Aug-2020 09:30:43

842 Views

An object graph contains a set of objects that are automatically serialized given that the object that contains the reference is serialized too. Any object that is serialized and contains an object reference, the object reference will be serialized by the JVM.Example Live Demoimport java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class One implements Serializable{    Two s2 = new Two(); } class Two implements Serializable{    Three s3 = new Three(); } class Three implements Serializable{    int i = 34;    int j = 67; } public class Demo_Serialize{    public static void main(String args[]) throws Exception{ ... Read More

Advertisements