0% found this document useful (0 votes)
5 views10 pages

Aditya Java Practical.

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

Aditya Java Practical.

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

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 and Multiply two
complex numbers. b. toString() method to display complex numbers in the form: x + i
y;

Output:
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
coordinates. 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.
Output:

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.
Output:
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.

Output:
6. Write a program that copies content of one file to another. Pass the names of the
files through command-line arguments.

Output:
7. Write a program to read a file and display only those lines that have the first two
characters as '//' (Use try with resources).
Output:

9. Write a program to display a string in frame window with pink


color as background.
import [Link].*;
public class DisplayString extends Frame{
DisplayString(){
setTitle("String Display");
setSize(300,200);
setBackground([Link]);
setVisible(true);
}
public void paint(Graphics g) {
[Link]([Link]);
[Link]("hello java!", 100,100);
}
public static void main(String[] args){
new DisplayString();
}
}
OUTPUT

You might also like