0% found this document useful (0 votes)
4 views60 pages

Core Java

Uploaded by

rohitreon09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views60 pages

Core Java

Uploaded by

rohitreon09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

1.

Create a java application where we have one class it contains the main method
provides any user-friendly message to the user on the console.
class A{
public static void main(String[]args){
System.out.println("HELLO WORLD");
}
}

2. Create a java application where we have one class it contains a local integer variable
under main method then print the value of the variable on the console.
class B{
public static void main(String[]args){
int n=10;
System.out.println(n);
}
}

3. Create a java application where we have one class it contains two static integer
variables then print the addition value of both the static variables under main method.
class j003{
static int a=70;
static int b=71;
public static void main (String[]args){
int c=a+b;
System.out.println(c);
}

4. Create a java application Where We Have One Class it contains two non-static
variables as integers, perform subtraction operation with these two variables under
main method and display the subtraction value on the console.
class j004{
int a=12;
int b=13;
public static void main(String[]args){
j004 obj=new j004();
int c=obj.a-obj.b;
System.out.println(c);
}
}
5. Create a java application where we have a class it contains two static variables as
Boolean and character, two non-static variables as string and long and two local
variables as integer and double then display all values of these variables on the
console through main method.
class j005{
static boolean b=true;
static char c='P';
String d ="PRASAD";
long l =123l;
public static void main(String[]args){
int e=178;
double f=178.1;
j005 obj=new j005();
System.out.println(b);
System.out.println(c);
System.out.println(obj.d);
System.out.println(obj.l);
System.out.println(e);
System.out.println(f);
System.out.println(obj.d+e+f);
}
}

6. Create a java application where we have one class it contains a non-static method
named display which doesn’t have any parameters and this method returns nothing.
This method has to print any user-friendly message on the console while we invoke
this method under main method.
class j006{
void display(){
System.out.println("HI");
}
public static void main(String[]args){
j006 obj=new j006();
obj.display();
}
}

7. Create a java application where we have a one class it contains a non-static method
named m1 having parameter integer and returns nothing this method has to print the
value of the parameter when we invoke this method under main method.
class j007{
void m1(int a){
System.out.println(a);
}
public static void main(String[]args){
j007 obj=new j007();
obj.m1(6);
}
}

8. Create a java application where we have class it contains non-static method m1


having parameter string returns nothing, this method has to print the value of
parameter when we invoke this method under main method.
class j008{
void m1(String b){
System.out.println(b);
}
public static void main(String[]args){
j008 obj=new j008();
obj.m1("PRASAD");
}
}

9. Create a java application where we have class it contains non-static method m1


having parameter float returns nothing, this method has to print the value of
parameter when we invoke this method under main method.
class j009{
void m1(float b){
System.out.println(b);
}
public static void main(String[]args){
j009 obj=new j009();
obj.m1(178.20f);
}
}

10. Create a java application where we have one class it contains a static method
named sm1 having parameters character and long and this method returns nothing,
this method has to print the value of both parameters when we invoke this method
under main method.
class j010{
static void sm1(char a ,long l){
System.out.println(a);
System.out.println(l);
}
public static void main(String[]args){
sm1('#',12345l);
}

11. Create a java application where we have one class it contains a non-static method
m1 having parameter Boolean and returns nothing, a static method m2 Having
parameter double returns nothing, a non-static method m3 having parameters long,
string and integer it returns nothing all these methods have to print the value of their
respective parameters when we invoke these under main method.
class j011{
void m1(boolean b){
System.out.println(b);
}
static void m2(double d){
System.out.println(d);
}
void m3(long l,String s,int i)
{
System.out.println(l);
System.out.println(s);
System.out.println(i);
}
public static void main(String[]args){
j011 obj=new j011();
obj.m1(true);
m2(123456.98765);
obj.m3(121l,"PRASAD",987);

}
}

12. Create a java application where we have one class it contains a non-static method
m1 having parameter long returns nothing this method has to print the value of
parameter while we invoke this method under main method by providing dynamic
inputs.
import java.util.Scanner;
class j012{
void m1(long l){
System.out.println(l);
}
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
long l=sc.nextLong();
j012 obj=new j012();
obj.m1(l);
}
}

13. Create a java application where we have one class it contains a static method m1
having parameter string and character, returns nothing, a non-static method m2
having parameter integer and byte returns nothing both methods has to print the value
of parameters while we invoke this method under main method by providing dynamic
inputs.
import java.util.Scanner;
class j013{
static void m1(String s,char c){
System.out.println(s);
System.out.println(c);
}
void m2(int i,byte b){
System.out.println(i);
System.out.println(b);
}
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
char c=sc.next().charAt(0);
int i=sc.nextInt();
byte b=sc.nextByte();
j013 obj=new j013();
m1(s,c);
obj.m2(i,b);
}
}
14. Create a java application where we have one class it contains a non-static method
m1 having parameter Boolean returns a Boolean value then invoke this method under
main method by providing static inputs.
class j014{
boolean m1(boolean b){
System.out.println(b);
return true;
}
public static void main(String[]args){
j014 obj=new j014();
boolean c=obj.m1(false);
System.out.println(c);
}

15. Create a java application where we have one class it contains a static method m1
with parameter string and it returns long value invoke this method under main method
by providing static inputs.
class j015{
static long m1(String s){
System.out.println(s);
return 12345l;
}
public static void main(String[]args){
long result=m1("PK");
System.out.println(result);

}
}

16. Create a java application where we have one class it contains a non-static method
m1 having parameter double and it returns a float value a static method m2 having
parameter integer and it returns string values invoke both methods under main
method by providing static inputs.
class j016{
float m1(double d){
System.out.println(d);
return 123.45f;
}
static String m2(int i){
System.out.println(i);
return "PRASAD";
}
public static void main(String[]args){
j016 obj=new j016();
float g=obj.m1(1234.987609);
System.out.println(g);
String t=m2(71);
System.out.println(t);

}
}

17. Create a java application where we have one class it contains a non-static method
m1 having parameter double and it returns a float value a static method m2 having
parameter integer and it returns string values invoke both methods under main
method by providing dynamic inputs.
import java.util.Scanner;
class j017{
static Scanner sc=new Scanner(System.in);
float m1(double d1){
System.out.println(d1);
float f1=sc.nextFloat();
return f1;
}
static String m2(int i1){
System.out.println(i1);
String s1=sc.next();
return s1;
}
public static void main(String[]args){
double d1=sc.nextDouble();
int i1=sc.nextInt();
j017 obj=new j017();
float res1=obj.m1(d1);
String res2=m2(i1);
System.out.println(res1);
System.out.println(res2);
}

}
18. Create a java application where we have one class it contains a non-static method
m1 having parameters int and Boolean and it returns string value invoke this method
under main method by providing Static inputs.
class j018{
String m1(int i,boolean b){
System.out.println(i);
System.out.println(b);
return "PRASAD";
}
public static void main(String[]args){
j018 obj=new j018();
String s=obj.m1(76,true);
System.out.println(s);

19. Create a java application where we have one class it contains a non -static method
m1 having parameter Boolean returns integer then invoke this method under main
method by providing dynamic inputs for both parameters and return types.
import java.util.Scanner;
class j019{
Scanner sc=new Scanner(System.in);
int m1(boolean b){
System.out.println(b);
int i=sc.nextInt();
return i;
}
public static void main(String[]args){
j019 obj=new j019();
int res=obj.m1(b);
System.out.println(res);
}
}

20. Create a java application where we have one class it contains three static methods
and three non-static methods with different parameters and return types each then
invoke all these methods under main method by providing dynamic inputs for both
parameters and return types.
import java.util.Scanner;
class j020{
static Scanner sc=new Scanner(System.in);
static int m1(boolean b)
{
System.out.println(b);
int i=sc.nextInt();
return i;
}
static float m2(byte c)
{
System.out.println(c);
float f=sc.nextFloat();
return f;
}
static String m3(double d)
{
System.out.println(d);
String s=sc.next();
return s;
}
double m4(String t)
{
System.out.println(t);
double x=sc.nextDouble();
return x;
}
char m5(short u)
{
System.out.println(u);
char e=sc.next().charAt(0);
return e;
}
short m6(long l)
{
System.out.println(l);
short z=sc.nextShort();
return z;
}
public static void main(String[]args)
{
boolean b=sc.nextBoolean();
byte c=sc.nextByte();
double d=sc.nextDouble();
String t=sc.next();
Short u=sc.nextShort();
long l=sc.nextLong();
j020 obj=new j020();
int res1=m1(b);
float res2=m2(c);
String res3=m3(d);
double res4=obj.m4(t);
char res5=obj.m5(u);
short res6=obj.m6(l);
System.out.println(res1);
System.out.println(res2);
System.out.println(res3);
System.out.println(res4);
System.out.println(res5);
System.out.println(res6);
}
}

21. Create a java application where we have one class it contains a non-static method
m1 having parameter integer and returns a Boolean value, a static method m2 having
parameter string and returns a character value then invoke m1 method under m2 and
m2 method under main by providing dynamic inputs.
import java.util.Scanner;
class j021
{
static Scanner sc=new Scanner(System.in);
boolean m1(int i)
{
System.out.println(i);
boolean b=sc.nextBoolean();
return b;
}
static char m2(String s)
{
System.out.println(s);1pk
j021 obj=new j021();
int i=sc.nextInt();
boolean res1=obj.m1(i);
System.out.println(res1);
char c=sc.next().charAt(0);
return c;
}
public static void main(String[]args)
{
String s=sc.next();
char res2=m2(s);
System.out.println(res2);
}

22. Create a java application where we have one class it contains two static methods
having parameter and return types named m1 and m2 respectively then invoke m1
under m2, and m2 under main by providing dynamic inputs.
import java.util.Scanner;
class j022
{
static Scanner sc=new Scanner(System.in);
static boolean m1(int i)
{
System.out.println(i);
boolean b=sc.nextBoolean();
return b;
}
static char m2(String s)
{
System.out.println(s);
int i=sc.nextInt();
boolean res1=m1(i);
System.out.println(res1);
char c=sc.next().charAt(0);
return c;
}
public static void main(String[]args)
{
String s=sc.next();
char res2=m2(s);
System.out.println(res2);
}

23. Create a java application where we have one class it contains two non-static
methods having parameter and return types named m1 and m2 respectively then
invoke m1 under m2, and m2 under main by providing dynamic inputs.
import java.util.Scanner;
class j023
{
static Scanner sc=new Scanner(System.in);
static j022 obj=new j022();
boolean m1(int i)
{
System.out.println(i);
boolean b=sc.nextBoolean();
return b;
}
char m2(String s)
{
System.out.println(s);
int i=sc.nextInt();
boolean res1=m1(i);
System.out.println(res1);
char c=sc.next().charAt(0);
return c;
}
public static void main(String[]args)
{
String s=sc.next();
char res2=obj.m2(s);
System.out.println(res2);
}

24. Create a java application where we have one class it contains it contains static
method m1, non-static method m2, static m3, non-static m4 having parameters and
return types for all methods then invoke m1 under m2, m2 under m3, m3 under m4,
and m4 under main by providing dynamic inputs.
import java.util.Scanner;
class j024
{
static Scanner sc=new Scanner(System.in);
static j024 obj=new j024();
static int m1(boolean b)
{
System.out.println(b);
int i=sc.nextInt();
return i;
}
char m2(byte x)
{
System.out.println(x);
boolean b=sc.nextBoolean();
int i=m1(b);
System.out.println(i);
char c=sc.next().charAt(0);
return c;
}
static long m3(String s)
{
System.out.println(s);
byte x=sc.nextByte();
char d=obj.m2(x);
System.out.println(d);
long l=sc.nextLong();
return l;
}
String m4(double z)
{
System.out.println(z);
String s=sc.next();
long l=m3(s);
System.out.println(l);
String y=sc.next();
return y;
}
public static void main(String[]args)
{
double z=sc.nextDouble();
String res=obj.m4(z);
System.out.println(res);
}
}

25. Create a java application where we have one class it contains it contains a non-
static method m1 having parameter integer and returns a string value then invoke this
method under main method by providing dynamic inputs.
NOTE: Don’t use import statement.
class j025
{
static java.util.Scanner sc=new java.util.Scanner(System.in);
String m1(int i)
{
System.out.print(i);
String s=sc.next();
return s;
}
public static void main(String[]args)
{

int i=sc.nextInt();
j025 obj=new j025();
String s=obj.m1(i);
System.out.print(s);
}
}

26. Create a java application where we have one class it contains a local variable
Integer. print this variable under main method by providing dynamic inputs without
importing scanner and without creating object of scanner.
class j026
{
public static void main(String[]args)
{
int i=new java.util.Scanner(System.in).nextInt();
System.out.println(i);
}
}

27. Create a java application where we have one class it contains one static method
m1 having parameter and return type, a non-static method m2 having parameter and
return type then invoke both the methods under main method in such a way that m1
methods return value should be the parameter of m2 method , provide dynamic
inputs.
import java.util.Scanner;
class j027
{
static Scanner sc=new Scanner(System.in);
static int m1(boolean b)
{
System.out.println(b);
int i=sc.nextInt();
return i;
}
String m2(int j)
{
System.out.println(j);
String s=sc.next();
return s;
}
public static void main(String[]args)
{
j027 obj=new j027();
boolean b=sc.nextBoolean();
int i=m1(b);
String s=obj.m2(i);
System.out.println(s);
}

28. Create a java application where we have one class it contains one static method
and two non-static methods alternatively named m1, m2, m3 having parameters and
return types then invoke all methods under main method in such a way that the return
values of non-static methods will be parameters for static method provide dynamic
inputs.
import java.util.Scanner;
class j028
{
static Scanner sc =new Scanner(System.in);
int m1(boolean b)
{
System.out.println(b);
//int i=sc.nextInt();
//return i;
return sc.nextInt();
}
String m2(int x)
{
System.out.println(x);
//String s=sc.next();
//return s;
return sc.next();
}
static byte m3(int y,String z)
{
System.out.print(y+z);
//byte b=sc.nextByte();
//return b;
return sc.nextByte();
}
public static void main(String[]args)
{
j028 obj=new j028();
//boolean b=sc.nextBoolean();
//int x=sc.nextInt();
//int res1=obj.m1(b);
//String res2=obj.m2(x);
//byte res3=m3(res1,res2);
//System.out.println(res1);
//System.out.println(res2);
//System.out.println(res3);
System.out.println(m3(obj.m1(sc.nextBoolean()),obj.m2(sc.nextInt())));
}
}
Single line calling
import java.util.Scanner;
class Main
{
static Scanner sc = new Scanner(System.in);
boolean m1(long l)
{
System.out.println("enter 1st method");
System.out.println(l);
return sc.nextBoolean();
}
long m2(String i)
{
System.out.println("enter 2nd method");
System.out.println(i);
return sc.nextLong();
}
String m3(char ch)
{
System.out.println("enter 3rd method");
System.out.println(ch);
return sc.next();
}
char m4(int b)
{
System.out.println("enter 4rd method");
System.out.print(b);
return sc.next().charAt(0);
}
public static void main(String[]args)
{
Main obj=new Main();
// System.out.print(obj.m1(sc.nextLong()));
// System.out.print(obj.m2(sc.next()));
// System.out.print(obj.m3(sc.next().charAt(0)));
// System.out.print(obj.m4(sc.nextInt()));
System.out.print(obj.m1(obj.m2(obj.m3(obj.m4(sc.nextInt())))));
}
}

29. Create a java application where we have one class named mathematics which
contains three methods like non-static method addition having two integer
parameters returns the addition value of both parameters ,non-static method
subtraction having two integer parameters returns the subtraction values of both
parameters , a non-static method named multiplication having no parameters returns
the multiplication value of both addition and subtraction method results then invoke
all methods appropriately by providing dynamic inputs.
import java.util.Scanner;
class mathematics
{
static Scanner sc=new Scanner(System.in);
int addition(int a,int b)
{
System.out.println(a);
System.out.println(b);
return a+b;
}
int subtraction(int c,int d)
{
System.out.println(c);
System.out.println(d);
return c-d;
}
int multiplication()
{
int addres=addition(sc.nextInt(),sc.nextInt());
int subres=subtraction(sc.nextInt(),sc.nextInt());
return addres*subres;
}
public static void main(String[]args)
{
mathematics obj=new mathematics();
System.out.print(obj.multiplication());
}
}

30. Create a java application where we have one class it contains a non- static method
m1 having parameter and return type, a static method m2 having parameter and return
types then invoke m1 under m2 under main method with dynamic inputs. Note:
object of our class should create under main method only.
import java.util.Scanner;
class j030
{
static Scanner sc=new Scanner(System.in);
boolean m1(String s)
{
System.out.println(s);
return sc.nextBoolean();
}
static long m2(j030 obj)
{
boolean b=obj.m1(sc.next());
System.out.println(b);
return sc.nextLong();
}
public static void main(String[]args)
{
j030 obj=new j030();
long l=m2(obj);
System.out.print(l);
}
}

31. Create a java application where we have one class it contains three static methods
and three non-static methods alternatively having parameter and return type,
implement method to method calling by providing dynamic inputs.
NOTE: object of our class should only create under main method only.
import java.util.Scanner;
class j031
{
static Scanner sc=new Scanner(System.in);
int m1(String s)
{
System.out.println(s);
return sc.nextInt();
}
static char m2(long l,j031 obj)
{
System.out.println(l);
System.out.println(obj.m1(sc.next()));
return sc.next().charAt(0);
}
byte m3(char p,j031 obj)
{
System.out.println(p);
System.out.println(m2(sc.nextLong(),obj));
return sc.nextByte();
}
static int m4(double d,j031 obj)
{
System.out.println(d);
System.out.println(obj.m3(sc.next().charAt(0),obj));
return sc.nextInt();
}
short m5(int h,j031 obj)
{
System.out.println(h);
System.out.println(m4(sc.nextDouble(),obj));
return sc.nextShort();
}
static float m6(long l,j031 obj)
{
System.out.println(l);
System.out.println(obj.m5(sc.nextInt(),obj));
return sc.nextFloat();
}
public static void main(String[]args)
{
j031 obj=new j031();
System.out.println(m6(sc.nextLong(),obj));
}

32. Create a java application where we have one class it contains one static method
m1, two non-static methods m2, m3 having parameter and return type then invoke
all methods under main method by providing dynamic inputs.
NOTE: Object of our class shouldn’t be created under either main method or
globally.(Factory Method )

import java.util.Scanner;
class j032
{
static Scanner sc=new Scanner (System.in);
static j032 m1(String s)
{
j032 obj=new j032();
return obj;
}
boolean m2(long l)
{
System.out.print(l);
return sc.nextBoolean();
}
String m3(int i)
{
System.out.print(i);
return sc.next();
}
public static void main(String[]args)
{
j032 obj=(m1(sc.next()));
System.out.print(obj.m2(sc.nextLong()));
System.out.print(obj.m3(sc.nextInt()));
}

33. Create a java application where we have one class it contains a non-static variable
character , A static variable Boolean ,we also have a non-static method m1 having
parameter integer and returns a float value then print both variables under main
method and invoke under main method by providing dynamic inputs.
import java.util.Scanner;
class j033
{
static Scanner sc=new Scanner(System.in);
char c=sc.next().charAt(0);
static boolean b =sc.nextBoolean();
float m1(int a)
{
System.out.println(a);
return sc.nextFloat();
}
public static void main(String[]args)
{
j033 obj=new j033();
System.out.println(obj.c);
System.out.println(b);
System.out.println(obj.m1(sc.nextInt()));
}

}
34. Create a java application where we have one class it contains three static and three
non-static variables alternatively, a static and non-static method with parameter and
return types then invoke all the properties under main method by providing dynamic
inputs
import java.util.Scanner;
class j034
{
static Scanner sc=new Scanner(System.in);
static int i=sc.nextInt();
boolean b=sc.nextBoolean();
static char c=sc.next().charAt(0);
String s=sc.next();
static byte d=sc.nextByte();
float f=sc.nextFloat();
static int m1(char x)
{
System.out.println(x);
return sc.nextInt();
}
float m2(String y)
{
System.out.print(y);
return sc.nextFloat();
}
public static void main(String[]args)
{
j034 obj = new j034();
System.out.println(i);
System.out.println(obj.b);
System.out.println(c);
System.out.println(obj.s);
System.out.println(d);
System.out.println(obj.f);
System.out.println(m1(sc.next().charAt(0)));
System.out.println(obj.m2(sc.next()));
}
}

STUDENTS DATA
import java.util.Scanner;
class Students
{
static Scanner sc = new Scanner(System.in);
int stdId=sc.nextInt();
String stdName=sc.next();
String stdBranch=sc.next();
float stdMarks=sc.nextFloat();
static String collegeName=sc.next();
static void displayStudent(Students s)
{
System.out.println("Student ID:"+s.stdId);
System.out.println("Student Name:"+s.stdName);
System.out.println("Student Branch:"+s.stdBranch);
System.out.println("Student Marks Percentage:"+s.stdMarks);
System.out.println("College Name:"+s.collegeName);
}
public static void main(String[]args)
{
System.out.println("Enter Student 1 values");
Students s1=new Students();
System.out.println("Enter Student 2 values");
Students s2=new Students();
System.out.println("Enter Student 3 values");
Students s3=new Students();
System.out.println("Enter Student 4 values");
Students s4=new Students();
System.out.println("Enter Student 5 values");
Students s5=new Students();
System.out.println("Students Database Created Succesfully");
System.out.println("Enter the stdId to display their details");
int id =sc.nextInt();
switch(id)
{
case 1:displayStudent(s1);break;
case 2:displayStudent(s2);break;
case 3:displayStudent(s3);break;
case 4:displayStudent(s4);break;
case 5:displayStudent(s5);break;
default:System.out.println("Invalid Student Id");
}
}
}

35.Create a java application where we have one class it contains default constructor
Print user friendly message then invoke default constructor under main method.
class j035
{
j035()
{
System.out.print("HI");
}
public static void main(String[]args)
{

j035 obj=new j035();

}
}

36.Create a java application where we have one class it contains a parameterized


constructor then we invoke this method under main method it has to print the value of
parameter
import java.util.Scanner;
class j036
{
static Scanner sc=new Scanner(System.in);
j036(int a)
{
System.out.print(a);

}
public static void main(String[]args)
{
j036 obj=new j036(sc.nextInt());
}
}
37.Create a java application where we have one class it contains default constructor
One single parametrized constructor and a double parameterized constructor and triple
parameterized constructor then invoke dc under single under double under triple by
providing dynamic inputs.
class j037
{
static Scanner sc=new Scanner(System.in);
j037()
{
System.out.println("HELLO AP");
}
j037(int a)
{
System.out.print(a);
new j037();
}
j037(double d,boolean b)
{
System.out.print(d+" "+b);
new j037(sc.nextInt());
}
j037(short s,String a,long l)
{
System.out.print(s+" "+a+" "+l);
new j037(sc.nextDouble(),sc.nextBoolean());
}
public static void main(String[]args)
{
new j037(sc.nextShort(),sc.next(),sc.nextLong());
}
}
38.Create a java application where we have one class it contains default constructor
One single parametrized constructor and a double parameterized constructor and triple
parameterized constructor then invoke all the constructors under main method by
providing a dynamic input.
import java.util.Scanner;
class j038
{
static Scanner sc=new Scanner(System.in);
j038()
{
System.out.print("Default");
}
j038(int a)
{
System.out.print(a);
}
j038(String b,float c)
{
System.out.print(b+" "+c);
}
j038(byte d,char e,double f)
{
System.out.print(d+" "+e+" "+f);
}
public static void main(String[]args)
{
j038 obj=new j038();
j038 obj1=new j038(sc.nextInt());
j038 obj2=new j038(sc.next(),sc.nextFloat());
j038 obj3=new j038(sc.nextByte(),sc.next().charAt(0),sc.nextDouble());

}
}
39.Create a java application where we have one class it contains three static variables
and three non-static variables alternatively two single parameterized constructors
invoke all the properties under main method by providing all dynamic inputs.
import java.util.Scanner;
class j039
{
static Scanner sc=new Scanner(System.in);
static int a=sc.nextInt();
String b=sc.next();
static float c=sc.nextFloat();
boolean d=sc.nextBoolean();
static char e=sc.next().charAt(0);
double f=sc.nextDouble();
j039(int g)
{
System.out.println(g);
}
j039(String h)
{
System.out.println(h);
}
public static void main(String[]args)
{
j039 obj=new j039(sc.nextInt());
j039 obg1=new j039(sc.next());
System.out.println(a);
System.out.println(obj.b);
System.out.println(c);
System.out.println(obj.d);
System.out.println(e);
System.out.println(obj.f);
}
}

//NOTE:In this program first we give three static variable inputs and then come to main
method in main method we give first constructor parameter and then give all non static
variables.now you give second constructor parameter and then give all non static
variables.

40.Create a java application where we have one class it contains one default constructor
one single parameterized constructor and one Double Parameterized Constructors , a
non-static method m1 having parameter and return type, a static method m2 having
parameter and return type, a non-static method m3 having parameter and return type
then invoke m1 under default constructor , m2 under SPC ,m3 under DPC invoke all
the constructors under main methods by providing all dynamic inputs.
import java.util.Scanner;
class j040
{
static Scanner sc=new Scanner(System.in);
j040()
{
System.out.println("PRASAD");
System.out.println(m1(sc.next()));
}
j040(int a)
{
System.out.println(a);
System.out.println(m2(sc.next().charAt(0)));
}
j040(long l,double d)
{
System.out.println(l+" "+d);
System.out.println(m3(sc.nextFloat()));
}
long m1(String s)
{
System.out.println(s);
return sc.nextLong();
}
static Boolean m2(char c)
{
System.out.println(c);
return sc.nextBoolean();
}
double m3(float f)
{
System.out.println(f);
return sc.nextDouble();
}
public static void main(String[]args)
{
new j040();
new j040(sc.nextInt());
new j040(sc.nextLong(),sc.nextDouble());
}
}

//First it print DC and then give String value(i.e m1 parameter) and give m1 return
value and give integer value(i.e SPC value) then char value(i.e m2 parameter) and give
return value ang give long,double values(i.e DPC paramters) then give float values(i.e
m3 parametrs) and then give return value of m3
41.Create a java application where we have one class it contains one object
parametrized constructor invoke this method under main method.
import java.util.Scanner;
class j041
{
static Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
j041()
{
System.out.println("Default");
}
j041(j041 obj)
{
System.out.print(obj.a);
}
public static void main(String[]args)
{
//j041 obj=new j041();
new j041(new j041());
}
}
42.Create a java application where we have one class it contains a single parametrized
constructor and object parameterized constructor then invoke both constructors under
main method by providing dynamic inputs.
import java.util.Scanner;
class j042
{
static Scanner sc=new Scanner(System.in);
j042(int a)
{
System.out.println(a);
}
j042(j042 obj)
{
System.out.println("m");
}
public static void main(String[]args)
{
new j042(new j042(sc.nextInt()));
}
}
43.Create a java application where we have one class it contains a DC, one PC, two
PC, three PC then invoke all these constructers under main method in a single line by
providing dynamic inputs.
import java.util.Scanner;
class j043
{
static Scanner sc=new Scanner(System.in);
j043()
{
System.out.println("Single line calling");
}
j043(j043 obj)
{
System.out.println(obj);
}
j043(String b,j043 obj)
{
System.out.println(b+" "+obj);
}
j043(double d,byte e,j043 obj)
{
System.out.println(d+" "+e+" "+obj);
}
public static void main(String[]args)
{
//j043 obj=new j043();
//j043 obj1=new j043(obj);
//String s=sc.next();
//jo43 obj2=new j043(s,obj1);
//double d=sc.nextDouble();
//byte e=sc.nextByte();
//j043 0bj3=new j043(d,e,obj2);
new j043(sc.nextDouble(),sc.nextByte(),new j043(sc.next(),new j043(new
j043())));

}
Pr. Create a java application where we have one class it contains a DC, one PC, then
invoke all these constructers under main method in a single line by providing dynamic
inputs. (without use object parametrized constructor)
import java.util.Scanner;
class A
{
static Scanner sc=new Scanner(System.in);
A()
{
System.out.print("Default");
}
A(int a)
{
System.out.print(a);
}
public static void main(String[]args)
{
new A(new A().sc.nextInt());

}
}

//Singelton Design Pattern


class A
{
static A obj;
static A m1()
{
if(obj==null)
obj=new A();
return obj;
}
public static void main(String[]args)
{
A a1=m1();
A a2=m1();
System.out.print(a1+" "+a2);
}
}
44.create a java application where we need satisfy this keyword at variable level.
import java.util.Scanner;
class A
{
static Scanner sc=new Scanner(System.in);
int a=10;
void m1(int a)
{
System.out.println(a);
System.out.println(this.a);

public static void main(String[]args)


{
A obj=new A();
obj.m1(sc.nextInt());

}
}
45. create a java application where we need satisfy this keyword at method level.
import java.util.Scanner;
class A
{
static Scanner sc=new Scanner(System.in);
int m1(float a)
{
System.out.print(a);
return sc.nextInt();
}
boolean m2(String s)
{
System.out.print(s);
System.out.print(this.m1(sc.nextFloat()));
return sc.nextBoolean();

}
public static void main(String[]args)
{
A obj=new A();
System.out.print(obj.m2(sc.next()));

}
}
46. create a java application where we need satisfy this keyword at constructor level.
import java.util.Scanner;
class A
{
static Scanner sc=new Scanner(System.in);
A()
{
System.out.println("Default");
}
A(int a)
{
this();
System.out.println(a);
}
A(String s)
{
this(sc.nextInt());
System.out.println(s);
}
public static void main(String[]args)
{
A obj =new A(sc.next());
}
}

// first we call static properties and it comes to main method and it goes to string
parameter constructor and this key word goes to int parameter constructer.

47.create a java application where we need satisfy this keyword to all levels.
import java.util.Scanner;
class A
{
static Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
void m1(int a)
{
System.out.print(a);
System.out.print(this.a);
}
String m2(double d)
{
System.out.print(d);
this.m1(sc.nextInt());
return sc.next();
}
A(float f)
{
System.out.print(f);
}
A(double d)
{
this(sc.nextFloat());
System.out.print(d);
}
public static void main(String[]args)
{
A obj=new A(sc.nextDouble());
System.out.print(obj.m2(sc.nextDouble()));
}

}
48.create a java application where we have one class named employee it contains
employee-id, employee-name, employee-salary initialize these variables by triple
parameterized constructor by using this keyword.
import java.util.Scanner;
class employee
{
static Scanner sc=new Scanner(System.in);
int empId;
String empName;
int empSlry;
employee(int empId,String empName,int empSlry)
{
this.empId=empId;
this.empName=empName;
this.empSlry=empSlry;

}
public static void main(String[]args)
{
employee obj=new employee(sc.nextInt(),sc.next(),sc.nextInt());
System.out.println(obj.empId);
System.out.println(obj.empName);
System.out.println(obj.empSlry);
}
}
49.create a java application where we have one class it contains two instance variables
without values and provide user defined values for instance variables by using double
parameterized constructor, we need to have one object parameterized constructor if we
call instance variables through the object of object parameterized constructor we need
to get the same user defined values whatever we had given for double parameter
constructor.
import java.util.Scanner;
class A
{
static Scanner sc=new Scanner(System.in);
int x;
boolean y;
A(int x,boolean y)
{
this.x=x;
this.y=y;
}
A(A obj)
{
System.out.println(obj.x);
System.out.println(obj.y);

}
public static void main(String[]args)
{
A obj=new A(sc.nextInt(),sc.nextBoolean());
A obj1=new A(obj);

}
}

50.create a java application where we have one class it contains a non-static variable
integer having without value, provide user defined value for the instance variable by
using parametrized constructor and then print the value of the variable under main
method by providing dynamic inputs
import java.util.Scanner;
class A
{
static Scanner sc=new Scanner(System.in);
int x;
A(int x)
{
this.x=x;
}
public static void main(String[]args)
{
A obj=new A(sc.nextInt());
System.out.print(obj.x);

}
51. create a java application where we have one class it contains a non-static variable
integer having without value, provide user defined value for the instance variable by
using parametrized constructor, we also have a one object parameterized constructor and
then print the value of the variable under Object parameterized constructor by providing
dynamic inputs.
import java.util.Scanner;
class A
{
static Scanner sc=new Scanner(System.in);
int x;
A(int x)
{
this.x=x;
}
A(A obj)
{
System.out.println(obj.x);

}
public static void main(String[]args)
{
A obj=new A(sc.nextInt());
A obj1=new A(obj);

}
52.create a java application where we have one class named employee it contains id,
name, salary, experience designation without values, provide user defined values for
instance variables by using parametrized constructor. Create five instances for
employee. We also have methods like display employees, Average salary, display
specific employee having respective parameter and return types and then according to
user choice invoke the methods accordingly by providing dynamic inputs.

import java.util.Scanner;
class Employee
{
static Scanner sc=new Scanner(System.in);
int id;
String name;
double salary;
int exp;
String Dsg;
Employee(int id,String name,double salary,int exp,String Dsg)
{
this.id=id;
this.name=name;
this.salary=salary;
this.exp=exp;
this.Dsg=Dsg;

}
static void displayEmployees(Employee emp1,Employee emp2,Employee
emp3,Employee emp4,Employee emp5)
{
System.out.println("Employee 1 Details:");
System.out.println("Employee id:"+emp1.id);
System.out.println("Employee name:"+emp1.name);
System.out.println("Employee salary:"+emp1.salary);
System.out.println("Employee Experience:"+emp1.exp);
System.out.println("Employee Designation:"+emp1.Dsg);

System.out.println("Employee 2 Details:");
System.out.println("Employee id:"+emp2.id);
System.out.println("Employee name:"+emp2.name);
System.out.println("Employee salary:"+emp2.salary);
System.out.println("Employee Experience:"+emp2.exp);
System.out.println("Employee Designation:"+emp2.Dsg);

System.out.println("Employee 3 Details:");
System.out.println("Employee id:"+emp3.id);
System.out.println("Employee name:"+emp3.name);
System.out.println("Employee salary:"+emp3.salary);
System.out.println("Employee Experience:"+emp3.exp);
System.out.println("Employee Designation:"+emp3.Dsg);

System.out.println("Employee 4 Details:");
System.out.println("Employee id:"+emp4.id);
System.out.println("Employee name:"+emp4.name);
System.out.println("Employee salary:"+emp4.salary);
System.out.println("Employee Experience:"+emp4.exp);
System.out.println("Employee Designation:"+emp4.Dsg);

System.out.println("Employee 5 Details:");
System.out.println("Employee id:"+emp5.id);
System.out.println("Employee name:"+emp5.name);
System.out.println("Employee salary:"+emp5.salary);
System.out.println("Employee Experience:"+emp5.exp);
System.out.println("Employee Designation:"+emp5.Dsg);

}
static double averageSalary(Employee emp1, Employee emp2, Employee emp3,
Employee emp4, Employee emp5)
{
double totalSalary =
emp1.salary+emp2.salary+emp3.salary+emp4.salary+emp5.salary;
double avgSal = totalSalary/5;
return avgSal;
}

static void displaySpecificEmployee(Employee emp)


{
System.out.println("Employee Details: ");
System.out.println("Employee ID: "+emp.id);
System.out.println("Employee Name: "+emp.name);
System.out.println("Employee Salary: "+emp.salary);
System.out.println("Employee Experience: "+emp.exp);
System.out.println("Employee Designation: "+emp.Dsg);
}

public static void main(String[]args)


{
System.out.println("Enter Employee 1 details:");
Employee e1=new
Employee(sc.nextInt(),sc.next(),sc.nextDouble(),sc.nextInt(),sc.next());
System.out.println("Enter Employee 2 details:");
Employee e2=new
Employee(sc.nextInt(),sc.next(),sc.nextDouble(),sc.nextInt(),sc.next());
System.out.println("Enter Employee 3 details:");
Employee e3=new
Employee(sc.nextInt(),sc.next(),sc.nextDouble(),sc.nextInt(),sc.next());
System.out.println("Enter Employee 4 details:");
Employee e4=new
Employee(sc.nextInt(),sc.next(),sc.nextDouble(),sc.nextInt(),sc.next());
System.out.println("Enter Employee 5 details:");
Employee e5=new
Employee(sc.nextInt(),sc.next(),sc.nextDouble(),sc.nextInt(),sc.next());

System.out.print("Enter 1 for all Employee details, 2 for average salaries of all


employees and 3 for specific employee details: ");
int input = sc.nextInt();
switch(input)
{
case 1:
displayEmployees(e1, e2, e3, e4, e5);
break;
case 2:
averageSalary(e1,e2,e3,e4,e5);
break;
case 3:
System.out.print("Enter Employee Id: ");
int x = sc.nextInt();
switch(x)
{
case 1: displaySpecificEmployee(e1);
break;
case 2: displaySpecificEmployee(e2);
break;
case 3: displaySpecificEmployee(e3);
break;
case 4: displaySpecificEmployee(e4);
break;
case 5: displaySpecificEmployee(e5);
break;
default: System.out.println("No such employee ID");
}
break;
default:System.out.println("Invalid Input please try again");
}
}
}
INHERITENCE
53. create a java application where we have a bank, it will provide customer details
like account number, IFSC Code, and ATM Pin No. and available balance to the
customer then, the customer has to display all these values with respect to one user-
defined method and then invoke that method under the main method.(Single Level)
import java.util.Scanner;
class Bank
{
static Scanner sc=new Scanner(System.in);
long ac=sc.nextLong();
String ifsc=sc.next();
int ap=sc.nextInt();
int bal=sc.nextInt();
}
class Customer extends Bank
{
void display()
{
System.out.println("Bank details");
System.out.println(ac);
System.out.println(ifsc);
System.out.println(ap);
System.out.println(bal);
}
public static void main(String[]args)
{
Customer c=new Customer();
c.display();
}

}
54.Create a java application where we have one Mobile Manufacturing Company, they
will design a respective branded mobile with specifications like RAM, ROM, OS
Name, OS Version, Camera, Processor, Battery, Display Type, and price then, this
mobile will be released into the wholesale market then the price has to increase, from
here it will be released into the retail market again price has to increase then from here
this mobile will be purchased by the customer, customer has to display all these values
with updated price with respect to one user-defined method and then invoke display
method under the main method.(Multi Level Inheritance)
//MULTI LEVEL INHERITANCE
import java.util.Scanner;
class Vivo
{
static Scanner sc=new Scanner(System.in);
int RAM=sc.nextInt();
int ROM=sc.nextInt();
String OSName=sc.next();
double OSVer=sc.nextDouble();
int cam=sc.nextInt();
String processor=sc.next();
int battery=sc.nextInt();
String disptype=sc.next();
double price=sc.nextDouble();
}
class Wholesale_market extends Vivo
{
double Wprice=price+(price*0.25);

}
class Retail_market extends Wholesale_market
{
double Rprice=Wprice+(Wprice*0.25);

}
class Customer extends Retail_market
{
void display()
{
System.out.println("RAM : "+RAM+" GB");
System.out.println("ROM : "+ROM+" GB");
System.out.println("Operating System : "+OSName);
System.out.println("OS Version :"+OSVer);
System.out.println("Camera :"+cam+" MP");
System.out.println("Processor :"+processor);
System.out.println("Battery :"+battery+" maH");
System.out.println("Dispay Type :"+disptype);
System.out.println("Price :"+Rprice+"/- Rupees");

}
public static void main(String[]args)
{
Customer c=new Customer();
c.display();
}

}
55. Create a java application where we have a software company, it will provide details
like EmpID, EmpDes, EmpSal, and EmpWorkLoc to the 5 employees, every employee
has to display their own details with respect to one method and then invoke all those
methods under main.(Hirerchial Inheritance)
//Hierarchial Inheritance
import java.util.Scanner;
class Organization{
static Scanner sc = new Scanner(System.in);
int empId = sc.nextInt();
String empName = sc.next();
double empSal = sc.nextDouble();
int empExp = sc.nextInt();
}
class Employee1 extends Organization{
void display(){
System.out.println("Employee ID: "+empId);
System.out.println("Employee Name: "+empName);
System.out.println("Employee Salary: "+empSal);
System.out.println("Employee Experience: "+empExp);
}
}
class Employee2 extends Organization{
void display(){
System.out.println("Employee ID: "+empId);
System.out.println("Employee Name: "+empName);
System.out.println("Employee Salary: "+empSal);
System.out.println("Employee Experience: "+empExp);
}
}
class Employee3 extends Organization{
void display(){
System.out.println("Employee ID: "+empId);
System.out.println("Employee Name: "+empName);
System.out.println("Employee Salary: "+empSal);
System.out.println("Employee Experience: "+empExp);
}
}
class Employee4 extends Organization{
void display(){
System.out.println("Employee ID: "+empId);
System.out.println("Employee Name: "+empName);
System.out.println("Employee Salary: "+empSal);
System.out.println("Employee Experience: "+empExp);
}
}
class Employee5 extends Organization{
void display(){
System.out.println("Employee ID: "+empId);
System.out.println("Employee Name: "+empName);
System.out.println("Employee Salary: "+empSal);
System.out.println("Employee Experience: "+empExp);
}
}
class Test{
public static void main(String[]args){
Employee1 emp1 = new Employee1();
emp1.display();
Employee2 emp2 = new Employee2();
emp2.display();
Employee3 emp3 = new Employee3();
emp3.display();
Employee4 emp4 = new Employee4();
emp4.display();
Employee5 emp5 = new Employee5();
emp5.display();
}
}
Hybrid inheritance
import java.util.Scanner;
class A{
static Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
}
class F extends A{
void display(){
System.out.println(a);
}
}
class B extends A{
int b = sc.nextInt();
}
class C extends B{
int c = sc.nextInt();
}
class D extends C{
void display(){
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
class E extends C{
void display(){
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
class Test{
public static void main(String[]args){
E e = new E();
e.display();
D d = new D();
d.display();
F f = new F();
f.display();
}
}
import java.util.*;
class Manufacturer
{
static Scanner sc=new Scanner(System.in);
String Ram=sc.next();
String Rom=sc.next();
String Brand=sc.next();
double Price=sc.nextDouble();
}
class Customer extends Manufacturer
{
double discount=sc.nextDouble();
void display()
{
System.out.println("RAM : "+Ram);
System.out.println("ROM : "+Rom);
System.out.println("BRAND : "+Brand);
System.out.println("PRICE : "+Price);
System.out.println("Discount :"+discount+"%");
}
}
class Wholesale extends Manufacturer
{
double Wprice=Price+1000.0;
void display()
{
System.out.println("RAM : "+Ram);
System.out.println("ROM : "+Rom);
System.out.println("BRAND : "+Brand);
System.out.println("PRICE : "+Wprice);
}
}
class Retailer extends Wholesale
{
double Rprice=Wprice+1000.0;
void display()
{
System.out.println("RAM : "+Ram);
System.out.println("ROM : "+Rom);
System.out.println("BRAND : "+Brand);
System.out.println("PRICE : "+Rprice);
}
}
class ShopOne extends Retailer
{
double shopprice=Rprice+500.0;
void display()
{
System.out.println("RAM : "+Ram);
System.out.println("ROM : "+Rom);
System.out.println("BRAND : "+Brand);
System.out.println("PRICE : "+shopprice);
}
}
class ShopTwo extends Retailer
{
double shopprice=Rprice+600.0;
void display()
{
System.out.println("RAM : "+Ram);
System.out.println("ROM : "+Rom);
System.out.println("BRAND : "+Brand);
System.out.println("PRICE : "+shopprice);
}
}
class Runner
{
public static void main(String[] args)
{
Customer c=new Customer();
c.display();
Retailer r=new Retailer();
r.display();
ShopOne s=new ShopOne();
s.display();
ShopTwo s1=new ShopTwo();
s1.display();
}
}

Super Keyword
56.Create a Java application where we need to satisfy the super keyword at a variable
level
import java.util.Scanner;
class A{
static Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
}
class B extends A{
int a = sc.nextInt();
void display(){
System.out.println("B - "+a);
System.out.println("A - "+super.a);
}
public static void main(String[]arhs){
B obj = new B();
obj.display();
}
}

57.create a Java application where we have one class it contains one method m1
having parameter String and it is going to return a boolean value and then inherit this
class into another class which also has the same method name, same parameter, and
same return type then inherit this class into one more class there also we have same
method, parameter and return type. then if we invoke the bottommost derived class
method under the main method from there it has to invoke the intermediate base class
method and from there it has to invoke top most base class method by providing Dyna
import java.util.Scanner;
class A{
static Scanner sc = new Scanner(System.in);
boolean m1(String a){
System.out.println(a);
return sc.nextBoolean();
}
}
class B extends A{
boolean m1(String a){
System.out.println(a);
System.out.println(super.m1(sc.next()));
return sc.nextBoolean();
}
}
class C extends B{
boolean m1(String a){
System.out.println(a);
System.out.println(super.m1(sc.next()));
return sc.nextBoolean();
}
public static void main(String [] args){
C c = new C();
System.out.println(c.m1(sc.next()));
}
}
mic inputs with respect to the super keyword.

58.Create a Java app where we have one class it contains one DC then inherit this class
into the DC of another class then inherit this class into the PC of another class and then
this class into the PC of another class and then inherit this class into the DC of another
class. then if we invoke the bottommost derived class constructor under the main
method from there it has to invoke intermediate base class constructors by using the
super keyword and then from there it has to invoke top most base class constructor by
using a super keyword by giving dynamic inputs
import java.util.Scanner;
class A{
static Scanner sc = new Scanner(System.in);
A(){
System.out.println("A - Default");
}
}
class B extends A{
B(){
System.out.println("B - Default");
}
}
class C extends B{
C(int a){
super();
System.out.println("C - Para "+a);
}
}
class D extends C{
D(String a){
super(sc.nextInt());
System.out.println("D - Para "+a);
}
}
class E extends D{
E(){
super(sc.next());
System.out.println("E - Default");
}
public static void main(String [] args){
new E();
}
}

59.create a java application where we need to satisfy the super keyword at the variable
method and constructor levels.
60.create a java application where we need to satisfy both this and the super keyword
at the variable method and constructor levels.
class A
{
static Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
void m1(int a)
{
System.out.println(a);
System.out.println(this.a);

}
void m2()
{
this.m1(sc.nextInt());

}
A(int a)
{
System.out.println(a);

}
A()
{
this(sc.nextInt());

}
}
class B extends A
{
int a=sc.nextInt();
void m2()
{
super.m2();
System.out.println(super.a);
}
B(int a)
{
super();
System.out.println(a);
}
public static void main(String[]args)
{
B obj=new B(sc.nextInt());
obj.m2();

You might also like