Escolar Documentos
Profissional Documentos
Cultura Documentos
B115017
CSE, 5th sem
Group - A
Set I
1. WAP to swap two numbers with using and without using third variable.
Solution :-
import java.util.Scanner;
class Swap {
public static void main(String args[]) {
Scanner sc = new Scanner(Scannerystem.in);
System.out.print("Enter two integers :- ");
int a, b;
a = sc.nextInt();
b = sc.nextInt();
// swapping using 3rd variable
int temp = a;
a = b;
b = temp;
System.out.println("values after 1st swap :- " + a + " and " + b);
// swapping without using 3rd variable
a = a + b;
b = a - b;
a = a - b;
System.out.println("values after 2nd swap :- " + a + " and " +
b);
}
}
4. WAP to check whether an input character is vowel or not.
Solution :-
import java.util.Scanner;
class Vowel {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the character :- ");
char ch = sc.next().charAt(0);
Solution :-
import java.util.*;
class PascalTriangle {
public static void main(String[] args) {
System.out.println("Enter the number of lines in the pascal
triangle to be printed");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[][] = new int[n+1][n+1];
System.out.println();
}
}
}
32. WAP to print primary and secondry diagonal of a square matrix.
Solution :-
import java.util.Scanner;
class MatrixDiagonals {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of the square matrix: ");
int n = sc.nextInt();
int array[][] = new int[n][n];
System.out.println("Enter matrix elements row wise: ");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
array[i][j] = sc.nextInt();
}
}
// print matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (hadamard[i][j]) System.out.print("* ");
else System.out.print(". ");
}
System.out.println();
}
}
}
36. WAP to find sum of the digits of the largest and smallest number
present in the array of size m x n.
Solution :-
import java.util.Scanner;
class MinMaxArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("enter the size of the row :- ");
int r = sc.nextInt();
System.out.print("enter the size of column :- ");
int c = sc.nextInt();
int numbers[][] = new int[r][c];
class CountPalindromes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string : ");
String str = sc.nextLine();
int count = 0;
String ar[] = str.split(" ");
for(String s: ar) {
if(checkPalindrome(s)) {
count++;
System.out.println(s);
}
}
for(String s: ar) {
if(check(s)) {
count++;
System.out.println(s);
}
}
class CheckDigit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string : ");
String str = sc.next();
boolean status = true;
for(int i=0;i<str.length();i++) {
if(Character.isDigit(str.charAt(i))) {
status = false;
break;
}
}
if(status)
System.out.println("no digits");
else
System.out.println("Contains Digit");
}
}
52. WAP to find the permutation of a string.
Solution :-
import java.util.*;
class Anagrams {
public static void main(String[] args) {
System.out.print("Enter the string whose anagrams are to be
printed :- ");
Scanner in = new Scanner(System.in);
String str = in.nextLine();
printAnagrams(str, 0, str.length() - 1);
System.out.println("");
}
Solution :-
import java.util.*;
class P57 {
static void sort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
static int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low-1);
return i+1;
}
public static void main(String args[]) {
int m;
Scanner scan = new Scanner(System.in);
System.out.println("enter the size of the matrix n>3 and
n<10");
m = scan.nextInt();
int a[][] = new int[m][m];
int e[] = new int[4*m-4];
System.out.println("enter elements row wise");
for(int i = 0 ;i < m;i++)
{
for(int j=0;j < m;j++)
{
a[i][j] = scan.nextInt();
}
}
int k =0;
int s =0;
for(int i=0;i<m;i++)
{
if(i==0 || i==m-1)
{
for(int j=0;j<m;j++)
{
s = s+a[i][j];
e[k] = a[i][j];
k++;
}
}
else
{
s = s + a[i][0] + a[i][m-1];
e[k] = a[i][0];
k++;
e[k] = a[i][m-1];
k++;
}
}
System.out.println("original matrix");
for(int i = 0 ;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
sort(e,0,4*m-5);
k=0;
System.out.println("Sum of boundary elemnts = "+s);
for(int i=0;i<m;i++)
{
a[0][i] = e[k];
k++;
}
for(int i=1;i<m;i++)
{
a[i][m-1] = e[k];
k++;
}
for(int i=m-2;i>=0;i--)
{
a[m-1][i] = e[k];
k++;
}
for(int i=m-2;i>=1;i--)
{
a[i][0] = e[k];
k++;
}
System.out.println("Sorted matrix");
for(int i = 0 ;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
58. Write a program to accept the year, month and the weekday name of
the 1st day of that month and generate its calendar.
Solution :-
import java.util.*;
class CalendarProgram
{
//Function to match the given month and return its maximum days
int findMaxDay(String mname, int y)
{
String months[] = {"","January", "February", "March", "April",
"May", "June",
"July", "August", "September", "October", "November",
"December"};
int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if((y%400==0) || ((y%100!=0)&&(y%4==0)))
{
D[2]=29;
}
int max = 0;
for(int i=1; i<=12; i++)
{
if(mname.equalsIgnoreCase(months[i]))
{
max = D[i]; //Saving maximum day of given month
}
}
return max;
}
//Function to match the given weekday name and return its weekday no.
int findDayNo(String wname)
{
String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday",
"Saturday"};
int f = 0;
for(int i=0; i<7; i++)
{
if(wname.equalsIgnoreCase(days[i]))
{
f = i; //Saving week day no. given day (e.g. '0' for Sunday)
}
}
return f;
}
for(int i=0;i<6;i++)
{
for(int j=f; j<7; j++)
{
if(x<=max)
{
A[i][j] = x;
x++;
}
}
f = 0;
}
for(int j=0; j<z; j++) //Adjustment to bring last (6th) row elements to
first row
{
A[0][j]=A[5][j];
}
System.out.println("\n\t----------------------------------------------------");
}
}
}
System.out.println("DistinctElement");
for(int i = 0;i<res_ind;i++)
{
System.out.println(a[i]);
}
}
}
Set II
7. WAP that illustrates java thread joins.
Solution :-
class ThreadJoin extends Thread{
public void run() {
for (int i = 0; i <= 5; i++) {
try {
Thread.sleep(500);
} catch(Exception ex) {
System.out.println(ex);
}
System.out.println(i + " " +
Thread.currentThread().getName());
}
}
public static void main(String args[]) {
ThreadJoin t1 = new ThreadJoin();
ThreadJoin t2 = new ThreadJoin();
ThreadJoin t3 = new ThreadJoin();
t1.start();
try {
t1.join();
}
catch(Exception e) {
System.out.println(e);
}
t2.start();
t3.start();
}
}
18. How to create Java URL object. Write a Program.
Solution :-
import java.net.URL;
import java.net.MalformedURLException;
class URLObject {
public static void main(String args[]) {
String link = "ftp://www.google.co.in";
try {
URL url = new URL(link);
System.out.println(url);
}
catch(MalformedURLException e) {
System.out.println(e);
}
}
}
20. How to get list of all IPs of a given Host in Java? Write a Program.
Solution :-
// Java program to find IP address of your computer
// java.net.InetAddress class provides method to get
// IP of any host name
import java.net.*;
import java.io.*;
import java.util.*;
import java.net.InetAddress;
class JavaProgram
{
public static void main(String args[]) throws Exception
{
// Returns the instance of InetAddress containing
// local host name and address
InetAddress localhost = InetAddress.getLocalHost();
System.out.println("System IP Address : " +
(localhost.getHostAddress()).trim());
class InsertBlob {
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
con.close();
}
}
}
33. WAP that illustrates the difference between method overloading and
method overriding.
Solution :-
class A
{
void cal(double x)
{
System.out.println("x = " + x);
}
class B extends A
{
void cal(double x)
{
System.out.println("x = " + x * x);
}
}
class Polymorphism {
public static void main(String args[]) {
A a = new A();
B b = new B();
// overloading
a.cal(15);
a.cal(15, 20);
// overriding
a.cal(10);
b.cal(10);
}
}
The End