Assignment-1..java Total
Assignment-1..java Total
Assignment:-8
Q) we can not make a private constructor in java program(show the error)
class shikha{
public int x = 10;
public shikha(){
[Link]("inside class shikha"); }
public void show(){
[Link](x);
}
}
public class Main {
public static void main(String[] args){
shikha ob = new shikha();
[Link]();
}
}
OUTPUT:-
inside class shikha
10
=== Code Execution Successful ===
Assignment:-9
Q)write a java program to implement facts java does not support mode of Access Specifiers .
class Shikha {
public int x = 10;
Shikha() {
[Link]("inside class Shikha");
}}
class B extends Shikha {
public void show() {
[Link]("this is the show method");
[Link](x);
}}
public class Main {
public static void main(String[] args) {
B obj = new B();
[Link]();
}}
OUTPUT:-
inside class Shikha
this is the show method
10
Assignment:-11
[Link] we implement multiple interface
interface A {
void disp();
}
interface B {
void display();
}
}
OUTPUT:-
default static void show(){
^
Assignment_14.java:13: error: cannot find symbol
[Link]();
^
symbol: method show()
location: variable obj of type A
2 error
Assignment:-15
Q) Write a java program that has to interface.
Interface -> A,B,C
Class :- D -> A & B
Class:- E-> D & C
INPUT:-
interface A{
public void Print(); }
interface B {
public void Show(); }
interface C{
public void Display(); }
class Student implements A,B{
public void Print(){
[Link]("this is the print method define
class Student ");
}
public void Show(){
[Link]("this is the show method inside
the class Student"); } }
class Child extends Student implements C{
public void Display(){
[Link]("this is the Display method inside
class child"); } }
public class Assignment_15
public static void main(String[] args){
Student ob1 = new Student();
Child ob2= new Child();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:-
this is the print method define class Student
this is the show method inside the class Student
this is the Display method inside class child
Assignment:-16
Q) Can we implement multiple inheritance?.
interface A {
public void show();
}
interface B{
public void display();}
interface C{
public void Print();
}
class shikha implements A,B,C{
public void show(){
[Link]("this is the show method");
}
public void display(){
[Link]("this is the display method");}
public void Print(){
[Link]("this is the Print method");
}}
public class Assignment_01 {
public static void main(String[] args){
shikha obj = new shikha();
[Link]();
[Link]();
[Link]();} }
OUTPUT:-this is the show method
this is the display method
this is the print methode
Assignment:-17
Q. CAN I REINITIALISE A VARIABLE IN AN IMPLEMENTING CLASS?MEANS .YOUHAVE INITIALISED A
VARIABLE INSIDE AN INTERFACE AND WANT TO ASSIGN A AGAIN IN THE CLASS IMPLEMENTING IT
CAN YOU DO THIS?
Input:-
Interface A{
Int x= 10
Int y=20;
}
Class B implement A{
X= 40;
Y=50;
Public void show(){
[Link](x);
[Link](y);
[Link](z);
}
}
Public class main {
Public static void main() {
B obk = new B();
[Link]();
}}
OUTPUT:- [Link]: error: expected
X = 40
^
[Link]:error:expected
Y = 50
^
Assignment:-18
1. CAN YOU REDEFINE A DEFAULT AND STATIC METHOD DEFINED AND
STATICMETHOD DEFINED IN INTERFACE IN THE CLASS IMPLEMENTING IT?
interface shikha{
default void show(){
[Link]("this is the show method");
}
static void display(){
[Link]("this is the display method");
}
}
class A implements shikha{
public void show(){
[Link]("inside the class a show method");
}
public void display(){
[Link]("inside the class a display
method");
}
}
public class Assignment_3 {
public static void main(String[] args){
A obj = new A();
[Link]();
[Link]();
}
}
OUTPUT:-
inside the class a show method
inside the class a display method
Assignment:-19
Q)Can you inherit a class and an interface together what is the order of writing
the inheritance ? Means what to write frist extending a class or implementing an
interface.
interface A{
public void Add();
}
interface B{
public void show();
}
interface C{
public void display();
}
class shikha implements A,B{
public void Add(){
[Link]("this is the add method");
}
public void show(){
[Link]("this is the show method");
}
}
class suman extends shikha implements C{
public void display(){
[Link]("this is the display method");
}
}
public class Assignment_04 {
public static void main(String[] args){
suman obj = new suman();
[Link]();
[Link]();
[Link]();
}}
OUTPUT:-
this is the add method
this is the show method
this is the display method
Assignment:-20
Q) What will happened when you don’t implement an abstract method declared
in interface in the class implementing it?
interface shikha{
public void show();
public void display();
}
class A implements shikha{
}
public class Assignment_5 {
public static void main(String[] args){
A obj = new A();
[Link]();
[Link]();
}
}
OUTPUT:-
Assignment_5.java:15: error: cannot find symbol
[Link]();
^
symbol: method show()
location: variable obj of type A
Assignment_5.java:16: error: cannot find symbol
[Link]();
^
symbol: method display()
location: variable obj of type A
Assignment:-21
Q) Can interface have Constructor?
interface shikha{
Add();
}
class Add implements shikha{
public int x = 10;
public void Add(){
[Link]("this is the class Add
constructor");
[Link](x);
} }
public class Assignment_6 {
public static void main(String[] args){
Add obj = new Add();
}}
OUTPUT:-
Assignment_6.java:4: error: <identifier> expected
Add();
^
1 error
Assignment:-22
Q) Suppose a class Is implementing both A & B interface? Both methods in it Then
how can it be alone?means what will be the syntax of mentioning add() method
of interface A in class C?.
interface A{
void Add();
}
interface B{
void Add(int y);
}
class shikha implements A,B{
public void Add(){
[Link]("Add method of interface A");
}
public void Add(int y){
[Link]("Add method of interface B");
}
}
public class Assignment_o7copy {
public static void main(String[] args){
shikha obj = new shikha();
[Link]();
[Link](10);
}
}
OUTPUT:-
Add method of interface A
Add method of interface B
Assignment:-23
Q) Can I define default method in a class rather than an interface?
interface Num{
public default void Show(){
[Link]("this is interface NUM show
method");
}
}
class shikha implements Num{
public void Show(){
[Link]("this is class shikha show
method");
}
}
public class Assignment_8 {
public static void shikha(String[] args){
shikha obj = new shikha();
[Link]();
}
}
OUTPUT:-
Error: Main method not found in class Assignment_8, please
define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend
[Link]
Assignment:-24
Q) Can I create an abstract class implementing an interface?
interface A{
public void Show();
}
abstract class shikha implements A{
}
class B extends shikha{
public void Show(){
[Link]("inside class B Show method");
}
}
public class Assignment_9 {
public static void main(String[] args){
B obj = new B();
[Link]();
}
}
OUTPUT:-
inside class B Show method
Assignment:-25
Q) Can a method in an interface be protected?
interface A {
protected void show();
}
class shikha implements A{
public void show(){
[Link]("this is the show method inside
the class");
}
}
public class Assignment_10 {
public static void main(String[] args){
shikha obj = new shikha();
[Link]();
}
}
OUTPUT:-
Assignment_10.java:4: error: modifier protected not allowed
here
protected void show
1 error
Assignment:-26
What will happened when you create an abstract method
declared in interface but not define inside the
implementing class it?
INPUT:-
interface A{
public void show();
}
class shikha implements A{
}
public class Abstract1 {
public static void main(String[] args){
shikha obj = new shikha();
[Link]();
}
}
OUTPUT:-
[Link]: error: shikha is not abstract and does not
override abstract method show() in A
class shikha implements A{
^
[Link]: error: cannot find symbol
[Link]();
^
symbol: method show()
location: variable obj of type shikha
2 error
Assignment:-27
What will happened when you create an default method
declared in interface and inside implementing class same
method name.
INPUT:-
interface A{
public default void show(){
[Link]("this is the show method inside
interface");
}
}
class shikha implements A{
public void show(){
[Link]("this is the show method inside
the shikha class");
[Link]();
}
}
public class Assignment_27 {
public static void main(String[] args){
shikha obj =new shikha();
[Link]();
}
}
OUTPUT:-
this is the show method inside the shikha class
this is the show method inside interface
ASSIGNMENT-28
Q.1) W.A.J.P THAT HANDLES AN EXCEPTION TRY ,CATCH.
CODE:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
public class A {
OUTPUT:
Division by zero is not allowed.
try {
int a = 10;
int b = 0;
int result = a / b;
[Link]("Result: " + result); } finally {
OUTPUT:
This block always executes.
ERROR!
public class A {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int result = a / b;
[Link]("Result: " + result);
}
catch (ArithmeticException e) {
[Link](" Division by zero is not allowed.");
}
finally {
[Link]("This block always executes.");
}
}
OUTPUT:
Division by zero is not allowed.
This block always executes.
Q.1) W.A.J.P THAT HANDLES AN EXCEPTION ARRAY INDEX OUT OF BOUNDS EXCEPTION.
CODE:
class ArrayException {
public static void main(String[] args) {
try {
a[8] = 10;
[Link]("executable code");
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index out of bound");
}
finally{
[Link]("executable code");
} }}
OUTPUT:
Array index out of bound
executable code
int a = 10 / 0;
int arr[] = new int[5];
arr[10] = 8; }
catch (ArithmeticException e) {
[Link]("Arithmetic Exception handled"); }
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array Index Out Of Bound");
}
catch (Exception e) {
[Link]("General Exception");
}
}
}
OUTPUT:
Arithmetic Exception handled
=== Code Execution Successful ===
ASSIGNMENT-31
try { try {
int a = 10 / 0;
}
catch (ArithmeticException e) {
[Link]("Inner catch: Arithmetic Exception"); }
OUTPUT:
Inner catch: Arithmetic Exception
Outer catch: Array Index Out Of Bound
OUTPUT:
Eligible to vote
=== Code Execution Successful ===
Q.2) W.A.P IN JAVA THAT DEMONSTRATE THE APPLICATION OF THROW KEYWORD USING METHOD.
CODE:
class ThrowExample {
static void myHandler(int age) {
try {
if (age < 18) {
throw new ArithmeticException("Underage");
} else {
[Link]("You are eligible");
}
} catch (ArithmeticException e) {
[Link]("Exception caught");
[Link]([Link]());
} }
public static void main(String[] args) {
myHandler(18); } }
OUTPUT:
[Link](a);
}
myMethod();
}
catch (ArithmeticException e) {
[Link]("BCA stream Sahi hai");
}}}
OUTPUT:
BCA stream Sahi hai
=== Code Execution Successful ===
ASSIGNMENT-34
Q.1) [Link] TO DEMONSTRATE THE USE OF 'THROWS' KEYWORD IN METHOD SIGNATURE.
CODE:
class ThrowsExample {
try {
int a = 10 / 0;
[Link](a);
} }
catch (ArithmeticException e) {
} }}
OUTPUT:
try {
catch (ArithmeticException e) {
} }}
4 errors
ERROR!
int a = 20/0;
}
}
catch(ArithmeticException e){
[Link]("you can divide by zero");
}
}
}
OUTPUT:-
Assignment_35.java:5: error: illegal start of expression
Static void my_method()throws ArithmeticException
{
^
Assignment_35.java:5: error: ';' expected
Static void my_method()throws ArithmeticException
{
Assignment_35.java:5: error: ';' expected Static void my_method()throws
ArithmeticException
Assignment:-36
Q)Write a java [Link] implement the use of following string function in
java.
public class Assignment_36 {
public static void main(String[] args){
String x = new String("Shikha");
Syst
[Link](x);
//lenght()
[Link]();
[Link]([Link]());
// CharAt()
[Link](2);
[Link]([Link](2));
//IndexOf()
[Link]([Link]('h'));
//replace()
[Link]([Link]('R', 'P'));
// reverse()
StringBuffer ob = new StringBuffer("Suman");
[Link]([Link]());
}}
OUTPUT:-
shikha
6
a
3
shikha
namuS
ASSIGNMENT-37
Q)Write a java Program that implements the use of following java function.
public class Assignment_37 {
public static void main(String[] args){
String x = new String("Shikha");
//toupperCase()
[Link]([Link]());
//toLowercase()
[Link]([Link]());
//trim()
[Link]([Link]());
//Contains()
[Link]([Link](" sonawane"));
// join()
[Link]([Link]("-","b","h","a","v","n","a"));
//Split()
[Link]("shikha sonawane".split(" "));
//substring()
[Link]([Link](1,4));
//valueOf()
[Link]([Link](100));
//equals()
[Link]([Link]("shikha"));
// JOIN
String s =
[Link]("-","my","name","is","shikha");
[Link](s);
}}
OUTPUT:-
SHIKHA
shikha
Shikha
false
b-h-a-v-n-a
[[Link];@7ad041f3
avn
100
true
my-name-is-shikha
ASSIGNMENT-38
Q) Write a Program in java to add 2 matrix of same Dimensional
public class Assignment_38 {
public static void main(String[] args) {
int[][] x = { {1, 2, 3}, {4, 5, 6} };
int[][] y = { {2, 4, 6}, {8, 10, 12} };
int[][] z = new int[2][3];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
z[i][j] = x[i][j] + y[i][j];
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
[Link](z[i][j] + "\t");
}
[Link]();
}
}
}
OUTPUT:-
3 6 9
12 15 18
ASSIGNMENT-39
Q) Write a program in java to multiply the value of 2D array with the scare county 7.
public class Assignment_39 {
public static void main(String[] args){
int [][] a = {{2,4},{6,8}};
int [][] b = new int[2][2];
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
b[i][j] = 7 * a[i][j];
}
}
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
[Link](b[i][j] + "\t");
}
[Link]();
}
}
}
OUTPUT:-
14 28
42 56
Assignment:-40(A)
Q)Write a Program in java the write the without give the size element are inserted.
INPUT:-
public class Assignment_40 {
public static void main(String[] args){
int[][] x = { {1, 2, 3}, {4, 5, 6} };
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
[Link](x[i][j] + "\t");
}
}
}}
OUTPUT:-
1 2 4 5
ASSIGNMENT:-40(B)
Q) Write a program in java the write they give the sizeof array element are inserted.
INPUT:-
public class Assignment_40 {
public static void main(String[] args){
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
[Link](x[i][j] + "\t");
}
}
}}
OUTPUT:-
Assignment_40.java:5: error: ']' expected
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
^
Assignment_40.java:5: error: not a statement
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
^
Assignment_40.java:5: error: illegal start of expression
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
^
Assignment_40.java:5: error: not a statement
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
^
Assignment_40.java:5: error: ';' expected
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
^
Assignment_40.java:5: error: illegal start of expression
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
^
Assignment_40.java:5: error: not a statement
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
^
Assignment_40.java:5: error: ';' expected
int[2][3] x = { {1, 2, 3}, {4, 5, 6} };
ASSIGNMENT:-40(c)
Q)Write a program in java that using the Arrays Function.
INPUT:-
import [Link];
public class Assignment_41B {
public static void main(String[]args){
int [] x = {2,3,4,5,6,7};
[Link](x);
[Link]("Sorted Arrays"+
[Link](x));
for(int i=0;i<6;i++){
[Link](x[i] + " ");
}