Comp Assignment 2
Comp Assignment 2
2021-22
TERM 2
INDEX
Content Page Number
Section A 3
Question 1 3
Section B 4
Question 2 4
Question 3 7
Question 4 9
Teacher’s Signature:______________
3
Section A:
Answer all the questions.
Question 1
Ans- 1) if
Section B:
Answer all the questions
Question 2
Given below is a hypothetical table showing rates of Income Tax for male citizens below the
age of 65 years:
Input:-
//to calculate and display Income Tax
import java.util.*;
public class Income_Tax
{
public static void main(String args[])
{
int age,ti;
char g;
double tax=0.0;
Scanner in=new Scanner(System.in);
System.out.println("Enter gender 'M' for male, 'F' for female");
g=in.next().charAt(0);
System.out.println("Enter age of the person");
age=in.nextInt();
System.out.println("Enter taxable income");
ti=in.nextInt();
if((age<=65)&&(g=='M'))
{
5
if(ti<=160000)
tax=0.0;
if(ti>160000 && ti<=500000)
tax=(ti-160000)*10/100;
if(ti>500000 && ti<=800000)
tax=((ti-500000)*20/100)+34000;
if(ti>800000)
tax=((ti-800000)*30/100)+94000;
System.out.println("*****Details of the Tax Payer*****");//the following statements
will print the details of the payer
System.out.println("Gender of the tax payer:"+g);
System.out.println("Age:"+age);
System.out.println("Taxable Income = Rs."+ti);
System.out.println("Income tax = Rs."+tax);
}
else
{
System.out.println("Wrong category!!");//these statements will output if the
conditions of the if statement aren't met
System.out.println("No calculation of Income Tax!!");
}
}
}
Variable-Description Table
Variable Description
age Age
g Gender
ti Taxable income
tax Income tax
Output:-
6
Question 3
A Mega Shop has different floors which display varieties of dresses as mentioned
below:
Ground floor : Kids Wear
First floor : Ladies Wear
Second floor : Designer Sarees
Third Floor : Men's Wear
The user enters floor number and gets the information regarding different items of the Mega
shop. After shopping, the customer pays the amount at the billing counter and the shopkeeper
prints the bill in the given format:
Name of the Shop: City Mart
Total Amount:
Visit Again!!
Write a menu driven program to perform the above task as per the user's choice. [15]
//the following statements will display details of the City Mart, the amount and a parting
message
System.out.println("Name of the Shop: City Mart");
System.out.println("Total Amount: " + amt);
System.out.println("Visit Again!!");
}
}
Variable-Description Table
Variable Description
floor Floors of the shop(1-4)
isFloorValid Checks whether the floor is within the range
of 1-4. If false, it displays “Incorrect Floor”
amt Displays the bill amount
Output:-
1. Ground floor
2. First floor
3. Second floor
4. Third floor
Select a floor: 2
Ladies Wear
Enter bill amount
2000
Name of the Shop: City Mart
Total Amount: 2000.0
Visit Again!!
9
Question 4
Using a switch case statement, write a menu driven program to convert a given temperature
from Fahrenheit to Celsius and vice-versa. For an incorrect choice, an appropriate message
should be displayed.
Hint: c = 5/9*(f-32) and f=1.8*c+32 [15]
Input:-
//program to covert temperature from farenheit to celcius scale and vice versa
import java.util.*;
public class temperature
{
public void main()
{
Scanner in= new Scanner(System.in);
System.out.println("Chose 1 or 2 depending on the conversion");
System.out.println("1. To convert from Fahrenheit to Celsius");
System.out.println("2. To convert from Celsius to Fahrenheit");
int choice = in.nextInt();
double f,c;
switch (choice)
{
case 1://executed if temperature is given in Fahrenheit
System.out.print("Enter temperature in Fahrenheit:");
f= in.nextDouble();
c= 5/9*(f-32);
System.out.println("Temperature in Celsius: "+c);
break;
case 2://executed if temperature is given in Celsius
System.out.print("Enter temperature in Celsius:");
c= in.nextDouble();
f= 1.8*c+32;
System.out.println("Temperature in Fahrenheit: "+f);
break;
default://executed if temperature is given in an invalid unit
System.out.println("Incorrect choice");
break;
}
}
}
Variable-Description Table
Variable Description
choice To check whether the given unit is valid
f Fahrenheit
c Celsius
10
Output:-
Chose 1 or 2 depending on the conversion
1. To convert from Farenheit to Celsius
2. To convert from celsius to Farenheit
2
Enter temperature in Celsius:100
Temperature in Farenheit: 212.0