Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
0Activity
0 of .
Results for:
No results containing your search query
P. 1
82406973-Java-Lab-4

82406973-Java-Lab-4

Ratings: (0)|Views: 9|Likes:
Published by adit_singhal2k

More info:

Published by: adit_singhal2k on Feb 28, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

02/28/2012

pdf

text

original

 
LAB NO.: 4PROGRAM NO.:8Date:21/02/2012PROGRAM :-
Create a class Complex for performing arithmetic with complex numbers.Complex numbers have the formreal part+ imaginary part *
i,
where i=√1Write a program to test your class. Use floating point variables to represent the private data of theclass. Provide the constructor that enables an object of this class to be initialized when it’sdeclared. 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 numbersc.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 hereComplex 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 Part1Enter The imaginary Part2Enter The real Part1Enter The imaginary Part2Sum :2.0 + 4.0iSub0.0 + 0.0iAdit Singhal 2K8-MRCE-CS-003
 
LAB NO.: 4PROGRAM NO.:10Date:21/02/2012PROGRAM :-
The greatest common divisor of integers x and y is the largest integer thatevenly divides into both x and y. write a recursive method gcd that returns the greatest commondivisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, thengcd(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 hereint 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

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->