Java File
Java File
switch (op) {
case '+':
System.out.println("Result : " + (a + b));
break;
case '-':
System.out.println("Result : " + (a - b));
break;
case '*':
System.out.println("Result : " + (a * b));
break;
case '/':
System.out.println("Result : " + (a / b));
break;
default:
System.out.println("Enter correct operator!");
}
s.close();
}
}
Output:
Enter value for A : 15
Enter value for B : 30
Enter Operator (+,-,*,/) : *
Result : 450
Q2 Write a program in java to sort an integer array in ascending
order.
public class IntArraySort
{
public static void main(String[] args) {
int[] arr1 = {15,7,10,5,21,11};
int i, j, temp;
Output:
Original Array -
{ 15 7 10 5 21 11 }
Sorted Array -
{ 5 7 10 11 15 21 }
Q3 Write a program in java to calculate the total total salary of 5
Employees by using array of Objects.
import java.util.Scanner;
int totalSalary = 0;
for (int i = 0; i < 5; i++){
totalSalary += emps[i].salary;
}
System.out.println("Total Salary of Employees : " +
totalSalary);
s.close();
}
}
class Employee{
public int id;
public int salary;
Output:
Enter salary of Employee 1 : 35000
Enter salary of Employee 2 : 12000
Enter salary of Employee 3 : 15000
Enter salary of Employee 4 : 45000
Enter salary of Employee 5 : 20000
Total Salary of Employees : 127000
class Parent {
public String parentName;
}
class parent{
void display(){
System.out.println("Parent display function.");
}
}
Output:
Child display function.
Print Number : 16
Print String : Java!
Output:
Before modification -
s1 and s2 refers to same reference : true
s1 and s3 refers to same reference : true
s3 and s2 refers to same reference : true
After modification -
s1 and s2 refers to same reference : false
s1 and s3 refers to same reference : false
s3 and s2 refers to same reference : true
interface Area{
public static float PI = 3.142857f;
float compute();
}
Output:
Width and length of Rectangle : 12.5, 4.4
Area of Rectangle : 55.0
File – ValidateUserPass.java
import java.util.Scanner;
import validation.Validators;
if(!Validators.validateUsername(username)){
System.out.println("\nUsername incorrect!");
}
else if(!Validators.validatePassword(password)){
System.out.println("\nPassword incorrect!");
}
else {
System.out.println("\nCredentials are correct.");
}
s.close();
}
}
Output:
Enter Username : Kailash sharma
Enter Password : admin@11
Error : Password needs,
* at least one digit, uppercase and lowercase.
* at least one symbol (!, @, #, $, %, ^, &, *).
Password incorrect!
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "system");
Output:
Table created successfully.
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "system");
Output:
7 rows inserted.
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "system");
Output:
2 rows updated.
Q12 Write a program in java to delete all the records whose salary
is in between 5000 and 10000.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "system");
Output:
2 rows deleted.
Q13 Write a program in java to display all the records whose salary
is greater than 50000 and but not in department "CS".
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
System.out.println("EMPID\tEMPNAME \
tEMPDEPARTMENT \tEMPSALARY");
System.out.println("------------------------------------------------
-");
while (resultSet.next()) {
System.out.printf("%1$s\t%2$-15s\t%3$-15s\t
%4$s\n",
resultSet.getString(1),resultSet.getString(2),resultSet.getString(3)
,resultSet.getString(4));
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
EMPID EMPNAME EMPDEPARTMENT EMPSALARY
-------------------------------------------------
7 Vijay Management 55000
package com.registration;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/register")
public class RegistrationForm extends HttpServlet {
res.setContentType("text/html");
out.println("<style>.inputfield{width:300px;
display:flex; justify-content:space-between;}</style>");
out.println("<center><form>");
out.println("<div class='inputfield'>First name <input
type='text' name='fname'></div>");
out.println("<div class='inputfield'>Last name <input
type='text' name='lname'></div>");
out.println("<div class='inputfield'>Email <input
type='email' name='email'></div>");
out.println("<div class='inputfield'>Address <input
type='text' name='address'></div>");
out.println("<div class='inputfield'>Password <input
type='password' name='password'></div>");
out.println("<div class='inputfield'>Confirm Password
<input type='password'></div>");
out.println("<div class='inputfield'><input type='submit'
value='submit'></div>");
out.println("</form></center>");
out.close();
}
}
Output:
Q15. Create a Servlet that validates username and password.
package com.validation;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/validate")
public class UserPassValid extends HttpServlet {
res.setContentType("text/html");
out.close();
}
Output: