1.
Demonstrate type conversions
public class conversion{ public static void main(String[] args){ boolean t = true; byte b = 2; short s = 100; char c = 'C'; int i = 200; long l = 24000; float f = 3.14f; double d = 0.000000000000053; String g = "string"; [Link]("Value of all the variables like"); [Link]("t = " + t ); [Link]("b = " + b ); [Link]("s = " + s ); [Link]("c = " + c ); [Link]("i = " + i ); [Link]("l = " + l ); [Link]("f = " + f ); [Link]("d = " + d ); [Link]("g = " + g ); [Link](); //Convert from boolean to byte. b = (byte)(t?1:0); [Link]("Value of b after conversion : " + b); //Convert from boolean to short. s = (short)(t?1:0); [Link]("Value of s after conversion : " + s); //Convert from boolean to int. i = (int)(t?1:0); [Link]("Value of i after conversion : " + i); //Convert from boolean to char. c = (char)(t?'1':'0'); [Link]("Value of c after conversion : " + c); c = (char)(t?1:0); [Link]("Value of c after conversion in unicode : " + c); //Convert from boolean to long. l = (long)(t?1:0); [Link]("Value of l after conversion : " + l); //Convert from boolean to float. f = (float)(t?1:0); [Link]("Value of f after conversion : " + f); //Convert from boolean to double. d = (double)(t?1:0); [Link]("Value of d after conversion : " + d);
//Convert from boolean to String. g = [Link](t); [Link]("Value of g after conversion : " + g); g = (String)(t?"1":"0"); [Link]("Value of g after conversion : " + g); int sum = (int)(b + i + l + d + f); [Link]("Value of sum after conversion : " + sum); } }
2. Implement operations on complex numbers class Complex { double r,i; public Complex(double re, double im) { r=re; i=im; } public Complex() { r=0; i=0; } void display() { [Link]("\n\tThe number is "+r+" i"+i); } Complex add( Complex a) { Complex n= new Complex(); n.r=this.r+a.r; n.i=this.i+a.i; return n; } Complex sub( Complex a) { Complex n= new Complex(); n.r=this.r-a.r; n.i=this.i-a.i; return n; } Complex mult( Complex a) {
Complex n= new Complex(); n.r=this.r*a.r-i*a.i; n.i=this.r*a.i+i*a.r; return n; } Complex div( Complex a) { double mag,maga,angle,anglea; mag= [Link](this.r*this.r+this.i*this.i); maga=[Link](a.r*a.r+a.i*a.i); angle=[Link](this.i/this.r); anglea=[Link](a.i/a.r); mag/=maga; angle-=anglea; Complex n= new Complex(); n.r=mag*([Link](angle)); n.i=mag*([Link](angle)); return n; } } class Operation { public static void main(String args[]) { Complex c1= new Complex(10,15); Complex c2= new Complex(20,25); Complex res=new Complex(); [Link]("\n\tAddition "); res= [Link](c2); [Link](); [Link]("\n\tMultiplication "); res=[Link](c2); [Link](); [Link]("\n\tDivision "); res=[Link](c2); [Link](); } } 3. Implement the following bank operations i) withdraw ii)deposit iii) check balance
import [Link];
public class BankAccount { public static void main(String[] args) { Scanner in = new Scanner([Link]); int userChoice; boolean quit = false; float balance = 0f; do { [Link]("1. Deposit money"); [Link]("2. Withdraw money"); [Link]("3. Check balance"); [Link]("Your choice, 0 to quit: "); userChoice = [Link](); switch (userChoice) { case 1: float amount; [Link]("Amount to deposit: "); amount = [Link](); if (amount <= 0) [Link]("Can't deposit nonpositive amount."); else { balance += amount; [Link]("$" + amount + " has been deposited.");
} break; case 2: [Link]("Amount to withdraw: "); amount = [Link](); if (amount <= 0 || amount > balance) [Link]("Withdrawal can't be completed."); else { balance -= amount; [Link]("$" + amount + " has been withdrawn."); } break; case 3: [Link]("Your balance: $" + balance); break; case 0: quit = true; break; default: [Link]("Wrong choice."); break; } [Link]();
} while (!quit); [Link]("Bye!"); } }
4. Demonstrate date class /* Program to demonstrate the methods of date class */
import [Link].*; class Datemeth { void date_meth() { Date d=new Date(); Date d1=new Date(2009-1900,9,22); [Link]("Present date is:"+d); if([Link](d1)) [Link]("The given date is after the specified date"); else [Link]("The given date is not after the specified date");
if([Link](d1)) [Link]("The given date is before the specified date"); else [Link]("The given date is not before the specified date");
int k=[Link](d1); if(k>0) [Link]("Second date follows the first date"); else if(k<0) [Link]("first date follows the second date"); else [Link]("Two dates are equal"); if([Link](d1)) [Link]("Two dates are equal"); else [Link]("Two dates are not equal"); [Link]("getDate():\t "+[Link]()); [Link]("getDay(): \t "+[Link]()); [Link]("getHours():\t "+[Link]()); [Link]("getMinutes():\t "+[Link]()); [Link]("getMonth(): \t"+[Link]()); [Link]("getSeconds(): \t"+[Link]()); [Link]("getTime():\t "+[Link]()); [Link]("getTimezoneOffset():"+[Link]()); [Link]("getYear():\t"+[Link]()); [Link](2000); [Link]("setDate():\t"+d1); [Link](72); [Link]("setHours():\t"+d1); [Link](60); [Link]("setMinutes():\t"+d1); [Link](9);
[Link]("setMonth():\t"+d1); [Link](24); [Link]("setSeconds():\t "+d1); [Link](253641); [Link]("setTime():\t"+d1); [Link](2010); [Link]("setYear():\t"+d1); [Link]("toString():\t"+[Link]()); [Link]("toGMTString():\t"+[Link]()); [Link]("toLocaleString():\t"+[Link]()); [Link]("UTC():\t"+[Link](2010,9,22,12,60,72)); } public static void main(String args[]) { Datemeth d=new Datemeth(); d.date_meth(); } }
5. Implement Method Overloading. class overLoading { public static void main(String[] args) { functionOverload obj = new functionOverload(); [Link](1,2); [Link](\"Life at \", \"?\"); [Link](11.5, 22.5);
} } class functionOverload { /* void add(int a, int b) {
// 1 - A method with two parameters
int sum = a + b; [Link](\"Sum of a+b is \"+sum); } */ {
void add(int a, int b, int c)
int sum = a + b + c; [Link](\"Sum of a+b+c is \"+sum); } void add(double a, double b) {
double sum = a + b; [Link](\"Sum of a+b is \"+sum); } void add(String s1, String s2) { String s = s1+s2; [Link](s); } } 6. Program to Demonstrate on Static variables and methods. class Book { //Declaring the data members. int price; int pages; //Defining the get() method. public void get(int mprice, int mpages) { price = mprice; pages = mpages; }
public void show() { [Link]( ); [Link](\t Books Information); [Link](\t Book Price: + price); [Link](\t Number of pages: + pages); [Link]( ); } }
lass SoftwareBook extends Book//Defining the subclass. { //Declaring the data members. String softwareName; String softwareVersion; //Defining the methods. public void getDetails(String msoftwareName, String msoftwareVersion) { softwareName = msoftwareName; softwareVersion = msoftwareVersion; } public void showDetails() { [Link]( ); [Link](\t Software Books Information); [Link](\t Software Name: + softwareName); [Link](\t Software Version: + softwareVersion); [Link]( ); } } class Cplus extends SoftwareBook //Declaration of the Subclass { //Declaring the data members. String author; String title; //Defining the methods. public void getData(String mauthor, String mtitle) { author = mauthor; title = mtitle; } public void showData() {
show(); showDetails(); [Link]( ); [Link](\t C++ Books Information); [Link](\t Author Name: + author); [Link](\t Book Title: + title); [Link]( ); } public static void main(String args[]) { Cplus c = new Cplus(); [Link](45, 450); // Calling the get() method of the Book class. // Calling the getDetails() method of the Software class. [Link](Borland C++, 5.0); // Calling the getData() method of the Cplus class. [Link](Lee Mitchell, Programming using C++); [Link](); } }
7. Program that demonstrates fields shadowing. When you declare a parameter to a method or a constructor, you provide a name for that parameter. This name is used within the method body to refer to the passed-in argument. The name of a parameter must be unique in its scope. It cannot be the same as the name of another parameter for the same method or constructor, and it cannot be the name of a local variable within the method or constructor. A parameter can have the same name as one of the class's fields. If this is the case, the parameter is said to shadow the field. Shadowing fields can make your code difficult to read and is conventionally used only within constructors and methods that set a particular field. For example, consider the following Circle class and its setOrigin method: public class Circle { private int x, y, radius; public void setOrigin(int x, int y) { ... } } The Circle class has three fields: x, y, and radius. The setOrigin method has two parameters, each of which has the same name as one of the fields. Each method parameter shadows the field that
shares its name. So using the simple names x or y within the body of the method refers to the parameter, not to the field. To access the field, you must use a qualified name.
8. Demonstrate the use of final lass Another { final int xyz; public Another() { xyz=1000; } public void display() { [Link](XYZ=+xyz); } } public class FinalTest { static final int y=3; public FinalTest() { } static { [Link](Just for fun); } public static void main(String[] args) { final int x=5; [Link](X=+x); [Link](Y=+y); Another ano=new Another(); [Link](); } }
9. Program to perform string sorting
import [Link].*; class Str_sort { public static void main(String args[]) { int n,i,j; n=[Link]; for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(args[j].compareTo(args[i])<0) { String t=args[i]; args[i]=args[j]; args[j]=t; } } [Link]("The Sorted Strings are:"); for(i=0;i<n;i++) [Link](args[i]); } }
10. Program to sort the array elements
import [Link].*; class Bub_sort { public static void main(String args[]) { Scanner s=new Scanner([Link]); int n,i,j; int a[]=new int[20]; [Link]("Enter size of an array: "); n=[Link](); [Link]("Enter elements into the array:"); for(i=0;i<n;i++) { [Link]("Enter a["+i+"] value:"); a[i]=[Link](); } for(i=0;i<n;i++) for(j=n-1;i<j;j--) { if(a[i]>a[j]) { int t=a[i]; a[i]=a[j]; a[j]=t; } } [Link]("The sorted elements are:");
for(i=0;i<n;i++) [Link](" "+a[i]); } }