SlideShare a Scribd company logo
1 of 16
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
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
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
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
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
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
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
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
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
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
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
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
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
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
BY O.W.O
public void init()
{
title=new Label("Arithmetic operationn");
fn=new Label("First numbern");t1=new TextField(20);
sn=new Label("Second numbern");t2=new TextField(20);
res=new Label("Resultn");
t3=new TextField(20);
add=new Button("Add");
add.addActionListener(this);
add(title);add(fn);add(sn);add(res);add(t1);add(t2);add(t3);add(add);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==add)
{
x=Integer.parseInt(t1.getText());
y=Integer.parseInt(t2.getText());
z=x+y;
t3.setText(String.valueOf(z))
}
}
}
OUTPUT
15
BY O.W.O
16

More Related Content

What's hot

C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: MethodsSvetlin Nakov
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdmHarshal Misalkar
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
OOP in C++
OOP in C++OOP in C++
OOP in C++ppd1961
 
Double taxation avoidance agreement (DTAA)
Double taxation avoidance agreement (DTAA)Double taxation avoidance agreement (DTAA)
Double taxation avoidance agreement (DTAA)Vinay Singh
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
Namespace in C++ Programming Language
Namespace in C++ Programming LanguageNamespace in C++ Programming Language
Namespace in C++ Programming LanguageHimanshu Choudhary
 
Exempt incomes - Income which do not form part of total income
Exempt incomes - Income which do not form part of total incomeExempt incomes - Income which do not form part of total income
Exempt incomes - Income which do not form part of total incomeCA Bala Yadav
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++Nitin Jawla
 
Effective Java - Enum and Annotations
Effective Java - Enum and AnnotationsEffective Java - Enum and Annotations
Effective Java - Enum and AnnotationsRoshan Deniyage
 

What's hot (20)

Java input
Java inputJava input
Java input
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Double taxation avoidance agreement (DTAA)
Double taxation avoidance agreement (DTAA)Double taxation avoidance agreement (DTAA)
Double taxation avoidance agreement (DTAA)
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Namespace in C++ Programming Language
Namespace in C++ Programming LanguageNamespace in C++ Programming Language
Namespace in C++ Programming Language
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Exempt incomes - Income which do not form part of total income
Exempt incomes - Income which do not form part of total incomeExempt incomes - Income which do not form part of total income
Exempt incomes - Income which do not form part of total income
 
This pointer
This pointerThis pointer
This pointer
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
 
Effective Java - Enum and Annotations
Effective Java - Enum and AnnotationsEffective Java - Enum and Annotations
Effective Java - Enum and Annotations
 

Viewers also liked

Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical FileSoumya Behera
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL fileRACHIT_GUPTA
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java AppletsTareq Hasan
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobGaruda Trainings
 

Viewers also liked (8)

Java programs
Java programsJava programs
Java programs
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical File
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
 
Java codes
Java codesJava codes
Java codes
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 

Similar to Java practical

Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Ayes Chinmay
 
Java Programs
Java ProgramsJava Programs
Java Programsvvpadhu
 
Java programs
Java programsJava programs
Java programsjojeph
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIjagriti srivastava
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdfanupamfootwear
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntuKhurshid Asghar
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34myrajendra
 
Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diplomamustkeem khan
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfmayorothenguyenhob69
 

Similar to Java practical (20)

Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Java programs
Java programsJava programs
Java programs
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Lab4
Lab4Lab4
Lab4
 
Java practical
Java practicalJava practical
Java practical
 
srgoc
srgocsrgoc
srgoc
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Java programs
Java programsJava programs
Java programs
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
Thread
ThreadThread
Thread
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntu
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diploma
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 

Recently uploaded

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 

Recently uploaded (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 

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
  • 15. BY O.W.O public void init() { title=new Label("Arithmetic operationn"); fn=new Label("First numbern");t1=new TextField(20); sn=new Label("Second numbern");t2=new TextField(20); res=new Label("Resultn"); t3=new TextField(20); add=new Button("Add"); add.addActionListener(this); add(title);add(fn);add(sn);add(res);add(t1);add(t2);add(t3);add(add); } public void actionPerformed(ActionEvent e) { if(e.getSource()==add) { x=Integer.parseInt(t1.getText()); y=Integer.parseInt(t2.getText()); z=x+y; t3.setText(String.valueOf(z)) } } } OUTPUT 15