Streamlining Python Development: A Guide to a Modern Project Setup
Java practical
1. BY O.W.O
1. WAP to find the average and sum of N numbers using command line argument.
ALGORITHMS
Step 1: Start and Pass the values through command line
Step 2: Convert the values to integer
Step 3: Find the sum and average
Step 4: Display the results.
Step 5: End the program.
Program Code
class CommandArg {
public static void main(String arg[]) {
int i,n;
double sum=0,Avg;
n=Integer.parseInt(arg[0]);
for(i=1;i<=n;i++) {
sum=sum+Double.parseDouble(arg[i]);
}
Avg=sum/n;
System.out.println ("The Sum is"+ sum);
System.out.println ("The Average is"+ Avg);
}
}
Output:
Java CommandArg 2 6 4
The Sum is 10
The Average is 5
1
2. BY O.W.O
2. Create a class student and read and display the details using member function
ALGORITHMS
Step 1: Create a class student with read and display the details using member function.
Step 2: Create the class student with data members (name, rollno, batch) and member
functions (read(), display() ).
Step 3: Create an object using class student.
Step 4: Using object call the functions read () and display ().
code
class Student
{
String stName;
int stAge;
void initialize()
{
stName="loguk pa gwanyank";
stAge=23;
}
void display()
{
System.out.println("Student name:" +stName);
System.out.println("Student Age:" +stAge);
}
public static void main(String []args){
objStudent = new Student();
objStudent.initialize();
objStudent.display();
}
} OUTPUT
2
3. BY O.W.O
3. Create class square with data members( length, area and perimeter) and member
functions
ALGORITHMS
Step 1: Create class square with data members( length, area and perimeter) and member
functions ( read() , compute() and display())
Step 2: Create an object using class square
Step 3: Using object call the member functions read(), compute() and display()
import java.util.Scanner;
class SquareAreaDemo {
public static void main (String[] args)
{
System.out.println("Enter Side of Square:");
//Capture the user's input
Scanner scanner = new Scanner(System.in);
//Storing the captured value in a variable
double side = scanner.nextDouble();
//Area of Square = side*side
double area = side*side;
double peri=side*4;
double length=side+2;
System.out.println("Area of Square is: "+area);
System.out.println("Perimeter of Square is: "+peri);
System.out.println("lenght of Square is: "+length);
}
}
output
3
4. BY O.W.O
4. WAP to create a class with the parameterized constructor?
ALGORITHMS
Step 1: Create a class with necessary data members and member functions.
Step 2: Create a constructor with arguments.
Step 3: Within the main function create the objects and pass the values to the constructor.
code
class Example{
//Default constructor
Example(){
System.out.println("Default constructor");
}
Example (int i, int j){
System.out.print("parameterized constructor");
System.out.println(" with Two parameters");
}
Example(int i, int j, int k){
System.out.print("parameterized constructor");
System.out.println(" with Three parameters");
}
public static void main(String args[]){
//This will invoke default constructor
Example obj = new Example();
Example obj2 = new Example(12, 12);
Example obj3 = new Example(1, 2, 13);
}
}
Output
4
5. BY O.W.O
5. WAP to create and save a new text file .
ALGORITHMS
Step 1: Start the program.
Step 2: create class FileOutput.
Step 3: Read the number of bytes to be stored
Step 4: Use FileOutputStream to create a new file.
Step 5: Use the write function to write the contents to the file.
import java.io.*;
public class Trying
public static void main(String[] args) throws IOException {
String w = "Hello worldnhownarenyou";
FileWriter fw = new FileWriter("example.txt");
System.out.println("write to the file example.text-----");
fw.write(w);
System.out.println("writing complete");
fw.close();
System.out.println();
FileReader fr=new FileReader("example.txt");
BufferedReader b=new BufferedReader(fr);
System.out.println("Reading the file example.txt-----");
while((w=b.readLine())!=null){
System.out.println(w);
}
fr.close();
System.out.println("Reading end");
}
}
OUTPUT
5
6. BY O.W.O
6. Write a program that illustrates the multilevel inheritance in java.
Algorithm:
Step 1: Create a class A
Step 2: Create another class B which derives from A using extends keyword
Step 3: Create another class C, which derives from B using, extends keyword.
Step 4: Create objects that can able to call the member functions of all the above classes from
the third class C.
CODE
class Person {
String name,address;
int age;
void personalDetails(String nm,int ag,String add) {
name = nm;
age = ag;
address =add;
}
void displaydetail() {
System.out.println("Name:"+name);
System.out.println("Age:"+age);
System.out.println("Address:"+address);
}
}
class employee extends Person {
int empid,salary;
void empdetails(int id,int sal) {
empid=id;
salary=sal;
}
void displayemployee() {
System.out.println("Employee ID"+empid);
System.out.println("Salary"+salary); }
}
interface bonus {
int bonus=1000;
void compute();
class worker extends employee implements bonus {
int amount;
public void compute() {
System.out.println("Bonus is :"+bonus);
amount=salary + bonus;
}
6
7. BY O.W.O
void workerdetails() {
displaydetail();
displayemployee();
compute();
System.out.println("Total Amount:"+amount); }
}
public class MultiIn {
public static void main(String[] args) {
worker obj = new worker();
obj.personalDetails("Dosh",25,"116,Tahiti");
obj.empdetails(101,2500);
obj.workerdetails(); }
}
Output:
Name: Dosh
Address: 116, Tahiti
Employee ID: 101
Salary: 2500
Bonus is 1000
Total Amount: 3500
7
8. BY O.W.O
7. Write a program that imports the user defined package and access the member
function of classes that are contained by the package.
Algorithm:
Step 1: Create a java program with data members and member functions.
Step 2: Package can be created by using package name;
Step 3: Save this in a separate folder
Step 4: Create another class outside the folder
Step 5: Import the package you created using import package name.
Step 6: Use the object to access the member function of that package class.
Code
package my_package;
public class Rate
{
public void firstresult()
{
System.out.println("This is first class results X-rated.");
}
}
import my_package.Rate;
class Bo
{
public static void main(String args[ ])
{
Rate obj= new Rate();
obj.firstresult();
}
}
Output:
This is first class results X-rated.
8
9. BY O.W.O
8. WAP that illustrates the exception handling concepts using try, catch and finally?
ALGORITHMS
Step 1: Write a program with necessary class name and functions.
Step 2: Write the code that are expected to cause error with in the try block.
Step 3: Write a catch block that can accept the error caused in try.
Step 4: Write some statements within finally block that are the statements to be executed
even when error occurred or not.
CODE
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class TryCatchFinally {
public static void main(String[] args) {
try { // main logic
System.out.println("Start of the main logic");
System.out.println("Try opening a file ...");
Scanner in = new Scanner(new File("test.in"));
System.out.println("File Found, processing the file ...");
System.out.println("End of the main logic");
} catch (FileNotFoundException ex) { // error handling separated from the main logic
System.out.println("File Not Found caught ...");
} finally { // always run regardless of exception status
System.out.println("finally-block runs regardless of the state of exception");
}
System.out.println("After try-catch-finally, life goes on...");
}
} OUTPUT
9
10. BY O.W.O
9. WAP to create a thread that implements the runnable interface
ALGORITHMS
Step 1: Create a class that implements the Runnable interface
Step 2: Call the run function and write a while loop that runs for certain condition
Step 3: Create another class runnable interface and write the main function
Step 4: Within the main function create the object for the first class
Step 5: Create an object for thread, pass the object created as an argument, and call the start
function.
Step 6: Execute the program to get the output.
code
class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
RunnableDemo( String name){
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
10
11. BY O.W.O
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
}
public class TestThread {
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
}
}
11
12. BY O.W.O
10. WAP to count the frequency of a given letter in a string.
ALGORITHMS
Step 1: Create a class that reads the a string value from keyboard
Step 2: Leave a prompt message that accepts the character whose frequency is to be found.
Step 3: Using an if statement compare that character with the char array and if found increase
the count of the character.
Step 4: Then display the message showing the total number of occurrences of that character.
CODE
import java.io.*;
class Frequency
{
static String n;
static int l;
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String : ");
n = br.readLine();
l = n.length();
freq();
}
12
13. BY O.W.O
public static void freq()
{
int s=0,f=-1;
for(int i=0;i<l;i++)
{
// Find frequecy
for(int j=0;j<l;j++)
{
if(n.charAt(i)==n.charAt(j))
s++;
}
// Check if the letter has occured before
for(int k=0;k<i;k++)
{
if(n.charAt(i)==n.charAt(k))
f = 1;
}
// Print the letter's frequency
if(f==-1)
System.out.println(n.charAt(i) +" = " +s);
s=0;
f=-1;
}
}
}
13
14. BY O.W.O
11. WAP to create a GUI which contains three labels, input box and a button. it should
perform the addition of two numbers entered in the input box when the button is clicked.
ALGORITHMS
Step 1: Import awt and applet package and the class must implements Actionlistener
interface.
Step 2: Create objects for Labels and Textboxes and Button
Step 3: Using add function add all the controls over the Applet.
Step 4: Use the addActionListener and actionPerformed function to perform the addition of
the values entered with in the textbox.
CODE
/*<applet code="ButtonAdd" height=200 width=200></applet>*/
import java.awt.*;
import java.applet.*;
import java.applet.Applet;
import java.awt.event.*;
public class ButtonAdd extends Applet implements ActionListener
{
Label title,fn,sn,res;
Button add;
TextField t1,t2,t3;
int x=0,y=0,z;
14