0% found this document useful (0 votes)
2 views

JAVA practical Answers 1523-1591

This document is a certificate for a student in the MCA program at Rajarshi Shahu Institute of Management, certifying their submission of a practical file on Java for the academic year 2024-2025. It includes an index of various Java programming assignments demonstrating different concepts such as primitive data types, static methods, multi-threading, and file handling. Each assignment is accompanied by code examples and expected outputs.

Uploaded by

om8007226255
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA practical Answers 1523-1591

This document is a certificate for a student in the MCA program at Rajarshi Shahu Institute of Management, certifying their submission of a practical file on Java for the academic year 2024-2025. It includes an index of various Java programming assignments demonstrating different concepts such as primitive data types, static methods, multi-threading, and file handling. Each assignment is accompanied by code examples and expected outputs.

Uploaded by

om8007226255
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

B.G.P.

S,
Rajarshi Shahu Institute of Management,
Aurangabad

Department
Of
Master of Computer Applications

Certificate
This is to certify that Mr. /Miss --------------------------------is
our student of MCA –I semester having a Roll No. -------and has
submitted his /her Practical File on the subject of “JAVA “in the
academic year 2024-2025, as per the requirement of Dr.
Babasaheb Ambedkar Marathwada University, Aurangabad.

Mr. Anil Wagh Dr. Ejaz Qureshi


Practical Guide HOD Director

External Examiner
INDEX

Sr.No Questions PAGE


NO
1 WAP to demonstrate the primitive datatypes with their default
value
2 WAP to demonstrate the function of predefined class scanner

3 WAP a program to demonstrate the use of Static member and static method

4 WAP a to implement Static binding in java

5 Write a program to demonstrate the use of Super Keyword in Java

6 WAP to demonstrate the Application of package in Java

7 WAP to demonstrate the implementation of multi-threading using runnable


interface

8 WAP to demonstrate canvas implementation

9 WAP to Create Scroll bar application using label

10 WAP to demonstrate the use of dialogue box

11 WAP to implement Window close events

12 Write a program to read and write character in file

13 Write a program to use of write and reader classes

14 WAP to demonstrate the keylistener in java

15 write a program to manage arithmetic exception in java


1. WAP a to demonstrate the primitive data types with their Default value

Program:
public class Demo {

static boolean val1;

static double val2;

static float val3;

static int val4;

static long val5;

static String val6;

public static void main(String[] args) {

System.out.println("Default values.....");

System.out.println("Val1 = " + val1);

System.out.println("Val2 = " + val2);

System.out.println("Val3 = " + val3);

System.out.println("Val4 = " + val4);

System.out.println("Val5 = " + val5);

System.out.println("Val6 = " + val6);

Output:

Default values.....

Val1 = false

Val2 = 0.0

Val3 = 0.0

Val4 = 0

Val5 = 0

Val6 = null
2. WAP a to demonstrate the Function of predefined class scanner

Program:

import java.util.*;

public class ScannerClassExample1 {

public static void main(String args[]){

String s = "Hello, This is JavaTpoint.";

//Create scanner Object and pass string in it

Scanner scan = new Scanner(s);

//Check if the scanner has a token

System.out.println("Boolean Result: " + scan.hasNext());

//Print the string

System.out.println("String: " +scan.nextLine());

scan.close();

System.out.println("--------Enter Your Details-------- ");

Scanner in = new Scanner(System.in);

System.out.print("Enter your name: ");

String name = in.next();

System.out.println("Name: " + name);

System.out.print("Enter your age: ");

int i = in.nextInt();

System.out.println("Age: " + i);

System.out.print("Enter your salary: ");

double d = in.nextDouble();

System.out.println("Salary: " + d);

in.close();

}
Output:

Boolean Result: true

String: Hello, This is JavaTpoint.

-------Enter Your Details---------

Enter your name: Abhishek

Name: Abhishek

Enter your age: 23

Age: 23

Enter your salary: 25000

Salary: 25000.0
3. WAP a program to demonstrate the use of Static member and static method

Program:

//Java Program to demonstrate the use of a static method.

class Student{

int rollno;

String name;

static String college = "ITS";

//static method to change the value of static variable

static void change(){

college = "BBDIT";

//constructor to initialize the variable

Student(int r, String n){

rollno = r;

name = n;

//method to display values

void display(){System.out.println(rollno+" "+name+" "+college);}

//Test class to create and display the values of object

public class TestStaticMethod{

public static void main(String args[]){

Student.change();//calling change method

//creating objects

Student s1 = new Student(111,"Karan");

Student s2 = new Student(222,"Aryan");

Student s3 = new Student(333,"Sonoo");

//calling display method

s1.display();
s2.display();

s3.display();

Output:

Output:111 Karan BBDIT

222 Aryan BBDIT

333 Sonoo BBDIT


4. WAP a to implement Static binding in java

class Animal{

void eat(){System.out.println("animal is eating...");}

class Dog extends Animal{

void eat(){System.out.println("dog is eating...");}

public static void main(String args[]){

Animal a=new Dog();

a.eat();

Output: dog is eating...


5. Write a program to demonstrate the use of Super Keyword in Java

Program :

class Animal{

String color="white";

class Dog extends Animal{

String color="black";

void printColor(){

System.out.println(color);//prints color of Dog class

System.out.println(super.color);//prints color of Animal class

class TestSuper1{

public static void main(String args[]){

Dog d=new Dog();

d.printColor();

}}

Output :

black
white
6. WAP to demonstrate the Application of package in Java

Program :

//save by A.java

package pack;

public class A{

public void msg(){System.out.println("Hello");}

//save by B.java

package mypack;

import pack.*;

class B{

public static void main(String args[]){

A obj = new A();

obj.msg();

Output:

Hello
7. WAP to demonstrate the implementation of multi-threading using runnable interface

Program :

public class ExampleClass implements Runnable {

@Override

public void run() {

System.out.println("Thread has ended");

public static void main(String[] args) {

ExampleClass ex = new ExampleClass();

Thread t1= new Thread(ex);

t1.start();

System.out.println("Hi");

Output:

Hi

Thread has ended


8. WAP to demonstrate canvas implementation

Program:

// importing awt class


import java.awt.*;
// class to construct a frame and containing main method
public class CanvasExample
{
// class constructor
public CanvasExample()
{
// creating a frame
Frame f = new Frame("Canvas Example");
// adding canvas to frame
f.add(new MyCanvas());
// setting layout, size and visibility of frame
f.setLayout(null);
f.setSize(400, 400);
f.setVisible(true);
}
// main method
public static void main(String args[])
{
new CanvasExample();
}
}
// class which inherits the Canvas class
// to create Canvas
class MyCanvas extends Canvas
{
// class constructor
public MyCanvas() {
setBackground (Color.GRAY);
setSize(300, 200);
}

// paint() method to draw inside the canvas


public void paint(Graphics g)
{
// adding specifications
g.setColor(Color.red);
g.fillOval(75, 75, 150, 75);
}
}
Output:
9. WAP to Create Scroll bar application using label

import javax.swing.*;

class ScrollBarExample

ScrollBarExample(){

JFrame f= new JFrame("Scrollbar Example");

JScrollBar s=new JScrollBar();

s.setBounds(100,100, 50,100);

f.add(s);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(String args[])

new ScrollBarExample();

}}

Output:
10. WAP to demonstrate the use of dialogue box

import java.awt.*;

import java.awt.event.*;

public class DialogExample {

private static Dialog d;

DialogExample() {

Frame f= new Frame();

d = new Dialog(f , "Dialog Example", true);

d.setLayout( new FlowLayout() );

Button b = new Button ("OK");

b.addActionListener ( new ActionListener()

public void actionPerformed( ActionEvent e )

DialogExample.d.setVisible(false);

});

d.add( new Label ("Click button to continue."));

d.add(b);

d.setSize(300,300);

d.setVisible(true);

public static void main(String args[])

new DialogExample();

}
Output:
11. WAP to implement Windowcloseevents

// importing necessary awt libraries

import java.awt.*;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

// class which inherits the Frame class

public class WindowExample extends Frame {

// class constructor

WindowExample() {

// adding WindowListener to the Frame

// and using the windowClosing() method of WindowAdapter class

addWindowListener (new WindowAdapter() {

public void windowClosing (WindowEvent e) {

dispose();

});

// setting the size, layout and visibility of frame

setSize (400, 400);

setLayout (null);

setVisible (true);

// main method

public static void main (String[] args) {

new WindowExample();

}
Output :
12. Write a program to read and write character in file

// Java Program to Write Into a File

// using writeString() Method

// Importing required classes

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;

// Main class

public class GFG {

// Main driver method

public static void main(String[] args)

throws IOException

// Assigning the content of the file

String text

= "Welcome to geekforgeeks\nHappy Learning!";

// Defining the file name of the file

Path fileName = Path.of(

"/Users/mayanksolanki/Desktop/demo.docx");

// Writing into the file

Files.writeString(fileName, text);

// Reading the content of the file

String file_content = Files.readString(fileName);

// Printing the content inside the file

System.out.println(file_content);

}
Output:

Welcome to geekforgeeks

Happy Learning!
13. Write a program to use of write and reader classes

//Java Program to illustrate how to define a class and fields

//Defining a Student class.

class Student{

//defining fields

int id;//field or data member or instance variable

String name;

//creating main method inside the Student class

public static void main(String args[]){

//Creating an object or instance

Student s1=new Student();//creating an object of Student

//Printing values of the object

System.out.println(s1.id);//accessing member through reference variable

System.out.println(s1.name);

Output:

Null
14. write a program to demonstrate the keylistener in java

// importing awt libraries

import java.awt.*;

import java.awt.event.*;

// class which inherits Frame class and implements KeyListener interface

public class KeyListenerExample extends Frame implements KeyListener {

// creating object of Label class and TextArea class

Label l;

TextArea area;

// class constructor

KeyListenerExample() {

// creating the label

l = new Label();

// setting the location of the label in frame

l.setBounds (20, 50, 100, 20);

// creating the text area

area = new TextArea();

// setting the location of text area

area.setBounds (20, 80, 300, 300);

// adding the KeyListener to the text area

area.addKeyListener(this);

// adding the label and text area to the frame

add(l);

add(area);

// setting the size, layout and visibility of frame

setSize (400, 400);

setLayout (null);

setVisible (true);
}

// overriding the keyPressed() method of KeyListener interface where we set the text of the label when
key is pressed

public void keyPressed (KeyEvent e) {

l.setText ("Key Pressed");

// overriding the keyReleased() method of KeyListener interface where we set the text of the label when
key is released

public void keyReleased (KeyEvent e) {

l.setText ("Key Released");

// overriding the keyTyped() method of KeyListener interface where we set the text of the label when a
key is typed

public void keyTyped (KeyEvent e) {

l.setText ("Key Typed");

// main method

public static void main(String[] args) {

new KeyListenerExample();

Output:
15. write a program to manage arithmetic exception in java

public class ArithmeticException

void divide(int a, int b)

// performing divison and storing th result

int res = a / b;

System.out.println("Division process has been done successfully.");

System.out.println("Result came after division is: " + res);

// main method

public static void main(String argvs[])

// creating an object of the class ArithmeticException

ArithmeticException obj = new ArithmeticException();

obj.divide(1, 0);

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero

at ArithmeticException.divide(ArithmeticException.java:6)

at ArithmeticException.main(ArithmeticException.java:16)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy