/* Simple Java Program for the implementation of ProgressBar, you can also refer to the progressbar code given beow and implement it in your programs */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ProgressBarr extends JFrame implements ActionListener
{
JButton jb;
JProgressBar jpb1;
int value =0;
ProgressBarr()
{
Container contentPane=getContentPane();
contentPane.setLayout(new FlowLayout());
jb = new JButton("Click");
contentPane.add(jb);
jb.addActionListener(this);
jpb1 = new JProgressBar(0,1000);
contentPane.add(jpb1);
jpb1.setStringPainted(true);
}
public static void main(String args[])
{
ProgressBarr pb1 = new ProgressBarr();
pb1.setSize(500,500);
pb1.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
while(value< =1000)
{
value = value+10;
jpb1.setValue(value);
}
}
}
Labels: Advanced Java, Frames, Swings
/* Java Program :
Components included in this session : Buttons, Labels, Text Fileds, Combo Box and Menu Bar.
The Given Program that accepts a login name and password from the user. The application contains two buttons. The 'Log In 'button when clicked, checks the user name and password entered in the text box, and the ‘cancel’ button will terminate the application. The user is excepted to enter both username and password before clicking the Log In button.
If the correct username and password is entered by the user, then a new frame is opened where product rate and quantity are displayed. The product is a combo box which has 5 options :
Product 1
Product 2
Product 3
Product 4
Product 5
On selecting product 1 the rate displayed on the text field will be 150, similarly product 2, product 3, product 4 and product5 will display rate as 250, 350, 450 and 550 respectively. The rate text field will be Editable false i.e. the contents of rate box cannot be edited. The select the respective Quantity for the product. On clicking the enter button again a new frame will be opened where all the components are editable false and a new text field is displayed where the total amount is displayed. In addition to this a menu bar called ‘file’ is also displayed on the container which has two menu items new and exit, on clicking new the log in page is opened and on clicking exit the application is terminated.
The username and password in the given program
username: javapgms
password: lionel
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Admin extends JFrame implements ActionListener
{
JTextField jft1,jft2;
JButton b1,b2;
Container cont;
Admin()
{
cont = getContentPane();
cont.setLayout(null);
JLabel jl1=new JLabel("Username:");
cont.add(jl1);
jl1.setBounds(20,30,100,20);
jft1=new JTextField();
cont.add(jft1);
jft1.setBounds(100,30,200,30);
JLabel jl2=new JLabel("Password:");
cont.add(jl2);
jl2.setBounds(20,80,100,20);
jft2=new JTextField();
cont.add(jft2);
jft2.setBounds(100,80,200,30);
b1=new JButton("Sign In");
cont.add(b1);
b1.setBounds(75,150,100,30);
b1.addActionListener(this);
b2=new JButton("Cancel");
cont.add(b2);
b2.setBounds(200,150,100,30);
b2.addActionListener(this);
}
public static void main(String args[])
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == b2)
System.exit(0);
String user=jft1.getText();
String password=jft2.getText();
if(user.equals("javapgms")&&password.equals("lionel"))
{
Admin2 jf=new Admin2();
jf.setSize(400,300);
jf.setVisible(true);
}
else
JOptionPane.showMessageDialog(cont,"Invalid Details","Lionel's Message",
JOptionPane.ERROR_MESSAGE);
}
}
class Admin2 extends JFrame implements ActionListener,ItemListener
{
JComboBox jc;
JTextField ft1,ft2,ft3;
JButton B1;
String z,t;
Admin2()
{
Container cont=getContentPane();
cont.setLayout(null);
JLabel l1=new JLabel("Product:");
cont.add(l1);
l1.setBounds(20,30,100,20);
jc=new JComboBox();
jc.addItem("product1");
jc.addItem("product2");
jc.addItem("product3");
jc.addItem("product4");
jc.addItem("product5");
cont.add(jc);
jc.setBounds(80,30,200,20);
jc.addItemListener(this);
JLabel l2=new JLabel("Rate:");
cont.add(l2);
l2.setBounds(20,60,50,20);
ft1=new JTextField();
cont.add(ft1);
ft1.setEditable(false);
ft1.setBounds(80,60,50,20);
JLabel l3=new JLabel("Quantity:");
cont.add(l3);
l3.setBounds(20,90,100,20);
ft2=new JTextField();
cont.add(ft2);
ft2.setBounds(80,90,50,20);
B1=new JButton("Enter");
cont.add(B1);
B1.setBounds(150,150,100,30);
B1.addActionListener(this);
}
public void actionPerformed(ActionEvent ne)
{
String m=ft1.getText();
int a=Integer.parseInt(m);
String n=ft2.getText();
int b=Integer.parseInt(n);
z=jc.getSelectedItem().toString();
int tot=a*b;
t = String.valueOf(tot);
Admin3 admn3=new Admin3(z,m,n,t);
admn3.setSize(400,400);
admn3.setVisible(true);
}
public void itemStateChanged(ItemEvent me)
{
z=jc.getSelectedItem().toString();
if(z.equals("product1"))
ft1.setText("150");
else if(z.equals("product2"))
ft1.setText("250");
else if(z.equals("product3"))
ft1.setText("350");
else if(z.equals("product4"))
ft1.setText("450");
else if(z.equals("product5"))
ft1.setText("550");
}
}
class Admin3 extends JFrame implements ActionListener
{
JTextField ftf1,ftf2,ftf3,ftf0;
String x,y,z,q;
int l,m,n,tot1;
JMenuBar bar= new JMenuBar();
JMenuItem it1,it2;
Admin3(String l,String m,String n,String t)
{
Container cont=getContentPane();
cont.setLayout(null);
JMenu file = new JMenu ("File");
it1 = new JMenuItem("New");
it2 = new JMenuItem("Exit");
file.add(it1);
file.add(it2);
it1.addActionListener(this);
it2.addActionListener(this);
bar.add(file);
setJMenuBar(bar);
JLabel lb1=new JLabel("Product:");
cont.add(lb1);
lb1.setBounds(20,30,100,20);
ftf0=new JTextField();
ftf0.setEditable(false);
ftf0.setBounds(80,30,200,20);
cont.add(ftf0);
ftf0.setText(l);
JLabel lb2=new JLabel("Rate:");
cont.add(lb2);
lb2.setBounds(20,60,50,20);
ftf1=new JTextField();
cont.add(ftf1);
ftf1.setEditable(false);
ftf1.setBounds(80,60,50,20);
ftf1.setText(m);
JLabel lb3=new JLabel("Quantity:");
cont.add(lb3);
lb3.setBounds(20,90,100,20);
ftf2=new JTextField();
cont.add(ftf2);
ftf2.setEditable(false);
ftf2.setBounds(80,90,50,20);
ftf2.setText(n);
JLabel lb4=new JLabel("Total Amt");
cont.add(lb4);
lb4.setBounds(20,120,100,20);
ftf3=new JTextField();
cont.add(ftf3);
ftf3.setEditable(false);
ftf3.setBounds(80,120,100,20);
ftf3.setText(t);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==it1)
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}
if(ae.getSource()==it2)
System.exit(0);
}
}
/* Kindly leave your useful comments or suggesstin if any. */
Labels: Advanced Java, Frames, Swings
/* Simple Java program to find whether a number is an armstrong number or not */
class Armstrong
{
int sumcube(int x)
{
int dig,sum = 0;
while(x >= 1)
{
dig = x % 10;
sum = sum + dig * dig * dig;
x = x / 10;
}
return(sum);
}
void display(int n)
{
if(sumcube (n)==n)
System.out.println(n + " is Armstrong Number");
else
System.out.println(n + " is not Armstrong Number");
}
void disp()
{
int x = 0,y = 0;
for(int k = x;k <= y;k++)
if(sumcube (k)==k)
System.out.println(k);
}
}
/* Java Program To Display A simple tree with root node as ENGINEERING and the courses under it as sub nodes or leaves. The program makes use of Frames. The Normal way of defining a tree node is 'DefaultMutableTreeNode root_name =new DefaultMutableTreeNode (argument_name)'.*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Tree1 extends JFrame
{
public static void main(String args[])
{
Tree1 frame = new Tree1(" A Tree");
frame.setSize(200,200);
frame.setVisible(true);
}
public Tree1(String title)
{
setTitle(title);
DefaultMutableTreeNode root=new DefaultMutableTreeNode ("Engineering");
DefaultMutableTreeNode style=new DefaultMutableTreeNode ("Information Technology");
root.add(style);
style=new DefaultMutableTreeNode("Electronics");
root.add(style);
style=new DefaultMutableTreeNode ("Computer Science");
root.add(style);
style=new DefaultMutableTreeNode ("Mechanical");
root.add(style);
style=new DefaultMutableTreeNode ("Electrical");
root.add(style);
style=new DefaultMutableTreeNode ("Sound");
root.add(style);
JTree jt=new JTree(root);
Container contentPane=getContentPane();
contentPane.add(new JScrollPane(jt));
}
}
Labels: Advanced Java, Frames, Swings
/* Java Program To Make Use of grid layout to displaying components onto the applet. The Components Includes Buttons Text Boxes,Check Boxes. Done using Applets. */
import java.awt.*;
import java.applet.*;
public class GridApp extends Applet
{
TextArea ta;
TextField tf;
Button b1,b2;
CheckboxGroup cbg;
Checkbox cb1,cb2,cb3,cb4;
GridBagLayout gb;
GridBagConstraints gbc;
public void init()
{
gb = new GridBagLayout();
setLayout(gb);
gbc = new GridBagConstraints();
ta = new TextArea("TextArea",5,10);
tf = new TextField("Enter your Name:");
b1 = new Button ("TextArea");
b2 = new Button ("TextField");
cbg = new CheckboxGroup();
cb1 = new Checkbox("Bold",cbg, false);
cb2 = new Checkbox("Italic",cbg,false);
cb3 = new Checkbox("Plain",cbg,false);
cb4 = new Checkbox("Bold/Italic",cbg,true);
gbc.fill = GridBagConstraints.BOTH;
addComponent(ta,0,0,4,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(b1,0,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(b2,0,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(cb1,2,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(cb2,2,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(cb3,1,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(cb4,3,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(tf,4,0,1,3);
}
public void addComponent(Component c, int row, int col, int nrow, int ncol)
{
gbc.gridx = col;
gbc.gridy = row;
gbc.gridwidth = ncol;
gbc.gridheight = nrow;
gb.setConstraints(c,gbc);
add(c);
}
}
//< applet code =GridApp width = 500 height = 500>< /applet>
/* Java Program On Flow Layout. The Components Inclues Buttons, TextField, label. Done using Applets.*/
import java.awt.*;
import java.applet.*;
public class FlowApp extends Applet
{
public void init()
{
TextField t1 = new TextField(20);
Label l1 = new Label ("Name");
Button b = new Button("OK");
TextField t2 = new TextField(20);
Label l2 = new Label ("Name");
Button b1 = new Button("OK");
add(l1);
add(t1);
add(b);
add(l2);
add(t2);
add(b1);
}}
//< applet code =FlowApp width = 500 height = 500>< /applet>
/* Java Program To Display Button On An Applet . Done using Applets.*/
import java.awt.*;
import java.applet.*;
public class BorderApp extends Applet
{
public void init()
{
setLayout(new BorderLayout());
Button b1 = new Button("EAST");
Button b2 = new Button("WEST");
Button b3 = new Button("NORTH");
Button b4 = new Button("SOUTH");
Button b5 = new Button("CENTER");
add("East",b1);
add("West",b2);
add("North",b3);
add("South",b4);
add("Center",b5);
}
}
//< applet code =BorderApp width = 500 height = 500>< /applet>
/* Java Program To Draw Image on the applet. Done using Applets. */
import java.awt.*;
import java.applet.*;
public class AppletImage extends Applet
{
Image img;
public void init()
{
String imagename= getParameter("Whilder e Pool");
img = getImage(getCodeBase(),imagename);
}
public void paint(Graphics c)
{
c.drawImage(img,10,10,this);
}
}
//< applet code = AppletImage width = 770 Height = 555>
//< param name = "Whilder e pool " value = "madonnablue6bp.gif">
//< /applet>
/*Java Program To Display Simple Text on the applet, making use of Color class. Done using Applets. */
import java.awt.*;
import java.applet.*;
public class AppletColor extends Applet
{
public void paint(Graphics c)
{
setBackground(Color.blue);
c.setColor(Color.white);
c.drawString("Lionel Is a Cool Boy",100,50);
}
}
//< code =" AppletColor" width =" 350" height =" 150">< / applet >
/*************** DES **************
*The program listed below, testDES.java:
takes the DES key input and a text string (to be encrypted) from the program itself (not from a file),
encrypts the string (to produce the ciphertext), writes the key and the ciphertext to a file DES.out, decrypts the ciphertext (still in computer memory), and writes the resulting plaintext string to the file.
In this example, we see see the following three numbers:
DES key: 3812A419C63BE771
Plaintext: 0101010101010101 0102030405060708 090A0B0C0D0E0F10 1112131415161718
Ciphertext: 3A2EAD12F475D82C 1FC97BB9A6D955E1 EA5541946BB4F2E6 F29555A6E8F1FB3C */
import java.io.*;
import java.security.*;
import java.math.*;
import cryptix.util.core.BI;
import cryptix.util.core.ArrayUtil;
import cryptix.util.core.Hex;
import cryptix.provider.key.*;
class testDES {
public static void main (String[] args) {
try {
FileOutputStream outFile1 = new FileOutputStream("DES.out");
// Note: PrintStream is deprecated, but still works fine in jdk1.1.7b
PrintStream output1 = new PrintStream(outFile1);
// convert a string to a DES key and print out the result
RawSecretKey key2 = new RawSecretKey("DES",Hex.fromString("3812A419C63BE771"));
RawKey rkey = (RawKey) key2;
byte[] yval = rkey.getEncoded();
BigInteger Bkey = new BigInteger(yval);
String w = cryptix.util.core.BI.dumpString(Bkey);
output1.println("The Encryption Key = " + w);
// use the DES key to encrypt a string
Cipher des=Cipher.getInstance("DES/ECB/NONE","Cryptix");
des.initEncrypt(key2);
byte[] ciphertext = des.crypt(Hex.fromString("01010101010101010102030405060708090A0B0C0D0E0F101112131415161718"));
// print out length and representation of ciphertext
output1.print("\n");
output1.println("ciphertext.length = " + ciphertext.length);
BigInteger Bciph = new BigInteger(ciphertext);
w = cryptix.util.core.BI.dumpString(Bciph);
output1.println("Ciphertext for DES encryption = " + w);
// decrypt ciphertext
des.initDecrypt(key2);
ciphertext = des.crypt(ciphertext);
output1.print("\n");
output1.println("plaintext.length = " + ciphertext.length);
// print out representation of decrypted ciphertext
Bciph = new BigInteger(ciphertext);
w = cryptix.util.core.BI.dumpString(Bciph);
output1.println("Plaintext for DES encryption = " + w);
output1.println(" ");
output1.close();
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
}
}}
Labels: Core Java
/* Simple Java Program To Find the Sum of digits of number */
class SumDigit
{
public static void main(String args[])
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}
Labels: Core Java
/* Simple Java Program For Displaying Name, Roll no. MArks and total of a student*/
class stud
{
public static float tot;
public static int rno;
public static String name;
public static int m1,m2,m3;
public void calc()
{
tot=(m1+m2+m3)/3;
}
public void op()
{
System.out.println("rollno="+rno);
System.out.println("name="+name);
System.out.println("%="+tot);
}
public static void main(String args[])
{
stud s=new stud();
rno=Integer.parseInt(args[0]);
name=args[1];
m1=Integer.parseInt(args[2]);
m2=Integer.parseInt(args[3]);
m3=Integer.parseInt(args[4]);
s.calc();
s.op();
}
}
Labels: Core Java
/* Simple Java Program For Deciding Employee Salary Depending On His Designation*/
class Salary2
{
public static void main(String args[])
{
float gross,bs,hre,da;
bs = Integer.parseInt(args[0]);
if(bs >15000)
{
hre = 0.3f* bs;
da = 0.2f*bs;
gross = bs + hre + da;
System.out.println("Gross Salary : "+ gross);
}
else if(bs >10000)
{
hre = 0.4f* bs;
da = 0.3f*bs;
gross = bs + hre + da;
System.out.println("Gross Salary : "+ gross);
}
else if(bs >5000)
{
hre = 0.3f* bs;
da = 0.2f*bs;
gross = bs + hre + da +500;
System.out.println("Gross Salary : "+ gross);
}
}
}
Labels: Core Java
/* Simple Java Program For Displaying Emplayee Details : NAme, Id No. Designation, and depending on designation deciding the salary Of the Employee*/
class emp
{
public static int emp_no;
public static String name;
public static String designation;
public void showdata()
{
System.out.println("Employee number:--->"+emp_no);
System.out.println("Employee name:------>"+name);
System.out.println("Employee designation:--->"+designation);
}
}
class salary extends emp
{
public static float bsal,hr,da,tsal;
public void cal()
{
tsal=bsal+hr+da;
System.out.println("Total salary:---->"+tsal);
}
}
class empsal extends salary
{
public void calc()
{
if (bsal >=15000)
{
hr=0.3f*bsal;
da=0.2f*bsal;
}
else if (bsal >=10000)
{
hr=0.4f*bsal;
da=0.3f*bsal;
}
else
hr=0.3f*bsal+500;
da=0.2f*bsal+500;
}
public static void main(String args[])
{
empsal E=new empsal();
emp_no=Integer.parseInt(args[0]);
name=args[1];
designation=args[2];
bsal=Integer.parseInt(args[3]);
E.showdata();
System.out.println("Employee Salary");
E.calc();
E.cal();
}
}
Labels: Core Java
class Color
{
public static void main(String args[])
{
String a;
a =args[0];
switch(a)
{
case "r" :
System.out.println("Red");
break;
case "b" :
System.out.println("Blue");
break;
case "g" :
System.out.println("Green");
break;
case "y" :
System.out.println("Yellow");
break;
case "w" :
System.out.println("White");
break;
case "o" :
System.out.println("Orange");
break;
}
}
}
Labels: Core Java
class Bubbesort
{
public static void main(String args[])
{
int sum, i,j,temp = 0;
int []a = new int[5];
for(i=0;i< 5;i++)
a[i] = Integer.parseInt(args[i]);
for(i=0;i< 5;i++)
{
for(j=i+1;j< 5;j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(i=0;i< 5;i++)
System.out.println(a[i]);
}
}
Labels: Core Java
/*Program Displaying the human face suing basic figures, circles, arcs, lines, etc. Done using Applets.
/* < applet code = "Face" width = 500 height =500>
< /applet> */
import java.awt.*;
import java.applet.*;
public class Face extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hi !!!!!!!!!!!!!!! I Am Ninad.",20,30);
g.drawArc(30,50,200,200,0,360); // Main Face
g.drawArc(82,80,30,30,20,160); //Left Eye Brow
g.fillArc(85,85,25,25,0,360); //Left Eye
g.drawArc(152,80,30,30,20,160); //Right Eye Brow
g.fillArc(155,85,25,25,0,360); //Right Eye
g.drawRect(90,190,80,20); //Mouth
g.drawLine(130,135,115,160); //Nose Left Line
g.drawLine(130,135,145,160); // Nose Right Line
g.drawLine(115,160,145,160); // Nose Base
g.drawString(" ------------- Created By Lionel.",10,350);
}
}
Labels: Advanced Java, Applet, Swings
class pyramid1
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
System.out.print(j+"\t");
System.out.println();
}
}
}
/* OUTPUT
1
1 2
1 2 3
Labels: Core Java
/* Java program to find percentage of student in 5 subjects
class Percentage
{
public static void main(String args[])
{
float Eng, Hindi, Marathi, Science, Social, percent;
Eng=Integer.parseInt(args[0]);
Hindi=Integer.parseInt(args[1]);
Marathi=Integer.parseInt(args[2]);
Science=Integer.parseInt(args[3]);
Social=Integer.parseInt(args[4]);
percent = (Eng+Hindi+Marathi+Science+Social)/5;
System.out.println("Subjects Offered : English, Hindi, Marathi, Science, Socail Studies");
System.out.println("Percentage"+percent);
}
}
Labels: Core Java
--BY Joel Mathias
/* All members and variables can be overriden by default in subclasses. Final Keyword Is Used in Java to prevent the subclass from overridding the members of the superclass. Making a method final ensures that the functionality defiened in the mathod will never be altered in any way. Similarily the value of the final variable can never be altered in any way. */
class fe
{
double fp;
int fr;
fe(double p1,int r1)
{
fp=p1;
fr=r1;
}
}
class se extends fe
{
double sp;
int sr;
se(double p1,int r1,double p2,int r2)
{
super(p1,r1);
sp=p2;
sr=r2;
}
}
final class te extends se
{
double tp;
int tr;
te(double p1,int r1,double p2,int r2,double p3,int r3)
{
super(p1,r1,p2,r2);
tp=p3;
tr=r3;
}
double average()
{
return (fp+sp+tp)/3;
}
}
class Student
{
public static void main(String args[])
{
te student = new te(75.6,40,78.6,44,70.5,48);
System.out.println("Year\tRoll no.\tPercentage");
System.out.println("FE\t"+student.fr+"\t\t"+student.fp);
System.out.println("SE\t"+student.sr+"\t\t"+student.sp);
System.out.println("TE\t"+student.tr+"\t\t"+student.tp);
System.out.println("Average performance is " + student.average());
}
}
/* Output *
Year Roll no. Percentage
FE 40 75.6
SE 44 78.6
TE 48 70.5
Average performance is 74.89999999999999 */
Labels: Core Java, Inheritance
--BY Joel Mathias
/* Program to Implement Hierarchical Inheritance in java Programming Language */
class Info
{
int pid;
char branch;
char year;
Info(int p,char ch,char y)
{
pid = p;
branch = ch;
year = y;
}
void display()
{
System.out.println("\nPID\t: "+pid);
System.out.print("Branch\t: ");
if(branch == 'i')
System.out.println("Information Technology");
if(branch =='e')
System.out.println("Electronics and Telecommunication");
if(branch =='c')
System.out.println("Computer Science");
System.out.print("Year\t: ");
if(year == 'f')
System.out.println("FE");
if(year == 's')
System.out.println("SE");
if(year == 't')
System.out.println("TE");
}
}
class Fe extends Info
{
int c;
int cpp;
Fe(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
c = m1;
cpp = m2;
}
void fdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tC\t"+c);
System.out.println("\tC++\t"+cpp);
}
}
class Se extends Info
{
int vb;
int html;
Se(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
vb = m1;
html= m2;
}
void sdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tVB\t"+vb);
System.out.println("\tHTML\t"+html);
}
}
class Te extends Info
{
int matlab;
int java;
Te(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
matlab = m1;
java = m2;
}
void tdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tMATLAB\t"+matlab);
System.out.println("\tSJava\t"+java);
}
}
class Language
{
public static void main(String args[])
{
Fe F = new Fe(1074,'i','f',9,8);
Se S = new Se(1064,'e','s',6,8);
Te T = new Te(1054,'c','t',9,9);
F.fdisplay();
S.sdisplay();
T.tdisplay();
}
}
/* Output *
PID : 1074
Branch : Information Technology
Year : FE
Performance:
C 9
C++ 8
PID : 1064
Branch : Electronics and Telecommunication
Year : SE
Performance:
VB 6
HTML 8
PID : 1054
Branch : Computer Science
Year : TE
Performance:
MATLAB 9
SJava 9 */
Labels: Core Java, Inheritance
--BY Joel Mathias
Program that makes use of Multilevel Inheritance in java
/* program To Implement Multilevel Inheritance */
class Fe
{
double fp;
int fr;
Fe(double p1,int r1)
{
fp = p1;
fr = r1;
}
}
class Se extends Fe
{
double sp;
int sr;
Se(double p1,int r1,double p2,int r2)
{
super(p1,r1);
sp = p2;
sr = r2;
}
}
class Te extends Se
{
double tp;
int tr;
Te(double p1,int r1,double p2,int r2,double p3,int r3)
{
super(p1,r1,p2,r2);
tp = p3;
tr = r3;
}
double average()
{
return (fp+sp+tp)/3;
}
}
class Student
{
public static void main(String args[])
{
Te student = new Te(78.5,41,71.73,44,79.8,48);
System.out.println("Year\tRoll no.\tPercentage");
System.out.println("FE\t"+student.fr+"\t\t"+student.fp);
System.out.println("SE\t"+student.sr+"\t\t"+student.sp);
System.out.println("TE\t"+student.tr+"\t\t"+student.tp);
System.out.println("\nAverage Performance is " + student.average());
}
}
/* Output *
Year Roll no. Percentage
FE 41 78.5
SE 44 71.73
TE 48 79.8
Average Performance is 76.67666666666668 */
Labels: Core Java, Inheritance
--BY Joel Mathias
Single Inheritance Program To Find The Volume Of A cylinder and a sphere
/* Program To Implement Simple Program On Single Inheritance */
class Sphere
{
double r;
double area()
{
return (r * r * r * 4 * 3.1416 / 3);
}
}
class Cylinder extends Sphere
{
double h;
double area()
{
return 3.1416 * r * r * h;
}
}
class Volume
{
public static void main(String args[])
{
Cylinder C = new Cylinder();
C.r = 2.0;
C.h = 6.0;
double cyl = C.area();
Sphere S = new Sphere();
S.r = 5.0;
double sph = S.area();
System.out.println("Area of sphere is "+sph+" and area of cylinder is "+cyl);
}
}
/* OUTPUT *
Area of sphere is 523.6 and area of cylinder is 75.3984 */
Labels: Core Java, Inheritance
--BY Joel Mathias
Single Inheritance inherits the properties of one class into another class here is a program that implements single inheritance to find area of a rectangle..
/* Single Inhetitance To Find Area Of Rectangle */
class Dimensions
{
int length;
int breadth;
}
class Rectangle extends Dimensions
{
int a;
void area()
{
a = length * breadth;
}
}
class Area
{
public static void main(String args[])
{
Rectangle Rect = new Rectangle();
Rect.length = 7;
Rect.breadth = 16;
Rect.area();
System.out.println("The Area of rectangle of length "
+Rect.length+" and breadth "+Rect.breadth+" is "+Rect.a);
}
}
/* Output *
The Area of rectangle of length 7 and breadth 16 is 112 */
Labels: Core Java, Inheritance
What Is an Interface?
In general, an interface is a device or a system that unrelated entities use to interact. According to this definition, a remote control is an interface between you and a television set, the English language is an interface between two people, and the protocol of behavior enforced in the military is the interface between people of different ranks.
Within the Java programming language, an interface is a type, just as a class is a type. Like a class, an interface defines methods. Unlike a class, an interface never implements methods; instead, classes that implement the interface implement the methods defined by the interface. A class can implement multiple interfaces.
The bicycle class and its class hierarchy define what a bicycle can and cannot do in terms of its "bicycleness." But bicycles interact with the world on other terms. For example, a bicycle in a store could be managed by an inventory program. An inventory program doesn’t care what class of items it manages, as long as each item provides certain information, such as price and tracking number. Instead of forcing class relationships on otherwise unrelated items, the inventory program sets up a protocol of communication. This protocol comes in the form of a set of method definitions contained within an interface. The inventory interface would define, but not implement, methods that set and get the retail price, assign a tracking number, and so on.
To work in the inventory program, the bicycle class must agree to this protocol by implementing the interface. When a class implements an interface, the class agrees to implement all the methods defined in the interface. Thus, the bicycle class would provide the implementations for the methods that set and get retail price, assign a tracking number, and so on.
You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following:
- Capturing similarities among unrelated classes without artificially forcing a class relationship
- Declaring methods that one or more classes are expected to implement
- Revealing an object's programming interface without revealing its class
- Modelling multiple inheritance, a feature that some object-oriented languages support that allows a class to have more than one superclass
Simple Program On Java for the implementation of Multiple inheritance using interfaces to calculate the area of a rectangle and triangle
/* Area Of Rectangle and Triangle using Interface * /
interface Area
{
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
area = rect;
System.out.println("Area Of Rectangle = "+ area.compute(1,2));
area = tri;
System.out.println("Area Of Triangle = "+ area.compute(10,2));
}
}
/** OUTPUT **
Area Of Rectangle = 2.0
Area Of Triangle = 10.0
*/
Labels: Core Java, Inheritance
/* Simple Program In Java To print on the screen */
class Simple
{
public static void main(String args[])
{
System.out.println("Java Is Better Than C and C++");
}
}
/* Output *
Java Is Better Than C and C++
*/
Labels: Core Java
/*The Given Program Calculates the Following Mathematical operations:
*1. Square root
*2. POwer of a number
*3. Sine Value
*4. Cosine Value
*5. Logarithm Value
*6. Absolute value
*
*
/* Simple Java Program For Various Mathematical Operation */
import java.lang.Math;
class MathFunctions
{
public static void main(System args[])
{
double x = 7;
double y;
System.out.println("Given Number "+x);
y = Math.sqrt(x);
System.out.println("Square Root : "+y);
y = Math.pow(x,3);
System.out.println("Power : "+y);
y = Math.sin(x);
System.out.println("Sine : "+y);
y = Math.cos(x);
System.out.println("Cosine : "+y);
y = Math.log(x);
System.out.println("Logrithm : "+y);
y = Math.abs(x);
System.out.println("Absolute Value : "+y);
}
}
Given Number 7.0
Square Root : 2.6457513110645907
Power : 343.0
Sine : 0.6569865987187891
Cosine : 0.7539022543433046
Logrithm : 1.9459101490553132
Absolute Value : 7.0
*/
/* Simple Java Program For Claculating Area Of A Rectangle Using two Classes */
class TwoClasses
{
float length, breadth;
void getdata(float a, float b)
{
length = a;
breadth = b;
}
}
class Area
{
public static void main(String args[])
{
float area;
TwoClasses Rectangle = new TwoClasses(); // Object Reactangle
Rectangle.getdata(25,35);
area = Rectangle.length * Rectangle.breadth;
System.out.println("Area : "+area);
}
}
/* Output *
*Area : 875
*/
/* Simple Java Program that uses Command line arguments as input */
class ComandLine
{
public static void main(String args[])
{
int count, i = 0;
String string;
count = args.length;
System.out.println("Number Of Arguments : "+count);
while(i< count)
{
string = args[i];
i = i + 1;
System.out.println(i+ " : "+"Java Is "+string+ "!");
}
}
}
/* *Compile and run the command line as follows *Java Commandline Simple Object_Oriented Distributed Robust Secure Portable Multithread Dynamic */
/*** OUTPUT ***
As Seen on Java E:\Java\bin>javac Commandline.java
E:\Java\bin>java ComandLine Simple Object_Oriented Disrtibuted Robust Secure por
table Multithread dynamic
Number Of Arguments : 8
1 : Java Is Simple!
2 : Java Is Object_Oriented!
3 : Java Is Disrtibuted!
4 : Java Is Robust!
5 : Java Is Secure!
6 : Java Is portable!
7 : Java Is Multithread!
8 : Java Is dynamic!
*/
Labels: Core Java
/* Simple Program In java to Print the Following Pyramid
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
*/
class PyramidNos
{
public static void main(String args[])
{
int i,j;
System.out.println("Displaying Numbers:");
for(i=1;i< 10;i++)
{
for(j=1;j< i+1;j++)
{
System.out.print(" " +i);
}
System.out.println();
}
}
}
/* OUTPUT *
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
*/
/****** Simple Java Program For Factorial ********/
import java.io.*;
class Fact
{
public static void main(String args[])throws IOException
{
int a = 1,n,i;
System.out.print("Enter The Number Whoes Factorial Is To Be Found : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
for(i=n;i>0;i--)
{
a = a * i;
}
System.out.println("Factorial of "+n +" is "+a);
}
}
/*********** OUTPUT **********
Enter The Number Whoes Factorial Is To Be Found : 5
Factorial of 5 is 120 */
Labels: Core Java
Pyramid Sequence
4
4 3
4 3 2
4 3 2 1
class Pyramid3
{
public static void main(String args[])
{
int i,j;
for(i=4;i>=1;i--)
{
for(j=4;j>=i;j--)
System.out.print(" "+j);
System.out.print("\n");
}
}
}
/********* OUTPUT ********
4
4 3
4 3 2
4 3 2 1 */
Pyramid Sequence
5
5 6
5 6 7
5 6 7 8
class Pyramid2
{
public static void main(String args[])
{
int i,j;
for(i=5;i<=8;i++)
{
for(j=5;j<=i;j++)
System.out.print(" "+j);
System.out.print("\n");
}
}
}
/********* OUTPUT ********
5
5 6
5 6 7
5 6 7 8 */
Pyramid Sequence
1
1 2
1 2 3
1 2 3 4
class Pyramid1
{
public static void main(String args[])
{
int i,j;
for(i=1;i< =4;i++)
{
for(j=1;j< =i;j++)
System.out.print(" "+j);
System.out.print("\n");
}
}
}
/********* OUTPUT ********
1
1 2
1 2 3
1 2 3 4 */
/* Java Program For Displaying Month */
public class Month {
public static void main(String[] args) {
int month = 8;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
default: System.out.println("Not a month!"); break;
}
}
}
/* OUTPUT *
*August
*/
Labels: Core Java
An object stores its state in variables.
Definition: A variable is an item of data named by an identifier.
You must explicitly provide a name and a type for each variable you want to use in your program. The variable's name must be a legal identifier — an unlimited-length sequence of Unicode characters that begins with a letter. You use the variable name to refer to the data that the variable contains. The variable's type determines what values it can hold and what operations can be performed on it. To give a variable a type and a name, you write a variable declaration, which generally looks like this:
type name
In addition to the name and type that you explicitly give a variable, a variable has scope. The section of code where the variable's simple name can be used is the variable's scope. The variable's scope is determined implicitly by the location of the variable declaration, that is, where the declaration appears in relation to other code elements. You'll learn more about scope in the section Scope.
/*** Java Program For Displaying Byte values of various Datatypes */
public class MaxVariables
{
public static void main(String args[])
{
//For integers
byte largestByte = Byte.MAX_VALUE;
short largestShort = Short.MAX_VALUE;
int largestInteger = Integer.MAX_VALUE;
long largestLong = Long.MAX_VALUE;
//For real numbers
float largestFloat = Float.MAX_VALUE;
double largestDouble = Double.MAX_VALUE;
//For other primitive types
char aChar = 'S';
boolean aBoolean = true;
//Output
System.out.println("The largest byte value is "
+ largestByte);
System.out.println("The largest short value is "
+ largestShort);
System.out.println("The largest integer value is "
+ largestInteger);
System.out.println("The largest long value is "
+ largestLong);
System.out.println("The largest float value is "
+ largestFloat);
System.out.println("The largest double value is "
+ largestDouble);
if (Character.isUpperCase(aChar)) {
System.out.println("The character " + aChar
+ " is upper case.");
} else {
System.out.println("The character " + aChar
+ " is lower case.");
}
System.out.println("The value of aBoolean is "
+ aBoolean);
}
}
/****** OUTPUT ******
The largest byte value is 127
The largest short value is 32767
The largest integer value is 2147483647
The largest long value is 9223372036854775807
The largest float value is 3.4028235E38
The largest double value is 1.7976931348623157E308
The character S is upper case.
The value of aBoolean is true */
Labels: Core Java
/************ Implementaion On Numbers In Java*************/
class Numbers
{
public static void main(String args[])
{
int i,great_no,small_no;
int no[] = {5,78,1,6,74,9,6,4,6,7};
System.out.println("Elements in the Array : ");
for(i=0;i< 10;i++)
System.out.print(no[i]+"\t");
great_no = no[0]; small_no = no[0];
for(i=1;i< 10;i++)
{
if(great_no< no[i])
great_no = no[i];
else if(small_no>no[i])
small_no = no[i];
}
System.out.print("\nGreatest Number : "+great_no);
System.out.print("\nSmallest Number : "+small_no);
}
}
/************** OUTPUT ****************
Elements in the Array :
5 78 1 6 74 9 6 4 6 7
Greatest Number : 78
Smallest Number : 1 */
/****** Java Program To Print Sum Of 'n' Number ******/
class Add
{
public static void main (String args[])
{
int n = 15;
int sum = 0;
for(int i = n;i>=0;i--)
sum = sum + i;
System.out.println("Sum = "+ sum);
}
}
/*************** OUTPUT *************
* *
* Sum = 120 */
/********** Factorial Of A Number **********/
class Factorial
{
public static void main(String args[])
{
int a = 5;
int fact = a;
for(int i=a-1;i>=1;i--)
fact = fact * i;
System.out.println("Factorial : "+ fact);
}
}
/********** OUTPUT ***********
Factorial : 120 */
Labels: Core Java
/* Java Program To Check Whether Number is Even or Odd */
class Even
{
public static void main(String args[])
{
int a = 2;
if((a % 2) == 0)
System.out.print("Number is even");
else
System.out.println("Number is odd");
}
}
/************** OUTPUT **************
Number is even */
Labels: Core Java
/********** Java Program To Print Reverse Of A Number ************/
class Reverse
{
public static void main(String args[])
{
int i,s=0,a = 120;
int no[] = new int[10];
System.out.print("Original Number : "+a);
for(i=0;i< 10;i++)
{
if(a!= '\0')
{
no[i] = a%10;
a = a / 10;
s++;
}
else break;
}
System.out.print("\n\nReversed Number : ");
for(i=s;i>0;i--)
{
System.out.print(no[i]);
}
}
}
/************ OUTPUT ************
* Original Number : 120
* Reversed Number : 021 */
/******* Java Implementation of Matrix Operation Using Arrays *******/
class Matrix
{
public static void main(String args[])
{
int i,j,k;
int mat1 [][]={ {10,11,12}, {13,14,15}, {16,17,18} };
int mat2 [][]={ {1,2,3}, {4,5,6}, {7,8,9} };
System.out.println("Operation ON Matrices \n1.Addition \n");
int sum [][] = new int [3][3];
for(i=0;i <3;i++)
{
for(j=0;j< 3;j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
System.out.print("\t" + sum[i][j]);
}
System.out.println("\t");
}
System.out.println("2.Subtraction\n");
int diff[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
diff [i][j] = mat1[i][j] - mat2[i][j];
System.out.print("\t"+ diff[i][j]);
}
System.out.println("\t");
}
System.out.println("3.Multiplication\n");
int prod[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
prod[i][j] = 0;
{
for(k=0;k< 3;k++)
prod[i][j] = prod[i][j]+mat1[i][k]*mat2[k][j];
}
System.out.print("\t"+ prod[i][j]);
}
System.out.println("\t");
}
}
}
/************* OUTPUT ***************
Operation ON Matrices
1.Addition
11 13 15
17 19 21
23 25 27
2.Subtraction
9 9 9
9 9 9
9 9 9
3.Multiplication
138 171 204
174 216 258
210 261 312 */