Java Practice Set 4
QUESTIONS ---
SOLUTIONS/CODE ---
package com.company;
import java.util.Scanner;
public class PS4 {
public static void main(String[] args) {
/* Q1 - What will be the output of this program:
int a = 10;
if (a=11){
System.out.println("I am 11");
}
else{
System.out.println("I am not 11");
}
ANSWER - This will simply throw an error because, in if statement, we used '=' as an assignment operator
to run the program correctly, we should have use a logical operator '==' */
/* Q2 - Write a program to find out to whether a student is pass or fail;
To pass it requires 40% of total marks and 33% marks of each subjects
Assume 3 subjects and take marks as an input from the user */
byte s1, s2, s3;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your marks in Physics");
s1 = sc.nextByte();
System.out.println("Enter your marks in Chemistry");
s2 = sc.nextByte();
System.out.println("Enter your marks in Mathematics");
s3 = sc.nextByte();
float avg = (s1+s2+s3)/3.0f;
System.out.println("Your overall percentage is: " + avg);
if (avg>=40 && s1>=33 && s2>=33 && s3>=33){
System.out.println("Congratulations! You have been PROMOTED");
}
else{
System.out.println("Sorry! You haven't been Promoted. Please do better next time");
}
/* Q3 - Calculate income tax paid by an employee to the government as per the slabs mentioned below:
Income Slab Tax
2.5L-5.0L 5%
5.0L-10.0L 20%
Above 10.0L 30%
Note - There is no tax below 2.5 lakhs. Take income as an input from the user. */
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter Your Income in Lakhs/anum");
float tax = 0;
float income = sc2.nextFloat();
if (income<2.5){
tax = tax +0;
}
else if (income>=2.5f && income<=5.0f){
tax = tax + 0.05f * (income - 2.5f);
}
else if (income>5.0f && income<=10.0f){
tax = tax + 0.05f * (5.0f - 2.5f);
tax = tax + 0.2f * (income - 2.5f);
}
else if (income>10.0f){
tax = tax + 0.05f * (5.0f - 2.5f);
tax = tax + 0.2f * (10.0f - 5.0f);
tax = tax + 0.3f * (income - 10f);
}
System.out.println("The Total Tax have to paid by this employee is: " + tax + " Lakhs");
/* Q4 - Write a java program to find out the day by the given number----
[1 for Monday, 2 for Tuesday......and so on] */
Scanner sc3 = new Scanner(System.in);
int day = sc3.nextInt();
switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
case 3 -> System.out.println("Wednesday");
case 4 -> System.out.println("Thursday");
case 5 -> System.out.println("Friday");
case 6 -> System.out.println("Saturday");
case 7 -> System.out.println("Sunday");
}
/* Q5 - Write a java program to find whether a year entered by the user is Leap year or not*/
/* Q6 - Write a program to find out the type of website fro m their URL ---
.com - Commercial Website
.org - Organization Website
.in - Indian Website */
Scanner sc4 = new Scanner(System.in);
String str = sc4.next();
if (str.endsWith(".org")){
System.out.println("This is a Organizational Website");
}
else if (str.endsWith(".com")){
System.out.println("This is Commercial Website");
}
else if (str.endsWith(".in")){
System.out.println("This is Indian Website");
}
}
}
Comments
Post a Comment