SIMEPLE IF...ELSE , SWITCH ETC...
1.) Print Hello World!
class HW {
public static void main(String args[]){
System.out.println("Hello World!");
}
}
OUTPUT :-
Hello World!
---------------------------------------------------------------------------------------------------------------------------------------
2.) Print the value of the variable.
class valueOfVariable {
public static void main(String args[]){
int a=10;
float f = 1.5f;
System.out.println(a);
System.out.println(f);
}
}
OUTPUT :-
10
1.5
---------------------------------------------------------------------------------------------------------------------------------------
3.) Print addition/sum, Subtraction, Multiplication, Division of two numbers.
class AO{
public static void main(String args[]){
int a=20, b=5;
int sum = a+b;
int sub = a-b;
int mul = a*b;
int div = a/b;
System.out.println("The addition is : " + sum);
System.out.println("The subtraction is : "+ sub);
System.out.println("The Multiplication is : "+mul);
System.out.println("The Division is : "+div);
}
}
OUTPUT :-
The addition is : 25
The subtraction is : 15
The Multiplication is : 100
The Division is : 4
---------------------------------------------------------------------------------------------------------------------------------------
4.) Print a number from 1 to 10 which is divisible by 2.
class AO{
public static void main(String args[]){
for(int i=1;i<=10;i++){
if(i%2==0){
System.out.print(i+" ");
}
}
}
}
OUTPUT :-
2 4 6 8 10
---------------------------------------------------------------------------------------------------------------------------------------
5.) Take two values and print maximum number.
class max{
public static void main(String args[]){
int i=10, j=20;
if(i>j){
System.out.println("The Maximum number is : "+i);
} else {
System.out.println("The Maximum number is : "+j);
}
}
}
OUTPUT :-
The Maximum number is : 20
---------------------------------------------------------------------------------------------------------------------------------------
6.) Enter employee basic salary and add the da which experience greater than 3 year.
class emp{
public static void main(String args[]){
int basicsalary = 10000, year = 5, da = 2000;
if(year > 3){
System.out.println("Salary is : "+ (basicsalary+da));
} else {
System.out.println("Salary is : "+basicsalary);
}
}
}
OUTPUT :-
Salary is : 12000
---------------------------------------------------------------------------------------------------------------------------------------
7.) Print given number is odd or even using ternary operator.
class ternary{
public static void main(String args[]){
int n=8;
String str = (n%2==0)?"Even":"ODD";
System.out.println("The Number is : "+str);
}
}
OUTPUT :-
The Number is : Even
---------------------------------------------------------------------------------------------------------------------------------------
8.) Print the given number is positive ,negative or zero using if else.
class ifelse{
public static void main(String args[]){
int n=8;
if(n>0){
System.out.println("Positive");
} else if(n<0){
System.out.println("Negative");
} else {
System.out.println("Zero");
}
}
}
OUTPUT :-
Positive
---------------------------------------------------------------------------------------------------------------------------------------
9.) Enter age and check you are eligible for driving or not or require parents permission.
class ifelse{
public static void main(String args[]){
int age=19;
if(age>18){
System.out.println("You are Eligible");
} else if(age<=18 && age>16){
System.out.println("Require parents permission");
} else {
System.out.println("You are not eligible");
}
}
}
OUTPUT :-
You are Eligible
---------------------------------------------------------------------------------------------------------------------------------------
10.) Switch case print day.
class Switch {
public static void main(String args[]){
int i=1;
switch(i) {
case 1:
System.out.println("Monday");
break;
case 2 :
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day number");
}
}
}
OUTPUT :-
Monday
---------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
MORE IF...ELSE SWITCH PROGRAM FOR PRACTICE
LOOPS
1.) Print 0 to 4 using for loop.
class forloop {
public static void main(String args[]){
for(int i=0;i<=4;i++){
System.out.print(i+" ");
}
}
}
OUTPUT :-
0 1 2 3 4
---------------------------------------------------------------------------------------------------------------------------------------
2.) Print 4 to 0 using for loop.
class forloop {
public static void main(String args[]){
for(int i=4;i>=0;i--){
System.out.print(i+" ");
}
}
}
OUTPUT :-
4 3 2 1 0
---------------------------------------------------------------------------------------------------------------------------------------
3.) Patttern
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
class pattern {
public static void main(String args[]){
for(int i=4;i>=0;i--){
for(int j=0;j<=i;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT :-
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
---------------------------------------------------------------------------------------------------------------------------------------
4.) Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
class pattern {
public static void main(String args[]){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT :-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
---------------------------------------------------------------------------------------------------------------------------------------
5.) Pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
class pattern {
public static void main(String args[]){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(i+" ");
}
System.out.println();
}
}
}
OUTPUT :-
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
---------------------------------------------------------------------------------------------------------------------------------------
6.) Pattern
1
1 2
1 2 1
1 2 1 2
1 2 1 2 1
class pattern {
public static void main(String args[]){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
if(j%2==0){
System.out.print(2+" ");
} else {
System.out.print(1+" ");
}
}
System.out.println();
}
}
}
OUTPUT :-
1
1 2
1 2 1
1 2 1 2
1 2 1 2 1
---------------------------------------------------------------------------------------------------------------------------------------
7.) Pattern
a
a b
a b c
a b c d
class pattern {
public static void main(String args[]){
for(int i=97;i<=100;i++){
for(int j=97;j<=i;j++){
System.out.print((char)j+" ");
}
System.out.println();
}
}
}
OUTPUT :-
a
a b
a b c
a b c d
---------------------------------------------------------------------------------------------------------------------------------------
8.) Pattern
a b c d
a b c
a b
a
class pattern {
public static void main(String args[]){
for(int i=100;i>=97;i--){
for(int j=97;j<=i;j++){
System.out.print((char)j+" ");
}
System.out.println();
}
}
}
OUTPUT :-
a b c d
a b c
a b
a
---------------------------------------------------------------------------------------------------------------------------------------
9.) While loop print o to n.
class whileloop {
public static void main(String args[]) {
int i=0,n=10;
while(i<=n){
System.out.print(i+" ");
i++;
}
}
}
OUTPUT :-
0 1 2 3 4 5 6 7 8 9 10
---------------------------------------------------------------------------------------------------------------------------------------
10.) Nested While Loop
class nestedwhileloop {
public static void main(String args[]) {
int i=0, j=5;
while(i<=5){
while(i<=j){
System.out.print("\t"+j);
j--;
}
System.out.print("\n"+i);
i++;
}
}
}
OUTPUT :-
5 4 3 2 1 0
0
1
2
3
4
5
---------------------------------------------------------------------------------------------------------------------------------------
11.) do...while loop
class dowhileloop {
public static void main(String args[]) {
int i=0;
do{
System.out.print(i+" ");
i++;
} while(i<=10);
}
}
OUTPUT :-
0 1 2 3 4 5 6 7 8 9 10
---------------------------------------------------------------------------------------------------------------------------------------
12.) Pattern using while loop
1
12
123
1234
12345
1234
123
12
1
class whileloop {
public static void main(String args[]) {
int i=1;
while(i<=5) {
int j=1;
while(j<=i) {
System.out.print(j+" ");
j++;
}
System.out.println();
i++;
}
int a=4;
while(a>=1){
int b=1;
while(b<=a) {
System.out.print(b+" ");
b++;
}
System.out.println();
a--;
}
}
}
OUTPUT :-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
---------------------------------------------------------------------------------------------------------------------------------------
13.) Pattern using while loop
1
1 2
1 2 3
1 2 3 4 5
5 4 3 2
5 4 3
5 4
5
class whileloop {
public static void main(String args[]) {
int i=1;
while(i<=5) {
int j=1;
while(j<=i) {
System.out.print(j+" ");
j++;
}
System.out.println();
i++;
}
int a=2;
while(a<=5){
int b=5;
while(b>=a) {
System.out.print(b+" ");
b--;
}
System.out.println();
a++;
}
}
}
OUTPUT :-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2
5 4 3
5 4
5
---------------------------------------------------------------------------------------------------------------------------------------
14.) Pattern
1
1 2
2 3 4
4 5 6 7
7 8 9 10 11
public class pattern {
public static void main(String[] args) {
int k=1, l;
for (int i = 1; i <= 5; i++) {
for (l = k; l < k+i; l++) {
System.out.print(l+" ");
}
k=l-1;
System.out.println();
}
}
}
OUTPUT :-
1
1 2
2 3 4
4 5 6 7
7 8 9 10 11
---------------------------------------------------------------------------------------------------------------------------------------
15.) Pattern
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
public class pattern {
public static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
16.) Pattern
1 0 1 0 1
0 1 0 1
1 0 1
0 1
1
public class pattern {
public static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
for (int j = i; j >= 1; j--) {
if(j%2==0) {
System.out.print(0+" ");
} else {
System.out.print(1+" ");
}
}
System.out.println();
}
}
}
OUTPUT :-
1 0 1 0 1
0 1 0 1
1 0 1
0 1
1
---------------------------------------------------------------------------------------------------------------------------------------
17.) Pattern
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
class pattern {
public static void main(String[] args) {
int j;
for(int i=1;i<=5;i++) {
for(j=5;j>=i;j--) {
System.out.print(" ");
}
for(j=i;j>=1;j--) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT :-
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
---------------------------------------------------------------------------------------------------------------------------------------
18.) Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
public class pattern {
public static void main(String[] args) {
int j;
for (int i = 1; i <= 5; i++) {
for(j=5;j>=i;j--){
System.out.print(" ");
}
for(j=1;j<=i;j++) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT :-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
---------------------------------------------------------------------------------------------------------------------------------------
19.) Pattern
*
* *
* * *
* * * *
* * * * *
public class pattern {
public static void main(String[] args) {
int j;
for (int i = 1; i <= 5; i++) {
for(j=5;j>=i;j--) {
System.out.print(" ");
}
for(j=1;j<=i;j++) {
System.out.print("*"+" ");
}
System.out.println();
}
}
}
20.) Pattern
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
class pattern {
public static void main(String[] args) {
int j;
for (int i =1; i <= 5; i++) {
for(j=5;j>=i;j--) {
System.out.print(" ");
}
for(j=1;j<=i;j++) {
System.out.print(j+" ");
}
for(j=i-1;j>=1;j--) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT :-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
---------------------------------------------------------------------------------------------------------------------------------------
21.) Pattern
1 2 3 4 5 4 3 2 1
2 3 4 5 4 3 2
3 4 5 4 3
4 5 4
5
public class pattern {
public static void main(String[] args) {
int j;
for(int i=1;i<=5;i++){
for(j=1;j<=i;j++) {
System.out.print(" ");
}
for(j=i;j<=5;j++) {
System.out.print(j+" ");
}
for(j=5-1;j>=i;j--) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT :-
1 2 3 4 5 4 3 2 1
2 3 4 5 4 3 2
3 4 5 4 3
4 5 4
5
---------------------------------------------------------------------------------------------------------------------------------------
22.) Pattern
A B C D E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
public class pattern {
public static void main(String[] args) {
int j;
for(int i=69;i>=65;i--){
for(j=65;j<=i;j++) {
System.out.print((char)j+" ");
}
for(j=69;j>=i;j--){
System.out.print(" ");
}
System.out.print("\b\b\b\b\b\b");
for(j=i;j>=65;j--) {
System.out.print((char)j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
23.) Pattern
1 2 3 4 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
public class pattern {
public static void main(String[] args) {
int j;
for(int i=5;i>=1;i--) {
for(j=1;j<=i;j++) {
System.out.print(j+" ");
}
for(j=5;j>=i;j--) {
System.out.print(" ");
}
System.out.print("\b\b\b\b\b\b");
for(j=i;j>=1;j--) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
24.) Pattern
A A
A B B A
A B C C B A
A B C D D C B A
A B C D E D C B A
public class pattern {
public static void main(String[] args) {
int j;
for(int i=65;i<=69;i++){
for(j=65;j<=i;j++) {
System.out.print((char)j+" ");
}
for(j=69;j>=i;j--) {
System.out.print(" ");
}
System.out.print("\b\b\b\b\b\b");
for(j=i;j>=65;j--) {
System.out.print((char)j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
25.) Pattern
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
public class pattern {
public static void main(String[] args) {
int j;
for(int i=1;i<=5;i++) {
for(j=5;j>i;j--) {
System.out.print(" ");
}
for(j=1;j<=i;j++) {
System.out.print(j+" ");
}
System.out.print("\b\b");
for(j=i;j>=1;j--) {
System.out.print(j+" ");
}
System.out.println("");
}
for(int i=4;i>=1;i--) {
for(j=4;j>=i;j--) {
System.out.print(" ");
}
for(j=1;j<=i;j++) {
System.out.print(j+" ");
}
System.out.print("\b\b");
for(j=i;j>=1;j--) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
26.) Pattern
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
public class pattern {
public static void main(String[] args) {
int j;
for(int i=1;i<=5;i++) {
for(j=5;j>i;j--) {
System.out.print(" ");
}
for(j=i;j>=1;j--) {
System.out.print(j+" ");
}
System.out.print("\b\b");
for(j=1;j<=i;j++) {
System.out.print(j+" ");
}
System.out.println();
}
for(int i=4;i>=1;i--) {
for(j=5;j>i;j--) {
System.out.print(" ");
}
for(j=i;j>=1;j--) {
System.out.print(j+" ");
}
System.out.print("\b\b");
for(j=1;j<=i;j++) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
27.) Pattern
A
A B A
A B C B A
A B C D C B A
A B C D E D C B A
A B C D C B A
A B C B A
A B A
A
public class pattern {
public static void main(String[] args) {
int j;
for (int i = 65; i < 69; i++) {
for(j=69;j>=i;j--){
System.out.print(" ");
}
for(j=65;j<=i;j++){
System.out.print((char)j+" ");
}
System.out.print("\b\b");
for(j=i;j>=65;j--){
System.out.print((char)j+" ");
}
System.out.println();
}
for(int i=69;i>=65;i--){
for(j=69;j>=i;j--){
System.out.print(" ");
}
for(j=65;j<=i;j++){
System.out.print((char)j+" ");
}
System.out.print("\b\b");
for(j=i;j>=65;j--){
System.out.print((char)j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
28.) Pattern
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
public class pattern {
public static void main(String[] args) {
int j;
for (int i = 5; i >= 1; i--) {
for(j=5;j>=i;j--){
System.out.print(" ");
}
for(j=1;j<=i;j++){
System.out.print(j+" ");
}
System.out.print("\b\b");
for(j=i;j>=1;j--){
System.out.print(j+" ");
}
System.out.println();
}
for(int i=2;i<=5;i++){
for(j=5;j>=i;j--){
System.out.print(" ");
}
for(j=1;j<=i;j++){
System.out.print(j+" ");
}
System.out.print("\b\b");
for(j=i;j>=1;j--){
System.out.print(j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
ARRAY
1.) Single Dimension Array.
class sarray{
public static void main(String args[]) {
int a [] = new int [5];
for(int i=0;i<5;i++){
a[i] = i;
System.out.print(a[i]+" ");
}
}
}
OUTPUT :-
0 1 2 3 4
---------------------------------------------------------------------------------------------------------------------------------------
2.) Single Dimension Array (Static)
class sarray{
public static void main(String args[]) {
int a [] = {10, 20, 30, 40, 50};
for(int i=0;i<5;i++){
System.out.print(a[i]+" ");
}
}
}
OUTPUT :-
10 20 30 40 50
---------------------------------------------------------------------------------------------------------------------------------------
3.) Double Dimension Array(2-D array)
class darray{
public static void main(String args[]) {
int a [][] = new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
OUTPUT :-
0 0
0 0
---------------------------------------------------------------------------------------------------------------------------------------
4.) Jagged Array
class jarray{
public static void main(String args[]) {
int a [][] = new int[2][];
a[0] = new int [3];
a[1] = new int [2];
int count=0;
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
a[i][j] = count++;
}
}
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
OUTPUT :-
012
34
---------------------------------------------------------------------------------------------------------------------------------------
5.) Write a program to read an array of 10 values and print it.
import java.util.Scanner;
class array{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a [] = new int[10];
System.out.println("Enter Array Elements : ");
for(int i=0;i<a.length;i++){
a[i] = sc.nextInt();
}
System.out.println("\nThe Value of an array is : ");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
INPUT :- Enter Array Elements :
1
2
3
4
5
6
7
8
9
10
OUTPUT :-
The Value of an array is :
1 2 3 4 5 6 7 8 9 10
---------------------------------------------------------------------------------------------------------------------------------------
6.) Write a program to read an array of 10 values and print the numbers in reverse order.
import java.util.Scanner;
class array{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a [] = new int[10];
System.out.println("Enter Array Elements : ");
for(int i=0;i<a.length;i++){
a[i] = sc.nextInt();
}
System.out.println("\nValues of an array is : ");
for(int i=a.length-1;i>=0;i--){
System.out.print(a[i]+" ");
}
}
}
INPUT :-
Enter Array Elements :
1
2
3
4
5
6
7
8
9
10
OUTPUT :-
Values of an array is :
10 9 8 7 6 5 4 3 2 1
---------------------------------------------------------------------------------------------------------------------------------------
7.) Get the 10 numbers from users & calculate Total and Average.
import java.util.Scanner;
class array{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a [] = new int[10];
int sum = 0;
System.out.println("Enter Array Elements : ");
for(int i=0;i<a.length;i++){
a[i] = sc.nextInt();
sum = sum + a[i];
}
float avg = sum / 10f;
System.out.println("\nThe Sum is : "+sum);
System.out.println("The Average is : "+avg);
}
}
INPUT :-
Enter Array Elements :
1
2
3
4
5
6
7
8
9
10
OUTPUT :-
The Sum is : 55
The Average is : 5.5
---------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
CLASS AND OBJECT
1.) Simple Class and Object
class cd {
int a;
cd(){
a=10;
}
void print(){
System.out.println(a);
}
}
class demo{
public static void main(String args[]){
cd obj = new cd();
obj.print();
}
}
OUTPUT :-
10
---------------------------------------------------------------------------------------------------------------------------------------
2.) Get Value from user and print them
class cd {
void print(int a){
System.out.println(a);
}
}
class demo{
public static void main(String args[]){
int i = Integer.parseInt(args[0]);
cd obj = new cd();
obj.print(i);
}
}
INPUT :- 8
OUTPUT :- 8
---------------------------------------------------------------------------------------------------------------------------------------
3.) Declaration of Method
class cd {
public void print(int i){
System.out.println("The Value is : "+i);
}
}
class demo{
public static void main(String args[]){
cd obj = new cd();
obj.print(10);
}
}
OUTPUT :-
10
---------------------------------------------------------------------------------------------------------------------------------------
4.) Method Overloading
class cd {
public void print(int i){
System.out.println("Value is : "+i);
}
public void print(){
System.out.println("Hello");
}
}
class demo{
public static void main(String args[]){
cd obj = new cd();
obj.print();
obj.print(10);
}
}
OUTPUT :-
Hello
Value is : 10
---------------------------------------------------------------------------------------------------------------------------------------
5.) Default Constructor.
class demo {
int i;
demo() {
i=10;
}
void print() {
System.out.println("The Value is : "+i);
}
}
class defaultcon {
public static void main(String args[]){
demo obj = new demo();
obj.print();
}
}
OUTPUT :-
The Value is : 10
---------------------------------------------------------------------------------------------------------------------------------------
6.) Constructor Overloading.
class demo {
int a, b;
demo() {
System.out.println("Default Constructor");
}
demo(int i) {
a=i;
System.out.println("Parameterized Constructor is : "+a);
}
demo(float f) {
System.out.println("Parameterized Constructor is : "+f);
}
}
class constructoroberloading {
public static void main(String args[]) {
demo obj = new demo();
demo obj1 = new demo(Integer.parseInt(args[0]));
demo obj2 = new demo(1.5f);
}
}
INPUT :- 10
OUTPUT :-
Default Constructor
Parameterized Constructor is : 10
Parameterized Constructor is : 1.5
---------------------------------------------------------------------------------------------------------------------------------------
7.) Get student information from user like rollno, name, city, mobile number and print this information.
class demo {
int r;
String name, city1;
Long mno;
demo(int rno, String nm, String city, Long mo) {
r = rno;
name = nm;
city1 = city;
mno = mo;
}
public void print() {
System.out.println("The Roll No is : " + r);
System.out.println("The Name is : " + name);
System.out.println("The City is : " + city1);
System.out.println("The Mobile No is : " + mno);
}
}
class constructor {
public static void main(String args[]){
demo obj = new demo(Integer.parseInt(args[0]),
args[1],
args[2],
Long.parseLong(args[3]));
obj.print();
}
}
INPUT :-
javac constructor.java
java constructor 1 abc abcd 1234567890
OUTPUT :-
The Roll No is : 1
The Name is : abc
The City is : abcd
The Mobile No is : 1234567890
---------------------------------------------------------------------------------------------------------------------------------------
8.) Static Member
class sd {
static int i;
sd() {
i=10;
}
void print() {
System.out.println(i);
}
}
class staticmember {
public static void main(String args[]) {
sd obj = new sd();
obj.print();
}
}
OUTPUT :-
10
---------------------------------------------------------------------------------------------------------------------------------------
9.) Static Method
class sd {
static int i;
sd() {
i=10;
}
static void print() {
System.out.println(i);
}
}
class staticmethod {
public static void main(String args[]) {
sd obj = new sd();
sd.print();
}
}
OUTPUT :-
10
---------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
Hello nice programs
ReplyDeleteThanks Deven katara
Delete