JAVA notes
JAVA notes
OUTLINE OPERATORS
1. Arithmetic Operators
* + (Addition): Adds two operands.
* int result = 5 + 3; // result will be 8
* - (Subtraction): Subtracts the second operand from the first.
* int result = 10 - 4; // result will be 6
* * (Multiplication): Multiplies two operands.
* int result = 2 * 6; // result will be 12
* / (Division): Divides the first operand by the second.
* int result = 15 / 3; // result will be 5 (integer division)
* double result = 15.0 / 3; // result will be 5.0 (double division)
* % (Modulus): Returns the remainder after division.
* int result = 17 % 5; // result will be 2
2. Relational Operators
* > (Greater Than): Checks if the left operand is greater than the right.
* boolean result = 10 > 5; // result will be true
* < (Less Than): Checks if the left operand is less than the right.
* boolean result = 3 < 8; // result will be true
* >= (Greater Than or Equal To): Checks if the left operand is greater
than or equal to the right.
* boolean result = 7 >= 7; // result will be true
* <= (Less Than or Equal To): Checks if the left operand is less than or equal
to the right.
* boolean result = 2 <= 4; // result will be true
* == (Equal To): Checks if both operands are equal.
* boolean result = 5 == 5; // result will be true
* != (Not Equal To): Checks if both operands are not equal.
* boolean result = 10 != 7; // result will be true
3. Logical Operators
* && (Logical AND): Returns true if both operands are true.
* boolean result = (5 > 3) && (2 < 8); // result will be true
* || (Logical OR): Returns true if at least one operand is true.
* boolean result = (10 < 5) || (2 > 1); // result will be true
* ! (Logical NOT): Reverses the logical state of its operand.
* boolean result = !(5 > 3); // result will be false
4. Bitwise Operators (operate on bits of integers)
* & (Bitwise AND)
* | (Bitwise OR)
*^ (Bitwise XOR)
* ~ (Bitwise NOT)
* << (Left Shift)
* >> (Right Shift)
* >>> (Unsigned Right Shift)
5. Assignment Operators
* = (Simple Assignment): Assigns the value of the right operand to the left
operand.
* int x = 10;
* += (Addition Assignment): Adds the right operand to the left
operand and assigns the result to the left operand.
* x += 5; // equivalent to x = x + 5;
System.out.println("a + b = " +
(a + b)); System.out.println("a
- b = " + (a - b));
System.out.println("a * b = " +
(a * b)); System.out.println("a
/ b = " + (a / b));
System.out.println("a % b = "
+ (a % b));
a += 3;
System.out.println("a += 3: " + a);
int x =
5; int y
= ++x;
System.out.println("++x: " + x + ", y: " + y);
}
}
2.SELECTION AND ITERATION CONTROL STRUCTURE
1. Selection Control Structures
* if Statement: This structure executes a block of code only if a
specific condition is true. int age = 25;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
Key Concepts
* Control Flow: The order in which statements are executed in a program.
* Condition: An expression that evaluates to true or false.
* Iteration: Repeated execution of a
block of code. Explanation
* Selection: Allows you to choose which block of code to execute based on a
condition.
* Iteration: Enables you to repeat a set of instructions multiple times.
These control structures are fundamental to programming in Java and
many other languages.
3. RABBIT AND CHICKEN
PROGRAM
public class
RabbitChickenProblem {
public static void
main(String[] args) {
int totalHeads = 35; // Replace with the actual number of heads
int totalLegs = 94; // Replace with the actual
findRabbitCount(totalHeads, totalLegs);
if (rabbitCount != -1) {
if (totalLegsCalculated ==
totalLegs) { return rabbits;
}
}
return -1; // No valid solution found
} }
4.(i) java program gift vocher and 150
Import
java.util.Scanner;
public class
FoodOrder {
if
(customerType.equalsIgnoreCase("Re
gular")) { totalCost *= 0.95; // Apply
5% discount
if (totalCost > 300) {
System.out.println("Congratulations! You have received a special gift
voucher.");
}
} else if
(customerType.equalsIgnoreCase("Guest"))
{ totalCost += 5.0; // Add delivery charge
} else {
System.out.println("Invalid customer type. Please enter 'Regular'
or 'Guest'."); return; // Exit the program
}
System.out.println("Total cost: Rs." + totalCost);
}
}
SwiftFood; public
class Restaurant {
// Instance variables
private String
restaurantName; private
long restaurantContact;
private String
restaurantAddress; private
float rating;
public int
getStock() {
return stock;
}
public boolean isAvailable(String title, String author) {
return this.title.equalsIgnoreCase(title) &&
this.author.equalsIgnoreCase(author);
}
public void displayDetails() {
System.out.println("Book
Details:");
System.out.println("Author: " +
author); System.out.println("Title:
" + title);
System.out.println("Price: " +
price);
System.out.println("Publisher: " +
publisher);
System.out.println("Stock: " +
stock);
}
7(ii)calculator
public class Calculator {
EmployeeRecord {
public EmployeeRecord(double[]
salaries) { this.salaries = salaries;
}
public double
calculateAverageSalary() {
double sum = 0;
for (double salary :
salaries) { sum +=
salary;
}
return sum / salaries.length;
}
public int countEmployeesAboveAverage(double
averageSalary) { int count = 0;
for (double salary :
salaries) { if (salary >
averageSalary) {
count++;
}
}
return count;
}
public int countEmployeesBelowAverage(double
averageSalary) { int count = 0;
for (double salary :
salaries) { if (salary <
averageSalary) {
count++;
}
}
return count;
}
public static void main(String[] args) {
double[] salaries = {23500.0, 25080.0, 28760.0, 22340.0, 19890.0};
EmployeeRecord employeeRecord = new EmployeeRecord(salaries);
double averageSalary =
employeeRecord.calculateAverageSalary();
System.out.printf("The average salary of the employee is: %.2f\n",
averageSalary);
int aboveAverage =
employeeRecord.countEmployeesAboveAverage(averageSalary);
System.out.printf("The number of employees having salary greater than
the average is:
%d\n", aboveAverage);
int belowAverage =
employeeRecord.countEmployeesBelowAverage(averageSalary);
System.out.printf("The number of employees having salary lesser than the
average is:
%d\n", belowAverage);
}
}
9.STUDENTS DATA DISPLAY PROGRAM
class Student {
private int
rollno;
public int
getRollno() {
return rollno;
}
public void setRollno(int
rollno) { this.rollno =
rollno;
}
}
class Test extends
Student { private int
marks1; private int
marks2;
public int
getMarks1() {
return marks1;
}
public void setMarks1(int
marks1) { this.marks1 =
marks1;
}
public int
getMarks2() {
return marks2;
}
public void setMarks2(int
marks2) { this.marks2 =
marks2;
}
}
public void
display() {
super.display(
);
System.out.println("Pages: " + pages);
}
}
public void
display() {
super.display(
);
System.out.println("Time: " + time);
}
System.out.println("Book
Details:"); book.display();
System.out.println("\nTape
Details:"); tape.display();
}
}
13.a.geometrical sequence
public class GeometricSequence {
System.out.print("Geometric
Sequence: "); for (int i = 0; i < n;
i++) {
System.out.print(currentTerm + " ");
currentTerm *= 2; // Double the current term for each iteration
}
}
}
class Tester {
public static void main(String[] args) {
// Create matrices
int[][] matrix1 = new
int[rows][cols]; int[][]
matrix2 = new
int[rows][cols];
// Display results
System.out.println("\nMatrix 1:");
printMatrix(matrix1);
System.out.println("\nMatrix 2:");
printMatrix(matrix2);
System.out.println("\nSum of
Matrices:");
printMatrix(sumMatrix);
System.out.println("\nDifference of
Matrices:"); printMatrix(differenceMatrix);
scanner.close();
return sum;
}
return difference;
}
if (reservation.bookTicket(1, 20)) {
System.out.println("First Tier tickets booked successfully.");
} else {
System.out.println("First Tier ticket booking failed.");
}
if (reservation.bookTicket(2, 80)) {
System.out.println("Second Tier tickets booked successfully.");
} else {
System.out.println("Second Tier ticket booking failed.");
}
reservation.displayStatus();
if (reservation.cancelTicket(1, 10)) {
System.out.println("First Tier tickets canceled successfully.");
} else {
System.out.println("First Tier ticket cancellation failed.");
}
reservation.displayStatus();
}
}
17. CHOCOLATE COMPANY PROGRAM
public class
Chocolate {
private int
barCode; private
String name;
private double
weight; private
double cost;
// Default
constructor
public
Chocolate() {
this.barCode = 0;
this.name = "Default
Chocolate"; this.weight =
0.0;
this.cost = 0.0;
}
// Parameterized constructor
public Chocolate(int barCode, String name, double weight,
double cost) { this.barCode = barCode;
this.name = name;
this.weight =
weight; this.cost =
cost;
}
// Getters and Setters (if
needed) public int
getBarCode() {
return barCode;
}
public void setBarCode(int
barCode) { this.barCode =
barCode;
}
// ... other getters and setters for name, weight,
and cost public static void main(String[] args)
{
// Creating a Chocolate object using the
parameterized constructor Chocolate chocolate1 =
new Chocolate(101, "Cadbury", 12.0, 10.0);