Ooplab 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Objective:

The objective of this lab is to understand how to use the ternary conditional
operator in Java to find the maximum and minimum values from a set of four
numbers provided by the user.
Requirement:
1. Java Development Kit (JDK) installed.
2. Basic understanding of Java programming language.
3. Knowledge of conditional (ternary) operators.
4. IDE or Text Editor to write and run the Java code (e.g., IntelliJ IDEA, Eclipse, or ).
5. Input four integers from the user and find both the maximum and minimum
values using the ternary conditional operator.
Description:
In Java, the ternary operator provides a shorthand method for decision-making. It
operates as a compact form of an if-else statement. The basic syntax for a ternary
operation is:
condition ? value_if_true : value_if_false
In this experiment, we will:
• Input four numbers from the user.
• Use the ternary operator to determine the maximum and minimum number.
• Print the maximum and minimum values to the console.

Code:
package operator;
import java.util.Scanner;
public class Ternaryop {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a,b,c,d;
int max,min;
System.out.print("enter the value of a: ");
a=input.nextInt();
System.out.print("enter the value of b: ");
b=input.nextInt();
System.out.print("enter the value of c: ");
c=input.nextInt();
System.out.print("enter the value of d: ");
d=input.nextInt();
max=a>b?(a>c?(a>d?a:d):(c>d?c:d)):(b>c?(b>d?b:d):(c>d?c:d));
System.out.println("the maximum number is: "+max);
min=a<b?(a<c?(a<d?a:d):(c<d?c:d)):(b<c?(b<d?b:d):(c<d?c:d));
System.out.println("The minimum number is: "+min);
input.close();
}
}
output

Conclusion:
In this lab, we successfully implemented a Java program that accepts four numbers
as input and determines the maximum and minimum numbers using the ternary
conditional operator. This helped in practicing and enhancing the understanding of
conditional operators, providing an efficient way to handle simple decision-making
tasks in Java.

Objective
To evaluate and understand the result of complex expressions in Java programming
by taking
variables as user input and applying increment, decrement, and conditional
operations.
Requirements
• Knowledge of Java programming, especially:
o Increment and decrement operators (++, --)
o Arithmetic operators (*, /, +, -)
o Bitwise AND operator (&)
o Conditional (ternary) operator (?:)
• Java Development Kit (JDK) installed on the system.
• An Integrated Development Environment (IDE) or a text editor to write and run the
code.
Description
In this lab, we will evaluate the expression:++a * b-- - --c/2 & (++x * y-- + z - --a*b)/2
+ (x>y ? z: a)Operators used:
1. Pre-increment (++a) and Pre-decrement (--a, --c): These increase or decrease the
value of a variable by one before its usage in the expression.
2. Post-increment (b--) and Post-decrement (y--): These increase or decrease the
value after the variable's current value is used in the expression.
3. Arithmetic operations: Multiplication (*), Division (/), Addition (+), Subtraction (-).
4. Bitwise AND (&): This compares binary values of two operands.
5. Ternary operator (?:): This acts like an if-else condition, returning the second
operand if the condition is true and the third operand if it’s false.We will take values
for the variables a, b, c, x, y, and z as input from the user. After applying the
operations step by step, we will observe the result.
Code:
package labreport;
import java.util.Scanner;
public class Lab2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("The value of a,b,c,x,y,z: ");
int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();
int x=input.nextInt();
int y=input.nextInt();
int z=input.nextInt();
int result1=++a*b-- - --c/2;
int result2=(++x*y-- + z - --a*b)/2+(x>y?z:a);
System.out.println("result1 is: "+result1);
System.out.println("result2 is: "+result2);
}
} output

Conclusion
The above code demonstrates the evaluation of a complex mathematical expression
in Java using user-provided input values for variables. The result depends on how
operators like pre/postincrement, pre/post-decrement, arithmetic, bitwise, and
conditional operators are applied.

You might also like