0% found this document useful (0 votes)
5 views

java basics1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

java basics1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

OOP Notes

General Java Programming Concepts:

First program:

public class Hello{


public static void main(String[] args){
System.out.println("ADP Class");
}
}
 Type the program in Notepad++.
 Save the file with the name “Hello.java”
 Open command prompt
 Open directory where java file exist
 Write command “javac Hello.java” and press enter.
 “Hello.class” file will be created in that folder.
 Write command “java Hello” and press enter.
 Output will be displayed after the command.

To print output we can use three functions as follows:


public class Hello{
public static void main(String[] args){
System.out.print("ADP Class");
System.out.println("ADP Class");
System.out.printf("ADP Class"); //format specifiers used for variables as c-language.

}
}
 We can also use escape sequences for output.
For example:
System.out.println( "Welcome \n to \n Java \n Programming!" );

Getting Input using Scanner command:


import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
char ch;
String s;
int n;
float f;
System.out.println("Enter a String");
s=input.nextLine();
System.out.println("Enter a Character");
ch= input.next().charAt(0);
System.out.println("Enter an Integer");
n=input.nextInt();
System.out.println("Enter a Float");
f=input.nextFloat();

System.out.println("\n Charater=" + ch);


System.out.println("\n String=" + s );
System.out.println("\n Integer=" + n );
System.out.println("\n Float=" + f );
}
}

// Addition program that displays the sum of two numbers.

import java.util.Scanner;
public class Addition
{
public static void main( String[] args ){
Scanner input = new Scanner( System.in );
int number1; // first number to add
int number2; // second number to add
int sum;
System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.print( "Enter second integer: " );
number2 = input.nextInt();
sum = number1 + number2;
System.out.printf( "Sum is %d\n", sum );
}
}

 Arithmetic operators: + - * / % (for both int and double)


 Relational operator: == != > < >= <=
 Increment/Decrement operators: ++ --
 Assignment operator: =
 The OP= Operator: += -= *= /= %=

Program:
import java.util.Scanner;
public class Product
{
public static void main( String[] args )
{
/ create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
int x;
int y;
int z;
int result;
System.out.print( "Enter first integer: " ); // prompt for input
x = input.nextInt(); // read first integer
System.out.print( "Enter second integer: " ); // prompt for input
y = input.nextInt(); // read second integer
System.out.print( "Enter third integer: " ); // prompt for input
z = input.nextInt(); // read third integer
result = x * y * z;
System.out.printf( "Product is %d\n", result );
}
}

Program to display character and ASCII Code and Type Casting:

public class CharCodeCalcs{


public static void main(String[] args){
char ch1=’A’;
char ch2=(char) (ch1 + 1);
char ch3=ch2;

System.out.println("Character in Sequence" + ch1 + ch2 + (++ch3));


System.out.println("ASCII Codes " +(int) ch1 + (int) ch2 + (int)(++ch3));

}
}

Exercise:
Q. 1: Write a Java program to get temperature from the user in Fahrenheit and print into Celsius using
formula C=5/9(F-32).
Q. 2: Write a Java program which takes radius of circle and prints its area and circumference of circle
using
formula. Area = πR2 Cir = 2πR
Q. 3: Write a Java program which takes a 4-digit number and print the sum, product of its digits.
Q. 4: Write a Java program that input two times in hh:mm:ss format, adds both times and displays total
time.
Q. 5: Write a Java program that input into pounds. Convert and print into kilograms.

Conditional Statements:

If Statement: If-else Statement


Syntax: Syntax:
if(expression){ if(expression){
Statement; statement;
Statement; statement;
….. }
} else
{
Statement;
Statement;
}

Program:

public class EvenOdd{


public static void main(String[] args){
int n;
n= input.nextInt();
if(n%2==0)
System.out.println(n + "is an Even Number");
else
System.out.println(n + "is an Odd Number");

}
}

Nested If:
Program:

public class Compare{


public static void main(String[] args){
int a, b, c;
a= input.nextInt();
b= input.nextInt();
c= input.nextInt();

if(a>b)
if(a>c)
System.out.println(a + "is Greatest”);
else
System.out.println(c + "is Greatest”);
else
if(b>c)
System.out.println(b + "is Greatest”);
else
System.out.println(c + "is Greatest”);
}
}

Program:

import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
char ch;
ch= input.next().charAt(0);

if((ch>='A') && (ch<='Z'))


System.out.println(ch + "s a capital letter");
else if((ch>='a') && (ch<='z'))
System.out.println(ch + "is small letter");
else if((ch>='0') && (ch<='9'))
System.out.println(ch + "is a digit");
else
System.out.println(ch + "is a symbol");

}
}

Conditional Statements:
Syntax:
logical_expression ? expression1 : expression2;

Switch statement:

import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
char op;
int n1,n2;

System.out.println("Enter an Integer");
n1=input.nextInt();

System.out.println("Enter an operator");
op= input.next().charAt(0);

System.out.println("Enter an Integer");
n2=input.nextInt();

switch (op){
case ‘+’:
System.out.println(n1 + "+" + n2 + ”=” + n1+n2 );
break;
case ‘-’:
System.out.println(n1 + "-" + n2 + ”=” + n1-n2 );
break;
case ‘*’:
System.out.println(n1 + "*" + n2 + ”=” + n1*n2 );
break;
case ‘/’:
System.out.println(n1 + "/" + n2 + ”=” + n1/n2 );
break;
case ‘%’:
System.out.println(n1 + "%" + n2 + ”=” + n1%n2 );
break;
default:
System.out.println( "Invalid Operator" );
}

}
}
Exercise:
Q.1: Write a Java program that contains if statement that may be used to compute the area of a square
(area=side*side) or triangle (area=0.5*base*height) after prompting the user to type the first character
of the figure name(S or T).
Q.2: Write a Java program that convert MILITARY time to STANDARD time.
Q.3: Write a Java program which takes three numbers in a, b and c from user. If a is not zero, find out
whether it is common divisor of b and c.
Q. 4: Write a Java program which gets a character and print is it vowel or consonant using switch
statement.
Q. 5: Write a Java program which takes year and print is it leap year or not.

Loops:
While loop:
public class Hello{
public static void main(String[] args){
int n=1;
while(n<=5){
System.out.println("n=" + n);
n++;
}

}
}

Do-while loop:
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
int tab, c;
System.out.println("Enter Table Number");
tab=input.nextInt();
c = 1;
do{
System.out.println(tab + "*" + c + “=” + tab*c );
c++;
}while(c<=10);
}
}

For loop:
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
int n, c , f ;
f = 1;
System.out.println("Enter Number for factorial");
n=input.nextInt();
for( c=1 ; c<=n ; c++)
f = f * c;
System.out.println( "Factorial=" +f );
}
}
Exercise:
Q.1: Write a Java program that takes two numbers and displays the result of first number raise to the
power of second number.
Q.2: Write a Java program that gets a number and print whether it is palindrome or not.
Q.3: Write a Java program which get a number and print whether it is a prime or not.
Q. 4: Write a Java program which takes two numbers and displays the GCD of both numbers.
Q. 5: Write a Java program which displays the sum of following series.
1 + 2x + 3x2 + 4x3 + 5x4
*
Q. 6: Write a Java program which prints the following output.
* *
* *
*
*
*
*
*
Arrays: *
* *
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
int n, i ;
int[] arr; // declare array as name arr
arr = new int[5]; //create array object

for( i = 0 ; i < 5 ; i++){


System.out.println("Enter Number for array");
arr[i]=input.nextInt();
}
for( i = 0 ; i < 5 ; i++)
System.out.println(arr[i] + "\n" );
}
}

Two-dimensional array:
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
int r, c ;
int[][] arr; // declare array as name arr
arr = new int[5][5]; //create array object

for( r = 0 ; r < 5 ; r++){


for( c = 0 ; c < 5 ; c++){
System.out.println("Enter Number for array");
arr[r][c]=input.nextInt();
}
}

for( r = 0 ; r < 5 ; r++){


for( c = 0 ; c < 5 ; c++){
System.out.println(arr[r][c] + "\t");
}
System.out.println( "\n");
}
}
}

Exercise:
Q.1: Write a Java program that takes ten numbers in array. It displays the numbers that are greater than
the average value of the array.
Q.2: Write a Java program that gets ten random numbers in array and displays array after sorting.
Q.3: Write a Java program in which declare a 2-dimensional array of 5 row and 5 columns. Input values
and displays the minimum and maximum value from array.

You might also like