Write a program to reverse a given number and print Example1) I/P: 1234 O/P:4321 Example2) I/P:1004 O/P:4001
Write a program to reverse a given number and print
Example1)
I/P: 1234
O/P:4321
Example2)
I/P:1004
O/P:4001
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args)
{
int n,rem;
Scanner s=new Scanner(System.in);
System.out.println("I/P:");
n=s.nextInt();
while(n!=0) {
rem=n%10;
System.out.println(rem);
n=n/10;
}
}
}
Write a program to print the sum of the elements of an array following the given below condition.
If the array has 6 and 7 in succeeding orders, ignore the numbers between 6 and 7 and consider the other numbers for calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10
//Use One Dimensional Array.
>> If you have any alternate methods then please comment below.
import java.io.*;
public class ConditionalSum {
static void conditionalSum(int l[], int a, int b) {
int sum = 0;
boolean add = true;
for (int i=0; i<l.length; i++) {
if (l[i] != a && add == true)
sum = sum + l[i];
else if (l[i] == a)
add = false;
else if (l[i] == b)
add = true;
}
System.out.println(sum);
}
public static void main(String[] args) {
int arr[] = {10,3,6,1,2,7,9};
int a = 6;
int b = 7;
conditionalSum(arr,a,b);
}
}
Write an interface called Playable, with a method void play(); Let this interface be placed in a package called music. Write a class called Veena which implements Playable interface. Let this class be placed in a package music.string Write a class called Saxophone which implements Playable interface. Let this class be placed in a package music.wind Write another class Test in a package called live. Then, a. Create an instance of Veena and call play() method b. Create an instance of Saxophone and call play() method c. Place the above instances in a variable of type Playable and then call play() Interfaces
//if you have any alternate method comment down below.
import music.Playable;
import music.string.Veena;
import music.wind.Saxophone;
public class Test {
public static void main(String[] args) {
Veena v = new Veena();
v.play();
Playable p = new Veena();
p.play();
Saxophone s = new Saxophone();
s.play();
Playable ps = new Saxophone();
ps.play();
}
}
package music;
public interface Playable {
void play();
}
package music.string;
import music.Playable;
public class Veena implements Playable {
public void play() {
System.out.println("Veena is Playing");
}
}
package music.wind;
import music.Playable;
public class Saxophone implements Playable {
public void play() {
System.out.println("Saxophone is Playing");
}
}
Write a program that takes as input the size of the array and the elements in the array. The program then asks the user to enter a particular index and prints the element at that index. Index starts from zero. This program may generate Array Index Out Of Bounds Exception or NumberFormatException . Use exception handling mechanisms to handle this exception. Sample Input and Output 1: Enter the number of elements in the array 2 Enter the elements in the array 50 80 Enter the index of the array element you want to access 1 The array element at index 1 = 80 The array element successfully accessed Sample Input and Output 2: Enter the number of elements in the array 2 Enter the elements in the array 50 80 Enter the index of the array element you want to access 9 java.lang.ArrayIndexOutOfBoundsException Sample Input and Output 3: Enter the number of elements in the array 2 Enter the elements in the array 30 j java.lang.NumberFormatException
//If you have any alternate method then comment below.
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionHandling {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the arrays");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements in the array ");
try {
for (int i = 0; i<n; i++)
arr[i] = sc.nextInt();
System.out.println("Enter the index of the array element you want to access");
int index = sc.nextInt();
System.out.println("The array element at index "+ index + " = "+ arr[index]);
System.out.println("The array element successfully accessed");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("java.lang.ArrayIndexOutOfBoundsException");
}catch (InputMismatchException e) {
System.out.println("java.util.InputMismatchException");
}
sc.close();
}
}
Write a Program to take care of Number Format Exception if user enters values other than integer for calculating average marks of 2 students. The name of the students and marks in 3 subjects are taken from the user while executing the program. In the same Program write your own Exception classes to take care of Negative values and values out of range (i.e. other than in the range of 0-100)
//If you have any alternate method then comment below.
import java.util.Scanner;
public class ExceptionHandling3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i=0; i<2; i++) {
String StudentName = "";
int sub1 = 0;
int sub2 = 0;
int sub3 = 0;
try {
StudentName = sc.nextLine();
if (sc.hasNextInt())
sub1 = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
sub2 = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
sub3 = sc.nextInt();
else
throw new NumberFormatException();
if (sub1 < 0) throw new NegativeValuesException();
if (sub1 > 100) throw new ValuesOutOfRangeException();
if (sub2 < 0) throw new NegativeValuesException();
if (sub2 > 100) throw new ValuesOutOfRangeException();
if (sub3 < 0) throw new NegativeValuesException();
if (sub3 > 100) throw new ValuesOutOfRangeException();
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} catch (NegativeValuesException e) {
System.out.println(e.getMessage());
} catch (ValuesOutOfRangeException e) {
System.out.println(e.getMessage());
}
System.out.println("Name: "+StudentName);
System.out.println("Marks of subject 1: "+sub1);
System.out.println("Marks of subject 2: "+sub2);
System.out.println("Marks of subject 3: "+sub3);
}
sc.close();
}
}
Comments
Post a Comment