Sunday, 16 July 2023

Class as the Basis of All Computation

Chapter : Class as the Basis of All Computation

About Java Programming Language

Topics Covered

  • Java: An Introduction
  • Java Virtual Machine
  • Class Libraries
  • Java Character Set
  • Tokens
  • Keywords
  • Identifiers
  • Naming Convention of an Identifier
  • Literals and its types
  • Concepts of Data Types
  • Variables
  • Reference Types
  • Constants
  • Operators in Java
  • Expressions
  • Java Statements
  • Significance of Classes

 JAVA : AN INTRODUCTION

                             Java was first developed by James Gosling at Sun Micro systems. It was released in May 1995 as a core component of Sun Micro systems '. Java was basically designed for electrical and electronic appliances for the operation of it remove and touch screen and finally it was found that Java can be used as a Programming language. It is divided into two types of programming

  1. Java Applications
  2. Java Applets

 The original and reference implementation Java compilers, virtual machines, and class libraries were originally released by Sun under proprietary licenses.

Java compilers: - Compiler is a program used to compile and run Java program.

Java Virtual Machine:-  A Java virtual machine is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java byte code. The J.V.M is detailed by a specification that formally describes what is required in a J.V.M.

Class libraries: - The Java Class Library is a set of dynamically loadable libraries that Java Virtual Machine languages can call at run time. Because the Java Platform is not dependent on a specific operating system, applications cannot rely on any of the platform-native libraries.

Tokens:-The Java compiler breaks the line of code into text (words) is called Java tokens. These are the smallest element of the Java program.

Keywords:- These are the reserve words used for writing Java code.

Identifiers:- Identifiers are the name given to different parts of the program.   

Naming conventions Identifiers :-

1. No Identifier (variable / array / method / class) name should starts with a java keyword.

2. There is no space between an identifiers name.

3. No special characters(such as !@#$%^&*) except _ and $.

4. No identifier should starts with a digit.

Literals and its types:- The physical value which is stored or assign in a variable are literals.

Integer Literal (23,44.44)- The integer values are integer literals 

Integer Literal of three types

  • Decimal (base 10)
  • Octal (base 8)
  • Hexadecimal (base 16)

Character Literal (A, B, a, b) - The character values are character literals

Floating Literals (23.43) - The small decimal values are float literals. Floating literal are also called Real literal.

Following are the

Double Literals (23.6889897) - The long decimal values are double literals

Boolean Literals (true,false) - The true-false values are boolean literals

Long Literals (38328236889897) - The long integer  values are Long literals

Shot Literals (45) - The shot integer  values are shot literals

Byte Literals (3) - The byte integer  values are byte literals

 
Concepts of Data Types

Sizes and values that can be stored in the variable can be specifies through data types. 

There are two types of data types in Java:

 1. Primitive data types: The primitive data types include

  • int   it helps to define integer data(2, 3, 4, -1, -4, -7 etc).           Ex int x=10;
  • char it helps to define character data(a, b, %, @, %, * etc).
  • float (23.43F, 3.33F etc).
  • short(22, 34, 44)
  • long(38829239392398L, 39990284884998949L)
  • boolean(true, false)
  • byte(3, 4, 4, 3 etc)
 2. Non-primitive data types: The non-primitive data types include Classes, Interface and Array.
             Class : A prototype definition having data member and member methods.

            Array: A collection set of similar values of same type.

           Interfaces: A collection of classes.

 
Reference Types
 
Constants
 
Operators in Java
 
Expressions
 
Java Statements
 
Significance of Classes

 

 

 

Friday, 14 July 2023

Conditional Statements [if..else]

 CONDITIONAL STATEMENT
IN JAVA

Topics Covered

  • Control Statements
  • Conditional Statement
  • Nested If
  • Switch..case default Statement
  • A limitation with switch..case
  • Break statement in switch
  • Lopping structure
1. Control Statements 

Control statements specify the order of execution of the instructions present in a program. These make it possible for the program to make certain decisions, perform various tasks repeatedly, or even jump from any one section of the code to a different section.

2. Conditional Statement
We can use these conditions to perform different actions for different decisions.Java supports the usual logical conditions from mathematics:
Less than a < b    
Less than or equal to a <= b 
Greater than a > b 
Greater than a >= b
Equal to a == b
Not Equal to a != b 

Java has the following conditional statements  
  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed
The if Statement
Use of the 'if statement' specifies a block of Java code to be executed if a condition got to be 
true
Syntax
if (condition) {
  // block of "if the condition is true"
}
Note:-  
1. if is in lowercase letters. 
2. Uppercase letters (If or IF) will generate an error.
Example:- 


The If..Else If Statement
Use of the 'if.elseIf statement' specifies a block of Java code to be executed if a condition got to be 
true or false means the binary condition. 
Syntax
if (condition) 
{
  // block of "if the condition is true" (if block statements)
}
else
{
  // block of "if the condition is false" (else block statement)
}
Note:-  
1. Never put a semi-colon after if(condition)
2. Semicolon will in each block statement ends.
Example:-

The If..Else..Else If Statement
Use of the 'if..ElseIf..Else statement' specifies a block of Java code to be executed if a condition got to be 
true or false means the binary condition. 
Syntax
if (condition) 
{
  // block of "if the condition is true" (if block statements)
}
else if(condition)
{
  // block of "else if the condition is false" (else-if block statement)
}
else
{
  // block of "if else the condition is also false" (else block statement)
}
Note:-  
1. Never put a semi-colon after if(condition)
2. Semicolon will in each block statement ends.
Example:-
Nested If Statement
Use of the 'Nested..Else statement' is if within an another if condition
Syntax
if (condition) 
{
  // block of "if the condition is true" (if block statements)

    if(condition)
    {
          // block of "Nested if the condition is true" (Nested-if block statement)
    }
    else
    {
          // block of "Nested if else's Block Nested if's if  condition is also false" (Nested-if block statement)
    }
}
else
{
       //block of Finally else 
}
Note:-  
1. Never put a semi-colon after if(condition)
2. Semicolon will in each block statement ends.
Example:-

PROGRAMS BASED ON CONDITIONAL STATEMENTS
Program 1: Write a program to input three numbers (positive or negative). If they are unequal then display the greatest number otherwise display they are equal. The program also displays whether the number entered by the users are "A; positive", "All Negative" or "Mixed Numbers".
Sample Input: 56, -15, 12
Sample Output:  The greatest number is 56
Entered numbers are mixed numbers.
Solution:-
 class Greatest {
  public static void main(String[] args) {
    // declaring double type variables
    double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest,negative;
    // checks if n1 is greater than or equal to n2
       if (n1 >= n2) {
      // if...else statement inside the if block
      // checks if n1 is greater than or equal to n3
      if (n1 >= n3) {
        largest = n1;
      }
      else {
        largest = n3;
      }
    } else {

      // if..else statement inside else block
      // checks if n2 is greater than or equal to n3
      if (n2 >= n3) {
        largest = n2;
      }
      if(n1<0)
      {
          negative=n1
      }
      if(n2<0)
      {
          negative=n2;
      }
      if(n3<0)
      {
          negative=n3;
      }

          }
};


CLASS X TYPES OF NUMBERS

 CLASS X  TYPES OF NUMBERS List of types of Numbers 1. Arm Strong Number 2.  Co-prime Number 3. Duck Number [I.C.S.E 2024] 4. Magic Number  ...