Showing posts with label ICSE Class 9. Show all posts
Showing posts with label ICSE Class 9. Show all posts

Saturday, 1 February 2025

Java Programming Fundamentals for Class 9 (ICSE Syllabus)

Question 1:

Below is a Java program to input integers, float and Decimal values from the user.

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter an integer: ");
        int integerValue = input.nextInt();
        System.out.println("Enter a float: ");
        float floatValue = input.nextFloat();
        System.out.println("Enter a decimal: ");
        Double decimalValue = input.nextDouble();
        System.out.println("\nYou entered:");
        System.out.println("Integer: " + integerValue);
        System.out.println("Float: " + floatValue);
        System.out.println("Decimal: " + decimalValue);
    }
}


Explanation:

Import necessary classes:

java.util.Scanner: For reading user input.

Create a Scanner object:

Scanner input = new Scanner(System.in); 
This creates a Scanner object named input that reads from the standard input stream (System.in), which is the keyboard by default.

Prompt the user:

System.out.println(...) displays a message on the console, asking the user to enter a value.

Read the input:

int integerValue = input.nextInt(); reads an integer from the user's input and stores it in the integerValue variable.
float floatValue = input.nextFloat(); reads a float from the user's input and stores it in the floatValue variable.
Double decimalValue = input.nextDouble(); reads a decimal number from the user's input and stores it in the decimalValue variable.

Display the entered values:

System.out.println(...) prints the values that the user entered back to the console, so the user can confirm they were entered correctly.

End of Explanation...................

Below is a Variable description Table for the above Java Program

VARIABLE DESCRIPTION TABLE

Variable Name

Data Type

Description

integerValue

int

Stores the integer value entered by the user.

floatValue

float

Stores the floating-point value entered by the user.

decimalValue

Double

Stores the double-precision floating-point value (decimal) entered by the user.


Application-Based/Practical Question for Students Homework:

Scenario: You are developing a simple program to collect personal information from a user. This program needs to gather the user's age (integer), height (float), and monthly salary (which can have decimal places, so you should use Double for accuracy).

Task:

  1. Write a Java program that prompts the user to enter their age, height in meters, and monthly salary.
  2. The program should store these values in appropriate variables.
  3. The program should then display the entered data back to the user in a clear and formatted way. Make sure to include appropriate units (e.g., "meters" for height, "Rs." for salary).
  4. Ensure that you use the correct data types for each piece of information:
    • int for age.
    • float for height.
    • Double for salary.

Example Output:

Welcome to the Data Input Program!

Enter your age (integer): 25

Enter your height in meters (float): 1.75

Enter your salary (decimal): 25000.50

 

--- Your Entered Data ---

Age: 25

Height: 1.75 meters

Salary: Rs 25000.50

Test this code using Java Compiler by clicking here

Note: Explanation for Homework is not mandatory. Variable Description Tables are mandatory.

Question 2:

Write a Java Program to accept a number from the user and determine if the entered number is a Positive Number, Negative Number or Zero.

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int number = input.nextInt();
        // Check if the number is positive
        if (number > 0) 
        {
            System.out.println(number + " is a positive number.");
        }
        // Check if the number is negative
        else if (number < 0) 
        {
            System.out.println(number + " is a negative number.");
        }
        // If neither positive nor negative, it must be zero
        else 
        {
            System.out.println(number + " is zero.");
        }
    }
}


Explanation:

Input: The program uses a Scanner to get integer input from the user.

First if-else block:

The first if condition checks if number is greater than 0. If it is, the corresponding message i.e. "number + " is a positive number." is printed.
The else if condition checks if number is less than 0. This is checked only if the first if condition is false.
The else block is executed only if both the if and else if conditions are false (meaning the number must be 0).

End of Explanation - - - - - - 

Below is the variable description table for the same

VARIABLE DESCRIPTION TABLE

Variable Name

Data Type

Description

number

int

Stores the integer value entered by the user.



Application-Based/Practical Question for Students Homework:

Scenario: A banking system tracks account balances. A positive number represents a credit, a negative number represents a debit, and zero means the account is at zero balance.

Detailed Scenario: Imagine a simplified banking system. Customers can perform transactions that affect their account balance. These transactions can be:
  • Deposits (Credits): Money added to the account (positive number).
  • Withdrawals (Debits): Money taken out of the account (negative number).
  • Balance Check: Checking the current account balance (can be positive, negative, or zero).
Transaction Classification: When a transaction is entered, the system needs to determine its type:
  • If the transaction amount is positive, it's a deposit (credit).
  • If the transaction amount is negative, it's a withdrawal (debit).
  • If the transaction amount is zero, it will represent (zero)

Example Output:

Welcome to the PR Webmatrix Banking System

Enter your transaction amount: 25000

 

--- PR Webmatrix Receipt ---

Transaction Amount: 25000

Transaction Classification: Deposit (Credit)

Test this code using Java Compiler by clicking here

Note: Explanation for Homework is not mandatory. Variable Description Tables are mandatory.


Question 3:

Write a simple Java program to check if the entered character is a letter or not.


import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = input.next().charAt(0);
        if (Character.isLetter(ch))
        {
            System.out.println(ch + " is a letter.");
        } 
        else 
        {
            System.out.println(ch + " is not a letter.");
        }
    }
}

Explanation:

Import Scanner: The java.util.Scanner class is imported to allow the program to read input from the user.

Create Scanner Object: A Scanner object is created to read input from the standard input stream (System.in), which is the keyboard by default.

Prompt for Input: The program prompts the user to enter a character.

Read Input:  input.next().charAt(0) reads the first character of the user's input.  input.next() reads the entire input as a string. charAt(0) extracts the character at index 0 (the first character).

Check if it's a letter: The Character.isLetter(ch) method is used to determine if the entered character ch is a letter (either uppercase or lowercase). This method returns true if it's a letter, and false otherwise.

Print Output:  Based on the result of Character.isLetter(), the program prints an appropriate message indicating whether the character is a letter or not.

End of Explanation -------------

Below is the variable description table for the same

VARIABLE DESCRIPTION TABLE

Variable Name

Data Type

Description

ch

char

Stores the single character entered by the user.



Application-Based/Practical Question for Students Homework:

Scenario: A small library uses a simple system to categorize books.  They assign a single character code to each book genre.  The system needs to be updated to validate these genre codes.  Only letters (A-Z or a-z) are valid genre codes.  Write a Java program that takes a character as input (representing a potential genre code) and checks if it is a valid genre code.  The program should print "Valid Genre Code" if the character is a letter, and "Invalid Genre Code" otherwise.

Example Output:

Welcome to the PR Webmatrix Library Management System

Enter a Library Genre Code: A 

--- PR Webmatrix Library Management Receipt ---

A is Valid Genre Code


Note: Explanation for Homework is not mandatory. Variable Description Tables are mandatory.


Question 4:

Write a simple Java program to check if the entered character is a digit or not.


import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = input.next().charAt(0);
        if (Character.isDigit(ch)) 
        {
            System.out.println(ch + " is a digit.");
        } 
        else 
        {
            System.out.println(ch + " is not a digit.");
        }
    }
}


Explanation

Import Scanner: The java.util.Scanner class is imported to allow the program to take user input from the console.

Create Scanner Object: A Scanner object is created to read input from System.in (the standard input stream, usually the keyboard).

Prompt for Input: The program prompts the user to enter a character.

Read Character: input.next().charAt(0) reads the first character of the user's input.  input.next() reads the entire input as a String, and .charAt(0) extracts the character at index 0 (the first character).

Method 1: Character.isDigit():

The most straightforward and recommended way to check if a character is a digit is to use the Character.isDigit(ch) method. This method returns true if the character ch is a digit (0-9), and false otherwise.

End of Explanation---------

Below is the variable description table for the same

VARIABLE DESCRIPTION TABLE

Variable Name

Data Type

Description

ch

char

Stores the single character entered by the user.


Application-Based/Practical Question for Students Homework:

Scenario: Simple Order Processing System

A small online store needs a Java program to calculate the total cost of an order. Customers can order multiple items, each with a name, price, and quantity.

Problem:

You need to write a Java program that calculates the total cost of an order.

Task:

Design and implement a Java program that takes a list of order items as input and calculates the total cost of the order.  Each order item has a name, a price, and a quantity.  The program should output the total cost.

Input:

The program should receive a list of order items.  Each order item should have the following information:

Item Name: A text description of the item (e.g., "T-Shirt").
Price: The price of one unit of the item (e.g., 20.0).
Quantity: The number of units ordered (e.g., 2).

Output:

The program should print the total cost of the order.

Example:

If the input is:

Item 1: Name = "T-Shirt", Price = 20.0, Quantity = 2

Then , output will be

Total Cost is: Rs. 40.0


Question 5:

Write a simple Java program to check if the entered character is a Special Character or not.


import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = input.next().charAt(0);
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == ' ') 
        {
            System.out.println(ch + " is NOT a special character.");
        } 
        else 
        {
            System.out.println(ch + " IS a special character.");
        }
    }
}

Explanation

Get Input:  The program asks the user to type in a single character and stores it in the ch variable.

Check if it's a Letter, Number, or Space: The if condition checks if the character ch is:

A lowercase letter (between 'a' and 'z')
An uppercase letter (between 'A' and 'Z')
A number (between '0' and '9')
A space
If it IS a Letter, Number, or Space: If the character matches any of the above, the program prints that it's not a special character.

Otherwise (It's Special): If the character is not a letter, number, or space, the else part of the code runs, and the program prints that it is a special character.

End of Explanation--


Application-Based/Practical Question for Students Homework:

Scenario:

Imagine you're designing a simple password checker for a website. One of the rules is that passwords must contain at least one "special character." For now, let's simplify and say a "special character" is anything that is not a letter (a-z or A-Z), a number (0-9), or a space.

Task:

Write a Java program that asks the user to enter a single character. The program should then determine if that character could be considered a "special character" for this password checker. If it is a special character (according to our simplified definition), the program should print "Valid for password." Otherwise, it should print "Not valid for password."

Example Interaction:

Enter a character: $
Valid for password

Enter a character: a
Not valid for password

Enter a character: 7
Not valid for password

Enter a character:  (space)
Not valid for password

Question 6:

Write a Java program that implements a Circle class. The Circle class has fields: radius, area, and circumference. It has methods to obtain the value of the radius and calculate the area and circumference of the circle. Write a Java program to invoke the methods of the Circle class and display the results.

import java.util.Scanner;
class Circle
{
    Scanner sc = new Scanner(System.in);
    double radius, area, circumference;
    void getValues()
    {
        System.out.println("Enter the radius of the Circle");
        radius = sc.nextDouble();
    }
    void Calculate()
    {
        area = 3.14*radius*radius;
        circumference = 2*3.14*radius;
    }
    void display()
    {
        System.out.println("Area of Circle = "+area);
        System.out.println("Circumference of Circle = "+circumference);
    }
    public static void main(String[] args) 
    {
        Circle mainobj = new Circle();
        mainobj.getValues();
        mainobj.Calculate();
        mainobj.display();
        
    }
}

Explanation

1. We define a class named Circle.
2. Inside the class, we create an object sc of type Scanner to read user input from the console.
3. We also declare three variables: radius, area, and circumference to store the radius of the circle and its calculated area and circumference.
4. The getValues() method prompts the user to enter the radius of the circle.
5. It then stores the entered value in the radius variable using sc.nextDouble(), which allows the user to input a decimal value.
6. The Calculate() method is responsible for calculating the area and circumference of the circle using the formulas
7. The display() method prints the calculated area and circumference of the circle to the console. The values are printed using System.out.println()
8. The main() method is where the program starts executing.
9. An object mainobj of the Main class is created.
10. The program calls the following methods on mainobj:

mainobj.getValues() - This method gets the radius from the user.
mainobj.Calculate() - This method calculates the area and circumference.
mainobj.display() - This method displays the results (area and circumference).

End of Explanation--

VARIABLE DESCRIPTION TABLE

Variable Name

Data Type

Description

radius

double

Stores the radius of the circle, which is provided by the user.

area

double

Stores the calculated area of the circle based on the given radius.

circumference

double

Stores the calculated circumference of the circle based on the given radius.



Application-Based/Practical Question for Students Homework:

Scenario: Generate a scenario on your own for this question and practice properly.


Also, please stay tuned to this section as Python important topics will be shared soon.