SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌Seok-joon Yun
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3Sunil OS
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in javasureshraj43
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)mehul patel
 
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016Codemotion
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2vikram mahendra
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in JavaJava2Blog
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4Sunil OS
 
JVM Memory Management Details
JVM Memory Management DetailsJVM Memory Management Details
JVM Memory Management DetailsAzul Systems Inc.
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in JavaJava2Blog
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinShariful Haque Robin
 

Mais procurados (20)

Rust-lang
Rust-langRust-lang
Rust-lang
 
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
 
Java practical
Java practicalJava practical
Java practical
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)
 
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
 
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime modulePython Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime module
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
Log4 J
Log4 JLog4 J
Log4 J
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
JVM Memory Management Details
JVM Memory Management DetailsJVM Memory Management Details
JVM Memory Management Details
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Java naming conventions
Java naming conventionsJava naming conventions
Java naming conventions
 

Destaque

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
 

Destaque (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
 

Semelhante a 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 programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
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
 

Semelhante a 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
 
srgoc
srgocsrgoc
srgoc
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
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
 

Último

Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
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
 
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 Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
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
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
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
 
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
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Último (20)

Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
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
 
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 Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
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
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
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
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
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
  • 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