Now I have sample code about how to use array one dimensional:
let's see my example on it.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ArrayDemo {
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input number of element of array : ");
int num = Integer.parseInt(bf.readLine());
int[]arr= new int[num];
// input array
for(int i=0;i<arr.length;i++){
System.out.print("arr["+i+"]=");
arr[i]= Integer.parseInt(bf.readLine());
}
// before sort
System.out.println("Befor Sort");
for(int i=0;i<arr.length;i++)
System.out.println("arr["+i+"]= "+arr[i]);
// sort as ascending
int temp;
for(int i=0;i<arr.length;i++)
for(int j=i;j<arr.length;j++){
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
System.out.println("Sort Ascending");
for(int i=0;i<arr.length;i++)
System.out.println("arr["+i+"]= "+arr[i]);
// sort as descending
for(int i=0;i<arr.length;i++)
for(int j=i;j<arr.length;j++){
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
System.out.println("Sort Discending");
for(int i=0;i<arr.length;i++)
System.out.println("arr["+i+"]= "+arr[i]);
}
}
Thursday, June 27, 2013
Wednesday, June 26, 2013
Structure for write file in java
// declare JFileChooserJFileChooser fileChooser = new JFileChooser();// let the user choose the destination fileif (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { // indicates whether the user still wants to export the settings boolean doExport = true; // indicates whether to override an already existing file boolean overrideExistingFile = false; // get destination file File destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath()); // check if file already exists while (doExport && destinationFile.exists() && !overrideExistingFile) { // let the user decide whether to override the existing file overrideExistingFile = (JOptionPane.showConfirmDialog(this, "Replace file?", "Export Settings", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION); // let the user choose another file if the existing file shall not be overridden if (!overrideExistingFile) { if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { // get new destination file destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath()); } else { // seems like the user does not want to export the settings any longer doExport = false; } } } // perform the actual export if (doExport) { ExportTable(destinationFile); }} public void ExportTable(File file)throws Exception
{
FileWriter writer = new FileWriter(file);
BufferedWriter bfw = new BufferedWriter(writer);
for(int i = 0 ; i < table.getColumnCount() ; i++)
{
bfw.write(table.getColumnName(i)+" ");
}
bfw.write("\n");
for (int i = 0 ; i < table.getRowCount(); i++)
{
for(int j = 0 ; j < table.getColumnCount();j++)
{
bfw.write((String)(table.getValueAt(i,j))+"\t");
}
bfw.write("\n");
}
bfw.close();
writer.close();
} Sunday, June 16, 2013
Count Character in java
package homework;
import java.io.*;
public class Topic6 {
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num=0;
int[] alpha = new int[26];
char[] ch=null;
System.out.print("How many character do you want to get(only lower case)? : ");
num = Integer.parseInt(in.readLine());
ch = new char[num];
System.out.print("Characters : ");
for(int i=0;i<ch.length;i++){
ch[i]=(char)System.in.read();
}
for(int i=0;i<ch.length;i++)
alpha[ch[i]-65]++;
for(int i=1;i<=alpha.length;i++){
System.out.print((char)(i+64)+"="+alpha[i-1]+" ");
if(i % 3== 0)
System.out.println();
}
}
}
import java.io.*;
public class Topic6 {
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num=0;
int[] alpha = new int[26];
char[] ch=null;
System.out.print("How many character do you want to get(only lower case)? : ");
num = Integer.parseInt(in.readLine());
ch = new char[num];
System.out.print("Characters : ");
for(int i=0;i<ch.length;i++){
ch[i]=(char)System.in.read();
}
for(int i=0;i<ch.length;i++)
alpha[ch[i]-65]++;
for(int i=1;i<=alpha.length;i++){
System.out.print((char)(i+64)+"="+alpha[i-1]+" ");
if(i % 3== 0)
System.out.println();
}
}
}
Text Calculator in java
package homework;
import java.io.*;
public class Topic4 {
public static void main(String[] args) throws IOException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int num1=0,num2=0,total=0;char operator;
while(true){
System.out.print("Num1 = ");
num1=Integer.parseInt(input.readLine());
do{
System.out.print("Operator (+, -, *, /, %) = ");
operator =(char)System.in.read();
System.in.read();
System.in.read();
}while(operator !='+' && operator !='-' && operator !='*' && operator !='/' && operator !='%');
System.out.print("Num2 = ");
num2=Integer.parseInt(input.readLine());
switch(operator){
case '+':total = num1+num2;break;
case '-':total = num1+num2;break;
case '*':total = num1+num2;break;
case '/':total = num1/num2;break;
case '%':total = num1%num2;break;
}
System.out.println("Result: "+num1+" + "+num2+" ="+total);
System.out.print("Do you want to continue(y/n)?");
char ch =(char)System.in.read();
if(ch=='n' || ch=='N')
break;
else{
System.in.read();
System.in.read();
}
}
}
}
import java.io.*;
public class Topic4 {
public static void main(String[] args) throws IOException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int num1=0,num2=0,total=0;char operator;
while(true){
System.out.print("Num1 = ");
num1=Integer.parseInt(input.readLine());
do{
System.out.print("Operator (+, -, *, /, %) = ");
operator =(char)System.in.read();
System.in.read();
System.in.read();
}while(operator !='+' && operator !='-' && operator !='*' && operator !='/' && operator !='%');
System.out.print("Num2 = ");
num2=Integer.parseInt(input.readLine());
switch(operator){
case '+':total = num1+num2;break;
case '-':total = num1+num2;break;
case '*':total = num1+num2;break;
case '/':total = num1/num2;break;
case '%':total = num1%num2;break;
}
System.out.println("Result: "+num1+" + "+num2+" ="+total);
System.out.print("Do you want to continue(y/n)?");
char ch =(char)System.in.read();
if(ch=='n' || ch=='N')
break;
else{
System.in.read();
System.in.read();
}
}
}
}
Check Character in java
package homework;
import java.io.*;
public class Topic2 {
public static void main(String[] args) throws IOException{
System.out.print("Input one character ");
char ch = (char)System.in.read();
if((int)ch >=65 && (int)ch <=90)
System.out.println(ch +" is upper-case");
if((int)ch>=97 && (int)ch<=122)
System.out.println(ch +" is lower-case");
}
}
import java.io.*;
public class Topic2 {
public static void main(String[] args) throws IOException{
System.out.print("Input one character ");
char ch = (char)System.in.read();
if((int)ch >=65 && (int)ch <=90)
System.out.println(ch +" is upper-case");
if((int)ch>=97 && (int)ch<=122)
System.out.println(ch +" is lower-case");
}
}
Hotel Management in java
-Use one dimensional Array:
package homework;
import java.io.*;
public class Topic8 {
public static void main(String[] args) throws IOException{
if(args.length !=1)
System.exit(0);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(args[0]);
//int num=Integer.parseInt(in.readLine());
String[] name = new String[num];
String temp ="";
while(true){
System.out.print("1. Check In, 2. Check Out, 3. Show All, 4. Exit = ");
int condition= Integer.parseInt(in.readLine());
if(condition == 1){
System.out.print("Check In : Room Number ? ");
int cr = Integer.parseInt(in.readLine());
if(cr>=0 && cr < name.length){
System.out.print("name = ");
temp=in.readLine();
if(name[cr] != null){
System.out.println("This room is busy!");
}else{
name[cr]=temp;
}
}else{
System.out.println("This room "+cr+" is not have.");
}
}else if(condition == 2){
System.out.print("Check Out : Room Number ? ");
int co = Integer.parseInt(in.readLine());
if(co>=0 && co < name.length){
if(name[co] !=null)
name[co]=null;
else
System.out.println("This room is already check out!");
}else{
System.out.println("Room "+co+" is not have.");
}
}else if(condition == 3){
for(int i=0;i<name.length;i++)
System.out.print("Room No."+i+" = "+name[i]+"\t");
System.out.println();
}else if(condition == 4){
System.out.print("End...");
System.exit(0);
}else{
System.out.println("Please choose that has available in option!");
}
}
}
}
-Use tow dimensional Array:
package homework;
import java.io.*;
public class Topic10 {
public static void main(String[] args) throws IOException{
if(args.length !=2)
System.exit(0);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(args[0]);//floor
int num1 = Integer.parseInt(args[1]);//room
String[][] name = new String[num][num1];
String temp="";
while(true){
System.out.print("1. Check In, 2. Check Out, 3. Show All, 4. Exit = ");
int condition = Integer.parseInt(in.readLine());
if(condition == 1){
System.out.print("Floor number = ");
int fn = Integer.parseInt(in.readLine());
if(fn>=0 && fn < num){
System.out.print("Room number = ");
int rn = Integer.parseInt(in.readLine());
if(rn>=0 && rn < num){
System.out.print("Customer name = ");
temp = in.readLine();
if(name[fn][rn] != null){
System.out.println("This room is busy!");
}else{
name[fn][rn]=temp;
}
}else{
System.out.println("This room "+rn+" is not have.");
}
}else{
System.out.println("This floor "+fn+" is not have.");
}
}else if(condition == 2){
System.out.print("Floor number = ");
int fn = Integer.parseInt(in.readLine());
if(fn>=0 && fn < num){
System.out.print("Room number = ");
int rn = Integer.parseInt(in.readLine());
if(rn>=0 && rn < num){
System.out.print("Customer name = ");
temp = in.readLine();
if(name[fn][rn] !=null)
name[fn][rn]=null;
else
System.out.println("This room is already check out!");
}else{
System.out.println("This room "+rn+" is not have.");
}
}else{
System.out.println("This floor "+fn+" is not have.");
}
}else if(condition == 3){
for(int i=0;i<num;i++){
System.out.println(i+" floor!");
for(int j=0;j<num1;j++){
System.out.print("Room No."+j+" : "+name[i][j]+"\t");
}
System.out.println();
}
}else if(condition == 4){
System.out.print("End...");
System.exit(0);
}else{
System.out.println("Please choose that has available in option!");
}
}
}
}
package homework;
import java.io.*;
public class Topic8 {
public static void main(String[] args) throws IOException{
if(args.length !=1)
System.exit(0);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(args[0]);
//int num=Integer.parseInt(in.readLine());
String[] name = new String[num];
String temp ="";
while(true){
System.out.print("1. Check In, 2. Check Out, 3. Show All, 4. Exit = ");
int condition= Integer.parseInt(in.readLine());
if(condition == 1){
System.out.print("Check In : Room Number ? ");
int cr = Integer.parseInt(in.readLine());
if(cr>=0 && cr < name.length){
System.out.print("name = ");
temp=in.readLine();
if(name[cr] != null){
System.out.println("This room is busy!");
}else{
name[cr]=temp;
}
}else{
System.out.println("This room "+cr+" is not have.");
}
}else if(condition == 2){
System.out.print("Check Out : Room Number ? ");
int co = Integer.parseInt(in.readLine());
if(co>=0 && co < name.length){
if(name[co] !=null)
name[co]=null;
else
System.out.println("This room is already check out!");
}else{
System.out.println("Room "+co+" is not have.");
}
}else if(condition == 3){
for(int i=0;i<name.length;i++)
System.out.print("Room No."+i+" = "+name[i]+"\t");
System.out.println();
}else if(condition == 4){
System.out.print("End...");
System.exit(0);
}else{
System.out.println("Please choose that has available in option!");
}
}
}
}
-Use tow dimensional Array:
package homework;
import java.io.*;
public class Topic10 {
public static void main(String[] args) throws IOException{
if(args.length !=2)
System.exit(0);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(args[0]);//floor
int num1 = Integer.parseInt(args[1]);//room
String[][] name = new String[num][num1];
String temp="";
while(true){
System.out.print("1. Check In, 2. Check Out, 3. Show All, 4. Exit = ");
int condition = Integer.parseInt(in.readLine());
if(condition == 1){
System.out.print("Floor number = ");
int fn = Integer.parseInt(in.readLine());
if(fn>=0 && fn < num){
System.out.print("Room number = ");
int rn = Integer.parseInt(in.readLine());
if(rn>=0 && rn < num){
System.out.print("Customer name = ");
temp = in.readLine();
if(name[fn][rn] != null){
System.out.println("This room is busy!");
}else{
name[fn][rn]=temp;
}
}else{
System.out.println("This room "+rn+" is not have.");
}
}else{
System.out.println("This floor "+fn+" is not have.");
}
}else if(condition == 2){
System.out.print("Floor number = ");
int fn = Integer.parseInt(in.readLine());
if(fn>=0 && fn < num){
System.out.print("Room number = ");
int rn = Integer.parseInt(in.readLine());
if(rn>=0 && rn < num){
System.out.print("Customer name = ");
temp = in.readLine();
if(name[fn][rn] !=null)
name[fn][rn]=null;
else
System.out.println("This room is already check out!");
}else{
System.out.println("This room "+rn+" is not have.");
}
}else{
System.out.println("This floor "+fn+" is not have.");
}
}else if(condition == 3){
for(int i=0;i<num;i++){
System.out.println(i+" floor!");
for(int j=0;j<num1;j++){
System.out.print("Room No."+j+" : "+name[i][j]+"\t");
}
System.out.println();
}
}else if(condition == 4){
System.out.print("End...");
System.exit(0);
}else{
System.out.println("Please choose that has available in option!");
}
}
}
}
Score Management in java
- Use one dimensional Array:
package homework;
import java.io.*;
public class Topic7 {
public static void main(String[] args) throws IOException{
if(args.length != 1)
System.exit(0);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int number_of_student= Integer.parseInt(args[0]);
//int number_of_student= Integer.parseInt(in.readLine());
String[] name = new String[number_of_student];
int[] kor = new int[number_of_student];
int[] eng = new int[number_of_student];
int[] mat = new int[number_of_student];
int[] tot = new int[number_of_student];
float[] avg = new float[number_of_student];
int[] rank = new int[number_of_student];
for(int i=1;i<=number_of_student;i++){
System.out.println("No. "+i+" Student Info :");
System.out.print("Name ? ");
name[i-1] = in.readLine();
System.out.print("kor ? ");
kor[i-1]=Integer.parseInt(in.readLine());
System.out.print("eng ? ");
eng[i-1]=Integer.parseInt(in.readLine());
System.out.print("mat ? ");
mat[i-1]=Integer.parseInt(in.readLine());
tot[i-1]=kor[i-1]+eng[i-1]+mat[i-1];
avg[i-1]=tot[i-1]/3;
rank[i-1]=1;
}
for(int i=0;i<number_of_student;i++){
for(int j=0;j<number_of_student;j++){
if(avg[i]>avg[j]) rank[j]++;
}
}
System.out.println();
for(int i=0;i<number_of_student;i++){
System.out.println(name[i]+"\t"+ kor[i] +"\t"+eng[i]+"\t"+mat[i]+"\t"+tot[i]+"\t"+avg[i]+"\t"+rank[i]);
}
}
}
-Use tow dimensional Array:
package homework;
import java.io.*;
public class Topic9 {
public static void main(String[] args) throws IOException{
if(args.length < 2){
System.out.println("Please you input argument at least 2: ");
System.exit(0);
}
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many classes ? ");
int x = Integer.parseInt(in.readLine());
String[][] name = new String[x][];
float[][][] sub = new float[x][][];
float[][]tot=new float[x][];
int[][]crank=new int[x][];
int[][]rank=new int[x][];
float[][] avg = new float[x][];
for(int i=0;i<x;i++){
System.out.print(i+"th class: number of students? ");
int num_of_student_per_class=Integer.parseInt(in.readLine());
name[i] = new String[num_of_student_per_class];
sub[i]=new float[name[i].length][args.length];
tot[i]=new float[name[i].length];
crank[i]=new int[name[i].length];
rank[i]=new int[name[i].length];
avg[i]=new float[name[i].length];
}
for(int i=0;i<x;i++){
for(int j=0;j<name[i].length;j++){
System.out.print("class"+i+": students"+j+"'s name = ");
name[i][j]=in.readLine();
for(int k=0;k<args.length;k++){
do{
System.out.print(args[k]+" = ");
sub[i][j][k]=Integer.parseInt(in.readLine());
}while(sub[i][j][k]<0 || sub[i][j][k]>100);
tot[i][j] +=sub[i][j][k];
}
crank[i][j]=1;rank[i][j]=1;avg[i][j]=(tot[i][j]/(args.length));
}
}
for(int i=0;i<name.length;i++){
for(int j=0;j<name[i].length;j++){
for(int k=0;k<name[i].length;k++)
if(tot[i][j]>tot[i][k]) crank[i][k]++;
}
}
for(int i=0;i<avg.length;i++)
for(int j=0;j<avg[i].length;j++)
for(int m=0;m<avg.length;m++)
for(int k=0;k<avg[m].length;k++)
if(avg[i][j]>avg[m][k]) rank[m][k]++;
System.out.println();
for(int i=0;i<x;i++){
for(int j=0;j<name[i].length;j++){
System.out.print(name[i][j]+"\t");
for(int k=0;k<sub[i][j].length;k++){
System.out.print(sub[i][j][k]+"\t");
}
System.out.print(tot[i][j]+"\t"+crank[i][j]+"\t"+rank[i][j]+"\t"+avg[i][j]+"\t");
System.out.println();
}
}
}
}
package homework;
import java.io.*;
public class Topic7 {
public static void main(String[] args) throws IOException{
if(args.length != 1)
System.exit(0);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int number_of_student= Integer.parseInt(args[0]);
//int number_of_student= Integer.parseInt(in.readLine());
String[] name = new String[number_of_student];
int[] kor = new int[number_of_student];
int[] eng = new int[number_of_student];
int[] mat = new int[number_of_student];
int[] tot = new int[number_of_student];
float[] avg = new float[number_of_student];
int[] rank = new int[number_of_student];
for(int i=1;i<=number_of_student;i++){
System.out.println("No. "+i+" Student Info :");
System.out.print("Name ? ");
name[i-1] = in.readLine();
System.out.print("kor ? ");
kor[i-1]=Integer.parseInt(in.readLine());
System.out.print("eng ? ");
eng[i-1]=Integer.parseInt(in.readLine());
System.out.print("mat ? ");
mat[i-1]=Integer.parseInt(in.readLine());
tot[i-1]=kor[i-1]+eng[i-1]+mat[i-1];
avg[i-1]=tot[i-1]/3;
rank[i-1]=1;
}
for(int i=0;i<number_of_student;i++){
for(int j=0;j<number_of_student;j++){
if(avg[i]>avg[j]) rank[j]++;
}
}
System.out.println();
for(int i=0;i<number_of_student;i++){
System.out.println(name[i]+"\t"+ kor[i] +"\t"+eng[i]+"\t"+mat[i]+"\t"+tot[i]+"\t"+avg[i]+"\t"+rank[i]);
}
}
}
-Use tow dimensional Array:
package homework;
import java.io.*;
public class Topic9 {
public static void main(String[] args) throws IOException{
if(args.length < 2){
System.out.println("Please you input argument at least 2: ");
System.exit(0);
}
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many classes ? ");
int x = Integer.parseInt(in.readLine());
String[][] name = new String[x][];
float[][][] sub = new float[x][][];
float[][]tot=new float[x][];
int[][]crank=new int[x][];
int[][]rank=new int[x][];
float[][] avg = new float[x][];
for(int i=0;i<x;i++){
System.out.print(i+"th class: number of students? ");
int num_of_student_per_class=Integer.parseInt(in.readLine());
name[i] = new String[num_of_student_per_class];
sub[i]=new float[name[i].length][args.length];
tot[i]=new float[name[i].length];
crank[i]=new int[name[i].length];
rank[i]=new int[name[i].length];
avg[i]=new float[name[i].length];
}
for(int i=0;i<x;i++){
for(int j=0;j<name[i].length;j++){
System.out.print("class"+i+": students"+j+"'s name = ");
name[i][j]=in.readLine();
for(int k=0;k<args.length;k++){
do{
System.out.print(args[k]+" = ");
sub[i][j][k]=Integer.parseInt(in.readLine());
}while(sub[i][j][k]<0 || sub[i][j][k]>100);
tot[i][j] +=sub[i][j][k];
}
crank[i][j]=1;rank[i][j]=1;avg[i][j]=(tot[i][j]/(args.length));
}
}
for(int i=0;i<name.length;i++){
for(int j=0;j<name[i].length;j++){
for(int k=0;k<name[i].length;k++)
if(tot[i][j]>tot[i][k]) crank[i][k]++;
}
}
for(int i=0;i<avg.length;i++)
for(int j=0;j<avg[i].length;j++)
for(int m=0;m<avg.length;m++)
for(int k=0;k<avg[m].length;k++)
if(avg[i][j]>avg[m][k]) rank[m][k]++;
System.out.println();
for(int i=0;i<x;i++){
for(int j=0;j<name[i].length;j++){
System.out.print(name[i][j]+"\t");
for(int k=0;k<sub[i][j].length;k++){
System.out.print(sub[i][j][k]+"\t");
}
System.out.print(tot[i][j]+"\t"+crank[i][j]+"\t"+rank[i][j]+"\t"+avg[i][j]+"\t");
System.out.println();
}
}
}
}
Tuesday, June 11, 2013
Sample Calculator in Java
package homework;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
JTextField field;
JButton numbtn[]=new JButton[10];
JButton plus,minus,multi,div,equal,sqrt,point,c,per,divbyx;
JPanel panel,btnpanel,oppanel;
Container conpane;
String s1,s2,s3,s4;
int op;
JMenuBar mbar;
JMenu file;
JMenuItem exit;
public Calculator()
{
super("Calculator");
setSize(320,240);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
mbar=new JMenuBar();
setJMenuBar(mbar);
file=new JMenu("File");
file.setMnemonic((int)'F');
exit=new JMenuItem("Exit");
exit.addActionListener(this);
panel=new JPanel();
mbar.add(file);
file.add(exit);
panel.setLayout(new FlowLayout());
field=new JTextField(36);
panel.add(field);
conpane=getContentPane();
conpane.add(panel,BorderLayout.NORTH);
btnpanel=new JPanel();
oppanel=new JPanel();
btnpanel.setBorder(BorderFactory.createTitledBorder(""));
btnpanel.add(point=new JButton("."));
point.addActionListener(this);
for(int i=9;i>=0;i--)
{
numbtn[i]=new JButton(Integer.toString(i));
numbtn[i].addActionListener(this);
numbtn[i].setToolTipText(Integer.toString(i));
btnpanel.add(numbtn[i]);
}
c=new JButton("C");
plus=new JButton("+");
minus=new JButton("-");
multi=new JButton("*");
div=new JButton("/");
equal=new JButton("=");
sqrt=new JButton("sqrt");
btnpanel.setLayout(null);
conpane.add(btnpanel,BorderLayout.CENTER);
btnpanel.add(plus);
btnpanel.add(minus);
btnpanel.add(multi);
btnpanel.add(div);
btnpanel.add(equal);
btnpanel.add(sqrt);
btnpanel.add(c);
per=new JButton("%");
divbyx=new JButton("1/x");
btnpanel.add(per);
btnpanel.add(divbyx);
plus.addActionListener(this);
minus.addActionListener(this);
multi.addActionListener(this);
div.addActionListener(this);
sqrt.addActionListener(this);
equal.addActionListener(this);
per.addActionListener(this);
divbyx.addActionListener(this);
numbtn[0].setBounds(10,120,50,20);
point.setBounds(70,120,50,20);
equal.setBounds(130,120,50,20);
div.setBounds(190,120,50,20);
numbtn[1].setBounds(10,90,50,20);
numbtn[2].setBounds(70,90,50,20);
numbtn[3].setBounds(130,90,50,20);
multi.setBounds(190,90,50,20);
numbtn[4].setBounds(10,60,50,20);
numbtn[5].setBounds(70,60,50,20);
numbtn[6].setBounds(130,60,50,20);
minus.setBounds(190,60,50,20);
numbtn[7].setBounds(10,30,50,20);
numbtn[8].setBounds(70,30,50,20);
numbtn[9].setBounds(130,30,50,20);
plus.setBounds(190,30,50,20);
c.setBounds(250,30,60,20);
sqrt.setBounds(250,60,60,20);
per.setBounds(250,90,60,20);
divbyx.setBounds(250,120,60,20);
c.addActionListener(this);
field.setHorizontalAlignment(JTextField.RIGHT);
plus.setToolTipText("Addition");
minus.setToolTipText("Subtraction");
multi.setToolTipText("Multiplication");
div.setToolTipText("Divide");
equal.setToolTipText("Equals");
c.setToolTipText("Clear Field");
sqrt.setToolTipText("Square Root");
per.setToolTipText("Percentage");
divbyx.setToolTipText("Divide By One");
point.setToolTipText("Decimal Point");
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Unsupported Look And Feel"+e);
}
field.setEditable(false);
field.setBackground(new Color(255,255,255));
field.setForeground(new Color(0,0,0));
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==exit)
{
System.exit(0);
}
else
if(evt.getSource()==numbtn[0])
{
String t=field.getText();
clear();
field.setText(t+"0");
}
else
if(evt.getSource()==numbtn[1])
{
String t=field.getText();
clear();
field.setText(t+"1");
}
else
if(evt.getSource()==numbtn[2])
{
String t=field.getText();
clear();
field.setText(t+"2");
}
else
if(evt.getSource()==numbtn[3])
{
String t=field.getText();
clear();
field.setText(t+"3");
}
else
if(evt.getSource()==numbtn[4])
{
String t=field.getText();
clear();
field.setText(t+"4");
}
else
if(evt.getSource()==numbtn[5])
{
String t=field.getText();
clear();
field.setText(t+"5");
}
else
if(evt.getSource()==numbtn[6])
{
String t=field.getText();
clear();
field.setText(t+"6");
}
else
if(evt.getSource()==numbtn[7])
{
String t=field.getText();
clear();
field.setText(t+"7");
}
else
if(evt.getSource()==numbtn[8])
{
String t=field.getText();
clear();
field.setText(t+"8");
}
else
if(evt.getSource()==numbtn[9])
{
String t=field.getText();
clear();
field.setText(t+"9");
}
else
if(evt.getSource()==point)
{
String t=field.getText();
clear();
field.setText(t+".");
}
else
if(evt.getSource()==plus)
{
s1=field.getText();
clear();
op=1;
}
else
if(evt.getSource()==minus)
{
s1=field.getText();
clear();
op=2;
}
else
if(evt.getSource()==multi)
{
s1=field.getText();
clear();
op=3;
}
else
if(evt.getSource()==div)
{
s1=field.getText();
clear();
op=4;
}
else
if(evt.getSource()==c)
{
field.setText("");
}
else
if(evt.getSource()==equal)
{
s2=field.getText();
if(op==1)
{
double n;
n=Double.parseDouble(s1)+Double.parseDouble(s2);
field.setText(String.valueOf(n));
}
else
if(op==2)
{
double n;
n=Double.parseDouble(s1)-Double.parseDouble(s2);
field.setText(String.valueOf(n));
}
else
if(op==3)
{
double n;
n=Double.parseDouble(s1)*Double.parseDouble(s2);
field.setText(String.valueOf(n));
}
else
if(op==4)
{
double n;
double n1=Double.parseDouble(s1);
double n2=Double.parseDouble(s2);
if(n1!=0 && n2!=0)
{
n=n1/n2;
field.setText(String.valueOf(n));
}
else
{
field.setText("Divide By Zero Error");
}
}
}
else
if(evt.getSource()==sqrt)
{
String s=field.getText();
double no=Double.parseDouble(s);
double result=Math.sqrt(no);
field.setText(String.valueOf(result));
}
else
if(evt.getSource()==divbyx)
{
String s=field.getText();
double no=Double.parseDouble(s);
double result=1/no;
field.setText(String.valueOf(result));
}
else
if(evt.getSource()==per)
{
s2=field.getText();
double no1=Double.parseDouble(s1);
double no2=Double.parseDouble(s2);
if(op==3)
{
double result=(no1/100)*no2;
field.setText(String.valueOf(result));
}
else
field.setText("Invalid");
}
}
public void clear()
{
field.setText("");
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
Calculator c = new Calculator();
c.setVisible(true);
}
});
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
JTextField field;
JButton numbtn[]=new JButton[10];
JButton plus,minus,multi,div,equal,sqrt,point,c,per,divbyx;
JPanel panel,btnpanel,oppanel;
Container conpane;
String s1,s2,s3,s4;
int op;
JMenuBar mbar;
JMenu file;
JMenuItem exit;
public Calculator()
{
super("Calculator");
setSize(320,240);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
mbar=new JMenuBar();
setJMenuBar(mbar);
file=new JMenu("File");
file.setMnemonic((int)'F');
exit=new JMenuItem("Exit");
exit.addActionListener(this);
panel=new JPanel();
mbar.add(file);
file.add(exit);
panel.setLayout(new FlowLayout());
field=new JTextField(36);
panel.add(field);
conpane=getContentPane();
conpane.add(panel,BorderLayout.NORTH);
btnpanel=new JPanel();
oppanel=new JPanel();
btnpanel.setBorder(BorderFactory.createTitledBorder(""));
btnpanel.add(point=new JButton("."));
point.addActionListener(this);
for(int i=9;i>=0;i--)
{
numbtn[i]=new JButton(Integer.toString(i));
numbtn[i].addActionListener(this);
numbtn[i].setToolTipText(Integer.toString(i));
btnpanel.add(numbtn[i]);
}
c=new JButton("C");
plus=new JButton("+");
minus=new JButton("-");
multi=new JButton("*");
div=new JButton("/");
equal=new JButton("=");
sqrt=new JButton("sqrt");
btnpanel.setLayout(null);
conpane.add(btnpanel,BorderLayout.CENTER);
btnpanel.add(plus);
btnpanel.add(minus);
btnpanel.add(multi);
btnpanel.add(div);
btnpanel.add(equal);
btnpanel.add(sqrt);
btnpanel.add(c);
per=new JButton("%");
divbyx=new JButton("1/x");
btnpanel.add(per);
btnpanel.add(divbyx);
plus.addActionListener(this);
minus.addActionListener(this);
multi.addActionListener(this);
div.addActionListener(this);
sqrt.addActionListener(this);
equal.addActionListener(this);
per.addActionListener(this);
divbyx.addActionListener(this);
numbtn[0].setBounds(10,120,50,20);
point.setBounds(70,120,50,20);
equal.setBounds(130,120,50,20);
div.setBounds(190,120,50,20);
numbtn[1].setBounds(10,90,50,20);
numbtn[2].setBounds(70,90,50,20);
numbtn[3].setBounds(130,90,50,20);
multi.setBounds(190,90,50,20);
numbtn[4].setBounds(10,60,50,20);
numbtn[5].setBounds(70,60,50,20);
numbtn[6].setBounds(130,60,50,20);
minus.setBounds(190,60,50,20);
numbtn[7].setBounds(10,30,50,20);
numbtn[8].setBounds(70,30,50,20);
numbtn[9].setBounds(130,30,50,20);
plus.setBounds(190,30,50,20);
c.setBounds(250,30,60,20);
sqrt.setBounds(250,60,60,20);
per.setBounds(250,90,60,20);
divbyx.setBounds(250,120,60,20);
c.addActionListener(this);
field.setHorizontalAlignment(JTextField.RIGHT);
plus.setToolTipText("Addition");
minus.setToolTipText("Subtraction");
multi.setToolTipText("Multiplication");
div.setToolTipText("Divide");
equal.setToolTipText("Equals");
c.setToolTipText("Clear Field");
sqrt.setToolTipText("Square Root");
per.setToolTipText("Percentage");
divbyx.setToolTipText("Divide By One");
point.setToolTipText("Decimal Point");
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Unsupported Look And Feel"+e);
}
field.setEditable(false);
field.setBackground(new Color(255,255,255));
field.setForeground(new Color(0,0,0));
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==exit)
{
System.exit(0);
}
else
if(evt.getSource()==numbtn[0])
{
String t=field.getText();
clear();
field.setText(t+"0");
}
else
if(evt.getSource()==numbtn[1])
{
String t=field.getText();
clear();
field.setText(t+"1");
}
else
if(evt.getSource()==numbtn[2])
{
String t=field.getText();
clear();
field.setText(t+"2");
}
else
if(evt.getSource()==numbtn[3])
{
String t=field.getText();
clear();
field.setText(t+"3");
}
else
if(evt.getSource()==numbtn[4])
{
String t=field.getText();
clear();
field.setText(t+"4");
}
else
if(evt.getSource()==numbtn[5])
{
String t=field.getText();
clear();
field.setText(t+"5");
}
else
if(evt.getSource()==numbtn[6])
{
String t=field.getText();
clear();
field.setText(t+"6");
}
else
if(evt.getSource()==numbtn[7])
{
String t=field.getText();
clear();
field.setText(t+"7");
}
else
if(evt.getSource()==numbtn[8])
{
String t=field.getText();
clear();
field.setText(t+"8");
}
else
if(evt.getSource()==numbtn[9])
{
String t=field.getText();
clear();
field.setText(t+"9");
}
else
if(evt.getSource()==point)
{
String t=field.getText();
clear();
field.setText(t+".");
}
else
if(evt.getSource()==plus)
{
s1=field.getText();
clear();
op=1;
}
else
if(evt.getSource()==minus)
{
s1=field.getText();
clear();
op=2;
}
else
if(evt.getSource()==multi)
{
s1=field.getText();
clear();
op=3;
}
else
if(evt.getSource()==div)
{
s1=field.getText();
clear();
op=4;
}
else
if(evt.getSource()==c)
{
field.setText("");
}
else
if(evt.getSource()==equal)
{
s2=field.getText();
if(op==1)
{
double n;
n=Double.parseDouble(s1)+Double.parseDouble(s2);
field.setText(String.valueOf(n));
}
else
if(op==2)
{
double n;
n=Double.parseDouble(s1)-Double.parseDouble(s2);
field.setText(String.valueOf(n));
}
else
if(op==3)
{
double n;
n=Double.parseDouble(s1)*Double.parseDouble(s2);
field.setText(String.valueOf(n));
}
else
if(op==4)
{
double n;
double n1=Double.parseDouble(s1);
double n2=Double.parseDouble(s2);
if(n1!=0 && n2!=0)
{
n=n1/n2;
field.setText(String.valueOf(n));
}
else
{
field.setText("Divide By Zero Error");
}
}
}
else
if(evt.getSource()==sqrt)
{
String s=field.getText();
double no=Double.parseDouble(s);
double result=Math.sqrt(no);
field.setText(String.valueOf(result));
}
else
if(evt.getSource()==divbyx)
{
String s=field.getText();
double no=Double.parseDouble(s);
double result=1/no;
field.setText(String.valueOf(result));
}
else
if(evt.getSource()==per)
{
s2=field.getText();
double no1=Double.parseDouble(s1);
double no2=Double.parseDouble(s2);
if(op==3)
{
double result=(no1/100)*no2;
field.setText(String.valueOf(result));
}
else
field.setText("Invalid");
}
}
public void clear()
{
field.setText("");
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
Calculator c = new Calculator();
c.setVisible(true);
}
});
}
}
Thursday, June 6, 2013
How to create Traingle using English aphabet in java
Please see this sample code:
public class TraingleAphabet1 {
public static void main(String[] args) {
String space =" ";
for(char i='M',increment ='N';i<=increment;i--,increment++){
if(increment>'Z' && i< 'A')
break;
System.out.print(space);
for(char j=i;j<=increment;j++)
System.out.print(j);
if(space.length()==0)
break;
space =space.substring(1);
System.out.println();
}
}
}
Result:
MN
LMNO
KLMNOP
JKLMNOPQ
IJKLMNOPQR
HIJKLMNOPQRS
GHIJKLMNOPQRST
FGHIJKLMNOPQRSTU
EFGHIJKLMNOPQRSTUV
DEFGHIJKLMNOPQRSTUVW
CDEFGHIJKLMNOPQRSTUVWX
BCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ
public class TraingleAphabet1 {
public static void main(String[] args) {
String space =" ";
for(char i='M',increment ='N';i<=increment;i--,increment++){
if(increment>'Z' && i< 'A')
break;
System.out.print(space);
for(char j=i;j<=increment;j++)
System.out.print(j);
if(space.length()==0)
break;
space =space.substring(1);
System.out.println();
}
}
}
Result:
MN
LMNO
KLMNOP
JKLMNOPQ
IJKLMNOPQR
HIJKLMNOPQRS
GHIJKLMNOPQRST
FGHIJKLMNOPQRSTU
EFGHIJKLMNOPQRSTUV
DEFGHIJKLMNOPQRSTUVW
CDEFGHIJKLMNOPQRSTUVWX
BCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ
How to create Traingle using alphabet in java
Please see this sample code:
public class TraingleAphabet {
public static void main(String[] args) {
for(char i='M',increment ='N';i<=increment;i--,increment++){
if(increment>'Z' && i< 'A')
break;
for(char j=i;j<=increment;j++)
System.out.print(j);
System.out.println();
}
}
}
Result:
MN
LMNO
KLMNOP
JKLMNOPQ
IJKLMNOPQR
HIJKLMNOPQRS
GHIJKLMNOPQRST
FGHIJKLMNOPQRSTU
EFGHIJKLMNOPQRSTUV
DEFGHIJKLMNOPQRSTUVW
CDEFGHIJKLMNOPQRSTUVWX
BCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ
public class TraingleAphabet {
public static void main(String[] args) {
for(char i='M',increment ='N';i<=increment;i--,increment++){
if(increment>'Z' && i< 'A')
break;
for(char j=i;j<=increment;j++)
System.out.print(j);
System.out.println();
}
}
}
Result:
MN
LMNO
KLMNOP
JKLMNOPQ
IJKLMNOPQR
HIJKLMNOPQRS
GHIJKLMNOPQRST
FGHIJKLMNOPQRSTU
EFGHIJKLMNOPQRSTUV
DEFGHIJKLMNOPQRSTUVW
CDEFGHIJKLMNOPQRSTUVWX
BCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ
How to make Traingle English Aphabet in java
Please see this sample code:
public class TraingleLetter {
public static void main(String[] args) {
char last='Z';
String space ="";
for(char i='A';i<='Z';i++){
System.out.print(space);
for(char j=i;j<=last;j++){
System.out.print(j);
}
last =(char)((int)last - 1);
space = space +" ";
System.out.println();
}
}
}
Result :
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXY
CDEFGHIJKLMNOPQRSTUVWX
DEFGHIJKLMNOPQRSTUVW
EFGHIJKLMNOPQRSTUV
FGHIJKLMNOPQRSTU
GHIJKLMNOPQRST
HIJKLMNOPQRS
IJKLMNOPQR
JKLMNOPQ
KLMNOP
LMNO
MN
public class TraingleLetter {
public static void main(String[] args) {
char last='Z';
String space ="";
for(char i='A';i<='Z';i++){
System.out.print(space);
for(char j=i;j<=last;j++){
System.out.print(j);
}
last =(char)((int)last - 1);
space = space +" ";
System.out.println();
}
}
}
Result :
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXY
CDEFGHIJKLMNOPQRSTUVWX
DEFGHIJKLMNOPQRSTUVW
EFGHIJKLMNOPQRSTUV
FGHIJKLMNOPQRSTU
GHIJKLMNOPQRST
HIJKLMNOPQRS
IJKLMNOPQR
JKLMNOPQ
KLMNOP
LMNO
MN
Subscribe to:
Comments (Atom)