You are on page 1of 8

LAB NO.: 4 PROGRAM NO.:8 Date:21/02/2012 PROGRAM :- Create a class Complex for performing arithmetic with complex numbers.

Complex numbers have the form real part+ imaginary part * i, where i=1 Write a program to test your class. Use floating point variables to represent the private data of the class. Provide the constructor that enables an object of this class to be initialized when its declared. Provide a no-argument constructor with default values in case no initializations are provided. Provide public methods that perform the following operations. a. Add two complex numbers b. Subtract two complex numbers c. Print complex numbers in the for (a,b) where a is the real part and b is the imaginary part.

CODE: package javaapplication17; import java.util.*; /** * * @author student */ class Complex { float real; float imaginary; public Complex() { } public Complex(float real, float imaginary) { this.real = real; this.imaginary = imaginary; } void getdata() { Scanner s = new Scanner(System.in); System.out.println("Enter The real Part"); real = s.nextFloat(); System.out.println("Enter The imaginary Part"); imaginary = s.nextFloat(); } void putdata() { System.out.println(real + " + " + imaginary + "i"); Adit Singhal 2K8-MRCE-CS-003

} void sum(Complex a, Complex b) { real = a.real + b.real; imaginary = a.imaginary + b.imaginary; System.out.println("Sum :" + real + " + " + imaginary + "i"); } void sub(Complex a, Complex b) { real = a.real - b.real; imaginary = a.imaginary - b.imaginary; System.out.println("Sub :" + real + " + " + imaginary + "i"); } } public class JavaApplication17 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Complex a = new Complex(); Complex b = new Complex(); Complex c = new Complex(); a.getdata(); b.getdata(); c.sum(a, b); c.sub(a,b); } } OUTPUT run: Enter The real Part 1 Enter The imaginary Part 2 Enter The real Part 1 Enter The imaginary Part 2 Sum :2.0 + 4.0i Sub0.0 + 0.0i

Adit Singhal 2K8-MRCE-CS-003

LAB NO.: 4 PROGRAM NO.:10 Date:21/02/2012 PROGRAM :- The greatest common divisor of integers x and y is the largest integer that
evenly divides into both x and y. write a recursive method gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd(x,y)is x; otherwise, gcd(x,y) is gcd(y,x%y), where % is the remainder operator.

CODE package javaapplication18; import java.util.*; /** * * @author student */ public class JavaApplication18 { /** * @param args the command line arguments */ public static int gcd(int x,int y) { if(y==0) { return x; } else { return gcd(y,x%y); } } public static void main(String[] args) { // TODO code application logic here int x,y,c; Scanner s = new Scanner(System.in); System.out.print("Enter X :"); x= s.nextInt(); System.out.print("Enter Y :"); y= s.nextInt(); c=gcd(x,y); System.out.println("GCD :" + c); }}

Adit Singhal 2K8-MRCE-CS-003

OUTPUT run: Enter X :16 Enter Y :32 GCD :16 BUILD SUCCESSFUL (total time: 5 seconds)

Adit Singhal 2K8-MRCE-CS-003

LAB NO.: 4 PROGRAM NO.:9 Date:21/02/2012 PROGRAM :- Use Exception to create an exception superclass called ExceptionA and
exception subclass ExceptionB and ExceptionC, where ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exception of types ExceptionB and ExceptionC.

CODE

package javaapplication19; /** * * @author student * */ class ExceptionA { int a; public ExceptionA(int a) { this.a = a; } public int getA() { return a; } public void setA(int a) { this.a = a; } } class ExceptionB extends ExceptionA { int b; public ExceptionB(int b, int a) { super(a); this.b = b; }

Adit Singhal 2K8-MRCE-CS-003

public int getB() { return b; } public void setB(int b) { this.b = b; } } class ExceptionC extends ExceptionB { int c; public ExceptionC(int c, int b, int a) { super(b, a); this.c = c; } public int getC() { return c; } public void setC(int c) { this.c = c; } } public class JavaApplication19 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here ExceptionC obj=new ExceptionC(1,2,3); ExceptionB obj1=new ExceptionB(2,3); System.out.println("Catch From ExceptionC Value of ExceptionA :" + obj.getA()); System.out.println("Catch From ExceptionC Value of ExceptionB :" + obj.getB()); System.out.println("Catch From ExceptionC Value of ExceptionC :" + obj.getC()); System.out.println("Catch From ExceptionB Value of ExceptionA :" + obj1.getA()); System.out.println("Catch From ExceptionB Value of ExceptionB :" + obj1.getB()); } }

Adit Singhal 2K8-MRCE-CS-003

OUTPUT run: Catch From ExceptionC Value of ExceptionA :3 Catch From ExceptionC Value of ExceptionB :2 Catch From ExceptionC Value of ExceptionC :1 Catch From ExceptionB Value of ExceptionA :3 Catch From ExceptionB Value of ExceptionB :2 BUILD SUCCESSFUL (total time: 1 second)

Adit Singhal 2K8-MRCE-CS-003

Department of Computer Science & Engineering, MRCE, Faridabad Lab Assignment-4 Semester: VIII Subject: Advance Java Year: 2012 Subject Code:CSE-406-E Prepared By: Kanika L. Chaudhary Faculty: Kanika L. Chaudhary Nikita Taneja

1. Create a class Complex for performing arithmetic with complex numbers. Complex numbers have the form real part+ imaginary part * i, where i=1 Write a program to test your class. Use floating point variables to represent the private data of the class. Provide the constructor that enables an object of this class to be initialized when its declared. Provide a no-argument constructor with default values in case no initializations are provided. Provide public methods that perform the following operations. d. Add two complex numbers e. Subtract two complex numbers f.Print complex numbers in the for (a,b) where a is the real part and b is the imaginary part.

2. Use Exception to create an exception superclass called ExceptionA and exception


subclass ExceptionB and ExceptionC, where ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exception of types ExceptionB and ExceptionC. 3. The greatest common divisor of integers x and y is the largest integer that evenly divides into both x and y. write a recursive method gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd(x,y)is x; otherwise, gcd(x,y) is gcd(y,x%y), where % is the remainder operator.

Adit Singhal 2K8-MRCE-CS-003

You might also like