You are on page 1of 10

//Explicit Type Conversion

class Explicit
{
public static void main(String args[])
{
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}

//Automatic Type Conversion

class Auto {

// Main driver method


public static void main(String[] args)
{
float i = 100;

// Automatic type conversion

long l = i;

// Automatic type conversion

int f = i;

// Print and display commands


System.out.println("float value " + i);
System.out.println("Long value " + l);
System.out.println("int value " + f);
}
}

//Conditional Statement

class If {
public static void main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x > y)
System.out.println("x is less than y");
else if(x == y)
System.out.println("x now equal to y");
else if (x < y)
System.out.println("x now greater than y");
else
System.out.println("you won't see this");
}
}

//Switch Statement

class Switch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i)
{
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}

//LOOP

//While Loop

import java.util.*;
class WhileDemo{
public static void main (String[] args){
int n,i=1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:");
n = sc.nextInt();
while(i <= n){
if(i%2==1)
System.out.println(i);
i++;
}
}
}

// Do While Loop
//code will print 1 to 9

public class DoWhileLoopDemo {


public static void main(String[] args) {
int number = 1;
do {
System.out.println(number);
number++;
}while(number < 10) ;
}
}

// For Loop

import java.util.*;
class MyProgram{
public static void main (String[] args){
int i=1;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(i=1; i<=n; i++) {
if(i%2==1)
System.out.println(i);
}
}
}

// Nested Loops
//Print Pattern

*
**
***
****
*****

class PatternDemo{
public static void main(String[] args) {
int n=5;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}

// Math Class

public class MathDemo {


public static void main(String[] args) {
double sinValue = Math.sin(Math.PI / 2);
double cosValue = Math.cos(Math.toRadians(80));
int randomNumber = (int)(Math.random() * 100);
// values in Math class must be given in Radians (not in degree)
System.out.println("sin(90) = " + sinValue);
System.out.println("cos(80) = " + cosValue);
System.out.println("Random = " + randomNumber);
}
}

// One Dimensional Array

import java.util.*;
class ArrayDemo1{
public static void main (String[] args){
int i, n;
int[] a=new int[5];
Scanner sc = new Scanner(System.in);
System.out.print("enter Array Length:");
n = sc.nextInt();
for(i=0; i<n; i++) {
System.out.print("enter a["+i+"]:");
a[i] = sc.nextInt();
}
for(i=0; i<n; i++)
System.out.println(a[i]);
}
}

// Multi Dimensional Array

import java.util.*;
class Array2Demo{
public static void main(String[] args) {
int size;
int a[][],b[][],c[][];
Scanner sc=new Scanner(System.in);
System.out.print("Enter size of an array:");
size=sc.nextInt();
a=new int[size][size];
System.out.println("Enter array elements:");
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){
System.out.print("Enter a["+i+"]["+j+"]:");
a[i][j]=sc.nextInt();
}
}
b=new int[size][size];
for(int i=0;i<b.length;i++){
for(int j=0;j<b.length;j++){
System.out.print("Enter b["+i+"]["+j+"]:");
b[i][j]=sc.nextInt();
}
}
c=new int[size][size];
for(int i=0;i<c.length;i++){
for(int j=0;j<c.length;j++){
System.out.print("c["+i+"]["+j+"]:"+(a[i][j]+b[i][j])+"\t");
}
System.out.println();
}
}
}

// Function

class MethodDemo{
public static void main(String[] args) {
int a=10,b=20,c;
MethodDemo md=new MethodDemo();
c=md.add(a,b);
System.out.println("a+b="+c);
}
int add(int i, int j){
return i+j;
}
}

// Method Overloading

class Addition{
int i,j,k;
void add(int a){
i=a;
System.out.println("add i="+i);
}
void add(int a,int b){\\overloaded add()
i=a;
j=b;
System.out.println("add i+j="+(i+j));
}
void add(int a,int b,int c){\\overloaded add()
i=a;
j=b;
k=c;
System.out.println("add i+j+k="+(i+j+k));
}
}
class OverloadDemo{
public static void main(String[] args){
Addition a1= new Addition();
//call all versions of add()
a1.add(20);
a1.add(30,50);
a1.add(10,30,60);
}
}

//Class and Objects

class MyProgram {
public static void main(String[] args) {
Person p1= new Person();
Person p2= new Person();
p1.name="abc";
p1.age=71;
p2.name="xyz";
p2.age=80;
System.out.println("p1.name="+p1.name);
System.out.println("p2.name="+p2.name);
System.out.println("p1.age="+p1.age);
System.out.println("p2.age="+p2.age);
}
}
class Person
{
String name;
int age;
}

//Static

class StaticDemo {
static int a = 4;
//static variable declared & initialized
static int b;
//static variable declared
static void dispValue(int x) {
System.out.println("Static method initialized.");
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b= a * 5;
}
public static void main(String args[]) {
System.out.println("inside main()...");
dispValue(44);
}
}

Output:
Static block initialized.
inside main()...
Static method initialized.
x = 44
a = 4
b = 20

//Inheritance

class A{
protected int i;
int j;
void showij(){
System.out.println("i="+i+" j="+j);
}
}
class B extends A{ //inheritance
int k;
void showk(){
System.out.println("k="+k);
}
void add(){
System.out.println("i+j+k="+(i+j+k));
}
}
class InheritanceDemo{
public static void main(String[] args)
{
A superObjA= new A();
superObjA.i=10;
superObjA.j=20;
B subObjB= new B();
subObjB.k=30;
superObjA.showij();
subObjB.showk();
subObjB.add();
}
}

Output:
i=10 j=20
k=30
i+j+k=30

//Super Keyword
//Using super to call superclass constructors

class Cube{
protected
double height,width,depth;
Cube(double h,double w,double d){
System.out.println("Constructor: CUBE");
height=h;
width=w;
depth=d;
}
double volume(){
return height*width*depth;
}
}
class CubeWeight extends Cube{
double weigth;
CubeWeight(double h,double w,double d, double m){
super(h,w,d); //call superclassConstructor
System.out.println("Constructor:CUBEWEIGTH");
weigth=m;
}
}
class CubeInheritSuper{
public static void main(String[] args) {
CubeWeight cw1= new
CubeWeight(10,10,10,20.5);
CubeWeight cw2= new
CubeWeight(100,100,100,200.5);
System.out.println("cw1.volume()="+cw1.volume());
System.out.println("cw1.weigth="+cw1.weigth);
System.out.println("cw2.volume()="+cw2.volume());
System.out.println("cw2.weigth="+cw2.weigth);
}
}

Output:
Constructor:CUBE
Constructor:CUBEWEIGTH
Constructor:CUBE
Constructor:CUBEWEIGTH
cw1.volume()=1000.0
cw1.weigth=20.5
cw2.volume()=1000000.0
cw2.weigth=200.5

//Using super to access members

class A{
}
class B extends A{
int i,k;
B(int a,int b){
this.i=b;
}
void show(){
System.out.println("super.i="+super.i);
System.out.println("this.i="+this.i);
}
}
class SuperMemberDemo{
public static void main(String[] args)
{
B b= new B(12,56);
b.show();
}
}

Output:
super.i=12
this.i=56

//Polymorphism
//Method Overloading

class Addition{
int i,j,k;
void add(int a){
i=a;
System.out.println("add i="+i);
}
void add(int a,int b){\\overloaded add()
i=a;
j=b;
System.out.println("add i+j="+(i+j));
}
void add(int a,int b,int c){\\overloaded add()
i=a;
j=b;
k=c;
System.out.println("add i+j+k="+(i+j+k));
}
}
class OverloadDemo{
public static void main(String[] args){
Addition a1= new Addition();
//call all versions of add()
a1.add(20);
a1.add(30,50);
a1.add(10,30,60);
}
}

Output:
add i=20
add i+j=80
add i+j+k=100

//Method Overriding

class Shape{
void draw(){
System.out.println("Draw Shape");
}
}
class Circle extends Shape{
void draw(){
System.out.println("Draw Circle");
}
}
class Square extends Shape{
void draw(){
System.out.println("Draw Square");
}
}
class OverrideDemo{
public static void main(String[] args) {
Circle c= new Circle();
c.draw(); //child class meth()
Square sq= new Square();
sq.draw();//child class meth()
Shape sh= new Shape();
sh.draw();//parentClass meth()
}
}

Output:
Draw Circle
Draw Square
Draw Shape

//Interface

interface StackIntf{
public
void push(int p);
public int pop();
}
class CreateStack implements StackIntf{
int mystack[];
int tos;
CreateStack(int size){
mystack= new int[size];
tos=-1;
}
public void push(int p){
if(tos==mystack.length-1){
System.out.println("StackOverflow");
}
else{
mystack[++tos]=p;
}
}
public int pop(){
if(tos<0){
System.out.println("StackUnderflow");
return 0;
}
else
return mystack[tos--];
}
}
class StackDemo{
public static void main(String[] args) {
CreateStack cs1= new CreateStack(5);
CreateStack cs2= new CreateStack(8);
for(int i=0;i<5;i++)
cs1.push(i);
for(int i=0;i<8;i++)
cs2.push(i);
System.out.println("MyStack1=");
for(int i=0;i<5;i++)
System.out.println(cs1.pop());
System.out.println("MyStack2=");
for(int i=0;i<8;i++)
System.out.println(cs2.pop());
}
}

You might also like