1)
package com.saavn;
import java.util.Scanner;
public class Book {
int bookPrice;
String bookName;
void bookTitle()
{
System.out.print("enter book titile: ");
Scanner scanner=new Scanner(System.in);
bookName=scanner.next();
System.out.print("enter the price of the book: ");
Scanner scan=new Scanner(System.in);
bookPrice=scan.nextInt();
}
void display()
{
System.out.println("Name of the book is "+bookName +" and price is : "+bookPrice);
}
public static void main(String[] args) {
Book a=new Book();
a.bookTitle();
a.display();
}
}
===========================================================
public class Calculations {
public static void main(String args[])
{
int a,b;
char ch;
System.out.println("1. Addition 2.subtraction 3.multiplication ");
System.out.print("enter your choice: ");
Scanner scan = new Scanner(System.in);
ch= scan.next().charAt(0);
//System.out.println("char is :"+ch);
System.out.println("enter a & b: ");
a=scan.nextInt();
b=scan.nextInt();
//System.out.println("a & b is : "+a +" ,"+b);
switch(ch)
{
case '1':
System.out.println("Addition of "+a +"& "+b +"is :"+(a+b));
break;
case '2':
System.out.println("Subtraction of "+a +"& "+b +"is :"+(a-b));
break;
case '3':
System.out.println("Multiplication of "+a +"& "+b +"is :"+(a*b));
break;
default:
System.out.println("Invalid choice");
}
}
}
==============================================================
public class Array {
public static void main(String[] args) {
System.out.print("enter length of array: ");
Scanner scan = new Scanner(System.in);
int x= scan.nextInt();
int[] arr = new int[x];
for(int i=0;i<x;i++)
{
System.out.print("enter arr[ "+i+"] :");
int y=scan.nextInt();
arr[i]=y;
}
System.out.print("The array is : {");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+",");
}
System.out.print("}");
}
}
package com.google;
class Priv
{
int a ;
public int b;
private int c;
void setc(int x)
{
c=x;
}
int getc(){
return c;
}
}
class Test {
public static void main(String[] args) {
Priv obj=new Priv();
obj.a=2;
obj.b=3;
obj.setc(4);
System.out.println("a & b : "+obj.a+" ,"+obj.b);
System.out.println("c : "+obj.getc());
}
}
class sample
}
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
public class Calculations {
public static void main(String args[])
{
int a,b;
char ch;
System.out.println("1. Addition 2.subtraction 3.multiplication ");
System.out.print("enter your choice: ");
Scanner scan = new Scanner(System.in);
ch= scan.next().charAt(0);
//System.out.println("char is :"+ch);
System.out.println("enter a & b: ");
a=scan.nextInt();
b=scan.nextInt();
//System.out.println("a & b is : "+a +" ,"+b);
switch(ch)
{
case '1':
System.out.println("Addition of "+a +"& "+b +"is :"+(a+b));
break;
case '2':
System.out.println("Subtraction of "+a +"& "+b +"is :"+(a-b));
break;
case '3':
System.out.println("Multiplication of "+a +"& "+b +"is :"+(a*b));
break;
default:
System.out.println("Invalid choice");
}
}
}
==============================================================
public class Array {
public static void main(String[] args) {
System.out.print("enter length of array: ");
Scanner scan = new Scanner(System.in);
int x= scan.nextInt();
int[] arr = new int[x];
for(int i=0;i<x;i++)
{
System.out.print("enter arr[ "+i+"] :");
int y=scan.nextInt();
arr[i]=y;
}
System.out.print("The array is : {");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+",");
}
System.out.print("}");
}
}
==================================================================
package com.saavn;
import java.util.Scanner;
class Squ
{
int squaring(int i)
{
return i*i;
}
}
class Square {
public static void main(String args[])
{
int x;
System.out.println("enter x value");
Scanner scan=new Scanner(System.in);
x=scan.nextInt();
Squ s1=new Squ();
System.out.println("the square of no is : " + s1.squaring(x));
}
}
======================================================================
Call by Ref
package com.google;
class Change
{
int a,b;
Change(int a,int b)
{
this.a=a;
this.b=b;
}
void method(Change obj1)
{
obj1.a=1;
obj1.b=2;
}
void display()
{
System.out.println("a & b are : "+a +" , "+b);
}
}
class CallByRef {
public static void main(String[] args) {
Change obj=new Change(3,5);
obj.display();
obj.method(obj);
obj.display();
}
}
====================================================================
class Priv
{
int a ;
public int b;
private int c;
void setc(int x)
{
c=x;
}
int getc(){
return c;
}
}
class Test {
public static void main(String[] args) {
Priv obj=new Priv();
obj.a=2;
obj.b=3;
obj.setc(4);
System.out.println("a & b : "+obj.a+" ,"+obj.b);
System.out.println("c : "+obj.getc());
}
}
===========================================================
package com.google;
class sample
{
static int m=4;
}
class TestStatic {
static int a=3;
static int b;
static
{
b=4;
}
static void method(int x)
{
System.out.println("x : "+x);
}
public static void main(String args[])
{
System.out.println("a& b : "+a+" ,"+b);
method(6);
System.out.println("m : "+sample.m);
/*Outside of the class in which they are defined, static methods and variables can be used
independently of any object. To do so, you need only specify the name of their class followed
by the dot operator.*/
}
}
===========================================================
package com.sree;
class Sample
{
static int a=3;
static void display()
{
System.out.println("a : "+a);
}
}
class StaticDemo{
public static void main(String args[])
{
System.out.println("a in sample class: "+Sample.a);
System.out.println("static method :");
Sample.display();
}
}
class Sample
{
static int a=3;
static void display()
{
System.out.println("a : "+a);
}
}
class StaticDemo{
public static void main(String args[])
{
System.out.println("a in sample class: "+Sample.a);
System.out.println("static method :");
Sample.display();
}
}
===========================================================
Passing Command line Arguments in Eclipse
=====================================================================
package com.google;
class Command {
public static void main(String args[])
{
System.out.println("in main");
for(int i=0;i<args.length;i++)
System.out.println("arg["+i+"] : "+args[i]);
}
}
- Click on Run -> Run Configurations
- Click on Arguments tab
- In Program Arguments section , Enter your arguments.
- Click Apply
=====================================================================
package com.google;
class Command {
public static void main(String args[])
{
System.out.println("in main");
for(int i=0;i<args.length;i++)
System.out.println("arg["+i+"] : "+args[i]);
}
}
===========================================================
package com.google;
class VarArgs {
static void Test(int arr[])
{
System.out.print("no of argments: "+arr.length+" contents are: ");
for(int x: arr)
{
System.out.print(x+ " ");
}
System.out.println();
}
public static void main(String args[])
{
int a[]={1};
int b[]={2,3};
int c[]={};
Test(a);
Test(b);
Test(c);
}
}
class VarArgs {
static void Test(int arr[])
{
System.out.print("no of argments: "+arr.length+" contents are: ");
for(int x: arr)
{
System.out.print(x+ " ");
}
System.out.println();
}
public static void main(String args[])
{
int a[]={1};
int b[]={2,3};
int c[]={};
Test(a);
Test(b);
Test(c);
}
}
------------------------------------------------------------------------------------------------------------------------------------
package com.google;
class VarArgs {
static void Test(int ... arr)
{
System.out.print("no of argments: "+arr.length+" contents are: ");
for(int x: arr)
{
System.out.print(x+ " ");
}
System.out.println();
}
public static void main(String args[])
{
Test(1);
Test(2,3);
Test();
}
}
===========================================================
package com.google;
class VarArgs {
static void Test(String msg,int ... arr)
{
System.out.print(msg +" :");
System.out.print("no of argments: "+arr.length+" contents are: ");
for(int x: arr)
{
System.out.print(x+ " ");
}
System.out.println();
}
public static void main(String args[])
{
Test("one",1);
Test("two three",2,3);
Test("no varags");
}
}
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
This keyword
- this keyword can only be the first statement in Constructor.
- A constructor can have either this or super keyword but not both.
========================================================
No comments:
Post a Comment