Computer Assignment But Word
Computer Assignment But Word
1.Sponge iron company announces an increment for their employees on seniority basis as per the
given conditions.
Age Increment
Write a program to find new basic by using the following class specifications.
Class : Increment
Data members:
Member Methods:
void getdata( String n, double b, int a):To accept name, basic and age of the employee.
void display():To display age and updated basic in the format given below:
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();
}
}
Data Members:
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;
}
}
3. Write a class with the name volume using function overloading that computes the volume of a cube
a sphere and a cuboid.
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);
}
}
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
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;
}
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
Ans:
import java.util.*;
class ArmstrongNumber
{
public int armstrong(int n)
{
int num = n, cubeSum = 0;
if (cubeSum == n)
return 1;
else
return 0;
}
if (r == 1)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
i. double series(double n): with one double argument and returns the sum of the series.
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;
}
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 Rs.8.50/unit
units
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:
n = sc.next();
number = sc.nextInt();
if(unit>=100)
bill = unit*5.50;
bill = unit*6.50;
bill = unit*7.50;
}
else
bill=unit*8.50;
System.out.println("Name: "+n);
System.out.println("Number: "+number);
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();
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();
if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
}
}
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();
}
}
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(' ');
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");
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();
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();
if (isPalin)
{
System.out.println(word);
word = "";
}
else {
word += ch;
}
}
}
}
****************************************************