0% found this document useful (0 votes)
6 views16 pages

Ameya 1

The document discusses experiments conducted in an Artificial Intelligence and Data Science course, including programs to calculate the roots of a quadratic equation, check if a number is prime, and demonstrate different types of operators using switch cases. It provides code examples and outputs for each program, discussing concepts like variables, loops, and operator types.

Uploaded by

9mcjxnr2s9
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)
6 views16 pages

Ameya 1

The document discusses experiments conducted in an Artificial Intelligence and Data Science course, including programs to calculate the roots of a quadratic equation, check if a number is prime, and demonstrate different types of operators using switch cases. It provides code examples and outputs for each program, discussing concepts like variables, loops, and operator types.

Uploaded by

9mcjxnr2s9
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/ 16

Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1

Grade :
Class/Roll No.:
-
Name : Ameya
Nikhil Kalgutkar
Kadam
D6ADA/26 7

1. Program on Basic programming construct like branching and looping.

Aim: A) To implement a program to print the roots of quadratic equations.

Theory: Variable: Variables are the data containers that save the data values during
Java program execution. Every Variable in Java is assigned a data type that
designates the type and quantity of value it can hold.

Types of variables:
1. Local Variables: A variable defined within a block or method or constructor is called a local
variable. These variables are created when the block is entered, or the function is called and
destroyed after exiting from the block or when the call returns from the function. The scope of
these variables exists only within the block in which the variables are declared, i.e., we can
access these variables only within that block. Initialization of the local variable is mandatory
before using it in the defined scope.

Time Complexity of the Method:


Time Complexity: O(1)
Auxiliary Space: O(1)

2. Instance Variables: Instance variables are non-static variables and are declared in a class
outside of any method, constructor, or block. As instance variables are declared in a class,
these variables are created when an object of the class is created and destroyed when the
object is destroyed. Unlike local variables, we may use access specifies for instance
variables. If we do not specify any access specifies, then the default access specified will be
used. Initialization of an instance variable is not mandatory. Its default value is dependent on
the data type of variable. For String it is null, for float it is 0.0f, for int it is 0, for Wrapper
classes like Integer it is null, etc. Instance variables can be accessed only by creating
objects. We initialize instance variables using constructors while creating an object. We can
also use instance blocks to initialize the instance variables.

The complexity of the method:


Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


Time Complexity: O(1)
Auxiliary Space: O(1)

3. Static Variables: Static variables are also known as class variables. These variables are
declared similarly to instance variables. The difference is that static variables are declared
using the static keyword within a class outside of any method, constructor, or block. Unlike
instance variables, we can only have one copy of a static variable per class, irrespective of
how many objects we create. Static variables are created at the start of program execution
and destroyed automatically when execution ends. Initialization of a static variable is not
mandatory. Its default value is dependent on the data type of variable. For String it is null, for
float it is 0.0f, for int it is 0, for Wrapper classes like Integer it is null, etc. If we access a static
variable like an instance variable (through an object), the compiler will show a warning
message, which won’t halt the program. The compiler will replace the object name with the
class name automatically. If we access a static variable without the class name, the compiler
will automatically append the class name. But for accessing the static variable of a different
class, we must mention the class name as 2 different classes might have a static variable
with the same name. Static variables cannot be declared locally inside an instance method.
Static blocks can be used to initialize static variables.

The complexity of the method:


Time Complexity: O(1)
Auxiliary Space: O(1)
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1

Syntax of for loop is:


For(Initialization ; Test Expression ; Updation){
// Body of the Loop (Statement(s))
}

Syntax of while loop is:


Initialization;
While(Test Expression){
// Body of the Loop (Statement(s))
Updation;
}

Syntax of do-while loop:


Initialization;
Do {
// Body of the loop (Statement(s))
// Updation;
}
While(Test Expression);
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


Program Code:
import java.util.Scanner;
public class QuadraticEquation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Quadratic Equation: ax^2 + bx + c = 0");

System.out.print("Enter the coefficient 'a': ");


double a = scanner.nextDouble();

System.out.print("Enter the coefficient 'b': ");


double b = scanner.nextDouble();

System.out.print("Enter the coefficient 'c': ");


double c = scanner.nextDouble();

double discriminant = b * b - 4 * a * c;

if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("The roots of the quadratic equation are: " + root1 + " and " +
root2);
} else {
System.out.println("The quadratic equation has complex roots.");
}
}
}
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


1. Programs on Basic programming constructs like branching and looping.

Output Screenshots :

Conclusion: The Java program successfully accomplishes the task of calculating


and displaying the roots of a quadratic equation. It provides a userfriendly interface
by prompting the user for the coefficients of the equation and then promptly
presenting the results.
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


Aim: B) To implement a Program to check if the entered no. is a prime no. or not.

Algorithm:
1. Start the program.
2. Display a user-friendly message asking the user to enter a positive integer.
3. Read the input number 'num' from the user.
4. Check if the 'num' is less than 2. If yes, display "The number must be greater than 1 to
be considered a prime number" and end the program.
5. Initialize a boolean variable 'isPrime' as true.
6. For each integer 'i' from 2 to the square root of 'num' (inclusive): a. If 'num' is divisible by
'i', set 'isPrime' as false and break the loop.
7. If 'isPrime' is true, display "The entered number is a prime number."
8. If 'isPrime' is false, display "The entered number is not a prime number."
9. End the program.

Program :

import java.util.Scanner;

public class PrimeNumberChecker {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int num = scanner.nextInt();
if (num < 2) {
System.out.println("The number must be greater than 1 to be considered a prime
number.");
} else {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}

if (isPrime) {
System.out.println("The entered number is a prime number.");
} else {
System.out.println("The entered number is not a prime number.");
}
}
}
}
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


Output :

Conclusion :

The Java program for the Prime Number Checker successfully determines whether the entered
number is a prime number or not. The program uses a simple primality test, checking if the
number has any divisors other than 1 and itself.
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


Aim: C) To implement a Program to demonstrate the working of types of operators(Bitwise,
Logical and relational) using switch cases .

Theory: There are multiple types of operators in Java


1. Arithmetic Operators
They are used to perform simple arithmetic operations on primitive data types.

• *: Multiplication
• / : Division
• % : Modulo
• + : Addition
• -: Subtraction

2. Unary Operators
Unary operators need only one operand. They are used to increment, decrement, or negate a
value.

• -: Unary minus, used for negating the values.


• + : Unary plus indicates the positive value (numbers are positive without this, however). It
performs an automatic conversion to int when the type of its operand is the byte, char, or
short. This is called unary numeric promotion.
• ++ : Increment operator, used for incrementing the value by 1. There are two varieties of
increment operators.
Post-Increment: Value is first used for computing the result and then incremented.
Pre-Increment: Value is incremented first, and then the result is computed.
• --: Decrement operator, used for decrementing the value by 1. There are two varieties of
decrement operators.
Post-decrement: Value is first used for computing the result and then decremented.
Pre-Decrement: The value is decremented first, and then the result is computed.
• ! : Logical not operator, used for inverting a boolean value.

3. Assignment Operator
Assignment operator is used to assign a value to any variable. It has right-to-left associativity, i.e.
value given on the right-hand side of the operator is assigned to the variable on the left, and
therefore right-hand side value must be declared before using it or should be a constant.
• +=, for adding the left operand with the right operand and then assigning it to the variable
on the left.
• -=, for subtracting the right operand from the left operand and then assigning it to the
variable on the left.
• *=, for multiplying the left operand with the right operand and then assigning it to the
variable on the left.
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


• /=, for dividing the left operand by the right operand and then assigning it to the variable
on the left.
• %=, for assigning the modulo of the left operand by the right operand and then assigning it
to the variable on the left.

4. Relational Operators
These operators are used to check for relations like equality, greater than, and less than. They
return boolean results after the comparison and are extensively used in looping statements as
well as conditional if-else statements.
• ==, Equal to returns true if the left-hand side is equal to the right-hand side.
• !=, Not Equal to returns true if the left-hand side is not equal to the right-hand side.
• <, less than: returns true if the left-hand side is less than the right-hand side.
• <=, less than or equal to returns true if the left-hand side is less than or equal to the right-
hand side.
• >, Greater than: returns true if the left-hand side is greater than the right-hand side.
• >=, Greater than or equal to returns true if the left-hand side is greater than or equal to the
right-hand side.

5. Logical Operators
These operators are used to perform “logical AND” and “logical OR” operations, i.e., a function
similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second
condition is not evaluated if the first one is false, i.e., it has a short-circuiting effect. Used
extensively to test for several conditions for making a decision. Java also has “Logical NOT”,
which returns true when the condition is false and vice-versa

Conditional operators are:

• &&, Logical AND: returns true when both conditions are true.
• ||, Logical OR: returns true if at least one condition is true.
• !, Logical NOT: returns true when a condition is false and vice-versa

6. Ternary operator
The ternary operator is a shorthand version of the if-else statement. It has three operands and
hence the name Ternary.

The general format is:

Condition ? if true : if false

The above statement means that if the condition evaluates to true, then execute the statements
after the ‘?’ else execute the statements after the ‘:’.
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1

7. Bitwise Operators
These operators are used to perform the manipulation of individual bits of a number. They can be
used with any of the integer types. They are used when performing update and query operations
of the Binary indexed trees.

• &, Bitwise AND operator: returns bit by bit AND of input values.
• |, Bitwise OR operator: returns bit by bit OR of input values.
• ^, Bitwise XOR operator: returns bit-by-bit XOR of input values.
• ~, Bitwise Complement Operator: This is a unary operator which returns the one’s
complement representation of the input value, i.e., with all bits inverted.

8. Shift Operators
These operators are used to shift the bits of a number left or right, thereby multiplying or dividing
the number by two, respectively. They can be used when we have to multiply or divide a number
by two.
• <<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a
result. Similar effect as multiplying the number with some power of two.
• >>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids
left as a result. The leftmost bit depends on the sign of the initial number. Similar effect to
dividing the number with some power of two.
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1

Program :

import java.util.Scanner;
public class OperatorDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("Menu:");
System.out.println("1. Arithmetic Operators (+, -, *, /, %)");
System.out.println("2. Shift Operators (Left Shift, Right Shift, Unsigned Right Shift)");
System.out.println("3. Bitwise Operators (AND, OR, XOR, NOT)");
System.out.println("4. Logical Operators (AND, OR, NOT)");
System.out.println("5. Relational Operators (>, <, >=, <=, ==, !=)");
System.out.println("6. Exit");
System.out.print("Enter your choice (1-6): ");
choice = scanner.nextInt();
System.out.print(“Enter value ‘a’: “);
double a = scanner.nextDouble();
System.out.print(“Enter value ‘b’: “);
double b = scanner.nextDouble();

switch (choice) {

case1:
System.out.println("Result of addition: " + (a + b));
System.out.println("Result of subtraction: " + (a - b));
System.out.println("Result of multiplication: " + (a * b));
System.out.println("Result of division: " + (a / b));
System.out.println("Result of modulus: " + (a % b));
break;

case 2:
System.out.print("Enter an integer value 'num': ");
int num = scanner.nextInt();
System.out.print("Enter the number of shifts 'n': ");
int n = scanner.nextInt();
System.out.println("Result of left shift: " + (num << n));
System.out.println("Result of right shift: " + (num >> n));
System.out.println("Result of unsigned right shift: " + (num >>> n));
break;
case 3:
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


System.out.print("Enter value 'a': ");
int aBitwise = scanner.nextInt();
System.out.print("Enter value 'b': "); int
bBitwise = scanner.nextInt();
System.out.println("Result of bitwise AND: " + (aBitwise & bBitwise));
System.out.println("Result of bitwise OR: " + (aBitwise | bBitwise));
System.out.println("Result of bitwise XOR: " + (aBitwise ^ bBitwise));
System.out.println("Result of bitwise NOT of 'a': " + (~aBitwise));
System.out.println("Result of bitwise NOT of 'b': " + (~bBitwise));
break;

case 4:
System.out.print("Enter boolean value 'bool1' (true or false): ");
boolean bool1 = scanner.nextBoolean();
System.out.print("Enter boolean value 'bool2' (true or false): ");
boolean bool2 = scanner.nextBoolean();
System.out.println("Result of logical AND: " + (bool1 && bool2));
System.out.println("Result of logical OR: " + (bool1 || bool2));
System.out.println("Result of logical NOT of 'bool1': " + (!bool1));
System.out.println("Result of logical NOT of 'bool2': " + (!bool2));
break;

case 5:
System.out.print("Enter value 'a': ");
int aRelational = scanner.nextInt();
System.out.print("Enter value 'b': ");
int bRelational = scanner.nextInt();
System.out.println("Result of greater than (a > b): " + (aRelational > bRelational));
System.out.println("Result of less than (a < b): " + (aRelational < bRelational));
System.out.println("Result of greater than or equal to (a >= b): " + (aRelational >= bRelational));
System.out.println("Result of less than or equal to (a <= b): " + (aRelational <= bRelational));
System.out.println("Result of equality (a == b): " + (aRelational == bRelational));
System.out.println("Result of inequality (a != b): " + (aRelational != bRelational));
break;

case 6:
System.out.println("Exiting the program.");
break;

default:
System.out.println("Invalid choice. Please select a valid option.");
}
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


System.out.println(); // Add a new line for readability
} while (choice != 6);
}
}

Output:

Example 1
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1


Example 2

Example 3
Artificial

Intelligence and Data Science Department

Subject/Odd Sem 2023-23/Experiment 1

Example 4

Conclusion: The Java program for operator demonstration demonstrates various types of
operators interactively, using a switch-case statement to efficiently handle user choices. It
covers Arithmetic, Shift, Bitwise, Logical, and Relational operators, providing prompts and
accurate results. The user-friendly implementation allows exploration and understanding at
their own pace. The program returns accurate results and handles invalid choices with error
messages. Overall, it is suitable for educational purposes and programming practice,
enhancing understanding of Java operators.

You might also like