Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

4/4/12

COMPILING AND RUNNING YOUR JAVA FILES USING COMMAND PROMPT (CMD WINDOW) – for WINDOWS


Step 1:
a.       Go to Start Menu -> Click ‘Run’ or
b.    Press Window Logo on your keyboard + R
*If you can’t find  the ‘Run’ on your Start Menu, right click the ‘Start Menu logo’ à Properties à choose ‘Customize’ button à put a tick on ‘Run command’ and hit OK and ‘Apply’.
Step 2:
                Type ‘cmd’ on the Run window.











Step 3:
                The command window will appear after.
a.       Type: cd C:\java\bin                                    to go to the directory  C:\java\bin> where your
java files (.java) are saved.   

b.       Compiling your java file. This will create a .CLASS file when compiling is done               . 

Type this:
javac myJavaFile.java       

*If no errors were found, proceed to running your java file.
*In compiling your java program, always remember that it translates the code line by line.

             c.       Running your java file.
Type,

 java myJavaFile.java



*Java is case-sensitive;
*Don’t forget to include the file extension (.JAVA) of your java file when compiling and running them.


10/25/11

Generating Odd Numbers


Java code for generating odd numbers:

import javax.swing.JOptionPane;


public class odd{


public static void main(String[] args){

int x = 0;

do{
String inputNum = JOptionPane.showInputDialog(null, "Enter any positive number: ",  "Input", JOptionPane.INFORMATION_MESSAGE);
x = Integer.parseInt(inputNum);
if(x<1){
JOptionPane.showMessageDialog(null, "ERROR. It must be a positive integer.", "Error", JOptionPane.ERROR_MESSAGE);
}
}while(x<1);
System.out.println("\nYou entered " + x + ".");
System.out.println("\n\nOdd numbers found: ");
System.out.println("------------------");
for(int j = 1; j<=x;){
System.out.print(j + ", ");
j = j + 2;
if(j>x)
break;
}
System.out.println("\n\n");
}



}




Sample Output:


9/27/10

Divisors of a Number (Java Program)

import javax.swing.JOptionPane;

public class DivisorsOfANumber{

public static void main(String[] args){
String input;
int number;
int numberDivisors = 0;
int moduloAnswer;

here:
do{
input = JOptionPane.showInputDialog(null, "Enter a positive real number:", "Divisors of A Number", JOptionPane.INFORMATION_MESSAGE);
number = Integer.parseInt(input);

if(number<0){
JOptionPane.showMessageDialog(null, "Invalid number! A positive number should be needed to enter", "Error!", JOptionPane.ERROR_MESSAGE);
continue here;
}
if(number == 0)
JOptionPane.showMessageDialog(null, "Error! No possible divisor for zero.", "Error!", JOptionPane.ERROR_MESSAGE);
continue here;
}while(number<=0);

System.out.print("The divisors of " + number + " are : ");

for(int possibleDivisors = 1; possibleDivisors<=number; possibleDivisors++){

if((number % possibleDivisors)==0)
System.out.print(possibleDivisors + " ");
}

}

}

9/22/10

Peso Dollar Converter (Java Code)


Code for converting peso-dollar written in Java:
(No available screenshots for now. :))


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


public class PesoDollar extends JFrame{


private JTextField pesoTF, dollarTF;
private JLabel pesoL, dollarL;


private Font fontBold;


private static final int JFRAME_WIDTH = 600;
private static final int JFRAME_LENGTH = 100;


private JButton exitB, clearAllB;


private CalculatePesoTextFieldHandler cpHandler;
private CalculateDollarTextFieldHandler cdHandler;
private ExitButtonHandler ebHandler;
private ClearAllButtonHandler clbHandler;


public PesoDollar(){


super("PESO-DOLLAR CONVERTER");


fontBold = new Font ("Arial Black", Font.BOLD, 12);


pesoL = new JLabel("Peso Value:", SwingConstants.RIGHT);
pesoL.setFont(fontBold);
dollarL = new JLabel("Dollar Value: ", SwingConstants.RIGHT);
dollarL.setFont(fontBold);


pesoTF = new JTextField(15);
dollarTF = new JTextField(15);


pesoTF.setToolTipText("Input amount of Peso and press 'ENTER' key.");
dollarTF.setToolTipText("Input amount of Dollar and press 'ENTER' key.");


//Create and set the clear all Button.
clearAllB = new JButton("Clear All");
clbHandler = new ClearAllButtonHandler();
clearAllB.addActionListener(clbHandler);
clearAllB.setToolTipText("Clears the entire text field.");


//Create and set the exit Button.
exitB = new JButton("Quit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
exitB.setToolTipText("Exit the program.");




Container con = getContentPane();


//con.setLayout(new GridLayout(3,2));


con.add(pesoL);
con.add(pesoTF);
con.add(dollarL);
con.add(dollarTF);
con.add(clearAllB);
con.add(exitB);


cpHandler = new CalculatePesoTextFieldHandler();
pesoTF.addActionListener(cpHandler);


cdHandler = new CalculateDollarTextFieldHandler();
dollarTF.addActionListener(cdHandler);


setLayout(new FlowLayout());
setSize(JFRAME_WIDTH, JFRAME_LENGTH);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);




}


public class CalculatePesoTextFieldHandler implements ActionListener{


public void actionPerformed(ActionEvent e){


double peso = Double.parseDouble(pesoTF.getText());
double answer= (peso/46);
dollarTF.setText("$" + "" + answer);


}


}


public class CalculateDollarTextFieldHandler implements ActionListener{


public void actionPerformed(ActionEvent e){


double dollar = Double.parseDouble(dollarTF.getText());
double answer= (dollar * 46);
pesoTF.setText("Php " + "" + answer);


}


}


public class ClearAllButtonHandler implements ActionListener{


public void actionPerformed(ActionEvent e){


pesoTF.setText("");
dollarTF.setText("");
}
}


public class ExitButtonHandler implements ActionListener{


public void actionPerformed(ActionEvent e){


int resp = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?", "QUIT", JOptionPane.YES_NO_OPTION);
if(resp == JOptionPane.YES_OPTION)
System.exit(0);
}
}


public static void main(String[] args){


setDefaultLookAndFeelDecorated(true);


PesoDollar pesodollarObject = new PesoDollar();


}


}

Fibonacci Code - Java

Java Code for generating fibonacci numbers:

import javax.swing.*;


public class Fibonacci {


 public static void main (String[] args) {


    String inputSequence = "";
    int a = 1, b = 1, sum = 0;
    int sequence= 0;


    inputSequence = JOptionPane.showInputDialog("Enter number of 
                          sequence you want to see: ");


    sequence = Integer.parseInt(inputSequence);


    int i = 1;
    while(i <= sequence){
         System.out.printf("Fib(%d)= %3d\n",i,a);
         sum = a + b;
         a = b;
         b = sum;
         i++;
    }


 System.exit(0);
 }
}

9/19/10

Converting Peso-Dollar and Vice- versa (Java Code)

//Conversion: $1=Php 46.00

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

public class PesoDollar extends JFrame{

    private JTextField pesoTF, dollarTF;
    private JLabel pesoL, dollarL;
   
    private static final int JFRAME_WIDTH = 400;
    private static final int JFRAME_LENGTH = 100;

    private CalculatePesoTextField cpHandler;
    private CalculateDollarTextField cdHandler;

    public PesoDollar(){       
       
        super("PESO-DOLLAR CONVERTER");       
        pesoL = new JLabel("Amount of Peso:", SwingConstants.RIGHT);
        dollarL = new JLabel("Amount of Dollar: ", SwingConstants.RIGHT);
   
        pesoTF = new JTextField(10);
        dollarTF = new JTextField(10);
       
        Container con = getContentPane();

        con.add(pesoL);
        con.add(pesoTF);
       
        con.add(dollarL);
        con.add(dollarTF);
       
        con.setLayout(new GridLayout(2,2));
       
        cpHandler = new CalculatePesoTextField();   
        pesoTF.addActionListener(cpHandler);

        cdHandler = new CalculateDollarTextField();   
        dollarTF.addActionListener(cdHandler);

        setSize(JFRAME_WIDTH, JFRAME_LENGTH);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);


    }

    public class CalculatePesoTextField implements ActionListener{

        public void actionPerformed(ActionEvent e){
   
            double peso = Double.parseDouble(pesoTF.getText());
            double answer= (peso/46);
            dollarTF.setText("$" + "" + answer);
       
        }

    }

    public class CalculateDollarTextField implements ActionListener{

        public void actionPerformed(ActionEvent e){
   
            double dollar = Double.parseDouble(dollarTF.getText());
            double answer= (dollar * 46);
            pesoTF.setText("Php" + "" + answer);
       
        }

    }

    public static void main(String[] args){
       
        setDefaultLookAndFeelDecorated(true);

        PesoDollar pesodollarObject = new PesoDollar();
   
    }

}

FEATURE

 

© 2013 andyfgo. All rights reserved. Designed by Templateism. Powered by Blogger
Protected by Copyscape DMCA Takedown Notice Violation Search

Back To Top