Sunday, 12 March 2023
10. Write a program that accepts 5 even numbers from command line , if any of the numbers is odd then throw custom exception OddException and count such invalid numbers.
class OddException extends Exception
{
OddException(int a)
{
System.out.println("number is odd: " + a);
}
}
class Program10
{
public static void main(String args[])
{
int arr[] = new int[5];
try
{
for(int i=0; i<5; i++)
{
arr[i] = Integer.parseInt(args[i]);
if(arr[i] % 2 != 0)
throw new OddException(arr[i]);
}
}
catch(Exception e)
{
}
}
}
9. Write an application that converts between meters and feet. Its first command line argument is a number and second command line argument is either "centimeter" or "meter". If the argument equals "centimeter" displays a string reporting the equivalent number of meters. If this argument equals "meters", display a string reporting the equivalent number of centimeter. If unit is not given properly then generate custom exception Unitformatexception. If first argument is not proper format then generate numberformatexception. Generate other exception as per requirements. (1 meter=100 centimeter)
class Unitformatexception extends Exception
{
Unitformatexception(String s)
{
super("unit is not valid: " + s);
}
}
class Program9
{
public static void main(String args[])
{
int no;
String str;
try
{
no = Integer.parseInt(args[0]);
str = args[1];
if((str.equals("centimeter")) || (str.equals("meter")))
{
if(str.equals("centimeter"))
{
int m = no/100;
System.out.println("meter is : " + m);
}
else if(str.equals("meter"))
{
int cm = no*100;
System.out.println("centimeter is : " + cm);
}
else
throw new Unitformatexception(str);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
catch(NumberFormatException n)
{
System.out.println(n.getMessage());
}
catch(ArithmeticException a)
{
System.out.println(a.getMessage());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
8. Write a program which takes the age of 5 persons from command line and find the average age of all persons. The program should handle exception if the argument is not correctly formatted and custom exception if the age is not between 1 to 100.
class RangeException extends Exception
{
RangeException(int a)
{
System.out.println("\nage is not valid because not uner 1 to 100");
}
}
class Program8
{
public static void main(String args[])
{
int arr[] = new int[5];
try
{
for(int i=0; i<5; i++)
{
arr[i] = Integer.parseInt(args[i]);
if(arr[i] > 100 || arr[i] < 1)
throw new RangeException(arr[i]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
catch(NumberFormatException n)
{
System.out.println(n.getMessage());
}
catch(ArithmeticException a)
{
System.out.println(a.getMessage());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
7. Write an application that accepts marks of three different subject from user. Marks should be between 0 to 100, if marks of any of the subject is not belong to this range, generate custom exception out of RangeException. If marks of each subjects are greater than or equal to 40 then display message “PASS” along with percentage, otherwise display message “FAIL”. Also write exception handling code to catch all the possible runtime exceptions likely to be generated in the program.
import java.util.*;
class RangeException extends Exception
{
RangeException(int i)
{
super("RangeException : Marks " + i + " is invalid ");
}
}
class Program7
{
public static void main(String args[])
{
int arr[] = new int[3];
int sum=0;
float per = 0.0f;
for(int i=0; i<3; i++)
{
try
{
Scanner sc = new Scanner(System.in);
System.out.print("enter marks subject " + (i+1) + ": ");
arr[i] = sc.nextInt();
if(arr[i] < 0 || arr[i] > 100)
{
throw new RangeException(arr[i]);
}
else if(arr[i] >= 40)
{
sum = sum+arr[i];
System.out.println("\npass in subject " + i+1);
}
else
{
sum = sum+arr[i];
System.out.println("\nfail in subject " + i+1);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
catch(NumberFormatException n)
{
System.out.println(n.getMessage());
}
catch(ArithmeticException a)
{
System.out.println(a.getMessage());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
per = (float)sum / 3;
System.out.println("\nPercentage: " + per);
}
}
6. Write a program that takes a string from the user and validate it. The string should be at least 5 characters and should contain at least one digit. Display an appropriate valid message.
import java.util.*;
class ValidateString extends Exception
{
ValidateString(String s)
{
System.out.println("\nString is not valid");
}
}
class Program6
{
public static void main(String args[])
{
System.out.print("enter a String under 5 Characters and
one character is number: ");
Scanner sc = new Scanner(System.in);
String str = sc.next();
char arr[] = str.toCharArray();
try{
int flag = 0;
if(str.length() == 5)
{
for(int i=0; i<5; i++)
{
if(Character.isDigit(arr[i]))
{
flag = 1;
}
}
if(flag == 1)
System.out.println("String is valid");
else
throw new ValidateString(str);
}
else
throw new ValidateString(str);
}
catch(Exception e)
{ }
}
}
5. Write a Java program to input n integer numbers and display lowest and second lowest number. Also handle the different exceptions possible to be thrown during execution.
import java.util.*;
class Program5
{
public static void main(String args[])
{
try
{
System.out.println("how many numbers you want to enter: ");
Scanner sc = new Scanner(System.in);
int no = sc.nextInt();
int arr[] = new int[no];
System.out.println("\nenter number one by one: ");
for(int i=0; i<arr.length; i++)
{
arr[i] = sc.nextInt();
}
int temp;
for(int i=0; i<arr.length; i++)
{
for(int j=0; j<arr.length; j++)
{
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("\nlowest number: " + arr[0] + "\nsecond lowwest: "
+ arr[1]);
}
catch(ArithmeticException e)
{
System.out.println("Arithmatic exception occured");
}
catch(ArrayIndexOutOfBoundsException a)
{
System.out.println("ArrayIndexOutOfBoundsException");
}
catch(NumberFormatException n)
{
System.out.println("NumberFormatException please enter only a numbers");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
4. Write a program that accepts a string from command line and perform following operations: 1. Display each character on separate line in reverse order. 2. Count total number of chracters and display each character's position too. 3. Identify that whether the string is palindrom or not. 4. Count total number of uppercase and lowercase characters in it.
class Program4
{
public static void main(String args[])
{
char arr[] = args[0].toCharArray();
char temp[] = new char[arr.length];
int flag=0;
for(int i=arr.length-1; i>=0; i--)
{
System.out.println(arr[i]);
temp[arr.length-1-i] = arr[i];
}
for(int i=0; i<arr.length; i++)
{
System.out.println(arr[i] + " character at " + i + " position");
}
System.out.println("total number of characters: " + arr.length);
for(int i=0; i<arr.length; i++)
{
if(arr[i] == temp[i])
flag++;
//System.out.print(" " + flag);
}
if(flag == arr.length)
System.out.println("\nString palindrom");
else
System.out.println("\nString is not palindrom");
System.out.println("\nCount total number of uppercase and lowercase
characters in i: ");
int up=0,lw=0;
for(int i=0; i<arr.length; i++)
{
if(Character.isUpperCase(arr[i]))
up++;
else
lw++;
}
System.out.println("\nupper case: " + up + "\nlower case: " + lw);
}
}
3. Create package pack1 within this package create class A which contains one instance variable and one instance method. Create another package pack2 within this package create class B. where class B is calling the method and variable of class A
/* save as a file 1 -> A.java */
package Pack1;
public class A
{
public int var1 = 10;
public void print()
{
System.out.println("this is pack1 in class A print method");
}
}
---------------------------------------------------------------------------
/* save as a file 2 -> B.java */
package Pack2;
import Pack1.*;
class B
{
public static void main(String args[])
{
A obj = new A();
System.out.println("pack1 in class a instance variable value is: " + obj.var1);
obj.print();
}
}
2. Write a program that creates three different classes in three different packages and access them from default package. All the three packages should be at the same level.
//* save as s file 1 Class1.java*/fi/*
package Pack1;
public class Class1
{
public void print()
{
System.out.println("\npack 1 class 1");
}
}
-------------------------------------------------------------------
/* save as a file 2 Class2.java */
package Pack2;
public class Class2
{
public void print()
{
System.out.println("\npack 2 class 2");
}
}
-------------------------------------------------------------------
/* save as a file 3 Class3.java */
package Pack3;
public class Class3
{
public void print()
{
System.out.println("\npack 3 class 3");
}
}
-------------------------------------------------------------------
/* save as a file 4 Program2.java */
package pack;
import Pack1.*;
import Pack2.*;
import Pack3.*;
public class Program2
{
public static void main(String args[])
{
Class1 c1 = new Class1();
Class2 c2 = new Class2();
Class3 c3 = new Class3();
c1.print();
c2.print();
c3.print();
}
}
1. Create a package P and within that package create class PackClass which have method called findmax( ) which find maximum value from three numbers. Now import the package within another class DemoClass and now display the maximum number.
/* file 1 Packclass.java */
package P;
public class Packclass
{
public int findmax(int a,int b,int c)
{
int max = (a>b)? ((a>c)? a:c) : ((b>c)? b:c);
return max;
}
}
---------------------------------------------------------------------------
/* file 2 Program1.java */
import P.Packclass;
import java.util.*;
class Program1
{
public static void main(String args[])
{
Packclass p1 = new Packclass();
Scanner sc = new Scanner(System.in);
System.out.print("enter three number: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println("\nmaximum number is: "+ p1.findmax(a,b,c));
}
}
Unit 3 Java Programs
2. Write a program that creates three different classes in three different packages and access them from default package. All the three packages should be at the same level.
5. Write a Java program to input n integer numbers and display lowest and second lowest number. Also handle the different exceptions possible to be thrown during execution.
10. The abstract Vegetable class has four subclasses named cabbage, carrot and potato. Write an application that demonstrates how to establish this class hierarchy. Declare one instance variable of type string that indicates the color of a vegetable. Create and display instances of these object. Override the toString() method of object to return a string with the name of the vegetable and its color.
abstract class Vegetable
{
String color;
abstract String tostring();
}
class Cabbage extends Vegetable
{
String tostring()
{
color = "green";
return "cabbage color: "+color;
}
}
class Carrot extends Vegetable
{
String tostring()
{
color = "red";
return "carrot color: "+color;
}
}
class Patato extends Vegetable
{
String tostring()
{
color = "yellow";
return "patato color: "+color;
}
}
class Program10
{
public static void main(String args[])
{
Cabbage obj1 = new Cabbage();
Carrot obj2 = new Carrot();
Patato obj3 = new Patato();
System.out.println(obj1.tostring());
System.out.println(obj2.tostring());
System.out.println(obj3.tostring());
}
}
9. Create class calculation with an abstract method area( ). Create Rectangle and Triangle subclasses of calculation and find area of rectangle and triangle.
import java.util.*;
abstract class Calculation
{
abstract void area();
double area;
}
class Rectangle extends Calculation
{
double l;
void area()
{
System.out.print("\nenter l for Rectangle: ");
Scanner sc = new Scanner(System.in);
l = sc.nextDouble();
area = l * l;
System.out.println("\narea of rectangle: " + area);
}
}
class Triangle extends Calculation
{
double l,b;
void area()
{
System.out.print("\nenter l for Triangle: ");
Scanner sc = new Scanner(System.in);
l = sc.nextDouble();
System.out.print("\nenter b for Triangle: ");
b = sc.nextDouble();
area = (l*b)/2;
System.out.println("\narea of triangle: " + area);
}
}
class Program9
{
public static void main(String args[])
{
Triangle t1 = new Triangle();
t1.area();
Rectangle r1 = new Rectangle();
r1.area();
}
}
8. Write an interface called Exam with a method Pass(int mark) that returns a Boolean. Write another interface called Classify with a method Division(int average) which returns a string. Write a class called Result which implements both Exam and Classify. The pass method should return true if the marks is greater than or equal to 35 else false. The division method must return “First” when the parameter average is 60 or more, “second” when average is 50 or more but below 60, “no division” when average is less than 50.
import java.util.*;
interface Exam
{
boolean pass(int mark);
}
interface Classify
{
String division(int average);
}
class Result implements Exam,Classify
{
public boolean pass(int mark)
{
if(mark >= 35)
return true;
else
return false;
}
public String division(int average)
{
if(average >= 60)
return ("first");
else if(average >= 50)
return ("second");
else
return ("no division");
}
}
class Program8
{
public static void main(String args[])
{
boolean pass;
String division;
Result r1 = new Result();
Scanner sc = new Scanner(System.in);
System.out.print("\nenter your marks: ");
int marks = sc.nextInt();
System.out.print("\nenter average: ");
int avg = sc.nextInt();
pass = r1.pass(marks);
division = r1.division(avg);
if(pass)
{
System.out.println("\npass " + "-" + division);
}
else
{
System.out.println("\nfail " + "-" + division);
}
}
}
7. Declare an abstract class Vehicle with an abstract method named numWheels( ).provide subclasses Car and Truck that each implements this method. Create instance of these subclasses and demonstrate the use of this method
abstract class Vehicle
{
abstract void numwheels();
}
class Car extends Vehicle
{
void numwheels()
{
System.out.println("\nnumber of wheels in car: 4");
}
}
class Truck extends Vehicle
{
void numwheels()
{
System.out.println("\nnumber of wheels in truck: 6");
}
}
class Program7
{
public static void main(String args[])
{
Vehicle obj = new Car();
obj.numwheels();
obj = new Truck();
obj.numwheels();
}
}
6. Create a class called NumberData that accept any array of the five numbers. Create a sub class called Numplay which provides methods for followings: 1. Display numbers entered. 2. Sum of the number. 3. Average of the numbers. 4. Maximum of the numbers. 5. Minimum of the numbers. Create a class that provides menu for above methods. Give choice from the command-line argument.
import java.util.*;
class NumberData
{
int arr[] = new int[5];
void createarray()
{
Scanner sc = new Scanner(System.in);
System.out.println("\nenter a elements: ");
for(int i=0; i<5; i++)
{
arr[i] = sc.nextInt();
}
}
}
class Numpay extends NumberData
{
void display()
{
System.out.print("your array is: ");
for(int i=0; i<5; i++)
{
System.out.print(arr[i] + " ");
}
}
void sum()
{
int temp=0;
for(int i=0; i<5; i++)
{
temp = temp + arr[i];
}
System.out.print("\nSum is: " + temp);
}
void avg()
{
int temp=0;
for(int i=0; i<5; i++)
{
temp = temp + arr[i];
}
float x = (float)temp / 2;
System.out.print("\nAverage is: " + x);
}
void max()
{
int m = arr[0];
for(int i=0; i<5; i++)
{
if(m < arr[i])
{
m = arr[i];
}
}
System.out.print("\nMax is: " + m);
}
void min()
{
int mn = arr[0];
for(int i=0; i<5; i++)
{
if(mn > arr[i])
{
mn = arr[i];
}
}
}
}
class Program6
{
public static void main(String args[])
{
int ch = Integer.parseInt(args[0]);
Numpay n1 = new Numpay();
switch(ch)
{
case 1: n1.createarray();
n1.display();
break;
case 2: n1.createarray();
n1.sum();
break;
case 3: n1.createarray();
n1.avg();
break;
case 4: n1.createarray();
n1.max();
break;
case 5: n1.createarray();
n1.min();
break;
default : System.out.println("\nenter a less than 6");
}
}
}
Subscribe to:
Posts (Atom)
python programs
1. sum of two number
-
● Stack ● Queue ● Searching ● Sorting
-
Semester 1 Semester 2 Semester 3 Semester 4 NOTICE: Click To Text And Open It
-
1. Find the Simple Interest. Inputs are principal amount, period in year and rate of interest. 2. Find the area and perimeter of square a...