-----------------------------------------------------------------------------------------------------------------
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.
//Package p Within Package Class Name Is Packclass
package p;
public class Packclass
{
public void findmax()
{
int a=5;
int b=44;
int c=23;
if(a>b||a>c)
{
System.out.println("Maxiumum Value is :-"+a);
}
else if(b>c)
{
System.out.println("Maximum value is:- "+b);
}
else
{
System.out.println("Maximum Value is:-"+c);
}
}
public static void main(String[] args)
{
//Packclass p1=new Packclass();
System.out.println("Max:-");
}
}
//Another Package
package mypack;
import p.*; //Import mypack package class Packclass
public class democlass
{
public static void main(String[] args)
{
Packclass p1=new Packclass();
p1.findmax();
}
}
//Run Above Java Package Program:-
//For Run and Complie Java Package Program:-//For Complie :- javac -d . class_name.java
//For Run:- java package_name.class_name
package second;
public class Triangle
{
public static void tri()
{
System.out.println("Triangle has a 3 Sides");
}
public static void main(String[] args)
{
tri();
}
}
//Third Package
package three;
public class Rectangle
{
public static void rect()
{
System.out.println("Rectangle has a 4 Sides");
}
public static void main(String[] args)
{
rect();
}
}
//Import All Package In Class Shape
package dpack;
import first.*;
import second.*;
import three.*;
public class shape
{
public static void main(String[] args)
{
square s1=new square();
s1.sqre();
Triangle t1=new Triangle();
t1.tri();
Rectangle r1= new Rectangle();
r1.rect();
}
}
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
//Package pack1 with class A
package pack1;
public class A
{
String Name;
int Roll_No;
public A(String nam,int r_no)
{
Name=nam;
Roll_No=r_no;
}
public void ShowDetails()
{
//String Name="Aarsh";
//int Roll_No=59;
System.out.println("Student Name := "+Name +" Roll No:-"+Roll_No);
}
public static void main(String[] args)
{
System.out.println("This is a Pack1 Package");
}
}
//Package pack2 with Class B
package pack2;
import pack1.*; //Import the package pack1 all Class
public class B
{
public static void main(String[] args)
{
A a =new A("Aarsh",59);
a.ShowDetails();
}
}
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
import java.util.*;
class operations
{
public void reverese(String str)
{
String strrev="";
int strlength=str.length();
int i;
for(i=strlength-1;i>=0;i--)
{
strrev=strrev +str.charAt(i);
}
System.out.println("Reverse String is:- " +strrev);
}
public void Position(String str)
{
int strlength=str.length();
int i;
System.out.println("Total Number Of Character is:- "+strlength);
for(i=strlength-1;i>=0;i--)
{
System.out.println("On Position "+i+" Character is "+str.charAt(i));
}
}
public void palindrom(String str)
{
String strrev="";
int strlength=str.length();
int i;
for(i=strlength-1;i>=0;i--)
{
strrev=strrev +str.charAt(i);
}
if(str==strrev)
{
System.out.println("String is Palindrom");
}
else
{
System.out.println("String is not Palindrom");
}
}
void count(String str)
{
int upper=0,lower=0,i;
int strlength=str.length();
for(i=strlength-1;i>=0;i--)
{
char ch=str.charAt(i);
if(ch>='A' && ch<='Z')
{
upper++;
}
else if(ch>='a' && ch<='z')
{
lower++;
}
}
System.out.println("Total Number Of Upper Case In String is:- "+upper);
System.out.println("Total Number Of Lower Case In String is:- "+lower);
}
}
public class u3_p4
{
public static void main(String[] args)
{
operations a= new operations();
//a.reverese("Aarsh");
String str;
System.out.println("Enter String:-");
Scanner s1=new Scanner(System.in);
str=s1.nextLine();
a.reverese(str);
a.Position(str);
a.palindrom(str);
a.count(str);
}
}
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 u3_p5
{
public static void main(String[] args)
{
Scanner s1=new Scanner(System.in);
int size,temp;
System.out.println("How Many Numbers You Want to Enter:- ");
size=s1.nextInt();
int a[]=new int[size];
try
{
for(int i=0;i<size;i++)
{
int j=i;
System.out.println("Enate "+(j+1) +"Element:- ");
a[i]=s1.nextInt();
}
for(int i=0;i<size-1;i++)
{
for(int j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Lowest Number is :- "+a[0]);
System.out.println("Secound Lowest Number Is:- "+a[1]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
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.*;
public class u3_p6
{
public static final int passwordlength=10;
public static void main(String[] args)
{
String str;
Scanner s1=new Scanner(System.in);
System.out.println("1.Passworld must be have 5 Characters\n2.Passworld has at least one digit\n Enter Password:- ");
str=s1.nextLine();
if(valid(str))
{
System.out.println("Passworld is valid:- "+str);
}
else
{
System.out.println("Passworld is not valid:- "+str);
}
}
public static boolean valid(String Passworld)
{
int a=Passworld.length();
int i;
int nchar=0,ndigit=0;
for(i=0;i<a;i++)
{
char ch=Passworld.charAt(i);
if(ch>='a'&&ch<='z')
{
nchar++;
}
else if(ch>='A'&&ch<='Z')
{
nchar++;
}
else if(ch>='0'&&ch<='9')
{
ndigit++;
}
/*else if(a<passwordlength)
{
return true;
}*/
}
if(nchar>=5&&ndigit>=1)
{
return true;
}
return false;
}
}
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.*;
public class u3_p7
{
public static void main(String[] args)
{
Scanner s1= new Scanner(System.in);
int subA,subB,subC;
String name;
try
{
System.out.println("Enter Student NAme:- ");
name=s1.nextLine();
System.out.println("Enter subA Markse:- ");
subA=s1.nextInt();
System.out.println("Enter subB Markse:- ");
subB=s1.nextInt();
System.out.println("Enter subC Markse:- ");
subC=s1.nextInt();
if(subA>=40&&subB>=40&&subC>=40)
{
System.out.println("pass");
int x= subA+subB+subC/3;
System.out.println("Persantage is:- "+x);
}
else
{
System.out.println("Fail");
}
}
catch(IndexOutOfBounds Exception e)
{
System.out.println(e);
}
}
}
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.
import java.util.*;
public class u3_p10 extends Exception
{
public static void main(String[] args)
{
Scanner s1=new Scanner(System.in);
int a[]=new int[5];
int invalid=0;
int i;
for(i=0;i<5;i++)
{
int j=i;
System.out.println("Enter "+(j+1)+"Number: ");
a[i]=s1.nextInt();
}
try
{
System.out.println("You Enter Even number is:- ");
for(i=0;i<5;i++)
{
if(a[i]%2==0)
{
int j=i;
System.out.println( (j+1)+"Number: "+a[i]);
}
else if(a[i]%2!=0)
{
invalid++;
}
}
if(invalid>0)
{
System.out.println("Total invlaid number is:- ");
throw ( new oddException("Invalid"));
}
}
catch(Exception e)
{
System.out.println("Error:- "+ e);
}
}
}
class oddException extends Exception
{
oddException(String error)
{
super(error);
}
}
/*class OddException extends Exception
{
OddException()
{
super("Input Only Even Number!");
}
OddException(String msg) //When Throw with Argument like throw new OddException(String s);
{
super(msg);
}
}*/
-------------------------------------------------The End--------------------------------------------------
No comments:
Post a Comment