OOPS Manual Updated-2
OOPS Manual Updated-2
:1 Sorting algorithms
Date:
Aim:
Procedure :
We can create a java program to sort array elements using selection sort.
element in the temp and the second element in the first, and then temp in the second
number and continue for the next match to sort the whole array in ascending order.
import java.util.Scanner;
int n = array.length;
int i = j-1;
i--;
array[i+1] = key;
for(int i:arr1){
System.out.print(i+" ");
System.out.println();
insertionSort(arr1);//sorting array using insertion sort
for(int i:arr1){
System.out.print(i+" ");
Output:
14
43
11
58
22
11
14
22
43
58
Algorithm
2{
6}
int t = a[i];
a[i] = a[j];
a[j] = t;
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
quick(a, p + 1, end);
int i;
int n = a.length;
q1.printArr(a, n);
q1.quick(a, 0, n - 1);
System.out.println("\nAfter sorting array elements are - ");
q1.printArr(a, n);
System.out.println();
Output:
Result :
Date:
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
There are many ways to write the factorial program in java language. Let's see the 2
ways to write the factorial program in java.
class FactorialExample{
int i,fact=1;
for(i=1;i<=number;i++){
fact=fact*i;
Output:
class FactorialExample2{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
int i,fact=1;
int number=4;//It is the number to calculate factorial
fact = factorial(number);
Output:
Factorial of 4 is: 24
Date:
Aim:
Program:
import java.util.Scanner;
class MpgCal
double mpg;
MpgCal(double k,double g)
double m=k/1.609344;
mpg= m/g;
}
}
class Mpg
double km = scan.nextDouble();
Output:
20
Enter gallons :
Another program
import java.util.Scanner;
float AvgMPG = 0;
int MilesDriven = 0;
int totalTrips = 0;
int GallonsUsed = 0;
int totalMilesPerGallon = 0;
int MilesPerGallon = 0;
MilesDriven = input.nextInt();
GallonsUsed = input.nextInt();
MilesDriven = input.nextInt();
GallonsUsed = input.nextInt();
if (totalTrips != 0)
else
}
}
Output:-
Result:-
Ex.no:4
Date:
String stringDate="2019-07-15";
cal.setTime(date1);
// manipulate date
cal.add(Calendar.DATE, 5);
Ex.no:5 Package
Date:
//save as Simple.java
package mypack;
System.out.println("Welcome to package");
Output:
Welcome to package
Ex.no:6 Interface
Date:
Program:
interface Printable
void print();
void show();
System.out.println("Hello");
}
public void show()
System.out.println("Welcome");
obj.print();
obj.show();
Output:
Hello
Welcome
Ex.no:7 Implements a Stack ADT that Converts the infix expression into postfix
expression
Date:
Aim:-
Algorithm:-
1. Scan the infix notation from left to right one character at a time.
2. If the next symbol scanned as an operand, append it to the postfix string.
3. If the next symbol scanned as an operator, the:
4. Pop and append to the postfix string every operator on the stack that:
5. Is above the most recently scanned left parenthesis, and
6. Has precedence higher than or is a right-associative operator of equal
precedence to that of the new operator symbol.
7. Push the new operator onto the stack
8. If a left parenthesis is scanned, push it into the stack.
9. If a right parenthesis is scanned, all operators down to the most recently
scanned left parenthesis must be popped and appended to the postfix string.
Furthermore, the pair of parentheses must be discarded.
10. When the infix string is fully scanned, the stack may still contain some
operators. All the remaining operators should be popped and appended to the
postfix string.
Program:-
import java.io.*;
class Stack
int top=-1;
void push(char c)
try
a[++top]= c;
catch(StringIndexOutOfBoundsException e)
{
System.exit(0);
char pop()
return a[top--];
boolean isEmpty()
return (top==-1)?true:false;
char peek()
return a[top];
String infix;
infix = keyboard.readLine();
//output as postfix
char symbol;
for(int i=0;i<infix.length();++i)
symbol = infix.charAt(i);
if (Character.isLetter(symbol))
else if (symbol=='(')
//push (
operators.push(symbol);
}
else if (symbol==')')
else
operators.push(symbol);
while (!operators.isEmpty())
return postfix;
if (x == '+' || x == '-')
return 1;
return 2;
return 0;
Output:
Result:-
Date:
BufferedReader Class
Scanner class
Using the Java BufferedRedaer class is the most common and simple way to read a
file line by line in Java. It belongs to java.io package. Java BufferedReader class
provides readLine() method to read a file line by line. The signature of the method
is:
Program:-
import java.io.*;
try
String line;
while((line=br.readLine())!=null)
{
catch(IOException e)
e.printStackTrace();
Output:
import java.io.*;
import java.util.Scanner;
try
while(sc.hasNextLine())
catch(IOException e)
e.printStackTrace();
Output:
Result:-
Ex.no:9 Copy the content of the one file to another file using fileinput streams
Date:
This creates a FileInputStream object fin given a filename to it from where it will
copy the content or do read operation.
Methods used:
1. read(): This method is used to read a byte of data. It returns that byte as an
integer value or return -1 if the end of file is reached.
FileOutputStream Class
It is a byte output stream class which helps in writing the bytes to a file. It provides
different functions to write the data to a file.
Methods used:
File Class
Program:-
import java.io.*;
import java.util.*;
try {
int n;
out.write(n);
finally {
if (in != null) {
// stream
in.close();
// the stream
if (out != null) {
out.close();
System.out.println("File Copied");
"Enter the source filename from where you have to read/copy :");
String a = sc.nextLine();
// source file
System.out.println(
String b = sc.nextLine();
// destination file
// contents from x to y
copyContent(x, y);
Output:-
sourcefile.txt
destinationfile.txt
File Copied
Another program:-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
try {
int length;
outs.write(buffer, 0, length);
ins.close();
outs.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
Output:-
Result:-
Date:
Aim:-
Procedure :-
Program:-
for(int i=0;i<=arr.length;i++) {
Output:
Result:-
Date:
super(str);
}
// class that uses custom exception InvalidAgeException
else {
System.out.println("welcome to vote");
// main method
try
validate(13);
Output:
Anorher program :-
try{
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
}
Test it Now
Output:
Result :-
Ex.no:12 Multithreading
Date :
Program:-
this.message = message;
while(true) {
System.out.println(message);
}
}
}
Following is another class which extends the Thread class −
this.number = number;
int counter = 0;
int guess = 0;
do {
counter++;
} while(guess != number);
}
Following is the main program, which makes use of the
above-defined classes −
thread1.setDaemon(true);
thread1.setName("hello");
thread1.start();
thread2.setPriority(Thread.MIN_PRIORITY);
thread2.setDaemon(true);
thread2.start();
System.out.println("Starting thread3...");
thread3.start();
try {
thread3.join();
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
System.out.println("Starting thread4...");
thread4.start();
System.out.println("main() is ending...");
This will produce the following result. You can try this example again and again
and you will get a different result every time.
Output:-
Hello
Hello
Hello
Hello
Hello
Hello
Goodbye
Goodbye
Goodbye
Goodbye
Goodbye
.......
Result:-
Ex.no:13 Applet
Date:
Program:
//First.java
import java.applet.Applet;
import java.awt.Graphics;
g.drawString("welcome",150,150);
Note: class must be public because its object is created by Java Plugin software that
resides on the browser.
myapplet.html
<html>
<body>
</applet>
</body>
</html>
To execute the applet by appletviewer tool, create an applet that contains applet tag
in comment and compile it. After that run it by: appletviewer First.java. Now Html
file is not required but it is for testing purpose only.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
g.drawString("welcome to applet",150,150);
/*
</applet>
*/
c:\>javac First.java
c:\>appletviewer First.java
Result: