Java Term Work Mid Arpita
Java Term Work Mid Arpita
NO : 44
CODE 01:
package termwork_java;
import java.util.Scanner;
public class Count_character_in_string {
int i , j,count=0 ;
for(i=0 ; i<l ; i++)
{
if(dup[i]==0 && arr[i]!=' ')
{
count=0;
for(j=0 ; j<l ; j++)
{
if(arr[i]==arr[j])
{
count++;
dup[j]=-1 ;
}
}
if(count!=0)
{
dup[i]= count ;
}
}
}
for(i=0 ; i<l ; i++)
{
if(dup[i]!=0 && dup[i]!=-1)
{
System.out.println(arr[i]+" = "+dup[i]);
}
}
}}
ARPITA GUPTA MCA II – A ROLL.NO : 44
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 02:
package termwork_java;
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 03:
package termwork_java;
import java.util.Scanner;
class String_Operations
{
Scanner sc = new Scanner(System.in);
void string_to_integer()
{
System.out.println("Enter string to convert in integer");
String s = sc.nextLine();
int s1 = Integer.parseInt(s);
System.out.println("Integer : "+s1);
String s2 = String.valueOf(s1);
System.out.println("String Again : "+s2);
}
void string_change_case()
{
System.out.println("Enter String for case conversion");
String s = sc.nextLine();
System.out.println("UpperCase : "+s.toUpperCase());
System.out.println("LowerCase : "+s.toLowerCase());
}
void extract_no_of_characters()
{
System.out.println("Enter String for getting no of characters");
String s = sc.nextLine();
int l = s.length();
int c=0 ;
for(int i=0 ; i<l ; i++)
{
if(s.charAt(i)!=' ')
c++;
}
System.out.println("No of characters : "+c);
}
void compare_strings()
{
System.out.println("Enter two Strings for comparing");
String s1 = sc.nextLine();
String s2 = sc.nextLine();
System.out.println("using equal method -- "+s1.equals(s2));
ARPITA GUPTA MCA II – A ROLL.NO : 44
a.string_to_integer();
a.string_change_case();
a.extract_no_of_characters();
a.compare_strings();
a.substring_search_and_replace();
a.count_various_characters();
}
}
ARPITA GUPTA MCA II – A ROLL.NO : 44
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 04:
package termwork_java;
import java.util.Scanner;
class Employee
{
Scanner sc = new Scanner(System.in);
String name ,pan;
long taxIncome;
double tax=0 ;
void inputinfo()
{
System.out.println("Enter name of employee");
name = sc.nextLine();
System.out.println("Enter Pan_Number of employee");
pan = sc.nextLine();
System.out.println("Enter Income of employee");
taxIncome = sc.nextLong();
}
void taxcalc()
{
if(taxIncome >=250000 && taxIncome <=300000 )
{
tax = (10.0/100)*(taxIncome-250000);
}
else if(taxIncome >300000 && taxIncome <= 400000 )
{
tax = (10.0/100)*50000 + ((20.0/100)*(taxIncome-300000)+5000) ;
}
else if(taxIncome >400000 )
{
tax = (10.0/100)*50000 + (20.0/100)*100000+5000 +
((30.0/100)*(taxIncome-400000)+25000);
}
}
void displayinfo()
{
System.out.println("*****Employeee Details*********");
System.out.println("Name :"+name);
System.out.println("Pan_Number :"+pan);
System.out.println("Annual_Income:"+taxIncome);
System.out.println("Tax_to_pay :"+tax);
}
ARPITA GUPTA MCA II – A ROLL.NO : 44
}
public class Q4_Tax_Calculation {
public static void main(String[] args)
{
Employee e = new Employee();
e.inputinfo();
e.taxcalc();
e.displayinfo();
}
}
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 05:
package termwork_java;
import java.util.Scanner;
class shape
{
Scanner sc = new Scanner(System.in);
void getPeri()
{
}
void getArea()
{
}
}
class circle extends shape
{
int r ;
circle()
{
System.out.println("Enter radius ");
r = sc.nextInt();
}
void getPeri()
{
double p = 2 * 3.14 * r ;
System.out.println("perimeter of circle = "+p);
}
void getArea()
{
double a = 3.14 * r * r ;
System.out.println("area of circle = "+a);
}
}
public class Q5_inheritance {
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 06:
package termwork_java;
import java.util.Scanner ;
class bank
{
long amount;
Scanner sc = new Scanner(System.in);
bank(int balance)
{
this.amount = balance ;
}
void deposit()
{
System.out.println("Enter amount to deposit ");
this.amount += sc.nextLong();
System.out.println("Amount deposited successfully");
System.out.println("Total Bank Balance "+amount+" rupees");
}
void withdraw()
{
}
}
}
else
{
System.out.println(withdraw +" rupees deducted from your account ");
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 07:
package termwork_java;
abstract class Animal
{
abstract void sound();
}
t.sound();
d.sound();
}
}
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 08:
package termwork_java;
import java.util.Scanner ;
interface Sortable
{
void sort(int[] arr);
}
{
min = i;
for(j=i+1 ; j<n-i-1 ; j++)
{
if(a[j]<a[min])
{
min = j ;
}
}
if(i!=min)
{
tmp = a[i];
a[i] = a[min];
a[min] = tmp;
}
}
System.out.println("Sorted Array using Selection Sort");
for(i=0 ; i<n ; i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
}
public class Q8_Sorting_using_interface
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter range for array");
int length = sc.nextInt();
int[] arr = new int[length];
System.out.println("Enter elements of array");
for(int i=0 ; i<length ; i++)
{
arr[i] = sc.nextInt();
}
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 09:
package termwork_java;
import java.util.Scanner;
public class Q9_Exception {
if(maxMarks <= 0)
{
throw new Exception("Max marks must be greater than 0");
}
if(obtainedMarks<0)
throw new Exception("marks must be positive");
else if(obtainedMarks > maxMarks)
throw new Exception("Obtained marks should be less than
maximum marks");
double p = (obtainedMarks*100)/maxMarks;
System.out.println("Percentage = "+p+"%");
}catch(NumberFormatException e)
{
System.out.println("Enter valid integral Number");
}
}
ARPITA GUPTA MCA II – A ROLL.NO : 44
OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44
CODE 10:
package termwork_java;
import java.util.Scanner;
try {
System.out.println("Enter Name of Employee");
String name = sc.nextLine();
if(!name.matches("[A-Z]+"))
{
throw new InvalidNameException("Name must be in UPPERCASE as
well as ALPHABETS only");
}
System.out.println("REQUIRED
DATA*******************************************");
System.out.println("Name : "+name);
System.out.println("Designation : "+desg);
System.out.println("Deparment_Id : "+dept_id);
}catch(InvalidNameException e)
{
System.out.println("Exception : "+e.getMessage());
}
catch(InvalidDesignationException e)
{
System.out.println("Exception : "+e.getMessage());
}
catch(InvalidIdException e)
{
System.out.println("Exception : "+e.getMessage());
}
}
}
ARPITA GUPTA MCA II – A ROLL.NO : 44
OUTPUT :