You are on page 1of 27

Computer Applications Assignment

1.Sponge iron company announces an increment for their employees on seniority basis as per the
given conditions.

Age Increment

56years and above 20% of basic

Above 45 and below 56 years 15% of basic

Upto 45 10% of basic

Write a program to find new basic by using the following class specifications.

Class : Increment

Data members:

String name : Name of the employee.

double basic : Basic pay of the employee.

Int age : Age of the employee.

Member Methods:

void getdata( String n, double b, int a):To accept name, basic and age of the employee.

void calculate( ):To find increment and update basic.

void display():To display age and updated basic in the format given below:

Name Age Updated Basic

Xxxxx xxx xxxxxxxxxxxxx

Ans:
import java.util.*;
class Employee
{
private int pan;
private String name;
private double taxincome;
private double tax;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter pan number: ");
pan = in.nextInt();
in.nextLine();
System.out.print("Enter Name: ");
name = in.nextLine();
System.out.print("Enter taxable income: ");
taxincome = in.nextDouble();
}

public void cal() {


if (taxincome <= 250000)
tax = 0;
else if (taxincome <= 500000)
tax = (taxincome - 250000) * 0.1;
else if (taxincome <= 1000000)
tax = 30000 + ((taxincome - 500000) * 0.2);
else
tax = 50000 + ((taxincome - 1000000) * 0.3);
}

public void display() {


System.out.println("Pan Number\tName\tTax-Income\tTax");
System.out.println(pan + "\t" + name + "\t"
+ taxincome + "\t" + tax);
}

public static void main(String args[]) {


Employee obj = new Employee();
obj.input();
obj.cal();
obj.display();
}
}

2.Define a class Marks as per the given specifications:

Data Members:

Name ,age, m1, m2, m3, maximum, average

Member methods:

i. A parameterised constructor to input details of a student .


ii. To compute the average and maximum out of three marks.
iii. To display the name , age, marks in three subjects, maximum marks and average.
Write a main method to create an object an object of a class and call the above member
methods.

Ans:
import java.util.*;
class Student
{
String name;
int m1, m2, m3,age,maximum;
double average;

public Student(String n, int a, int marks1, int marks2, int marks3, int max,
double avg)
{
name = n;
age = a;
m1 = marks1;
m2 = marks2;
m3 = marks3;
maximum = max;
average = avg;
}

/* accept() method to accept name,age and marks*/


public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter age: ");
age =sc.nextInt();
System.out.print("Enter marks of 1st subject: ");
m1 = sc.nextInt();
System.out.print("Enter marks of 2nd subject: ");
m2 = sc.nextInt();
System.out.print("Enter marks of 3rd subject: ");
m3 = sc.nextInt();
}

public void calculate()


{
average = (m1 + m2 + m3) / 3.0;
maximum = Math.max(m1, (Math.max(m2, m3)));
}

public void display()


{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks1 " + m1);
System.out.println("Marks2 " + m2);
System.out.println("Marks3 " + m3);
System.out.println("Maximum: " + maximum);
System.out.println("Average: " + average);
}

public static void main(String[] args)


{
Student ob1 = new Student("",0,0,0,0,0,0);
ob1.accept();
ob1.calculate();
ob1.display();
}

}
3. Write a class with the name volume using function overloading that computes the volume of a cube
a sphere and a cuboid.

Volume of a cube =s*s*s

Volume of a sphere =4/3πr3

Volume of a cuboid =lbh

Ans:
import java.util.*;
class volume
{
public void vol(int side)
{
int area=side*side*side;
System.out.println("area of the cube="+area);
}
public void vol(int l,int b,int h)
{
int are=l*b*h;
System.out.println("area of the cuboid ="+are);
}
public void vol(double r)
{
double sp=4.0/3*3.14*r*r*r;
System.out.println("area of the sphere="+sp);
}
public static void main()
{
volume ob = new volume();
ob.vol(4);
ob.vol(2,4,3);
ob.vol(2.0);
}
}

4. Design a class to overload a function num_cal as follows:

I. void num_cal(int num, char ch) - with one integer argument and one character argument it computes
the square of an integer if choice Ch is ‘s' otherwise computer its cube.

ii. void num_cal(int a,int b, char ch)- with two integer arguments and one character argument it
computes the product of integer argument. It compute the product of integer argument if ch is ‘p’ else
add the integers.

Ans:
import java.util.*;
class Overloaded
{
void num_calc( int num, char ch )
{
int s = 0;
if(ch == 's' )
s = num * num;
else
s = num * num * num;
System.out.println(" s = " + s );
}
void num_calc( int a, int b, char ch )
{
int s = 0;
if(ch == 'p' )
s = a * b;
else
s = a + b;
System.out.println(" s = " + s );
}
void num_calc( String s1, String s2 )
{
if(s1.equals(s2))
System.out.println("Both Strings are equal.");
else
System.out.println("Both Strings are not equal.");
}
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
Overloaded1 ob = new Overloaded1( );
ob.num_calc( 5, 's' );
ob.num_calc( 8, 3, 'n' );
ob.num_calc( "Raman", "Naman" );
}
}

5. A prime number is said to be twisted Prime if the new number obtained after
reversing the digits is also a prime number write a program to accept a number and
check whether the number is prime or not.
Sample input :167

sample output: 761, 167 is a twisted Prime

Ans:

import java.util.*;

class TwistedPrime
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();

if (num == 1)
{
System.out.println(num + " is not a twisted prime number");
}
else
{
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}

if (isPrime)
{

int t = num;
int revNum = 0;

while (t != 0)
{
int digit = t % 10;
t /= 10;
revNum = revNum * 10 + digit;
}

for (int i = 2; i <= revNum / 2; i++)


{
if (revNum % i == 0)
{
isPrime = false;
break;
}
}
}

if (isPrime)
System.out.println(num + " is a twisted prime number");
else
System.out.println(num + " is not a twisted prime number");
}

}
}
6. Write a program to accept a number and check whether the number is armstrong or not using
function.

Sample input = 153

sample output.= 13 + 53 + 33 =153. It is an Armstrong number.

Ans:

import java.util.*;
class ArmstrongNumber
{
public int armstrong(int n)
{
int num = n, cubeSum = 0;

while (num > 0)


{
int digit = num % 10;
cubeSum = cubeSum + (digit * digit * digit);
num /= 10;
}

if (cubeSum == n)
return 1;
else
return 0;
}
public void main()
{

Scanner in = new Scanner(System.in);


System.out.print("Enter Number: ");
int num = in.nextInt();

ArmstrongNumber obj = new ArmstrongNumber();


int r = obj.armstrong(num);

if (r == 1)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
7. Define a class to overload a function series as follows.

i. double series(double n): with one double argument and returns the sum of the series.

Sum =1/1 + ½ + 1/3+ …………+1/n.

ii. double series (double a, double n )with two double arguments and returns the sum of the series.

Sum = 1/a2 + 4/a5 + 7/a8 + 10/a11+………………….n terms.

Ans:

import java.util.*;
class Series
{
double series(double n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
{
double term = 1.0 / i;
sum += term;
}
return sum;
}

double series(double a, double n)


{
double sum = 0;
int x = 1;
for (int i = 1; i <= n; i++)
{
int e = x + 1;
double term = x / Math.pow(a, e);
sum += term;
x += 3;
}
return sum;
}

public void main()


{
Series obj = new Series();
System.out.println("First series sum = " + obj.series(5));
System.out.println("Second series sum = " + obj.series(3, 8));
}
}
8.Write a program to find the sum of the series:

a) s= 1/a + 2/a + 3/a +……………………..n terms.

b) s= 1+(1+2)+(1+2+3)+…………………….+(1+2+3+……..n).

9. Write a menu driven program to find the area and perimeter of a rectangle.

[Area = l * b, Perimtere = 2*(l+b)

10. The Electricity Board charges from their consumers according to the units consumed per
month .The amount to be paid as per the given tariff. [6]
Units Consumed Charges
Up to 100units Rs.5.50/unit
For next 200 units Rs.6.50/unit
For next 300 units Rs.7.50/unit
More than 600 units Rs.8.50/unit

Write a program to input consumer’s name, consumer number and the units consumed. The
program displays the following information at the time receiving the money receipt as:
Money Receipt
Consumer Number:
Consumer Name:
Units consumed:
Amount to be paid:
Ans:

System.out.println("Please enter your name");

n = sc.next();

System.out.println("Please enter your number");

number = sc.nextInt();
System.out.println("Please enter the amount of units ");

unit = sc.nextInt();

if(unit>=100)

bill = unit*5.50;

else if(unit>100 && unit<=300)

bill = unit*6.50;

else if(unit>300 && unit<=600)

{
bill = unit*7.50;

else

bill=unit*8.50;

System.out.println("Name: "+n);

System.out.println("Number: "+number);

System.out.println("Units consumed: "+unit);

System.out.println("Bill amount: "+bill);

}
11.Write a program to accept a string and display the string by replacing vowels with ‘*’.
Sample Input: computer Sample Output:c*mp*t*r

Ans:

import java.util.*;

class VowelReplace
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a string in uppercase:");
String str = in.nextLine();
String newStr = "";
int len = str.length();

for (int i = 0; i < len; i++)


{
char ch = str.charAt(i);
if (ch == 'A' ||ch == 'E' ||ch == 'I' ||ch == 'O' ||ch == 'U')
{
newStr = newStr + '*';
}
else
{
newStr = newStr + ch;
}
}
System.out.println(newStr);
}
}
12. Write a program to accept a string and check whether it is palindrome word or not.
Sample Input: Malayalam. Sample Out: It is Palindrome.

Ans:

import java.util.*;

class Palindrome
{
public void main()
{
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
int length = str.length();

for ( int i = length - 1; i >= 0; i-- )


rev = rev + str.charAt(i);

if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");

}
}
13. Write a program to accept a string and display each letter.
Sample Input: BLUEJ Sample Output: B
L
U
E
J
Ans:

import java.util.*;
class letter
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
System.out.println(c);
}
}
}

14. Write a program to accept a word and display the same in Piglatin form:
S.I: TROUBLE S.O: OUBLETRAY

Ans:

import java.util.*;
class PigLatin
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter word: ");
String word = in.next();
int l = word.length();
word=word.toUpperCase();
String piglatin="";
int flag=0;

for(int i = 0; i < len; i++)


{
char x = word.charAt(i);
if(x=='A' || x=='E' || x=='I' || x=='O' || x=='U')
{
piglatin=word.substring(i) + word.substring(0,i) + "AY";
flag=1;
break;
}

if(flag == 0)
{
piglatin = word + "AY";
}
System.out.println(word + " in Piglatin format is " + piglatin);
}
}

15. Write a program to accept a string. Count and output the number of double letter sequences
that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4

Ans:

import java.util.*;

class LetterSeq
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter string");
String s = in.nextLine();
int count = 0;
int l = s.length();

for (int i = 0; i < len - 1; i++)


{
if (str.charAt(i) == str.charAt(i + 1))
count++;
}

System.out.println("Double Letter Sequence Count = " + count);

}
}

16. Write a program to accept a string and count the consecutive letters present in the string.
S.I.: IT WAS NOT TOUGH FOR HIM TO RESIDE ABOVE THE HILL.
S.O: No. of consecutive letters-6

Ans:

import java.util.*;
class consecutive
{
public void main()
{
int i;
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String s=in.nextLine();
int l= s.length();
char c,c1,c2;
int count=0;
for( i=0;i<l-1;i++)
{
c=s.charAt(i);
c1=s.charAt(i+1);
c2=(char)(c+1);
if(c1 == c2)
count++;
}
System.out.println("No.of Consecutive letters:"+count);
}
}

17. Write a program to accept a string and display the last word first followed by the first and
second word.
S.I : Mohandas Karamchand Gandhi
S.O : Gandhi Mohandas Karamchand

Ans:

import java.util.*;
class lastfirstwords
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter some words");
String name = in.nextLine();

/*
* Get the last index
* of space in the string
*/
int lastSpaceIdx = name.lastIndexOf(' ');

String lastword = name.substring(lastSpaceIdx + 1);


String initialword = name.substring(0, lastSpaceIdx);

System.out.println(lastword + " " + initialword);


}
}

18. Write a program to accept a string and display the first letter of each word.
S.I : Subash Chandra Bose
S.O : S.C.B.

Ans:
import java.util.*;
class FirstCharacter
{
public void main()
{
String s = "HELLO WORLD";
char c[] = s.toCharArray();
System.out.println("The first character of each word");

for (int i=0; i < c.length; i++)


{
// Logic to implement first character of each word in a string

if(c[i] != ' ' && (i == 0 || c[i-1] == ' '))


{
System.out.println(c[i]);
}
}
}
}

19. Write a program to accept the path given below and extract the filename and pathname.

C:\Pictures\sample\flowers.jpg

Filename : flowers
Pathname : C:\Pictures\sample

Ans:

import java.util.*;

class Filepath
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter full path");
String filepath = in.next();

char pathSep = '\\';


char dotSep = '.';

int pathSepIdx = filepath.lastIndexOf(pathSep);


System.out.println("Path:\t\t" + filepath.substring(0, pathSepIdx));

int dotIdx = filepath.lastIndexOf(dotSep);


System.out.println("File Name:\t" + filepath.substring(pathSepIdx + 1, dotIdx));

System.out.println("Extension:\t" + filepath.substring(dotIdx + 1));


}
}

20. Write a program in java to enter a sentence.Display the words which are only palindrome.
S.I: MOM AND DAD ARE NOT AT HOME
S.O: MOM
DAD

Ans:

import java.util.*;
class PalindromeWords
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String s = in.nextLine();
s = s + " ";
String word = "";
int l = s.length();

for (int i = 0; i < len; i++)


{
char ch = str.charAt(i);
if (ch == ' ')
{
int wordLen = word.length();
boolean isPalin = true;
for (int j = 0; j < wordLen / 2; j++)
{
if (word.charAt(j) != word.charAt(wordLen - 1 - j))
{
isPalin = false;
break;
}
}

if (isPalin)
{
System.out.println(word);
word = "";
}
else {
word += ch;
}
}
}
}

****************************************************

You might also like