[go: up one dir, main page]

0% found this document useful (0 votes)
3 views22 pages

Java Term Work Mid Arpita

The document contains multiple Java code examples demonstrating various programming concepts including character counting in strings, string operations, tax calculation, inheritance, bank management, abstract classes, sorting algorithms, and exception handling. Each code snippet is accompanied by a brief description of its functionality and expected output. The author, Arpita Gupta, is a student in MCA II – A with roll number 44.

Uploaded by

Ayush Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Java Term Work Mid Arpita

The document contains multiple Java code examples demonstrating various programming concepts including character counting in strings, string operations, tax calculation, inheritance, bank management, abstract classes, sorting algorithms, and exception handling. Each code snippet is accompanied by a brief description of its functionality and expected output. The author, Arpita Gupta, is a student in MCA II – A with roll number 44.

Uploaded by

Ayush Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

ARPITA GUPTA MCA II – A ROLL.

NO : 44

CODE 01:
package termwork_java;
import java.util.Scanner;
public class Count_character_in_string {

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Strings");
String s = sc.nextLine();
char[] arr = s.toCharArray();
int l = s.length();

int[] dup = new int[l];

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;

public class Q2_print_char_place_of_num {

public static void main(String[] args)


{
int i;
for(i=1 ; i<50 ; i++)
{
if(i % 3 == 0)
{
if(i%5 == 0)
System.out.println("TF");
else
System.out.println("T");
}
else if(i % 5 == 0)
{
if(i%3 == 0)
System.out.println("TF");
else
System.out.println("F");
}
else
System.out.println(i);
}
}
}

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

System.out.println("using equalsIgnoreCase-- "+s1.equalsIgnoreCase(s2));


System.out.println("using compareTo-- "+s1.compareTo(s2));
}
void substring_search_and_replace()
{
System.out.println("Enter Main String for searching");
String s1 = sc.nextLine();
System.out.println("Enter substring to replace");
String s2 = sc.nextLine();
System.out.println("Enter string by which substring replaced");
String s3 = sc.nextLine();

String s4 = s1.replace(s2, s3);


System.out.println("new string "+ s4);
}
void count_various_characters()
{
int vowels=0 , digits=0 , sp_char=0 ,lower=0 , upper=0 , words=1 ;
System.out.println("enter a string for counting");
String s = sc.nextLine();
String Vowel = "AEIOUaeiou" ;
int l1 = Vowel.length();
int l = s.length();
for(int i=0 ; i<l ; i++)
{
for(int j=0 ; j<l1 ; j++)
{
if(s.charAt(i)==Vowel.charAt(j))
vowels++;
}
if(Character.isDigit(s.charAt(i)))
digits++;
else if(Character.isUpperCase(s.charAt(i)))
upper++;
else if(Character.isLowerCase(s.charAt(i)))
lower++;
else if(Character.isWhitespace(s.charAt(i)))
words++;
else
sp_char++;
}

System.out.println("Number of vowels : " + vowels);


System.out.println("Number of digits : " + digits);
ARPITA GUPTA MCA II – A ROLL.NO : 44

System.out.println("Number of special characters: " + sp_char);


System.out.println("Number of lowercase letters : " + lower);
System.out.println("Number of uppercase letters : " + upper);
System.out.println("Number of words : " + words);
}
}
public class Q3_String_Operations {

public static void main(String[] args)


{
String_Operations a = new String_Operations();

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 {

public static void main(String[] args)


{
circle c = new circle();
c.getPeri();
c.getArea();
}}
ARPITA GUPTA MCA II – A ROLL.NO : 44

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()
{

}
}

class custAccount extends bank


{
custAccount(int balance)
{
super(balance);
}
void withdraw()
{
System.out.println("Do u want to withdraw type -- 1 for yes and 0 for no");
int ch = sc.nextInt();
if(ch == 1) {
System.out.println("Enter amount for withdrawl--- ");
long withdraw = sc.nextLong();

amount = amount - withdraw;


if(amount <= 250 || amount < withdraw)
{
System.out.println("cannot Withdraw");
ARPITA GUPTA MCA II – A ROLL.NO : 44

}
else
{
System.out.println(withdraw +" rupees deducted from your account ");

System.out.println(amount+" rupees left in your account");


}}
}
}
public class Q6_Bank_mangaement {

public static void main(String[] args)


{
custAccount c = new custAccount(5000);
c.deposit();
c.withdraw();
}
}

OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44

CODE 07:

package termwork_java;
abstract class Animal
{
abstract void sound();
}

class tiger extends Animal


{
void sound()
{
System.out.println("Roarrrrrrrrrrrrrrr");
}
}
class dog extends Animal
{
void sound()
{
System.out.println("barkkkkkkkkkkkkkkkk");
}
}
public class Q7_abstract_Ex {

public static void main(String[] args)


{
tiger t = new tiger();
dog d = new dog();

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);
}

class Bubble implements Sortable


{
public void sort(int[] a)
{
int i,j,tmp=0 ;
int n = a.length;
for(i=0 ; i<n-1 ; i++)
{
for(j=0 ; j<n-i-1 ; j++)
{
if(a[j]>a[j+1])
{
tmp = a[j];
a[j]=a[j+1];
a[j+1]=tmp ;
}
}
}

System.out.println("Sorted Array using Bubble Sort");


for(i=0 ; i<n ; i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
}
class Selection implements Sortable
{
public void sort(int[] a)
{
int i,j,tmp=0,min ;
int n = a.length;
for(i=0 ;i<n-1 ; i++)
ARPITA GUPTA MCA II – A ROLL.NO : 44

{
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();
}

Bubble b = new Bubble();


b.sort(arr);

Selection s = new Selection();


s.sort(arr);
}}
ARPITA GUPTA MCA II – A ROLL.NO : 44

OUTPUT :
ARPITA GUPTA MCA II – A ROLL.NO : 44

CODE 09:

package termwork_java;
import java.util.Scanner;
public class Q9_Exception {

public static void main(String[] args) throws Exception


{
Scanner sc = new Scanner(System.in);
int maxMarks = 0;
float obtainedMarks = 0;
try {
if(args.length == 0)
throw new Exception("one command line argument must needed ");
maxMarks = Integer.parseInt(args[0]);

if(maxMarks <= 0)
{
throw new Exception("Max marks must be greater than 0");
}

System.out.println("Enter obtained marks");


obtainedMarks = sc.nextFloat();

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;

class InvalidNameException extends Exception


{
public InvalidNameException(String msg)
{
super(msg);
}
}
class InvalidDesignationException extends Exception
{
public InvalidDesignationException(String msg)
{
super(msg);
}
}
class InvalidIdException extends Exception
{
public InvalidIdException(String msg)
{
super(msg);
}
}
public class Q10_Exception {

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);

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("Enter designation of employee");


ARPITA GUPTA MCA II – A ROLL.NO : 44

String desg = sc.nextLine();


if(!desg.equalsIgnoreCase("manager") && !desg.equalsIgnoreCase("clerk")
&& !desg.equalsIgnoreCase("peon"))
{
throw new InvalidDesignationException("Designation must be MANAGER
or CLERK or PEON only");
}

System.out.println("Enter department ID");


int dept_id = sc.nextInt();
if(dept_id<1 || dept_id > 5)
{
throw new InvalidIdException("Department id should be between 1
and 5");
}

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 :

You might also like