Java Assigment New
Java Assigment New
9 User-defined exception
package assignment1;
public class sorting {
for(i=0;i<a.length;i++) {
for(j=i+1;j<a.length;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}System.out.println(a[i]);
}
}
}
Output:
Output:
First Matrix....
2 3 4
6 7 4
7 9 3
Transpose Matrix....
2 6 7
3 7 9
4 4 3
3
package assignment1;
import java.util.Scanner;
Output:
import java.util.Scanner;
class Palindrom_string {
public static boolean isPalindrome(String str)
{
String rev = "";
boolean ans = false;
String str;
System.out.print("Enter string:");
str=sc.next();
str = str.toLowerCase();
boolean A = isPalindrome(str);
if (A==true)
{
System.out.println(str+" is palindrom");
}
else
{
System.out.println(str+" is not palindrom");
}
}
}
Output:
Enter string:Nitin
Nitin is palindrom
5
5.Create a package mca1 which will have 2 classes as class Mathematics with a methods to
add two numbers, add three float numbers and class Maximum with a method to find
maximum of three numbers.
package MCA1;
import java.util.*;
}
6
class Maximum
{
Scanner s=new Scanner(System.in);
public void max()
{
int i,max=0;
int a[]= {2,5,3};
for(i=0;i<a.length;i++)
{
System.out.println("Array element are:"+a[i]);
}
max=a[0];
int sc2=0;
for(i=0;i<a.length;i++)
{
if(max<a[i])
{
max=a[i];
}
}
System.out.println(" maximum element in the array: "+max);
}
}
Output:
6. Write a Java program to create Animal interface that contains run() and eat() method.
And implement methods in Dog and Cat class.
package assignment2;
public class interface_demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
dogg d=new dogg();
catt c=new catt();
System.out.println("Class Dog...");
d.run();
d.eat();
System.out.println("Class Cat...");
c.run();
c.eat();
}
}
interface AAnimall
{
public void run();
public void eat();
}
class dogg implements AAnimall
{
public void run()
{
System.out.println("Dog is running...");
}
public void eat()
{
System.out.println("Dog is eating....");
}
}
class catt implements AAnimall
{
public void run()
{
System.out.println("Cat is running...");
}
public void eat()
{
System.out.println("Cat is running...");
}
}
Output:
Class Dog...
Dog is running...
Dog is eating....
Class Cat...
Cat is running...
Cat is running...
8
7. Write a Java program to create an abstract class Animal that contains non abstract run()
and abstract eat() method…Derive two classes Dog and Cat from it.
package assignment2;
public class abstract_class {
Output:
package assignment2;
import java.lang.*;
public class exception {
void arrayindex()
{
int[] num= {2,4,5,2,5};
try {
System.out.println("arrayvalue:"+num[6]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(" ArrayIndexOutOfBoundsException exception is created");
}
}
void numberformat()
{
10
try {
int num1 = Integer.parseInt ("akki") ;
}
catch(NumberFormatException e) {
System.out.println("Number format exception");
}
}
void arithmatic()
{
try {
String a = "hey charlie "; // length is 22
char c = a.charAt(24); // accessing 25th element
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBoundsException");
}
}
}
Output:
9. User-defined exception
Output:
Caught
Problem arrived... please solve
12
10. Write a Java program to create a Thread by extending the Thread class. And print the
name of currently executing thread.
package assignment2;
import java.lang.Thread;
public class thread1_demo {
}
class thread11 extends Thread
{
public void run()
{
try {
System.out.println( "The name of Thread is:"+Thread.currentThread().getName());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
11. Write a Java program to create a Thread by Implementing the Runnable Interface. And
print the name of currently executing thread.
package assignment2;
public class thread_demo2 {
}
class thread_run implements Runnable
{
public void run()
{
try {
System.out.println( "The name of Thread is:"+Thread.currentThread().getName());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
12. Write a multithreaded program to print even and odd numbers. Create two threads,
one thread prints even number and second thread prints odd number
package assignment2;
public class Multi {
public static void main(String[]args)
{
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
}}
class Thread1 extends Thread
{
public void run()
{
for(int i=0;i<=20;i++)
{
if (i % 2 == 0)
System.out.println(i);
}
}
}
class Thread2 extends Thread
{
public void run()
{
for(int i=0;i<=20;i++){
if (i % 2 != 0)
System.out.println(i);
}
}
}
Output:
0 6
2 8
1 10
3 12
5 14
7 16
9 18
1 20
13
15
17
19
4
15
package assignment2;
import java.util.ArrayList;
class array_list{
public static void main(String[] args)
{
ArrayList<Integer> s=new ArrayList<Integer>();
//arr.add(value)-ToaddvalueintotheArrayList arr.add(5);
s.add(35);
s.add(-5);
s.add(14);
s.add(3);
s.add(12);
s.add(-5);
System.out.println("BeforeRemovingduplicate elements:"+s);
Output:
14. Write a code to sort a linked list and Reverse a linked list in java.
package assignment2;
import java.util.LinkedList;
}
}
Output:
15. Write a Java program to copy the contents of a file to another file.
package assignment2;
import java.io.*;
import java.util.*;
try {
int i;
outputStream.write(i);
catch(Exception e) {
finally {
if (inputStream != null) {
inputStream.close();
if (outputStream != null) {
outputStream.close();
18
System.out.println("File Copied");
sc.close();
copyData(a, b);
Output:
Enter the name of the file from where the data would be copied :
picture
Enter the name of the file from where the data would be written :
downloads
19
16. Write Java AWT code to accept Student information and display Student details on the Screen.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
StudentInformation() {
setLocation(550, 100); // Set Loaction of the
Frame setSize(450, 580); // Set Size of the
Frame
ul = new JLabel("
")
; ul.setBounds(130, 18, 300, 20);
ul.setFont(new Font("TimesRoman", Font.BOLD,
15)); p.add(ul);
p.add(l3);
t3 = new JTextField();
t3.setBounds(150, 140, 200,
20); p.add(t3);
l4 = new JLabel("Phone
Number :"); l4.setBounds(20, 180,
150, 20);
l4.setFont(new Font("TimesRoman", Font.BOLD,
20)); p.add(l4);
t4 = new JTextField();
t4.setBounds(150, 180, 200,
20); p.add(t4);
t5 = new JTextField();
t5.setBounds(150, 220, 200,
20); p.add(t5);
if(ch1.isSelected()
){ flag++;
hobbies += "\n\t"+flag+"."+ch1.getText();
}
if(ch2.isSelected()
){ flag++;
hobbies += "\n\t"+flag+"."+ch2.getText();
}
22
if(ch3.isSelected()
){ flag++;
hobbies += "\n\t"+flag+"."+ch3.getText();
}
if(ch4.isSelected()
){ flag++;
hobbies += "\n\t"+flag+"."+ch4.getText();
}
if(flag == 0)
hobbies = "Not Selected";
String details = "------- Student Details "+"\n"
+"RollNo : "+t1.getText()+"\n"
+"Name : "+t2.getText()+"\n"
+"Address : "+t3.getText()+"\n"
+"Phone Number : "+t4.getText()+"\n"
+"Email : "+t5.getText()+"\n"
+"Gender : "+gender+"\n"
+"Hobbies : "+hobbies+"\n";
17.Write a JAVA program to design a screen using Swing to perform String operations.
StringOperations() {
setLocation(250, 100); // Set Loaction of the Frame
setSize(430, 330); // Set Size of the Frame
t2 = new JTextField();
t2.setBounds(180, 110, 180, 25);
p.add(t2)
25
1b2 = new
JButton("UPPER");
b2.setBounds(20, 150, 100,
25); p.add(b2);
t3 = new JTextField();
t3.setBounds(180, 150, 180,
25); p.add(t3);
b3 = new JButton("Italic");
b3.setBounds(20, 190, 100, 25);
t5.setFont(new Font("TimesRoman", Font.BOLD, 15)); //BOLD
}
}
18.Write a Java code to Read, Insert, Update and Delete any record from the database.(Employee table).
package mca1;
import java.sql.*;
import java.util.*;
class Employee{
Connection c;
Statement s;
Scanner sc = new Scanner (System.in);
public Employee(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
c
=DriverManager.getConnection("jdbc:mysql:///mca1","root","");
s =c.createStatement();
}catch(Exception e){
System.out.println(e);
}
}
ps.executeUpdate();
System.out.println("***Employee record inserted Successfully !!***");
}catch(Exception e){
System.out.println(e
);
}
}
); break;
case 3:
e.update(
); break;
case 4:
e.delete(
); break;
case 0:
System.exit(0);
}
}
}
29
Output:
30
19: Write a Java Servlet Application for login page with proper validations.
Login1>login.html
<html>
<body>
<form
action=http://localhost:8080/Login1/Logi
n> Email: <input type=email name=email
required>
<br>
Password: <input type=password name=pass required><br>
<input type=submit value=LOGIN>
</form>
<p>correct Email: abcd@gmail.com<br>password:1234
</body
</html>
Login1>WEB-INF>web.xml
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
</web-app>
Login1>WEB-INF>classes>Login.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
31
String email =
request.getParameter("e
mail"); String pass =
request.getParameter("p
ass");
out.println("<p>You entered:<br>Email: "+email+"<br>Password:
"+pass+"</p>");
if(email.equals("abcd@gmail.com") &&
pass.equals("1234")){
out.println("<p>Login Successfull!!
</p>");
}
else{
out.println("<p>Incorrect LoginId or Password!!</p>");
}
out.println("</html></body>");
}
catch(Exception e){
System.out.printl
n("Exception :
"+e);
}
}
}
Ouput:
1.Login Form
32
3.Validation 4.Validation
5.Incorrect ID/Password
33
Register>register.html
<html>
<body>
<form action=http://localhost:8080/Register/register.jsp>
<h2>REGISTERATION PAGE:</h2><br>
Email: <input type=email name=email required>
<br>
Password: <input type=password name=pass required><br>
<input type=submit value=LOGIN>
</form>
</body
</html>
Register>register.jsp
try{
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql:te512","postgres","");
st=con.createStatement();
out.println("<br><br>Email:"+email+"<br>Password:"+pass);
}catch(Exception e){
}
34
Output: