0% found this document useful (0 votes)
55 views32 pages

Blue J Solutions Class 9

Uploaded by

Mukesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views32 pages

Blue J Solutions Class 9

Uploaded by

Mukesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

---

### **Assignment 1: Operators in Java**

```java

public class Operators {

public static void main(String[] args) {

// 1. Evaluate x

int x = 5;

x = x++ * 2 + 3 * --x;

System.out.println("Result in x: " + x);

// 2. Output of program segment

char xChar = 'A';

int m = (xChar == 'a') ? 'A' : 'a';

System.out.println("m = " + m);

// 3. Evaluate a += a++ + ++a + --a + a--; when a = 7

int a = 7;

a += a++ + ++a + --a + a--;

System.out.println("Result in a: " + a);

// 4. Using ternary operator for even/odd check

int num = 4;
System.out.println((num % 2 == 0) ? "Even" : "Odd");

```

---

### **Assignment 2: Programs Using Assignment Statements**

```java

public class AssignmentStatements {

public static void main(String[] args) {

// 1. Volume of a cuboid

int length = 5, breadth = 3, height = 4;

int volume = length * breadth * height;

System.out.println("Volume of cuboid: " + volume);

// 2. Swapping two numbers without a third variable

int a = 5, b = 10;

a = a + b;

b = a - b;

a = a - b;

System.out.println("After swapping: a = " + a + ", b = " + b);

// 3. Calculate Simple Interest


double principal = 10000, rate = 18, time = 20;

double simpleInterest = (principal * rate * time) / 100;

System.out.println("Simple Interest: " + simpleInterest);

```

---

### **Assignment 3: Programs Based on Input Through Parameters**

```java

public class InputParameters {

public static void main(String[] args) {

// 1. Extract first and last digits of a 5-digit integer

int number = 12345;

int firstDigit = Integer.parseInt(Integer.toString(number).substring(0, 1));

int lastDigit = number % 10;

System.out.println("First digit: " + firstDigit + ", Last digit: " + lastDigit);

// 2. Salary increment of 15%

double salary = 50000;

double newSalary = salary + (salary * 0.15);

System.out.println("New Salary after 15% increment: " + newSalary);


// 3. Convert Fahrenheit to Celsius

double fahrenheit = 98.6;

double celsius = (fahrenheit - 32) * 5 / 9;

System.out.println("Temperature in Celsius: " + celsius);

```

---

### **Assignment 4: Programs Based on Input Through Scanner Class**

```java

import java.util.Scanner;

public class ScannerInput {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// 1. Calculate Gross Salary and Net Salary

System.out.print("Enter basic salary: ");

double basicSalary = sc.nextDouble();

double hra = 0.15 * basicSalary;

double da = 0.70 * basicSalary;

double ta = 0.02 * basicSalary;


double pf = 0.10 * basicSalary;

double grossSalary = basicSalary + hra + da + ta;

double netSalary = grossSalary - pf;

System.out.println("Gross Salary: " + grossSalary + ", Net Salary: " + netSalary);

// 2. Calculate net bill after VAT

System.out.print("Enter rate and quantity: ");

double rate = sc.nextDouble();

int quantity = sc.nextInt();

double amount = rate * quantity;

double vat = 0.125 * amount;

double netBill = amount + vat;

System.out.println("Net Bill after VAT: " + netBill);

// 3. Convert time duration to seconds

System.out.print("Enter hours, minutes, and seconds: ");

int hours = sc.nextInt();

int minutes = sc.nextInt();

int seconds = sc.nextInt();

int totalSeconds = (hours * 3600) + (minutes * 60) + seconds;

System.out.println("Total seconds: " + totalSeconds);

```
---

### **Assignment 5: Programs Based on Mathematical Methods**

```java

public class MathMethods {

public static void main(String[] args) {

// 1. Calculate area of triangle using Heron's formula

double side1 = 5, side2 = 6, side3 = 7;

double s = (side1 + side2 + side3) / 2;

double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

System.out.println("Area of the triangle: " + area);

// 2. Volume and surface area of a sphere

double radius = 5;

double volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);

double surfaceArea = 4 * Math.PI * Math.pow(radius, 2);

System.out.println("Volume: " + volume + ", Surface Area: " + surfaceArea);

// 3. Side of a cube from its volume

double cubeVolume = 27;

double side = Math.cbrt(cubeVolume);

System.out.println("Side of the cube: " + side);

// 4. Greatest and smallest of three numbers


int num1 = 15, num2 = 25, num3 = 10;

int largest = Math.max(num1, Math.max(num2, num3));

int smallest = Math.min(num1, Math.min(num2, num3));

System.out.println("Greatest: " + largest + ", Smallest: " + smallest);

```

---

### **Assignment 6: Questions Based on Operators and Expressions in Java**

```java

public class OperatorsExpressions {

public static void main(String[] args) {

// 1. Expressions with m = 5

int m = 5;

System.out.println((3 * ++m) % 4);

System.out.println((3 * m++) % 4);

// 2. Conditional expressions

int A = 2000, B = 1000;

System.out.println((A + B > 1750) ? 400 : 200);

A = 1000; B = 1000;

System.out.println((A + B > 1750) ? 400 : 200);


// 3. Expression with m, p, n

int p = 10, n = 7;

m = --p + p * ++n;

System.out.println("Value of m: " + m);

// 4. Expression with x and y

int y = 4;

int x = y++ + ++y + y;

System.out.println("Value of x: " + x);

// 5. Expressions with a = 4

int a = 4;

System.out.println((4 * ++a) % 5);

System.out.println((4 * a++) % 5);

```

---

---

### **Assignment 7: Questions Based on Mathematical Methods**


```java

public class MathFunctions {

public static void main(String[] args) {

// 1. Mathematical functions

System.out.println(Math.max(-17, -19));

System.out.println(Math.ceil(7.8));

// 2. Program segment output

double x = 2.9, y = 2.5;

System.out.println(Math.min(Math.floor(x), y));

System.out.println(Math.max(Math.ceil(x), y));

// 3. Math functions

System.out.println(Math.ceil(4.2));

System.out.println(Math.abs(-4));

// 4. More mathematical functions

int p = Math.abs(Math.max(-91, -97));

double m = Math.cbrt(9.261);

System.out.println("p = " + p + ", m = " + m);

// 5. Final values of variables

double a = -99.51, b = -56.25;

double valP = Math.abs(Math.floor(a));

double valQ = Math.sqrt(Math.abs(b));


System.out.println("p = " + valP + ", q = " + valQ);

```

---

### **Assignment 8: Programs Based on Conditional Constructs (if Programs)**

```java

import java.util.Scanner;

public class IfPrograms {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// 1. Find the largest of two numbers

System.out.print("Enter two numbers: ");

int num1 = sc.nextInt();

int num2 = sc.nextInt();

if (num1 > num2) {

System.out.println(num1 + " is larger.");

} else {

System.out.println(num2 + " is larger.");

}
// 2. Check if a number is divisible by 7

System.out.print("Enter a number: ");

int number = sc.nextInt();

if (number % 7 == 0) {

System.out.println(number + " is divisible by 7.");

} else {

System.out.println(number + " is not divisible by 7.");

```

---

### **Assignment 9: Programs Based on Conditional Constructs (if-else Programs)**

```java

import java.util.Scanner;

public class IfElsePrograms {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// 1. Check if an integer is even or odd


System.out.print("Enter an integer: ");

int num = sc.nextInt();

if (num % 2 == 0) {

System.out.println(num + " is even.");

} else {

System.out.println(num + " is odd.");

// 2. Check if an integer is a three-digit number

System.out.print("Enter an integer: ");

num = sc.nextInt();

if (num >= 100 && num <= 999) {

System.out.println(num + " is a three-digit number.");

} else {

System.out.println(num + " is not a three-digit number.");

// 3. Check if a character is uppercase or lowercase

System.out.print("Enter a character: ");

char ch = sc.next().charAt(0);

if (Character.isUpperCase(ch)) {

System.out.println(ch + " is uppercase.");

} else {

System.out.println(ch + " is lowercase.");

}
// 4. Check if a character is a vowel or consonant

System.out.print("Enter a character: ");

ch = sc.next().charAt(0);

if ("AEIOUaeiou".indexOf(ch) != -1) {

System.out.println(ch + " is a vowel.");

} else {

System.out.println(ch + " is a consonant.");

// 5. Check voting eligibility based on age

System.out.print("Enter your age: ");

int age = sc.nextInt();

if (age >= 18) {

System.out.println("You are eligible to vote.");

} else {

System.out.println("You are not eligible to vote.");

```

---
### **Assignment 10: Programs Based on Conditional Constructs (if-else-if
Programs)**

```java

import java.util.Scanner;

public class IfElseIfPrograms {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// 1. Find grade based on marks in English

System.out.print("Enter marks in English: ");

int marks = sc.nextInt();

if (marks >= 90) {

System.out.println("Grade: A");

} else if (marks >= 70) {

System.out.println("Grade: B");

} else if (marks >= 50) {

System.out.println("Grade: C");

} else if (marks >= 35) {

System.out.println("Grade: D");

} else {

System.out.println("Grade: E");

}
// 2. Calculate Gross Salary based on allowances

System.out.print("Enter Basic Salary: ");

double basic = sc.nextDouble();

double da, sa;

if (basic <= 10000) {

da = 0.10 * basic;

sa = 0.05 * basic;

} else if (basic <= 20000) {

da = 0.12 * basic;

sa = 0.08 * basic;

} else if (basic <= 30000) {

da = 0.15 * basic;

sa = 0.10 * basic;

} else {

da = 0.20 * basic;

sa = 0.12 * basic;

double grossSalary = basic + da + sa;

System.out.println("Gross Salary: " + grossSalary);

```

---
### **Assignment 11: Profit or Loss Calculation**

```java

import java.util.Scanner;

public class ProfitLoss {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter cost price: ");

double costPrice = sc.nextDouble();

System.out.print("Enter selling price: ");

double sellingPrice = sc.nextDouble();

if (sellingPrice > costPrice) {

double profit = sellingPrice - costPrice;

double profitPercent = (profit / costPrice) * 100;

System.out.println("Profit: " + profit + ", Profit Percent: " + profitPercent + "%");

} else if (sellingPrice < costPrice) {

double loss = costPrice - sellingPrice;

double lossPercent = (loss / costPrice) * 100;

System.out.println("Loss: " + loss + ", Loss Percent: " + lossPercent + "%");

} else {

System.out.println("Neither profit nor loss.");

}
}

```

---

---

### **Assignment 12: Triangle Type Check**

```java

import java.util.Scanner;

public class TriangleType {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter three angles of a triangle: ");

int angle1 = sc.nextInt();

int angle2 = sc.nextInt();

int angle3 = sc.nextInt();

if (angle1 + angle2 + angle3 == 180) {

if (angle1 == 90 || angle2 == 90 || angle3 == 90) {


System.out.println("Right-angled triangle.");

} else if (angle1 < 90 && angle2 < 90 && angle3 < 90) {

System.out.println("Acute-angled triangle.");

} else {

System.out.println("Obtuse-angled triangle.");

} else {

System.out.println("Triangle not possible.");

```

---

### **Assignment 13: Income Tax Calculation**

```java

import java.util.Scanner;

public class IncomeTax {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter name: ");


String name = sc.nextLine();

System.out.print("Enter age: ");

int age = sc.nextInt();

System.out.print("Enter taxable income: ");

double income = sc.nextDouble();

if (age > 60) {

System.out.println("Wrong Category");

} else {

double tax;

if (income <= 250000) {

tax = 0;

} else if (income <= 500000) {

tax = (income - 250000) * 0.10;

} else if (income <= 1000000) {

tax = (income - 500000) * 0.20 + 25000;

} else {

tax = (income - 1000000) * 0.30 + 125000;

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

System.out.println("Income Tax: " + tax);

```
---

### **Assignment 14: Volume and Resistance Calculations Using Switch Case**

```java

import java.util.Scanner;

public class VolumeResistance {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Choose an option:");

System.out.println("1. Volume of Cuboid");

System.out.println("2. Volume of Cylinder");

System.out.println("3. Volume of Cone");

int choice = sc.nextInt();

switch (choice) {

case 1:

System.out.print("Enter length, breadth, and height: ");

double length = sc.nextDouble();

double breadth = sc.nextDouble();

double height = sc.nextDouble();

System.out.println("Volume of Cuboid: " + (length * breadth * height));


break;

case 2:

System.out.print("Enter radius and height: ");

double radius = sc.nextDouble();

double cylinderHeight = sc.nextDouble();

System.out.println("Volume of Cylinder: " + (Math.PI * radius * radius *


cylinderHeight));

break;

case 3:

System.out.print("Enter radius and height: ");

double coneRadius = sc.nextDouble();

double coneHeight = sc.nextDouble();

System.out.println("Volume of Cone: " + (Math.PI * coneRadius * coneRadius *


coneHeight) / 3);

break;

default:

System.out.println("Invalid choice.");

```

---
### **Assignment 15: Programs Based on Looping Statement (for)**

#### **1. Display the First Ten Terms of the Series 1, 4, 9, 16, 25, ...**

```java

public class SeriesTerms {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

System.out.print((i * i) + " ");

```

#### **2. Find the Sum of the Series 1² + 2² + 3² + ... + 10²**

```java

public class SumOfSeries {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 10; i++) {

sum += (i * i);

System.out.println("Sum of the series: " + sum);

}
```

---

### **Assignment 16: Programs Based on Looping Statement (while)**

#### **1. Enter a Number and Display the Sum of its Digits**

```java

import java.util.Scanner;

public class SumOfDigits {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = sc.nextInt();

int sum = 0;

while (number != 0) {

sum += number % 10;

number /= 10;

System.out.println("Sum of digits: " + sum);

```
#### **2. Check and Display Whether a Number is a Niven Number**

(A Niven number is divisible by the sum of its digits)

```java

import java.util.Scanner;

public class NivenNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = sc.nextInt();

int sum = 0, temp = number;

while (temp != 0) {

sum += temp % 10;

temp /= 10;

if (number % sum == 0) {

System.out.println(number + " is a Niven number.");

} else {

System.out.println(number + " is not a Niven number.");

}
```

---

### **Assignment 17: Programs Based on Looping Statement (do…while)**

#### **1. Display the First Ten Numbers of the Fibonacci Series**

```java

public class FibonacciSeries {

public static void main(String[] args) {

int a = 0, b = 1, count = 1;

System.out.print("Fibonacci series: ");

do {

System.out.print(a + " ");

int next = a + b;

a = b;

b = next;

count++;

} while (count <= 10);

```

#### **2. Check Whether a Number is a Palindrome**


```java

import java.util.Scanner;

public class PalindromeNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = sc.nextInt();

int original = number, reverse = 0;

do {

int digit = number % 10;

reverse = reverse * 10 + digit;

number /= 10;

} while (number != 0);

if (original == reverse) {

System.out.println(original + " is a palindrome.");

} else {

System.out.println(original + " is not a palindrome.");

```
---

### **Assignment 18: Menu-Driven Program for Electronics Discount Calculation**

```java

import java.util.Scanner;

public class KumarElectronics {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter customer name: ");

String name = sc.nextLine();

System.out.print("Enter purchase amount: ");

double amount = sc.nextDouble();

System.out.print("Enter type of purchase (L for Laptop, D for Desktop): ");

char type = sc.next().charAt(0);

double discount = 0.0;

switch (type) {

case 'L':

if (amount <= 25000) discount = 0;

else if (amount <= 50000) discount = 0.05 * amount;

else if (amount <= 100000) discount = 0.075 * amount;


else discount = 0.10 * amount;

break;

case 'D':

if (amount <= 25000) discount = 0.05 * amount;

else if (amount <= 50000) discount = 0.075 * amount;

else if (amount <= 100000) discount = 0.10 * amount;

else discount = 0.15 * amount;

break;

default:

System.out.println("Invalid type of purchase.");

return;

double netAmount = amount - discount;

System.out.println("Customer Name: " + name);

System.out.println("Purchase Amount: " + amount);

System.out.println("Discount: " + discount);

System.out.println("Net Amount to be Paid: " + netAmount);

```

---
### **Assignment 19: Menu-Driven Program for Palindrome or Perfect Number**

```java

import java.util.Scanner;

public class NumberCheck {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Choose an option:");

System.out.println("1. Check Palindrome");

System.out.println("2. Check Perfect Number");

int choice = sc.nextInt();

System.out.print("Enter a number: ");

int number = sc.nextInt();

switch (choice) {

case 1:

int original = number, reverse = 0;

while (number != 0) {

int digit = number % 10;


reverse = reverse * 10 + digit;

number /= 10;

System.out.println((original == reverse) ? "Palindrome" : "Not Palindrome");

break;

case 2:

int sum = 0;

for (int i = 1; i <= number / 2; i++) {

if (number % i == 0) sum += i;

System.out.println((sum == number) ? "Perfect Number" : "Not Perfect


Number");

break;

default:

System.out.println("Invalid choice.");

```

---

### **Assignment 20: Menu-Driven Program for Series Calculations**


```java

import java.util.Scanner;

public class SeriesMenu {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Choose an option:");

System.out.println("1. Find sum of series 1^2 + 2^2 + ... + 10^2");

System.out.println("2. Display series: 1, 11, 111, 1111, 11111");

int choice = sc.nextInt();

switch (choice) {

case 1:

int sum = 0;

for (int i = 1; i <= 10; i++) {

sum += i * i;

System.out.println("Sum of series: " + sum);

break;

case 2:

String number = "1";

for (int i = 1; i <= 5; i++) {


System.out.print(number + " ");

number += "1";

System.out.println();

break;

default:

System.out.println("Invalid choice.");

```

---

You might also like