0% found this document useful (0 votes)
2 views7 pages

Java Ref

The document contains multiple Java programs demonstrating various concepts including string reversal with and without the reverse() function, finding prime numbers between 1 to 100, handling unchecked exceptions like ArrayIndexOutOfBoundsException and NullPointerException, sorting an ArrayList in ascending order, and implementing multiple inheritance using interfaces. Each program includes code snippets and expected outputs. The programs are structured within a package named com.StarAgile.

Uploaded by

achunavinkumar
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)
2 views7 pages

Java Ref

The document contains multiple Java programs demonstrating various concepts including string reversal with and without the reverse() function, finding prime numbers between 1 to 100, handling unchecked exceptions like ArrayIndexOutOfBoundsException and NullPointerException, sorting an ArrayList in ascending order, and implementing multiple inheritance using interfaces. Each program includes code snippets and expected outputs. The programs are structured within a package named com.StarAgile.

Uploaded by

achunavinkumar
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

1.

Write a Java program to reverse a string with and without reverse() function

package [Link];

import [Link];

public class ReverseString {

public static void main(String[] args) {

// TODO Auto-generated method stub

String name;

Scanner scn = new Scanner([Link]);

[Link]("Enter name");

name = [Link]();

// String name = "Banti nayak";

String reverse = "";

for (int i = [Link]() - 1; i >= 0; i--) {

reverse = reverse + [Link](i);

[Link]("with-out reverse() function :" + " " + reverse);

StringBuffer y = new StringBuffer(name);// with reverse()

[Link]();

[Link]("with reverse() function by using String bufferclass :"


+ " " + y);

[Link]();

}
Output-Enter name

banti nayak

with-out reverse() function : kayan itnab

with reverse() function by using String bufferclass : kayan itnab

2. Write a Java Program to find prime numbers between 1 to 100

package [Link];

public class PrimeNumber {

public static void main(String[] args) {

//find prime number between 1 to 100

int i, j, num = 100;

[Link]("Prime number between 1 to 100 are :" );

for (i = 2; i <= num; i++) {

for (j = 2; j < i; j++) {

if (i != j) {

if (i % j != 0)

continue;

else

break;

if (i == j)

[Link](i + ",");

}
Output-

Prime number between 1 to 100 are :

2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,

3. Write a Java Program to handle given uncheck exception


[Link]

[Link]

package [Link];

public class ExceptionProgram {

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

int arr[] = { 1, 2, 3, 4 };

[Link](arr[9]);

} catch (ArrayIndexOutOfBoundsException a) {

[Link](a);// get message ArrayIndexOutOfBoundsException

try {

int arr[] = null;

[Link]([Link]);

} catch (NullPointerException f) {

[Link](f);// get message NullPointerException

}
[Link]: Index 9 out of bounds for
length 4

[Link]: Cannot read the array length because "arr" is null

4. Write a Java Program to sort the ArrayList in Ascending order

package [Link];

import [Link];

import [Link];

public class AssigmentOrderAscendingOrder {

public static void main(String[] args) {

// TODO Auto-generated method stub

ArrayList<Integer> d = new ArrayList<>();

[Link](20);

[Link](10);

[Link](60);

[Link](35);

[Link](95);

[Link]("Print the orginal array :" + d);

[Link](d);

[Link]("Sorted ArrayList with Ascending Order" + d);

Output-Print the orginal array :[20, 10, 60, 35, 95]

Sorted ArrayList with Ascending Order[10, 20, 35, 60, 95]


5. Write a Java Program to implement multiple inheritance

package [Link];

interface A {

void attack();

interface B {

void Defend();

class Worrier implements A, B {

@Override

public void Defend() {

// TODO Auto-generated method stub

[Link]("interface A");

@Override

public void attack() {

// TODO Auto-generated method stub

[Link]("INTERFACE B");

class Worrier2 implements A, B {

@Override

public void Defend() {

// TODO Auto-generated method stub

[Link]("ATTACK WITH KNIFE");


}

//

@Override

public void attack() {

// TODO Auto-generated method stub

[Link]("DEFEND WITH KNIFE");

public static void main(String[] args) {

// TODO Auto-generated method stub

package [Link];

public class WorrierClassCalled {

public static void main(String[] args) {

// TODO Auto-generated method stub

Worrier2 e = new Worrier2();

[Link]();

[Link]();

Worrier d = new Worrier();

[Link]();

[Link]();

}}

Output-DEFEND WITH KNIFE

ATTACK WITH KNIFE

INTERFACE B

interface A

You might also like