Chapter 8
Ans:
20 > 13 ? 20 : 13 → S = 20
12 > 21 ? 12 : 21 → S = 21
Ans:
350 > 300 ? ‘P’ : ‘I’ → S = ‘P’
250 > 300 ? ‘P’ : ‘I’ → S = ‘I’
Ans:
3000 <= 2100 ? 3000 * 0.1 : 3000 * 0.2 → cm = 600
2000 <= 2100 ? 2000 * 0.1 : 2000 * 0.2 → cm = 200
Ans:
Error:
- a => b The correct syntax is a >= b
- b = 10 Assignment operator has been used. The correct Syntax is b ==10
- “Yes’ String should be enclosed within double quotes.
Correct Code:
- int a = 15;
- int b = 10;
- if( (a >= b) || (b==10) )
- System.out.println(“YES”);
- else
- System.out.println(“No”);
Ans:
if (x == 1)
y = 100;
else if( x == 1 && x%3 == 1)
y = 200;
Ans:
(5 >= 10) ? ++X : ++Y;
Output:
The value of Y = 11
Ans:
int ch = 1, x1 = 0, x2 = 0, x3 = 0, x4 = 0;
switch(ch)
{
case 1: x1 = 8;
x2 = 9;
case 2: x4 = 11;
x3 = 12;
break;
}
Ans:
When n = 2
- Programming Language
When n = 3
- Language
When n = 4
- JAVA
When n = 1
- Computer Programming Language
When n = 5
- Thanks
Ans:
public class p9
{
void findLargest(int x, int y)
{
if (x > y)
System.out.println(x + " is largest.");
else if (y > x)
System.out.println(y + " is largest.");
}
}
Ans:
import java.util.Scanner;
public class p10
{
void convertChar()
{
Scanner sc = new Scanner(System.in);
System.out.print("Please Enter a Character: ");
char ch = sc.next().charAt(0);
if(ch >= 'A' && ch <= 'Z')
{
//Convert to Lowercase
ch += 32;
System.out.println("Converted to LowerCase: " + ch);
}
if(ch >= 'a' && ch <= 'z')
{
//Convert to Uppercase
ch -= 32;
System.out.println("Converted to UpperCase: " + ch);
}
else
System.out.println("Invalid Input. Please enter a valid alphabet");
}
}
Ans:
import java.util.Scanner;
public class p11
{
static void calcDiscount(double totalCost)
{
double discount = 0, finalPrice=0;
if(totalCost <= 2000)
discount = 5.0/100;
else if(totalCost <= 5000)
discount = 25.0/100;
else if(totalCost <= 10000)
discount = 35.0/100;
else
discount = 50.0/100;
discount = discount * totalCost;
finalPrice = totalCost - discount;
System.out.println("Total Price: " + totalCost);
System.out.println("Discount: " + discount);
System.out.println("Final Price: " + finalPrice);
}
}
import java.util.Scanner;
public class p13
{
void calcBill(double NoOfCalls)
{
double rate = 0, totalCost = 0;
if(NoOfCalls <= 100)
{
rate = 2.5;
totalCost = NoOfCalls * rate;
}
else if(totalCost <= 200)
{
rate = 0.6;
totalCost = 100 * 2.5 + (NoOfCalls - 100) * rate;
}
else if(totalCost <= 300)
{
rate = 0.5;
totalCost = 100 * 2.5 + 100 * 0.6 + (NoOfCalls - 200) * rate;
}
else
{
rate = 0.4;
totalCost = 100 * 2.5 + 100 * 0.6 + 100 * 0.5 + (NoOfCalls - 300) * rate;
}
System.out.println("Total Calls: " + NoOfCalls);
System.out.println("Total Cost: " + totalCost);
}
}
import java.util.Scanner;
public class p14
{
void calcAmount(double p, int n)
{
double r = 0.0, a = 0;
if(n <= 1)
{
r = 9;
a = p * Math.pow((1 + r/100), n);
}
else if(n <= 2)
{
r = 10;
a = p * Math.pow((1 + r/100), n);
}
else if(n <= 3)
{
r = 11;
a = p * Math.pow((1 + r/100), n);
}
else
{
r = 12;
a = p * Math.pow((1 + r/100), n);
}
System.out.println("Principal: " + p);
System.out.println("Rate: " + r);
System.out.println("Time: " + n);
System.out.println("Amount: " + a);
}
}
Ans:
public class p15
{
void calcPension(int age, char gender)
{
double pension = 0;
if(gender == 'f')
{
if(age > 60 && age <= 65)
pension = 45;
else if(age > 65)
pension = 70;
}
else if(gender == 'm')
{
if(age > 65 && age <= 70)
pension = 50;
else if(age > 70)
pension = 70;
}
System.out.println("Gender: " + gender);
System.out.println("Weekly Pension Amount: " + pension);
}
}
Ans:
class p16
{
int Exponent(int x, int y)
{
return( Math.pow(x,y) );
}
int Rectangle(int l, int b)
{
return ( l * b );
}
}
Ans:
int y = (y >= 150) ? y*15 : y-15;
Ans:
if(m > 300)
ch = (m/10)*2;
else
ch = (m/20)-2;
Ans:
char res = (mark > 300) ? ‘P’ : ‘F’;
Ans:
int n = 0;
switch(ch)
{
case 1: n = n + 2;
break;
case 2: n = n + 4;
break;
case 3: n = n + 6;
break;
default: System.out.println(“OOPs ! Try again”);;
}
Ans:
if ( v == 1)
System.out.println(“First”);
else if ( v == 0)
System.out.println(“Zero”);
else if ( v == -3)
System.out.println(“Minus Three”);
Ans: Same as above
Ans:
switch(op)
{
case ‘+’ : z = m + n;
break;
case ‘-’ : z = m - n;
break;
case ‘/’ : z = m / n;
break;
case ‘*’ : z = m * n;
break;
default : System.out.println(“OOPs! wrong”);
}
Ans:
class p24
{
int findLargest(int n1, int n2, int n3)
{
int smallest= Math.min(n1, n2);
smallest = Math.min(smallest, n3)
return smallest;
}
}
Ans:
class p25
{
int findLargest(int b, int e)
{
int power= Math.pow(b, e);
return power;;
}
}
Ans:
class p26
{
int findLargest(int x, int y, int z)
{
if ( x > y && x > z)
return x;
else if ( y > z && y > x)
return y;
else
return z;
}
}
class p27
{
int findSmallest(int x, int y, int z)
{
if ( x < y && x < z)
return x;
else if ( y < z && y < x)
return y;
else
return z;
}
}
Ans:
class p28
{
void findVowelConsonant(char ch)
{
if ( ch >= ‘a’ && ch <= ‘z’ )
{
switch(ch)
{
case ‘a’ :
case ‘e’ :
case ‘i’ :
case ‘o’ :
case ‘u’ : System.out.println(ch + “ is a vowel”);
break;
default : System.out.println(ch + “ is a consonant”);
break;
}
}
else System.out.println(“Invalid alphabet”);
}
}
Ans:
public class p29
{
char check(char ch)
{
if(ch >= 'a' && ch <= 'z')
return 'S';
else if(ch >= 'A' && ch <= 'Z')
return 'C';
else return 0;
}
}
Ans:
import java.util.Scanner;
public class p30
{
void check(int n)
{
switch(n)
{
case 1: System.out.print("January"); break;
case 2: System.out.print("February"); break;
case 3: System.out.print("March"); break;
case 4: System.out.print("April"); break;
case 5: System.out.print("May"); break;
case 6: System.out.print("June"); break;
case 7: System.out.print("July"); break;
case 8: System.out.print("August"); break;
case 9: System.out.print("September"); break;
case 10: System.out.print("October"); break;
case 11: System.out.print("November"); break;
case 12: System.out.print("December"); break;
default: System.out.print("Wrong Input");
}
}
}
Ans:
public class p31
{
void calcBill(long metreNo, int NoOfUnits)
{
double rate = 0, totalCost = 0;
if(NoOfUnits >= 1 && NoOfUnits <= 100)
{
rate = 400;
totalCost = rate;
}
else if(NoOfUnits <= 300)
{
rate = 1.5;
totalCost = 400 + (NoOfUnits - 100) * rate;
}
else if(NoOfUnits <= 500)
{
rate = 2.0;
totalCost = 400 + 200 * 1.5 + (NoOfUnits - 300) * rate;
}
else
{
rate = 2.5;
totalCost = 400 + 200 * 1.5 + 200 * 2.0 + (NoOfUnits - 300) * rate;
}
System.out.println("Metre No: " + metreNo);
System.out.println("No. Of Units Consumed: " + NoOfUnits);
System.out.println("Bill Amount: " + totalCost);
}
}
Ans:
import java.util.Scanner;
public class p32
{
void calc()
{
Scanner sc = new Scanner(System.in);
System.out.println("***Calculator***");
System.out.print("Choose operator from the following: +, -, *, / ");
char opt = sc.next().charAt(0);
System.out.print("Enter Num1: ");
int n1 = sc.nextInt();
System.out.print("Enter Num2: ");
int n2 = sc.nextInt();
int result = 0;
switch(opt)
{
case '+' : result = n1 + n2; break;
case '-' : result = n1 - n2; break;
case '*' : result = n1 * n2; break;
case '/' : result = n1 / n2; break;
default: System.out.println("Wong Input, Try Again");
}
System.out.println("\n\nOperator chosen: " + opt);
System.out.println("Numbers: " + n1 + ", " + n2);
System.out.println("Result: " + result);
}
}
import java.util.Scanner;
public class p33
{
void calc()
{
Scanner sc = new Scanner(System.in);
System.out.println("***Calculator***");
System.out.print("Choose operator from the following: +, -, *, / ");
char opt = sc.next().charAt(0);
System.out.print("Enter Num1: ");
int n1 = sc.nextInt();
System.out.print("Enter Num2: ");
int n2 = sc.nextInt();
int result = 0;
if(opt == '+')
result = n1 + n2;
else if(opt == '-')
result = n1 - n2;
else if(opt == '*')
result = n1 * n2;
else if(opt == '/')
result = n1 / n2;
else
System.out.println("Wong Input, Try Again");
System.out.println("\n\nOperator chosen: " + opt);
System.out.println("Numbers: " + n1 + ", " + n2);
System.out.println("Result: " + result);
}
}
public class p34
{
void calc(long pc, int qty, double price)
{
double discount = 0;
double totalCost = qty * price, finalPrice = 0;
if(totalCost <= 3000)
discount = 0;
else if(totalCost <= 15000)
discount = 10.0/100;
else if(totalCost > 15000)
discount = 15.0/100;
discount = discount * totalCost;
finalPrice = totalCost - discount;
System.out.println("\n\nProduct Code: " + pc);
System.out.println("Quantity: " + qty);
System.out.println("Total Cost: " + totalCost);
System.out.println("Discount: " + discount);
System.out.println("Payable Amount: " + finalPrice);
}
}
Ans:
public class p35
{
void check(int num)
{
if(num >= 1 && num <= 9)
System.out.println("You have entered a Single Digit Number");
else if(num >= 10 && num <= 99)
System.out.println("You have entered a Double Digit Number");
else if(num >= 100 && num <= 999)
System.out.println("You have entered a Three Digit Number");
else
System.out.println("Error! Out Of Range");
}
}