top of page

Margins

 

{
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}

Java Program to Add two Numbers

First Example: Sum of two numbers

 

public class AddTwoNumbers {

 

   public static void main(String[] args) {

       

      int num1 = 5, num2 = 15, sum;

      sum = num1 + num2;

 

      System.out.println("Sum of these numbers: "+sum);

   }

}

Output:

 

Sum of these numbers: 20

Second Example: Sum of two numbers using Scanner

The scanner allows us to capture the user input so that we can get the values of both the numbers from user. The program then calculates the sum and displays it.

 

import java.util.Scanner;

public class AddTwoNumbers2 {

 

    public static void main(String[] args) {

       

        int num1, num2, sum;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter First Number: ");

        num1 = sc.nextInt();

       

        System.out.println("Enter Second Number: ");

        num2 = sc.nextInt();

       

        sc.close();

        sum = num1 + num2;

        System.out.println("Sum of these numbers: "+sum);

    }

}

 

Output:

Enter First Number:

121

Enter Second Number:

19

Sum of these numbers: 140

Adding two complex numbers in Java

In this program we have a class ComplexNumber. In this class we have two instance variables real and img to hold the real and imaginary parts of complex numbers.

 

We have declared a method sum() to add the two numbers by adding their real and imaginary parts together.

 

The constructor of this class is used for initializing the complex numbers. For e.g. when we create an instance of this class like this ComplexNumber temp = new ComplexNumber(0, 0);, it actually creates a complex number 0 + 0i.

 

public class ComplexNumber{

   //for real and imaginary parts of complex numbers

   double real, img;

               

   //constructor to initialize the complex number

   ComplexNumber(double r, double i){

                this.real = r;

                this.img = i;

   }

Basic Date Formatting

 

package com.ack.j2se.date;

 

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

 

public class BasicDateFormatting {

  public static void main( String[] args ) throws Exception {

    // get today's date

    Date today = Calendar.getInstance().getTime();

 

    // create a short version date formatter

    DateFormat shortFormatter

        = SimpleDateFormat.getDateInstance( SimpleDateFormat.SHORT );

 

    // create a long version date formatter

    DateFormat longFormatter

        = SimpleDateFormat.getDateInstance( SimpleDateFormat.LONG );

 

    // create date time formatter, medium for day, long for time

    DateFormat mediumFormatter

        = SimpleDateFormat.getDateTimeInstance( SimpleDateFormat.MEDIUM,

                                                SimpleDateFormat.LONG );

 

    // use the formatters to output the dates

    System.out.println( shortFormatter.format( today ) );

    System.out.println( longFormatter.format( today ) );

    System.out.println( mediumFormatter.format( today ) );

 

    // convert form date -> text, and text -> date

    String dateAsText = shortFormatter.format( today );

    Date textAsDate = shortFormatter.parse( dateAsText );

    System.out.println( textAsDate );

  }

}

bottom of page