





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






































QUESTION1 - 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+iy. ANSWER- class complex { int real; int imaginary; complex() { // default } complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } void add(complex c1, complex c2) { this.real = c1.real + c2.real; this.imaginary = c1.imaginary + c2.imaginary; System.out.println(real + "+" + imaginary + "i");
return; } void mul(complex c1, complex c2) { this.real = c1.real * c2.real - c1.imaginary * c2.imaginary; this.imaginary = c1.real * c2.imaginary + c1.imaginary * c2.real; System.out.println(real + "+" + imaginary + "i"); return; } } public class bsc_ques1_complex_class { public static void main(String[] args) { complex c1 = new complex(3, 2); complex c2 = new complex(1, 7); complex c3 = new complex(); c3.add(c1, c2); c3.mul(c1, c2); } } OUTPUT-
this.x = x; this.y = y; } public String toString() { return x + "," + y; } } package p2; import p1.; public class ThreeDim extends TwoDim { private int z; public ThreeDim() { super(); z = 0; } public ThreeDim(int x, int y, int z) { super(x, y); this.z = z; } public String toString() { System.out.print(super.toString()); return "," + z; } } package p; import p1.; import p2.*; public class OneDim { public static void main(String[] args) {
TwoDim two = new TwoDim(); TwoDim two_ = new TwoDim(1, 2); ThreeDim three = new ThreeDim(); ThreeDim three_ = new ThreeDim(1, 2, 3); TwoDim r; r = two; System.out.println(r.toString()); r = two_; System.out.println(r.toString()); r = three; System.out.println(r.toString()); r = three_; System.out.println(r.toString()); } } OUTPUT-
package P2; import P1.; public class Rectangle extends Shape { public double length; public double breadth; public Rectangle(double l, double b) { length = l ; breadth = b; } public void getData( ) { this.length=length; this.breadth=breadth; System.out.print("\t\tDimensions of rectangle : "); System.out.println("\n\t\t\t\t\tLength = " + length + " \n\t\t\t\t\tBreadth = " +breadth); } public double area() { return lengthbreadth;} } /**** Circle.java ****/ package P3; import P1.*; public class Circle extends Shape { public double radius; public Circle(double r)
radius= r; } public void getData( ) { this.radius=radius; System.out.println("\t\tRadius of the circle : " +radius); } public double area() { return 3.14radiusradius; } } /**** Main.java ****/ import java.util.Scanner; import P1.; import P2.; import P3.*; class Main { public static void main(String args[]) { Scanner input = new Scanner(System.in); int ch; char c; do { System.out.println("\n\n\tSelect the shape of which you want to find the area " ); System.out.println("\tPRESS 1 TO FIND AREA OF RECTANGLE");
System.out.print("\n\n\tDO YOU WANT TO CONTINUE (Y/N) : "); c=input.next().charAt(0); } while(c=='Y' || c=='y'); } } OUTPUT- QUESTION4- 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. ANSWER- import java.util.Scanner; class UnderAge extends Exception { UnderAge(String s, int age) { super(s); } } public class exceptionDemo { static void test(int age) throws UnderAge { if (age < 18) { throw new UnderAge("Under Age", age); } else System.out.println("Eligible"); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter age to check validity :"); int age = sc.nextInt(); try { test(age); } catch (UnderAge e) {
Underflow(String s) { super(s); } } class Overflow extends Exception { Overflow(String s) { super(s); } } class stack_implementation { int top; int arr[]; stack_implementation(int size) { arr = new int[size]; top = -1; } void push(int x) throws Overflow { if (top == 9) { throw new Overflow("Stack Overflow"); } else arr[++top] = x; } void pop() throws Underflow {
if (top == -1) { throw new Underflow("Stack Underflow"); } else top--; } void printArray() { for (int i = 0; i < top + 1; i++) { System.out.println(arr[i]); } } } public class question_5 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("ENTER SIZE OF STACK :"); int size = sc.nextInt(); stack_implementation s1 = new stack_implementation(size); try { for (int i = 0; i < size; i++) { s1.push(i); } } catch (Overflow e) { System.out.println(e); }
QUESTION6- Write a program that copies content of one file to another. Pass the names of the files through command-line arguments. ANSWER- import java.io.*; public class filecontent { public static void main(String[] args) { // TODO Auto-generated method stub FileInputStream f1=null; int i1,i; char ch,ch1,ch2; if(args.length!=1) { System.out.println("File name not mentioned");
return; } try { f1=new FileInputStream(args[0]); do { i=f1.read(); if(i!=-1) { i1=f1.read(); ch=(char)i; ch1=(char)i1; do { ch2=(char)f1.read(); if(ch=='/'&&ch1=='/') System.out.print(ch2); }while(ch2!='\n'); } }while(i!=-1); } catch(FileNotFoundException o1) { System.out.println(o1);} catch(IOException o1) { System.out.println(o1); } } } OUTPUT-
public class filecopy { public static void main(String[] args) { // TODO Auto-generated method stub FileInputStream f1=null; FileOutputStream f2=null; int i; for(int i1=0;i1<args.length;i1++) { System.out.println("args["+i1+"]"+args[i1]); } if(args.length!=2) { System.out.println("File name not mentioned"); return; } try { f1=new FileInputStream(args[0]); f2=new FileOutputStream(args[1]); do { i=f1.read(); if(i!=-1) f2.write(i); System.out.println("File is opening"); System.out.println((char)i); } while(i!=-1); } catch(FileNotFoundException o1) {
System.out.println(o1); } catch(IOException o1) { System.out.println(o1); } System.out.println("File termination has been done successfully"); } } OUTPUT- QUESTION8- Write a program to create a frame using AWT, Implement mouseClicked(), mouseEntered() and mouseExited() events. Frame should become visible when mouse enters it. ANSWER- import java.awt.*;