0% found this document useful (0 votes)
9 views45 pages

JAVA Practical File

The document contains 13 programming problems related to Java. It provides sample code and output for problems involving classes, inheritance, exceptions, files, stacks, GUI using AWT and Swing.

Uploaded by

email4ishan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
9 views45 pages

JAVA Practical File

The document contains 13 programming problems related to Java. It provides sample code and output for problems involving classes, inheritance, exceptions, files, stacks, GUI using AWT and Swing.

Uploaded by

email4ishan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 45

JAVA PRACTICAL

FILE

Student Name: Ishan Sharma


Roll Number: 2102027
Subject: Programming in Java
Year/ Semester: I/II
1) Design a class Complex having a real part (x) and an
imaginary part (y). Provide methods to perform the
following on complex numbers:
a) Add two complex numbers.
b) Multiply two complex numbers.
c) toString() method to display complex numbers in
the form: x + i y

CODE
RUN

2) Create a class TwoDim which contains private members


as x and y coordinates in package P1. Define the default
constructor, a parameterized constructor and override
toString() method to display the co-ordinates. Now reuse
this class and in package P2 create another class
ThreeDim, adding a new dimension as z as its private
member. Define the constructors for the subclass and
override toString() method in the subclass also. Write
appropriate methods to show dynamic method dispatch.
The main() function should be in a package P

CODE

Package 1
Package 2

Package containing Main

RUN
3. Define an abstract class Shape in package P1. Inherit two
more classes: Rectangle in package P2 and Circle in
package P3. Write a program to ask the user for the type
of shape and then using the concept of dynamic method
dispatch, display the area of the appropriate subclass.
Also write appropriate methods to read the data. The
main() function should not be in any package.
CODE
RUN

4. Create an exception subclass UnderAge, which prints


“Under Age” along with the age value when an object of
UnderAge class is printed in the catch statement. Write a
class exceptionDemo in which the method test() throws
UnderAge exception if the variable age passed to it as
argument is less than 18. Write main() method also to
show working of the program.
CODE

RUN
5. Write a program to implement stack. Use exception
handling to manage underflow and overflow
conditions.
CODE
RUN
6. Write a program that copies content of one file to
another. Pass the names of the files through
command-line arguments.
CODE

RUN

7. Write a program to read a file and display only


those lines that have the first two characters as '//'
(Use try with resources)).

CODE
From file: p.txt

RUN
8. Write a program to create a frame using AWT.
Implement mouseClicked(), mouseEntered() and
mouseExited() events such that: a) Size of the
frame should be tripled when mouse enters it. b)
Frame should reduce to its original size when
mouse is clicked in it. c) Close the frame when
mouse exits it.
CODE
RUN
a)
b)

C)
9. Using AWT, write a program to display a string in
frame window with pink color as background.

CODE

RUN

10. Using AWT, write a program to create two


buttons named “Red” and “Blue”. When a button
is pressed the background color should be set to
the color named by the button’s label.

CODE

RUN
11. Using AWT, write a program using appropriate
adapter class to display the message (“Typed character
is: <typedCharacter>") in the frame window when user
types any key.

CODE
RUN
12. Using AWT, write a program to create two buttons
labelled ‘A’ and ‘B’. When button ‘A’ is pressed, it
displays your personal information (Name, Course,
Roll No, College) and when button ‘B’ is pressed, it
displays your CGPA in previous semester
CODE
RUN
13. Rewrite all the above GUI programs using Swing
1.
CODE
RUN
2.
CODE
RUN
3.
CODE
RUN
4.
CODE
RUN
5.
CODE
RUN

You might also like