Practical Java
Practical Java
Program:
package college;
import java.util.Scanner;
Output:
Enter the n value: 135
Prime numbers between 1 to N are :2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
61 67 71 73 79 83 89 97 101 103 107 109 113 127 131
//
//
import java.util.Scanner;
int year;
System.out.println("Enter an year: ");
Scanner scanner = new Scanner(System.in);
year = scanner.nextInt();
Output:
Enter an year:
2035
Specified year is a not leap year
//
//
Q3: Write a program to create simple interest and input by the user.
Program:
package college;
import java.util.Scanner;
Output:
Enter the principal: 2500
Enter the Rate of Interest: 4
Enter the Time Period: 2
Simple Interest is = 200.0
Q4: Write a program to create simple class to find out the area and
perimeter of rectangle and box using this and super keyword.
Program:
package college;
import java.util.Scanner;
a =this.l * this.b;
System.out.println("Area of rectangle :" +a);
}
public static void main(String[] args)
{
area_rect a = new area_rect();
a.add();
}
Output:
Perimeter of rectangle :120
Area of rectangle :3000
i = 1;
while (decimalNumber != 0) {
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
Output:
110001 in binary = 61 in octal
while (decimal != 0)
{
octalNumber += (decimal % 8) * i;
decimal /= 8;
i *= 10;
}
return octalNumber;
}
}
Output:
98 in decimal = 142 in octal
Q7: Write a program to print pyramid star pattern.
Program:
package college;
}
}
Output:
*
* *
* * *
* * * *
* * * * *
}
}
Output:
* * * * *
* * * *
* * *
* *
*
String name;
float bal;
String account_type;
bank( String name,float bal,String account_type){
this.name=name;
this.bal=bal;
this.account_type=account_type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getBal() {
return bal;
}
public void setBal(float bal) {
this.bal = bal;
}
public String getAccount_type() {
return account_type;
}
public void setAccount_type(String account_type) {
this.account_type = account_type;
}
}
class Account extends bank{
Account(String name, float bal, String account_type) {
super(account_type, bal, account_type);
}
public void witdraw(int amt) {
if(super.bal>=amt) {
float withdraw=super.bal-amt;
super.setBal(withdraw);
System.out.println("amout reaming after withdraw " +getBal());
}else {
System.out.println("low balance");
}
}
public void deposite(int amt) {
float deposite= super.bal+amt;
super.setBal(deposite);
System.out.println(getBal());
}
public static void main(String[] args) {
Account obj= new Account("RAM", 5000, "saving");
obj.witdraw(500);
obj.deposite(5000);
}
Output:
amout reaming after withdraw 4500.0
9500.0
Program:
package college;
import java.util.Scanner;
}
}
Output:
Enter the number: 8
The factorial is: 40320
//
Q11: Write a program to design a class using abstract methods and
classes.
Program:
package college;
Output:
WELCOME World 0
Output:
Equal
olleH
HELLO
WORLD
hello
world
import java.util.Arrays;
Output:
Arrays are not equal.
Output:
0 15 25 35 45 55 65 75 85
Output:
10 1 9
Q16: Write a program to copy all the elements of one array to another
array.
Program:
package college;
public class copy_array {
for(int j=0;j<b.length;j++){
System.out.print(b[j]+" ");
}
}
}
Output:
10 20 30 40 50 60 70
Q17: Write a program to handle the exception using try and multiple
catch block.
Program:
package college;
}
Output:
ArrayIndexOutOfBounds Exception occurs
Element at such index does not exists
e.printStackTrace();
System.out.println("Divide by zero");
}
}
}
Output:
Divide by zero
Output:
0: New World
1: New World
2: New World
3: New World
4: New World
5: New World
0: hello World
1: hello World
2: hello World
3: hello World
4: hello World
5: hello World
Q22: Write a program that import the user define Package and access
the member variable of classes that combined by Package.
Program:
Combine1.java
package college;
import java.util.Scanner;
}
Output:
welcome to vote
Q24: Write a program to create a thread that implement the runnable
interface.
package college;
Output:
Implements Runnable
this.amount -= amount;
System.out.println("Withdraw Completed");
}
synchronized void deposit(int amount) {
System.out.println("Going to Deposit");
this.amount += amount;
System.out.println("Deposit Completed");
notify();
}
}
class interthread1
{
public static void main(String[] args) {
final interthread c = new interthread();
new Thread()
{
- public void run() {
c.withdraw(15000);
}
}.start();
new Thread() {
public void run() {
c.deposit(15000);
}
}.start();
}
}
Output:
Going to withdraw
Less balance; Waiting for Deposit
Going to Deposit
Deposit Completed
Withdraw Completed
import java.io.*;
// thread t1 starts
t1.start();
catch(Exception ex)
{
System.out.println("Exception has " +
"been caught" + ex);
}
// t2 starts
t2.start();
catch(Exception ex)
{
System.out.println("Exception has been" +
" caught" + ex);
}
t3.start();
}
}
Output:
Current Thread: main
Current Thread: Thread-0
0
Current Thread: Thread-0
1
Current Thread: main
Current Thread: Thread-1
0
Current Thread: Thread-1
1
Current Thread: Thread-2
0
Current Thread: Thread-2
1
Q: Write a program of database connectivity using JDBC & ODBC
Drivers.
Program:
package college;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//
/**** Information.java ****/
import java.awt.*;
import java.awt.event.*;
class Information extends Frame {
Button btnClose;
Panel panelForm;
Label labelName, labelCourse, labelRollNo, labelCollege;
TextField fieldName, fieldCourse, fieldRollNo, fieldCollege;
Information(String name, String course, String rollNo, String college) {
super("Personal Information");
labelName = new Label("Name:");
labelName.setBounds(20, 20, 80, 30);
labelCourse = new Label("Course:");
labelCourse.setBounds(20, 50, 80, 30);
labelRollNo = new Label("Roll No.:");
labelRollNo.setBounds(20, 80, 80, 30);
labelCollege = new Label("College:");
labelCollege.setBounds(20, 110, 80, 30);
fieldName = new TextField(name);
fieldName.setBounds(100, 22, 200, 24);
fieldName.setEditable(false);
fieldCourse = new TextField(course);
fieldCourse.setBounds(100, 52, 200, 24);
fieldCourse.setEditable(false);
fieldRollNo = new TextField(rollNo);
fieldRollNo.setBounds(100, 82, 200, 24);
fieldRollNo.setEditable(false);
fieldCollege = new TextField(college);
fieldCollege.setBounds(100, 112, 200, 24);
fieldCollege.setEditable(false);
btnClose = new Button("Close");
btnClose.setBounds(100, 150, 125, 30);
btnClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
panelForm = new Panel();
panelForm.setLayout(null);
panelForm.add(labelName);
panelForm.add(fieldName);
panelForm.add(labelCourse);
panelForm.add(fieldCourse);
panelForm.add(labelRollNo);
panelForm.add(fieldRollNo);
panelForm.add(labelCollege);
panelForm.add(fieldCollege);
panelForm.add(btnClose);
this.add(panelForm);
this.setSize(350, 250);
this.setVisible(true);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
}
//
//
acceptuser.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Verifying Details</h1>
--%>
--%>
<%-- Set the value of the created bean using form data --%>
ValidateUser.java class
--%>
<%if(snr.validate("GeeksforGeeks", "GfG")){%>
<%}else{%>
<%}%>
</body>
</html>
import java.io.Serializable;
// by setter methods
return true;
else
return false;