Java Programs
Java Programs
class Room { float length,breadth; +void getdata(float a, float b) { length =a; breadth=b; } } class RoomArea { public static void main(String args[]) { float area; Room room1=new Room(); room1.getdata(14,10); area=room1.length*room1.breadth; System.out.println("Area : -\n"+area); } }
Output: Enter an Integer value: 10 Enter a String value: 50 The Integer value is 10 The String value is 50
Output: Enter the Number: 10 This Number is Even Enter the Number: 3 This Number is Odd
class Nested_ifelse { public static void main(String[] args) { int a=325,b=712,c=478; System.out.println("Largest Value is: -\n"); if(a>b) { if(a>c) { System.out.println(a); } else { System.out.println(c); } } else if(c>b) { System.out.println(c); } else { System.out.println(b); } } }
Output: 111 Honours 222 Ist Divison 333 Fail 444 IInd Divison
class cityguide { public static void main(String args[]) { char choice; System.out.println("Select your choice"); System.out.println("M-> Madras"); System.out.println("B-> Bombay"); System.out.println("C-> Cacutta"); System.out.println("Choice --->"); System.out.flush(); try { switch(choice=(char)System.in.read()) { case 'M': case 'm': System.out.println("Madras: Booklet 5"); break; case 'B': case 'b': System.out.println("Bombay: Booklet 9"); break; case 'C': case 'c': System.out.println("Calcutta: Booklet 15"); break; default: System.out.println("Invalid Choice (IC)"); } } catch(Exception e) { System.err.println("I/O Error"); } } }
Output: M-> Madras B-> Bombay C-> Cacutta Choice ---> M Madras: Booklet 5
Output: Enter a String: MANPREET KAUR You have entered.. MANPREET KAUR
*A
class Rectangle { int length,width; void getdata(int x,int y) { length=x; width=y; } int rectarea() { int area=length*width; return(area); } } class rectarea1 { public static void main(String args[]) { int area1,area2; Rectangle rect1=new Rectangle(); Rectangle rect2=new Rectangle(); rect1.length=15; rect1.width=10; area1=rect1.length*rect1.width; rect2.getdata(20,12); area2=rect2.rectarea(); System.out.println("Area1: -\n"+area1); System.out.println("Area2: -\n"+area2); } }
Output: B: 10
Output: Dimension # 1 Array[0][0]=10 Array[0][1]=-1 Array[0][1]=28 Array[0][1]=13 Array[0][1]=44 Dimension # 2 Array[1][0]=5 Array[1][1]=36 Array[1][2]=97 Array[1][3]=-18 Array[1][4]=11
putmarks(); putwt(); System.out.println("Total Score: -\n"+total); } } class hybrid { public static void main(String args[]) { Results student1=new Results(); student1.getnumber(1234); student1.getmarks(27.5F,33.0F); student1.display(); } }
Output: part 1: 27.5 part 2: 33.0 Sports WT: 6.0 Total score: 66.5
OUTPUT:-
Output: I: - 1 I: - 2 I: - 3 I: - 4 I: - 5 I: - 6 I: - 7 I: - 8 I: - 9 I: - 10 Sum: - 1 Sum: - 1.5 Sum: - 1.83333 Sum: - 2.08333 Sum: - 2.2833 Sum: - 2.45 Sum: - 2.59286 Sum: - 2.71786 Sum: - 2.82897 Sum: - 2.92897
HelloJava
Applet started
Output: % java TestThreads Runnable running Runnable running Runnable running Runnable running Runnable running
Output: Outer x is 7 Inner class ref is MyOuter$MyInner@113708 Outer class ref is MyOuter@33f1d7
package package1; public class classA { public void displayA() { System.out.println(Class A); } } //Now save it to destination with folder name package1 import package1.classA; class PackageTest { public static void main(String args[]) { classA objectA=new classA(); object.displayA(); } }
Output: Class A
Output: false true true And also produces an empty file in your current directory. If you run the code a second time you get the output true false true