1. Observe the interactions involved in the process of booking a railway ticket....
Shared By : Your Name (College - Place ) Show/Hide Program
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
2. Write a simple java application to print a pyramid with 5 lines...
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRM DEFINATION :- Write a simple java application to print a pyramid with 5 lines. The first line has one character, 2nd line has two characters and so on. The character to be used in the pyramid is taken as a commandline argument. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ class pyramid { public static void main(String str[]) { int i,j,n; n= Integer.parseInt(str[1]); //convert string to integer if(str[0].length() != 1 ) //check length of first argument System.out.println("Operation failed !! "); else { //First solution for(i=0;ii;j--) //for displaying space System.out.print(' '); for(j=0;j<=i;j++) //for displaying character System.out.print( str[0] +' '); System.out.println(); // Newline } /*second solution ..... j=1; for(;n>0;n--) // Loop for no of line { for(i=1;i
Shared By : dizy (College - SVICS ) Show/Hide Program
//save prog. as prog2.java and pass one argu as command line... class prog2 { public static void main(String arg[]) { int a; a=Integer.parseInt(arg[0]); int b=a; for(int i=0;i=0;k--) { System.out.print(" "); } b--; for(int j=0;j<=i;j++) { System.out.print("D "); } System.out.println(""); } } }
Shared By : Sonam Patel (SRIMCA - Tarsadi ) Show/Hide Program
class P2 { public static void main(String args[]) { int i,j; String ch; ch=args[0]; if(ch.length() != 1) System.out.println("Enter Only 1 character as Command Line Argument..."); else { for(i=0;i<5;i++) j="5-i;j">0;j--) System.out.printf(" "); for(j=0;j<=i;j++) { System.out.printf(" "); System.out.printf(ch); } System.out.println(); } } } }
3. Write a Java application which takes several command line arguments,...
/* PROBLEM NO :- 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRM DEFINATION :- Write a Java application which takes several command line arguments, which are supposed to be names of students and prints output as given below: (Suppose we enter 3 names then output should be as follows): Number of arguments = 3 1: First Student Name is =Tom 2: Second Student Name is =Dick 3: Third Student Name is =Harry Hint: An array may be used for converting from numeric values from 1 to 20 into String. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ //..............................FIRST SOLUTION USING ARRAY........................ class student { public static void main(String str[]) { int n,i=0; n=str.length;// store no of argumnet //string array fo display numbers in words String [] s={"FIRST","SECOND","THIRD","FOURTH","FIFTH","SIXTH","SEVENTH", "EIGHTH","NINETH","TENTH","ELEVENTH","TWELVETH","THIRTEENTH", "FOURTEENTH","FIFTEENTH","SIXTEENTH","SEVENTEENTH","EIGHTEENTH", "NINETEENTH","TWENTEENTH" }; //check no of arguments if(n >= 1 && n <=20 ) { System.out.println("\nNo of arguments are := " +n); System.out.println(); for (i=0;i= 1 && n <=20 ) //check no of argumnet { System.out.println("\nNo of arguments are := " +n); System.out.println(); for (words wd : words.values()) // vaues fetched from enum { if(i
Shared By : dizy (College - SVICS ) Show/Hide Program
//change coding as per your requirement... class prog3 { public static void main(String arg[]) { int a; a=arg.length; String str[]={"First","Second","Third","four","five","six","seven","eight","nine","ten"}; for(int i=0;i
class P3 { public static void main(String args[]) { int i,j; String[] s=new String[10]; s[0]="First"; s[1]="Second"; s[2]="Third"; s[3]="Fourth"; s[4]="Fifth"; s[5]="Sixth"; s[6]="Seventh"; s[7]="Eighth"; s[8]="Ninth"; s[9]="Tenth"; if(args.length > 10) System.out.println("Too many Command Line Arguments..."); else { System.out.println("Number of Arguments=" + args.length); for(i=0;i
4. Write a class, with main method, which declares floating point...
Shared By : dizy (College - SVICS ) Show/Hide Program
class prog4 { public static void main(String arg[]) { double a=123456789123.4; double b; b=a/0; int c; c=(int)a; System.out.println(a+" "+c +" " +b); } }
class P4 { public static void main(String args[]) { float f=123.45f; int i; System.out.println("Float Value: " + f); System.out.println("Float Value divide By 0: " + f/0); f=12345678; System.out.println("Int Value in Float Variable: " + f); i=(int)f; System.out.println("Float Value in Int Variable: " + i); } }
5. Write a class called Statistics, which has a static method called...
//Prog no 5... class state { public static void avg(double d[]) { double tot=0; for(int i=0;i
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class Statistics { public static void average(double d[]) { double avg=0; int i; for(i=0;i<7;i++) avg =" avg" tempreture = " + avg/7); } } class P5 extends Statistics { public static void main(String args[]) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); double tempr[][]=new double[4][7]; double wk[]=new double[7]; int i,j; String s; try{ for(i=0;i<4;i++) { for(j=0;j<7;j++) { System.out.println(" s="br.readLine();" i="0;i<4;i++)" j="0;j<7;j++)">
Shared By : bhautik sanghvi Show/Hide Program
class state { public static void avg(double a[]) { static double sumt=0; static double avg=0; for(int i=0;i
6. Define a class called Product, each product has a name, a product...
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class Product { int Pid; String Pname,Mname; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public Product() { this.Pid = 0; this.Pname = null; this.Mname = null; } public Product(int i,String s1,String s2) { this.Pid = i; this.Pname = s1; this.Mname = s2; } public void getDetail() throws IOException { String t; System.out.printf("Enter Product ID: "); t = br.readLine(); this.Pid=Integer.parseInt(t); System.out.printf("Enter Product Name: "); this.Pname = br.readLine(); System.out.printf("Enter Manufacturer Name: "); this.Mname = br.readLine(); } public void Display() { System.out.println("Product ID: " + this.Pid); System.out.println("Product Name: " + this.Pname); System.out.println("Manufacturer Name: " + this.Mname); } } class P6 extends Product { public static void main(String args[]) { Product p = new Product(); try{ p.getDetail(); } catch(IOException e) { System.out.println("Error occured while Reading from console"); } p.Display(); } }
Shared By : Keval Nagaria Show/Hide Program
class product { int p_code; String p_name; String m_name; product() { p_code=1; p_name="Pendrive"; m_name="Transcand"; } product(int i,String s1,String s2) { p_code=i; p_name=s1; m_name=s2; } void print_data() { System.out.println("Product code is="+p_code); System.out.println("Product name is="+p_name); System.out.println("Manufature name is="+m_name); } } class p6 { public static void main(String args[]) { product p=new product(); product p1=new product(2,"Card reader","iball"); p.print_data(); p1.print_data(); } }
7. Define a class called Cartesian Point, which has two instance ...
Shared By : Keval Nagaria Show/Hide Program
class product { int p_code; String p_name; String m_name; product() { p_code=1; p_name="Pendrive"; m_name="Transcand"; } product(int i,String s1,String s2) { p_code=i; p_name=s1; m_name=s2; } void print_data() { System.out.println("Product code is="+p_code); System.out.println("Product name is="+p_name); System.out.println("Manufature name is="+m_name); } } class p6 { public static void main(String args[]) { product p=new product(); product p1=new product(2,"Card reader","iball"); p.print_data(); p1.print_data(); } }
/*7. Define a class called Cartesian Point, which has two instance variables, x and y. Provide the methods get X() and get Y() to return the values of the x and y values respectively, a method called move() which would take two integers as parameters and change the values of x and y respectively, a method called display() which would display the current values of x and y. Now overload the method move() to work with single parameter, which would set both x and y to the same values, . Provide constructors with two parameters and overload to work with one parameter as well. Now define a class called Test Cartesian Point, with the main method to test the various methods in the Cartesian Point class. */ import java.lang.*; class CP { int x,y; public CP(int x,int y) { this.x=x; this.y=y; } public CP(int a) { x=y=a; } int getx() { return x; } int gety() { return y; } void move(int x,int y) { this.x=x; this.y=y; } void display() { System.out.println("Current value of x : "+getx()); System.out.println("Current value of y : "+gety()); } void move(int a) { x=y=a; } } class p7 { public static void main(String arg[]) { CP cp1 = new CP(5,25); System.out.println("\n\nDefault Values with "+5+" And "+25); cp1.display(); System.out.println("After Function move with two arguments is called,"); cp1.move(11,25); cp1.display(); System.out.println("After Function move with one arguments is called,"); cp1.move(95); cp1.display(); System.out.println("-------------------------------------"); CP cp2 = new CP(15); System.out.println("\n\nDefault Values with"+15); cp2.display(); System.out.println("After Function move with two arguments is called,"); cp2.move(35,65); cp2.display(); System.out.println("After Function move with one arguments is called,"); cp2.move(105); cp2.display(); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class CartesianPoint { private int x,y; public CartesianPoint(int a) { x=y=a; } public CartesianPoint(int a,int b) { x=a; y=b; } public int getX() { return x; } public int getY() { return y; } public void move(int a,int b) { x=a; y=b; } public void move(int a) { x=y=a; } public void Display() { System.out.printf("(" + x + "," + y + ")"); } } class P7 { public static void main(String args[]) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); CartesianPoint c=new CartesianPoint(0,0); int ch=5,a=0,b=0; do { do { System.out.println("\n1. Get value of X"); System.out.println("2. Get value of Y"); System.out.println("3. Change value of X and Y"); System.out.println("4. Display the Point"); System.out.println("5. Exit"); System.out.println("Enter Your Chopice: "); try { ch = Integer.parseInt(br.readLine()); } catch(IOException e) { System.out.println("Error occurs while taking Input"); } }while(ch<1>5); switch(ch) { case 1: System.out.println("Value of X is " + c.getX()); break; case 2: System.out.println("Value of Y is " + c.getY()); break; case 3: System.out.println("Enter 2 Points"); try { a = Integer.parseInt(br.readLine()); b = Integer.parseInt(br.readLine()); } catch(IOException e) { System.out.println("Error occurs while taking Input"); break; } if(a == b) c.move(a); else c.move(a,b); System.out.println("Value of X and Y Point is changed..."); break; case 4: System.out.printf("Point is "); c.Display(); } }while(ch != 5); } }
8. Define a class called Triangle, which has constructor with three...
Shared By : Jani Anand (SRIMCA - BARDOLI) Show/Hide Program
download this prog in one file-=>link::http://www.megaupload.com/?d=KJ7YUYU7 OR | | v Package MyPackage ------------------------------------------------------------------------------ package MyPack; public class CP { public float x,y; public CP(float x,float y) { this.x=x; this.y=y; } public CP(float a) { x=y=a; } public float getx() { return x; } public float gety() { return y; } public void move(float x,float y) { this.x=x; this.y=y; } public void display() { System.out.println("Current value of x : "+getx()); System.out.println("Current value of y : "+gety()); } public void move(float a) { x=y=a; } } --------------------------------------------------------------------------------- package MyPack; import MyPack.CP; import java.lang.Math; import java.util.Scanner; import java.lang.Double; public class Rectangle{ public CP a,b,c,d; public Rectangle(CP a,CP b,CP c,CP d){ this.a=a; this.b=b; this.c=c; this.d=d; System.out.println("Rectangle is Created..."); } public void Area(){ float Area; Area = ((a.x*(b.y - c.y)) + (b.x*(c.y-a.y)) + ((c.x*(a.y-b.y)))); System.out.println("The area of Triangle is "+Math.abs(Area)); } public void Display(){ System.out.println("\t\tA("+a.x+","+a.y+") # \t\t\t # B("+b.x+","+b.y+")\n\n\n\n"); System.out.println("\t\tD("+d.x+","+d.y+") # \t\t\t # C("+c.x+","+c.y+")"); } public void move(float x,float y){ float new_x=x-a.x,new_y=y-a.y; a.x=x; a.y=y; b.x=b.x+new_x; b.y=b.y+new_y; c.x=c.x+new_x; c.y=c.y+new_y; d.x=d.x+new_x; d.x=d.y+new_y; } public void rotate(CP A,float a){ double a1 = (A.x*Math.cos(a))-(A.y*Math.sin(a)); double b2 =(A.x*Math.sin(a))+(A.y*Math.cos(a)); A.x = (float) a1; A.y = (float) b2; } } ----------------------------------------------------------------------------------------------------- package MyPack; import MyPack.CP; import java.lang.Math; import java.util.Scanner; import java.lang.Double; public class Triangle{ public CP a,b,c; public Triangle(CP a,CP b,CP c){ this.a=a; this.b=b; this.c=c; System.out.println("Triangle is created.."); } public void Area(){ float Area; Area = ((a.x*(b.y - c.y)) + (b.x*(c.y-a.y)) + ((c.x*(a.y-b.y))))/2; System.out.println("The area of Triangle is "+Math.abs(Area)); } public void Display(){ System.out.println("\t\t\t\t # A("+a.x+","+a.y+")\n\n\n\n"); System.out.println("\t\tB("+b.x+","+b.y+") #\t\t # C("+c.x+","+c.y+")"); /*System.out.println("Point A = "+a.x+" : "+a.y); System.out.println("Point B = "+b.x+" : "+b.y); System.out.println("Point C = "+c.x+" : "+c.y);*/ } public void move(float x,float y){ float new_x=x-a.x,new_y=y-a.y; a.x=x; a.y=y; b.x=b.x+new_x; b.y=b.y+new_y; c.x=c.x+new_x; c.y=c.y+new_y; } public void rotate(CP A,float a){ double a1 = (A.x*Math.cos(a))-(A.y*Math.sin(a)); double b2 =(A.x*Math.sin(a))+(A.y*Math.cos(a)); A.x = (float) a1; A.y = (float) b2; } } ---------------------------------------------------------------------------------------------------- Package is Over ---------------------------------------------------------------------------------------------------- Main method in class p8 ---------------------------------------------------------------------------------------------------- /*Define a class called Triangle, which has constructor with three parameters, which are of type Cartesian Point, defined in the exercise 7. Provide methods to find the area and the perimeter of the Triangle, a method display() to display the three Cartesian Points separated by ':' character, a method move() to move the first Cartesian Point to the specified x, y location, the move should take care of relatively moving the other points as well, a method called rotate, which takes two arguments, one is the Cartesian Point and other is the angle in clockwise direction. Overload the move method to work with Cartesian Point as a parameter. Now define a class called Test Triangle to test the various methods defined in the Triangle class. Similarly also define a class called Rectangle which has four Cartesian Point. Triangle's Points a b c Rectangle a b c d */ import MyPack.CP; import MyPack.Triangle; import MyPack.Rectangle; import java.util.Scanner; class p8{ public static void main(String args[]){ Scanner s = new Scanner(System.in); float x,y; int n=0; while(true){ System.out.println("1. Triangle"); System.out.println("2. Rectangle"); System.out.println("3. Exit"); System.out.print("Enter your choice..."); n=s.nextInt(); switch(n){ case 1 : System.out.println("Create a Triangle. " ); System.out.print("Enter x for point A "); x=s.nextFloat(); System.out.print("Enter y for point A "); y=s.nextFloat(); CP A = new CP(x,y); System.out.print("Enter x for point B "); x=s.nextFloat(); System.out.print("Enter y for point B "); y=s.nextFloat(); CP B = new CP(x,y); System.out.print("Enter x for point C "); x=s.nextFloat(); System.out.print("Enter y for point C "); y=s.nextFloat(); CP C = new CP(x,y); Triangle T = new Triangle(A,B,C); while(true){ System.out.println("-------------------MENU-----------------------"); System.out.println("1. Display Current Points."); System.out.println("2. Calculate The Area." ); System.out.println("3. Move The Triangle."); System.out.println("4. Rotate The Triangle "); System.out.println("5. Exit"); System.out.println("Enter Your Choice..."); n=s.nextInt(); switch(n){ case 1 : System.out.println("The Current Points Are..."); T.Display(); break; case 2 : T.Area(); break; case 3 : System.out.print("Enter new x :"); x=s.nextFloat(); System.out.print("Enter new y :"); y=s.nextFloat(); T.move(x,y); System.out.println("Triangle has been replaced.."); break; case 4 : float degree; System.out.print("Enter a degree to rotate : "); degree=s.nextFloat(); T.rotate(T.a,degree); T.rotate(T.b,degree); T.rotate(T.c,degree); System.out.println("The triangle has been rotated with "+degree+" degree"); break; case 5 : System.exit(0); default : System.out.println("Invalid Input..."); } } case 2 : System.out.println("Create a Reactangle. " ); System.out.print("Enter x for point A "); x=s.nextFloat(); System.out.print("Enter y for point A and B"); y=s.nextFloat(); CP A2 = new CP(x,y); System.out.print("Enter x for point B "); x=s.nextFloat(); CP B2 = new CP(x,A2.y); System.out.print("Enter y for point C "); y=s.nextFloat(); CP C2 = new CP(B2.x,y); CP D2 = new CP(A2.x,C2.y); Rectangle R = new Rectangle(A2,B2,C2,D2); while(true){ System.out.println("-------------------MENU-----------------------"); System.out.println("1. Display Current Points."); System.out.println("2. Calculate The Area." ); System.out.println("3. Move The Rectangle."); System.out.println("4. Rotate The Rectangle "); System.out.println("5. Exit"); System.out.println("Enter Your Choice..."); n=s.nextInt(); switch(n){ case 1 : System.out.println("The Current Points Are..."); R.Display(); break; case 2 : R.Area(); break; case 3 : System.out.print("Enter new x :"); x=s.nextFloat(); System.out.print("Enter new y :"); y=s.nextFloat(); R.move(x,y); System.out.println("Rectangle has been replaced.."); break; case 4 : float degree; System.out.print("Enter a degree to rotate : "); degree=s.nextFloat(); R.rotate(R.a,degree); R.rotate(R.b,degree); R.rotate(R.c,degree); R.rotate(R.d,degree); System.out.println("The rectangle has been rotated with "+degree+" degree"); break; case 5 : System.exit(0); default : System.out.println("Invalid Input..."); } } case 3 : System.exit(0); default : System.out.println("Invalid Input..."); } } } } -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Thank you ::: Jani Anand.(09MCA11-SRIMCA) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
9. Override the to String, equals and the hash Code methods of the...
Shared By : Divyang s Panchal
(Asst Professor College - D.L.Patel institute of mng & tec ,MCA College )
Show/Hide Program
(Asst Professor College - D.L.Patel institute of mng & tec ,MCA College )
Show/Hide Program
class Cartesianpoint { int x,y; Cartesianpoint(int x,int y) { this.x=x; this.y=y; } } public class p9 { Cartesianpoint a,b,c; p9(Cartesianpoint a,Cartesianpoint b,Cartesianpoint c) { this.a=a; this.b=b; this.c=c; } void display() { System.out.print(toString()); } public String toString() { return("For This Instance A(x,y) is ("+a.x+","+a.y+")\n For point B(x,y) is ("+b.x+","+b.y+")\n For point C(x,y) is ("+c.x+","+c.y+")\n\n"); } public boolean equals(p9 obj) { if(this.a.x==obj.a.x && this.a.y==obj.a.y) { if(this.b.x==obj.b.x && this.b.y==obj.b.y) { if(this.c.x==obj.c.x && this.c.y==obj.c.y) { return(true); } } } return(false); } public int hashCode() { int i=Integer.parseInt(""+a.x+""+a.y+""+""+b.x+""+b.y+""+""+c.x+""+c.y+""); return(i); } public static void main(String[] arg) { Cartesianpoint a,b,c; a=new Cartesianpoint(1,1); b=new Cartesianpoint(3,5); c=new Cartesianpoint(5,1); p9 p,q; p=new p9(a,b,c); a=new Cartesianpoint(1,1); b=new Cartesianpoint(3,5); c=new Cartesianpoint(5,1); q=new p9(a,b,c); p.display(); q.display(); System.out.print(p.equals(q)); System.out.println(p.hashCode()); System.out.println(q.hashCode()); } }
10. Define an abstract class called Polygon. Provide a constructor ...
Shared By : Jani Anand (SRIMCA - Bardoli ) Show/Hide Program
download link: http://www.megaupload.com/?d=CEW9U3ZC
No comments:
Post a Comment