Download Java Programs Examples new 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java New Demo progs 2014 by Dr. M V S Peri Sastry
If no arguments are there, In Eclipse Run As -> Java Application
But if you have to input arguments,
In Eclipse Run As -> Run Configurations -> arguments tab type-in args -> Run
1)
public class ja1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello Rahul Welcome to Java");
}
}
------------------------------------------------------------------------------2)
public class ABCD {
public static void main ( String[] Args )
{
int n=3;
System.out.println ("num=" + n + " n cube=" + n*n*n );
}
}
----------------------------------------------------------------------3)
import javax.swing.JOptionPane;
public class peri_swing_addition {
public static void main(String args[] ) {
// TODO Auto-generated method stub
String fno, sno;
int ifno, isno, isum;
fno = JOptionPane.showInputDialog( "Peri Eclipse Enter first integer:");
sno = JOptionPane.showInputDialog( "Peri Eclipse Enter second integer:");
ifno = Integer.parseInt(fno);
isno = Integer.parseInt(sno);
isum= ifno + isno;
JOptionPane.showMessageDialog(
null, "The Sum Is : " + isum,
"RESULT PANE", JOptionPane.PLAIN_MESSAGE );
System.exit(0);
}
}
-------------------------------------------------------------------4)
public class ja1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello Rahul Welcome to Java");
}
}
-----------------------------------------------------------------------------------------5)
public class helloWithArgszero {
// hello program with Args[0]
// in eclipse run as, configurations, arguments tab, SASTRYGAARU
public static void main(String[] args) {
// TODO Auto-generated method stub
if (args[0].equals("")) System.out.println("Hello Peri Java ");
else
System.out.println("Hello Peri Java " + args[0]);
}
}
---------------------------------------------------------------------------------------------------6)
public class peri1withargsnum {
//
peri1 num program with args [] and try catch
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 3;
try{
int n2= Integer.parseInt(args[0]);
System.out.println("num=" + n2 + " n2 square=" + n2*n2 );
}
catch (
NumberFormatException nfe){
System.out.println("input Number in error Sir " + nfe);
}
finally {
System.out.println("num=" + n + " n square=" + n*n );
}
}
}
----------------------------------------------------------------------7)
public class peri1withtrycatch {
//
public static void main(String[] args) {
peri1 num program with args [] and try catch
// TODO Auto-generated method stub
int n = 3;
try{
int n2= Integer.parseInt(args[0]);
System.out.println("num=" + n2 + " n2 square=" + n2*n2 );
}
catch (
Exception nfe){
System.out.println("input Number in error " + nfe);
}
System.out.println("num=" + n + " n square=" + n*n );
}
}
------------------------------------------------------------------------8) java program example: use of Scanner class from util package and
System.out.printf statement. Use only with jdk1.7 USE CMD AND COMPILE AND
TEST (NOT Eclipse)
import java.util.Scanner;
public class Additionscan {
public static void main(String[] args) {
// TODO Auto-generated method stub
//
String fno, sno;
int
ifno, isno, isum;
Scanner input = new Scanner( System.in );
System.out.println( "Enter first integer:");
ifno = input.nextInt();
System.out.println( "Enter second integer:");
isno = input.nextInt();
//
ifno = Integer.parseInt(fno);
//
isno = Integer.parseInt(sno);
//
isum= ifno + isno;
isum= ifno + isno;
System.out.printf("The Sum Is : %d\n" , isum );
input.close();
//
System.exit(0);
}
}
---------------------Steps for Compilation and testing of above java program Additionscan
--------------------------------------------in cmd do as below
-----------------------------------Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Peri Sastry>cd\
C:\>cd Program Files
C:\Program Files>cd java
C:\Program Files\Java>cd jdk1.7.0_45
C:\Program Files\Java\jdk1.7.0_45>cd bin
C:\Program Files\Java\jdk1.7.0_45\bin>javac c:\jmvsp\Additionscan.java
C:\Program Files\Java\jdk1.7.0_45\bin>java -cp c:\jmvsp Additionscan
Enter first integer:
12
Enter second integer:
23
The Sum Is : 35
C:\Program Files\Java\jdk1.7.0_45\bin>
Related documents