Oct

19

import java.io.*;
class Ex{
public static void main(String args[]){
Console c=System.console();
String snum1=c.readLine(“1st No: “);
String snum2=c.readLine(“2nd No: “);
try{
int n1=Integer.parseInt(snum1);
int n2=Integer.parseInt(snum2);
System.out.println(n1+n2);
System.out.println(n1-n2);
System.out.println(n1*n2);
System.out.println(n1/n2);
}
catch(ArithmeticException e){
System.out.println(e);
}
finally{
System.out.println(“Finally Called”);
}
System.out.println(“Exit from main”);
}
}

————————————————————————————–

import java.io.*;
class Ex1{
public static void main(String args[]){
Console c=System.console();
String snum1=c.readLine(“1st No: “);
String snum2=c.readLine(“2nd No: “);
try{
int n1=Integer.parseInt(snum1);
int n2=Integer.parseInt(snum2);
System.out.println(n1+n2);
System.out.println(n1-n2);
System.out.println(n1*n2);
System.out.println(n1/n2);
}
catch(Exception e){  // it will generate compile error
System.out.println(e);
}
catch(ArithmeticException e){
System.out.println(e);
}
catch(NullPointerException e){
System.out.println(e);
}

finally{
System.out.println(“Finally Called”);
}
System.out.println(“Exit from main”);
}
}

————————————————————————————-

import java.io.*;
class Ex2{
public static void main(String args[]){
Console c=System.console();
String snum1=c.readLine(“1st No: “);
String snum2=c.readLine(“2nd No: “);
int n1=Integer.parseInt(snum1);
int n2=Integer.parseInt(snum2);
if(n1<=5){
//throw new NumberFormatException(“Explict Thrown <=5”);
try{
throw new InterruptedException(“Explict Intruupt”);
}catch(InterruptedException i){
}
}
System.out.println(n1+n2);
}
}

——————————————————————————————-

import java.io.*;
class Ex3{
public static void main(String args[])throws InterruptedException{
Console c=System.console();
String snum1=c.readLine(“1st No: “);
String snum2=c.readLine(“2nd No: “);
int n1=Integer.parseInt(snum1);
int n2=Integer.parseInt(snum2);
if(n1<=5){
//throw new NumberFormatException(“Explict Thrown <=5”);
throw new InterruptedException(“Explict Intruupt”);
}
System.out.println(n1+n2);
}
}

——————————————————————————————————

import java.io.*;
class Ex4{
public static void main(String args[])throws MyExcep{
System.out.println(“Thows Exception”);
int i=0;
if(i==0)
throw new MyExcep(“This exception is created by programmer”);
System.out.println(“Exit from main”);
}
}
class MyExcep extends Exception{
MyExcep(String s){
super(s);
}
}

——————————————————————————————————-

import java.io.*;
class Ex5{
public static void main(String args[]){
System.out.println(“Thows Exception”);
int i=0;
try{
if(i==0)
throw new MyExcep(“This exception is created by programmer”);
}catch(MyExcep e){
}
System.out.println(“Exit from main”);
}
}
class MyExcep extends Exception{
MyExcep(String s){
super(s);
}
}

—————————————————————————————————-

import java.io.*;
class Ex6{
public static void main(String args[]){
System.out.println(“Thows Exception”);
Ex6 e6=new Ex6();
try{
e6.m1();
}catch(MyExcep e){
}
System.out.println(“Exit from main”);
}
void m1()throws MyExcep{
int i=0;
if(i==0)
throw new MyExcep(“This exception is created by programmer”);

}
}
class MyExcep extends Exception{
MyExcep(String s){
super(s);
}
}

————————————————————————————————————-

import java.io.*;
class Ex7{
public static void main(String args[]){
}
void m1()throws MyExcep{
}
void m2() throws Exception{
}
void m3() throws NumberFormatException{
}
}
class Ex extends Ex7{
void m1() throws NumberFormatException{
}
void m3()throws NumberFormatException,NullPointerException{
}
}
class MyExcep extends Exception{
MyExcep(String s){
super(s);
}
}

 

 

Oct

17

class TestString2{
public static void main(String args[]){
String s1=”ABC”;
String s2=new String(“ABC”);
String s3=”abc”;
String s4=” AB C “;
String s5=”abcabcabc”;
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println(s4.equalsIgnoreCase(s3));
System.out.println(s1.length());
System.out.println(s4.length());
System.out.println(s4.trim().length());
System.out.println(s3.toUpperCase());
//s3=s3.toUpperCase();
System.out.println(s3);
System.out.println(s1.toLowerCase());
System.out.println(s3.charAt(1));
System.out.println(s3.indexOf(‘b’));
System.out.println(s5.indexOf(‘b’));
System.out.println(s5.lastIndexOf(‘b’));
System.out.println(s5.indexOf(‘b’,5));
System.out.println(s5.indexOf(“ab”));
System.out.println(s5.indexOf(“ab”,3));
System.out.println(s5.lastIndexOf(‘a’,3));
System.out.println(s5.lastIndexOf(‘a’,2));
System.out.println(s5.lastIndexOf(‘a’,7));
}
}

——————————————————————————————

class TestString1{

public static void main(String args[]){
char ch[]={‘a’,’b’,’c’,’d’};
String s1=new String(“ABC”);
String s2=”ABC”;
String s3=new String(“ABC”);
String s4=new String(s3);
String s5=new String();
String s6=new String(ch);
System.out.println(s1+” “+s2+” “+s3+” “+s4+” “+s5+” “+s6);
if(s1==s2){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
if(s1==s3){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
}
}

——————————————————————————————————-

class T1{
public static void main(String arg[]){
String s1=”ABC”;
String s3=”ABC”;
String s2=new String(“ABC”);
String s4=new String(s2);
if(s1==s3){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
if(s1==s2){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
if(s2==s4){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
if(s1.equals(s4)){
System.out.println(“SAME”);
}
else{
System.out.println(“NOT SAME”);
}
}
}

————————————————————————————————–

import java.io.*;

class StringTest1{
public static void main(String args[]){
Console c=System.console();
String st1=c.readLine();
String st2=st1.toUpperCase();
System.out.println(st2);
System.out.println(st1.toLowerCase());
System.out.println(“——————“);
for(int i=0;i<st1.length();i++){ System.out.println(st1.charAt(i)); } for(int i=st1.length()-1;i>=0;i–){
System.out.print(st1.charAt(i));
}
int ctr=0;
for(int i=0;i<st1.length();i++){
char ch=st1.charAt(i);
switch(ch){
case ‘a’:case ‘e’:case ‘i’:case ‘o’:case ‘u’:
case ‘A’:case ‘E’:case ‘I’:case ‘O’:case ‘U’:
System.out.print(“\n”+ch);
ctr++;
}
}
System.out.println(“\n”+ctr);
}
}

————————————————————————————————-

import java.io.*;

class StringTest2{
public static void main(String args[]){
Console c=System.console();
String st1=c.readLine();
System.out.println(st1.substring(3));//from 3rd index to length
System.out.println(st1.substring(3,5));// from 3rd index to 5-1index
}
}

—————————————————————————————————-

import java.io.*;

class StringTest3{
public static void main(String args[]){
Console c=System.console();
String st1=c.readLine(“Enter File Name”);
int i=st1.lastIndexOf(‘.’);
String s=st1.substring(i+1);
System.out.println(“File Type is ” + s);
}
}

Oct

09

Java Interface Examples

 

Filled Under: Java (Core)

Sep

23

Java Sample Code for observing File name, Class Name concept, Getting Input from console, main method etc.
Click to JAVA-1

Filled Under: Java (Core)

Sep

06

Aug

10

Campus Commune is a professional network rolled out by TCS for the academic community. It has been acknowledged and appreciated by the student community across India as an effective tool for collaboration. sharing, learning and exploration.

TCS Campus Commune has launched Code Vita (the biggest Coding Contest in India) on 22nd July,13. This contest shall be open to all the students( FY 14,15 and 16 and 17 batches) of your esteemed Institutions. Request you to kindly ask the students from FY 14,15,16 and 17 batch to subscribe to the portal. The process of launching Campus Commune has been attached below. Please share it with all the students at your Institute. The students registering on this portal from a TCS non accredited Institute are called Direct Candidates.

The contest will provide the candidates a competition platform on a national level to test their talents and they will witness a really enriching learning experience.

�The Registration and Team Formation process for Code Vita is open till 12th August,13.

The subscription to TCS Campus Commune is a prerequisite to register and participate in Code Vita. The subscription to Campus Commune is free of cost. Please see the attachment below which needs to be supplemented with the process of launching Campus Commune and sent across to the students.�

FOR MORE DETAILS CLICK THE FOLLOWING URLS:-

Codevita_Participation_Guidelines_For_Direct_Trainees

CodeVita_2013_Registration_and_Team_Formation

Jun

14

JDK1.6 DOWNLOAD

Mar

16

Click to Download Sample Code Day-5

Mar

09

School of Management Sciences, Varanasi with Waayoo.com will organize a workshop on “Web Application Development(RIA)” using Java Technology for MCA Students to match the present web development requirements. This workshop will cover all the advanced java technology topics like GWT, Spring, Hibernet, MVC etc. from 11.03.2013 to 16.03.2013.

SMS VARANASI WORKSHOP

 

 

 

For the complete content details of the workshop click here:-

Workshop Content Details

 

Prerequisite to do this workshop: Participants are adviced to develop some java programs using Eclipse IDE and revise JAVA OOPs concepts

Jan

31

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class HtmlDemo1 extends JPanel
implements ActionListener {
JLabel theLabel;
JTextArea htmlTextArea;

public HtmlDemo1() {
setLayout(null);
this.setSize(400,400);
String initialText = “<html>\n” +
“Color and font test:\n” +
“<ul>\n” +
“<li><font color=red>red</font>\n” +
“<li><font color=blue>blue</font>\n” +
“<li><font color=green>green</font>\n” +
“<li><font size=-2>small</font>\n” +
“<li><font size=+2>large</font>\n” +
“<li><i>italic</i>\n” +
“<li><b>bold</b>\n” +
“</ul>\n”;

htmlTextArea = new JTextArea(10, 20);
htmlTextArea.setText(initialText);
theLabel=new JLabel();
theLabel.setText(initialText);
theLabel.setBounds(20,20,200,200);
htmlTextArea.setBounds(250,20,200,200);
add(theLabel);
add(htmlTextArea);
JButton b=new JButton(“Change”);
b.setBounds(20,300,100,30);
add(b);
b.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {
theLabel.setText(htmlTextArea.getText());
}

public static void main(String[] args) {
HtmlDemo1 hd=new HtmlDemo1();
Frame f=new Frame();
f.add(hd);
f.setSize(400,400);
f.setVisible(true);
}
}

Jan

31

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class HtmlDemo extends JPanel
implements ActionListener {
JLabel theLabel;
JTextArea htmlTextArea;

public HtmlDemo() {
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

String initialText = “<html>\n” +
“Color and font test:\n” +
“<ul>\n” +
“<li><font color=red>red</font>\n” +
“<li><font color=blue>blue</font>\n” +
“<li><font color=green>green</font>\n” +
“<li><font size=-2>small</font>\n” +
“<li><font size=+2>large</font>\n” +
“<li><i>italic</i>\n” +
“<li><b>bold</b>\n” +
“</ul>\n”;

htmlTextArea = new JTextArea(10, 20);
htmlTextArea.setText(initialText);
JScrollPane scrollPane = new JScrollPane

(htmlTextArea);

JButton changeTheLabel = new JButton(“Change the

label”);
changeTheLabel.setMnemonic(KeyEvent.VK_C);
changeTheLabel.setAlignmentX

(Component.CENTER_ALIGNMENT);
changeTheLabel.addActionListener(this);

theLabel = new JLabel(initialText) {
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public Dimension getMinimumSize() {
return new Dimension(200, 200);
}
public Dimension getMaximumSize() {
return new Dimension(200, 200);
}
};
theLabel.setVerticalAlignment(SwingConstants.CENTER);
theLabel.setHorizontalAlignment

(SwingConstants.CENTER);

JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel,

BoxLayout.PAGE_AXIS));
leftPanel.setBorder

(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(
“Edit the HTML, then click the button”),
BorderFactory.createEmptyBorder

(10,10,10,10)));
leftPanel.add(scrollPane);
leftPanel.add(Box.createRigidArea(new Dimension

(0,10)));
leftPanel.add(changeTheLabel);

JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel,

BoxLayout.PAGE_AXIS));
rightPanel.setBorder

(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(“A

label with HTML”),
BorderFactory.createEmptyBorder

(10,10,10,10)));
rightPanel.add(theLabel);

setBorder(BorderFactory.createEmptyBorder

(10,10,10,10));
add(leftPanel);
add(Box.createRigidArea(new Dimension(10,0)));
add(rightPanel);
}

//React to the user pushing the Change button.
public void actionPerformed(ActionEvent e) {
theLabel.setText(htmlTextArea.getText());
}

/**
* Create the GUI and show it.  For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame(“HtmlDemo”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add content to the window.
frame.add(new HtmlDemo());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application’s GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal’s use of bold fonts
UIManager.put(“swing.boldMetal”,

Boolean.FALSE);
createAndShowGUI();
}
});
}
}
For more details see this page: http://docs.oracle.com/javase/tutorial/uiswing/components/html.html

Jan

17

Welcome to Ram Gopal Gupta Blog, to share our knowledge..