Skip to main content

Write a program to print even numbers between 23 and 57. Each number should be printed in a separate row.

Write a program to print even numbers between 23 and 57. Each number should be printed in a separate row.




public class PrimeNumbersBetween {


public static void main(String[] args) {

          int check,i;

          for(i=0;i<=99;i++) {

          check=0;

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

          if(i%j==0) {

          check=1;

          break;

          }

          }

          if(check==0)

          System.out.println(i);

          }

}


}




Write a program to accept gender ("Male" or "Female") and age from command line arguments and print the percentage of interest based on the given conditions.


If the gender is 'Female' and age is between 1 and 58, the percentage of interest is 8.2%.


If the gender is 'Female' and age is between 59 and 100, the percentage of interest is 9.2%.


If the gender is 'Male' and age is between 1 and 58, the percentage of interest is 8.4%.


If the gender is 'Male' and age is between 59 and 100, the percentage of interest is 10.5%.



//Use One Dimensional Array. 

>> If you have any alternate methods then please comment below.




public class GenderInterest {

public static void main(String[] args)
 {
String gender=args[0];
int age=Integer.parseInt(args[1]);

if(gender.equalsIgnoreCase("Female")) {

   if(age>=1&&age<=58)
        System.out.println("Interest=8.2%");

else if(age>58&&age<=100)
    System.out.println("Interest=9.2%");

else
System.out.println("No result");

}
else {

       if(age>=1&&age<=58)
      System.out.println("Interest=8.4%");

       else if(age>58&&age<=100)
       System.out.println("Interest=10.5%");

   else 
   System.out.println("No result");

}

}

}



Create an abstract class Compartment to represent a rail coach. Provide an abstract function notice in this class. 


public abstract String notice();


Derive FirstClass, Ladies, General, Luggage classes from the compartment class. Override the notice function in each of them to print notice message that is suitable to the specific type of  compartment.


Create a class TestCompartment.Write main function to do the following:

Declare an array of Compartment of size 10.

Create a compartment of a type as decided by a randomly generated integer in the range 1 to 4.

Check the polymorphic behavior of the notice method.

[i.e based on the random  number genererated, the first compartment can be Luggage, the second one could be Ladies and so on..]


Abstract Class

A class which contains the abstract keyword in its declaration is known as abstract class.

  • Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )
  • But, if a class has at least one abstract method, then the class must be declared abstract.
  • If a class is declared abstract, it cannot be instantiated.
  • To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
  • If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.


>> If you have any alternate methods then please comment below.



public abstract class Compartment {

abstract void notice();

}

public class FirstClass extends Compartment {

void notice()

{

System.out.println("It is FirstClass");

}

}

public class Ladies extends Compartment {

void notice()

{

System.out.println("It is Ladies Compartment");

}

}

public class General extends Compartment {

void notice()

{

System.out.println("It is General Compartment");

}

}

public class Luggage extends Compartment {

void notice()

{

System.out.println("It is Luggage");

}

}

public class TestCompartment {

public static void main(String[] args) {

Compartment c[] = new Compartment[10];
double i = Math.random()*5;
int x = (int)i;
System.out.println(x);
switch(x)
{

case 1: c[0] = new FirstClass();
            c[0].notice();
            break;
case 2: c[1] = new Ladies();
                            c[1].notice();
                            break;
        
case 3: c[2] = new General();
                            c[2].notice();
                            break;
        
case 4: c[3] = new Luggage();
                            c[3].notice();
                            break;
        
                default: System.out.println("Invalid");

}

}

}



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

Popular posts from this blog

Classification of Elements & Periodicity in Properties Handwritten Notes | Chemistry Class 11 Chapter 3 | Classification of Elements & Periodicity in Properties Notes for Board Exams | STAR tube Notes

Classification of Elements & Periodicity in Properties Handwritten Notes | Chemistry Class 11 Chapter 3 | Classification of Elements & Periodicity in Properties Notes for Board Exams | STAR tube Notes Chapter 3 Classification of Elements & Periodicity in Properties Classification of Elements & Periodicity in Properties Notes Higher Secondary is the most crucial stage of school education because at this juncture specialized discipline based, content  ‐oriented courses are introduced. Students reach this stage after 10 years of general education and opt for Chemistry with a purpose of pursuing their career in basic sciences or professional courses like medicine, engineering, technology and study courses in applied areas of science and technology at tertiary level. Therefore, there is a need to provide learners with sufficient conceptual background of Chemistry, which will make them competent to meet the challenges of academic and professional courses after the senior seco

NCERT All Chapters Handwritten Notes | Physics Chemistry Best Handwritten Notes | STAR tube Notes

NCERT All Chapters Handwritten Notes | Physics Chemistry Best Handwritten Notes | STAR tube Notes Are you looking for handwritten notes which are easy and simple to understand.Then you are on the right place because here you would get handwritten notes which are very easy to understand. It's not possible to cover whole syllabus and revise it during exam time because you have to revise lots of subjects in very less time. In this case,notes are one of the best option to cover whole syllabus in very short period of time. STAR tube will provide you the Best Handwritten Notes of Class 11 , Class 12 , Btech/Bsc Electrical and Electronics. Follow and Subscribe the STAR tube on  YouTube . Chemistry Handwritten Notes | Class 11 Unit I : Some Basic Concepts of Chemistry   -    PDF Unit II : Structure of Atom   -    PDF Unit III : Classification of Elements and Periodicity in Properties   -    PDF Unit IV : Chemical Bonding and Molecular Structure   -    PDF Unit V : States of Matter: Gas

Some Basic Concepts of Chemistry Handwritten Notes | Chemistry Class 11 Chapter 1 | Some Basic Concepts of Chemistry Notes for Board Exams | STAR tube Notes

Some Basic Concepts of Chemistry Handwritten Notes | Chemistry Class 11 Chapter 1 | Some Basic Concepts of Chemistry Notes for Board Exams | STAR tube Notes Chapter 1 Some Basics Concepts of Chemistry Some Basic Concepts of Chemistry Best Handwritten Notes Higher Secondary is the most crucial stage of school education because at this juncture specialized discipline based, content  ‐oriented courses are introduced. Students reach this stage after 10 years of general education and opt for Chemistry with a purpose of pursuing their career in basic sciences or professional courses like medicine, engineering, technology and study courses in applied areas of science and technology at tertiary level. Therefore, there is a need to provide learners with sufficient conceptual background of Chemistry, which will make them competent to meet the challenges of academic and professional courses after the senior secondary stage. STAR tube will provide you the Best Handwritten Notes of Class 11 ,