You are on page 1of 29

Pachara R Project Java Basics Intro In this program, I put all the programs that Ive done throughout

my first semester of Java Class. I have the GUI Panel along with items for main folders and sub items for each program to access each program. Folders are Math Programs, Algorithm Programs, and Graph Programs. Within Math folder, there are calculation programs for harmonic numbers, factorial number, grade calculator, and conversion of binary number to decimal. Within Algorithm folder, there are programs to swap characters, integers, bubble sort, and reverse strings. Within Graph folder, there is the program to draw using mouse draw. In the math folder, the harmonic program takes an input and compute each harmonic term and gives the summation of the harmonic series. The factorial program takes the input number and computes the products and returns the factorial number. The grade calculator program takes the input and finds the corresponding letter grade and shows it. The conversion program takes the binary number and finds the remainders of the input divided by two to compute the decimal number. In the algorithm folder, the program to swap characters takes the two input strings and swaps only the first two characters with each other. The program to swap integers takes the two input integers and swap the whole set of number with each other. In bubble sort program, the program takes the input array of numbers and check if the first term is larger than the second term, if yes, then it will swap the two numbers then check if the second term is larger than the third term and so on; when the program reaches the end of the array, numbers will be sorted in an increasing order. In reverse string program, the program takes the input string and swaps the characters from the beginning to the end.

In the graph folder, the drawing folder will create a drawing panel, which the user can draw lines on it using mouse.

Here is the link to my .jar compiled file https://skydrive.live.com/redir.aspx?cid=32d20acd483a9fbe&resid=32D20ACD483A9FBE!151 &parid=32D20ACD483A9FBE!148&authkey=!AC-orFlU84YT_IQ

Here is the link to my source code

https://skydrive.live.com/redir.aspx?cid=32d20acd483a9fbe&resid=32D20ACD483A9FBE!1 50&parid=32D20ACD483A9FBE!149&authkey=!ABxP4Pg3vrFOJlc

Math

JTextField In 1-3

Math calculations for: Harmonics Factorial Convert Decimal to Binary Letter Grade

JTextField Out 1-3

Harmonics: Letter Grade: Methods Check the input value corresponds to which letter grade. Methods The loop goes on as long as the input number. Then each time the loop is run, the result will be added with 1/the amount of time the loop has been running.

Factorial: Methods The loop goes on as long as the input number. Then each time the loop is run, the result will be multiplied with the amount of time the loop has been running.

Convert Decimal to Binary: Methods The loop runs as long as the input number is more than 0. Each time the loop is run, the input will be divided by 10 and its remainder will be saved.

Algorithm

Parse input

JTextField In 1-3

Jbuttons 1-3

Count items, Construct a string[] element for every separator, stop when reach end of input string Int[] a Char[] a

Add features for more separators and trimming: s.charAt(i) == ',' || s.charAt(element) !=' ' ||s.charAt(element) !='-' And For(int I = 0; i<p.values.lenght; i++){ p.values[i].trim() ; }

String[] s JTextField Out 1-3 Jbutton Event handler Parse output

Construct output string in a loop

Add features for more types of input for sorting:

Integer = Change the data type of the storage object into integer. Double = Change the data type of the storage object into double. Character = Change the data type of the storage object into character.

BubbleSort 3 overloaded methods

Graph

JTextField In 1-3

JTextField Out 1-3 Graph programs for: Bouncing Ball Drawing

Drawing: Methods Set the back ground color and instantiate the Mouse Painter. Add mouse motion listener to the mouse pointer.

package GUI; import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; /** * * @author pratanabanthoon */ public class Project extends JFrame{ private JMenuBar menubar ; private JMenu mathmenu, graphmenu, algorithmmenu ; //main private JMenuItem draw, string, integ, harmo, grade, bounce, pan, fac, btd, bbs, rs ; //sub private static myWinHandler w ; private Gui_MouseDraw2 mouse; private HarmonicNumber harmon ; private BinaryToDecimal bidem ; private BubbleSort bubble ; private ReverseString reverse ; private GradeInput gradein ; private SwapString stringsw ; private SwapInt numsw ; private Gui_MousePanel2 panel ; private BouncingBall bouncing ; private Factorial factorial ; private Container c ; public Project() { super("programs in sem1") ; c = getContentPane() ; //assign each item and sub item into the panel menubar = new JMenuBar() ; mathmenu = new JMenu("Math") ; menubar.add(mathmenu) ; harmo = new JMenuItem("Harmonics") ; mathmenu.add(harmo) ; grade = new JMenuItem("Grade") ; mathmenu.add(grade) ; fac = new JMenuItem("Factorials") ; mathmenu.add(fac) ; btd = new JMenuItem("Binary To Decimal") ; mathmenu.add(btd) ;

bbs = new JMenuItem("Bubble Sort") ; mathmenu.add(bbs) ; graphmenu = new JMenu("Graph") ; menubar.add(graphmenu) ; algorithmmenu = new JMenu("Algorithm") ; menubar.add(algorithmmenu) ; draw = new JMenuItem("Drawing") ; graphmenu.add(draw) ; pan = new JMenuItem("Panel") ; graphmenu.add(pan) ; bounce = new JMenuItem("Bouncing Ball") ; graphmenu.add(bounce) ; string = new JMenuItem("Swap Strings") ; algorithmmenu.add(string) ; integ = new JMenuItem("Swap Integer") ; algorithmmenu.add(integ) ; rs = new JMenuItem("Reverse Strings") ; algorithmmenu.add(rs) ; c.add(menubar, BorderLayout.NORTH) ; menuHandler h = new menuHandler() ; w = new myWinHandler() ; //add action listener to each of sub itme draw.addActionListener(h); bounce.addActionListener(h); rs.addActionListener(h); bbs.addActionListener(h); btd.addActionListener(h); fac.addActionListener(h); pan.addActionListener(h); harmo.addActionListener(h); grade.addActionListener(h); string.addActionListener(h); integ.addActionListener(h);

setSize(600,600) ; setVisible(true) ; } private class menuHandler implements ActionListener{

public void actionPerformed (ActionEvent e) { //action listener that see which sub item we choose if(e.getSource() == draw) mouse = new Gui_MouseDraw2() ; else if(e.getSource() == string) stringsw = new SwapString() ; else if(e.getSource() == integ) numsw = new SwapInt() ; else if(e.getSource() == grade) gradein = new GradeInput() ; else if(e.getSource() == harmo) harmon = new HarmonicNumber() ; else if(e.getSource() == pan) panel = new Gui_MousePanel2() ; else if(e.getSource() == bounce) bouncing = new BouncingBall() ; else if(e.getSource() == fac) factorial = new Factorial() ; else if(e.getSource() == string) stringsw = new SwapString() ; else if(e.getSource() == rs) reverse = new ReverseString() ;if(e.getSource() == draw) mouse = new Gui_MouseDraw2() ; else if(e.getSource() == string) stringsw = new SwapString() ; else if(e.getSource() == integ) numsw = new SwapInt() ; else if(e.getSource() == grade) gradein = new GradeInput() ; else if(e.getSource() == harmo) harmon = new HarmonicNumber() ; else if(e.getSource() == pan) panel = new Gui_MousePanel2() ; else if(e.getSource() == bounce) bouncing = new BouncingBall() ; else if(e.getSource() == fac) factorial = new Factorial() ; else if(e.getSource() == string) stringsw = new SwapString() ; else if(e.getSource() == bbs) bubble = new BubbleSort() ; else if(e.getSource() == btd) bidem = new BinaryToDecimal() ;

} } public static void main (String[] args) {

Project app1 = new Project() ; app1.addWindowListener(w); } private class myWinHandler extends WindowAdapter{ public void windowClosing (WindowEvent w_e){ System.exit(0); } } }

package GUI; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author jonathanwu */ public class BubbleSort { public void bubbleSort(int[] a){ int n=a.length; int temp=a[0]; // swapping two integers for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i]>a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } } } } public void bubbleSort(double[] a){ int n=a.length; double temp=a[0]; // swapping two doubles for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i]>a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } } } } public void bubbleSort(char[] a){ int n=a.length; char temp; // swapping two characters for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i] >a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } }

} } public void bubbleSort(String[] ss){ String temp=ss[0]; // swapping two strings for (int pass=1; pass<ss.length; pass++){ for (int i=0; i<ss.length-pass; i++){ if ((ss[i].compareTo(ss[i+1]))>0){ temp= ss[i]; ss[i]=ss[i+1]; ss[i+1] = temp; } } } } } class BubbleSort_GUI extends JFrame { JButton Integer, Character, string, Double; JTextField Input; JLabel Output; BubbleSort b= new BubbleSort(); Parser_e p= new Parser_e(); String out="",s=""; BubbleSort_GUI() { super("Bubble Sort"); setLayout(new FlowLayout()); //instantiate the input textfield nad output label Input= new JTextField("Input List here"); Output= new JLabel("Sorted List will be displayed here"); //instantiate buttons to each of methods Integer= new JButton("Sort Integer Values"); Character= new JButton("Sort Character Values"); string= new JButton("Sort String Values"); Double = new JButton("Sort Double Values"); //add objects add(Input); add(Integer); add(Character); add(string); add(Double); add(Output); Action a= new Action(); //add action listener

Integer.addActionListener(a); Character.addActionListener(a); string.addActionListener(a); Double.addActionListener(a); setSize(500,500); setVisible(true); } class Action implements ActionListener { public void actionPerformed(ActionEvent e) { //action lestener for integer inputs if(e.getSource()== Integer) { s=Input.getText(); p.parseIn(s); p.convertInt(p.values); b.bubbleSort(p.int_values); out= p.convertInt_String(p.int_values); Output.setText(out); } //action lestener for characters inputs else if(e.getSource()== Character) { s=Input.getText(); p.parseIn(s); p.convertChar(p.values); b.bubbleSort(p.char_values); out= p.convertChar_String(p.char_values); Output.setText(out); } //action lestener for string inputs else if(e.getSource()==string) { s=Input.getText(); p.parseIn(s); b.bubbleSort(p.values); out= p.convertStringArray_String(p.values); Output.setText(out); } //action lestener for double inputs else if(e.getSource()== Double) { s=Input.getText(); p.parseIn(s); p.convertDouble(p.values); b.bubbleSort(p.Double_values); out= p.convertDouble_String(p.Double_values);

Output.setText(out); } } } }

package GUI; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author jonathanwu */ public class BinaryToDecimal extends JFrame{ JTextField txt1, txt2; JButton btn1, btn2; BinaryToDecimal() { super("Binary To Decimal"); setLayout(new FlowLayout()); //----------Input and Output Text Fields--------------------// txt1= new JTextField("Enter Number Here"); txt2= new JTextField("The Result Value displayed here "); txt2.setEditable(false); add(txt1); add(txt2); //----------Decimal To Binary Components--------------------// btn1= new JButton("Binary --> Decimal"); btn2= new JButton("Decimal --> Binary"); add(btn1); add(btn2); //------------------------------------------------------// //-----------Adding Action Listeners-------------// Action a= new Action(); btn1.addActionListener(a); btn2.addActionListener(a); //-----------------------------------------------// setSize(500,500); setVisible(true); } public String BinToInt(int i) { String result; result = Integer.toBinaryString(i);// method in the Integer class that converts Integer values to Binary return result; } public String IntToBin(String str) { long num = Long.parseLong(str);

long rem; while(num > 0) { rem = num % 10; num = num / 10; if(rem != 0 && rem != 1)// checks to see if input is binary { JOptionPane.showMessageDialog(null,"This is not a binary number. /n Please Try again"); } } int i= Integer.parseInt(str,2); String s= "" +i; return s; } class Action implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==btn1) { txt2.setText(BinToInt(Integer.parseInt(txt1.getText()))); } else if(e.getSource()==btn2) { txt2.setText(IntToBin(txt1.getText())); } } } }

package GUI; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * Factorial JFrame Program * @author michaelrodda */ public class Factorial extends JFrame{ // declared variables for GUI private JLabel text; private Container c; private JTextField input; //constuctor is made public Factorial(){ super ("Factorial of a Number"); text = new JLabel("Answer"); c = getContentPane(); TextFieldHandler h = new TextFieldHandler(); input = new JTextField("Enter Number Here."); input.addActionListener(h); c.add(text, BorderLayout.NORTH); c.add(input, BorderLayout.SOUTH);

setSize(200,100); setVisible(true); }

//The factorial for the number input is found public static double findFactorial(int input) { double result = 1; for (int i = 1; i <= input; i++) { result *= ((double)i); } return result; } // event handler is written here and handles the conversion // of the input to the factorial private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ String s = ""; int q = 0; double r = 0;

if (event.getSource()==input){ s = input.getText(); q = Integer.parseInt(s); r = findFactorial(q); s = String.valueOf(r); text.setText(s); } } } //main method public static void main(String[] args){ Factorial app = new Factorial(); windowHandler wl = new windowHandler(); app.addWindowListener(wl); } // window event handler that closes the program if the window is exited private static class windowHandler extends WindowAdapter{ public void windowClosing(WindowEvent event){ System.exit(0); } } }

package GUI; import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * * @author michaelrodda */ public class GradeInput extends JFrame{ private JLabel grade; private JLabel gradeOut; private JTextField input; private Container c; private ActionHandler action; private static myWinHandler w; public GradeInput(){ super("Grade Converter"); // creating the panel and textfield for input c = getContentPane(); action = new ActionHandler(); w = new myWinHandler(); grade = new JLabel("Grade:"); gradeOut = new JLabel(""); input = new JTextField("Enter the Numerical Grade Here."); input.addActionListener(action); //add objects c.add(grade, BorderLayout.WEST); c.add(gradeOut, BorderLayout.EAST); c.add(input, BorderLayout.SOUTH); setSize(200,100); setVisible(true); } public String findGrade(int a){ // innitialize range of numbers to a letter grade String gradereturn = ""; if (a>=87){ gradereturn = "A+"; } else if (a>=82){ gradereturn = "A";

} else if (a>=80){ gradereturn = "A-"; } else if (a>=77){ gradereturn = "B+"; } else if (a>=73){ gradereturn = "B"; } else if (a>=70){ gradereturn = "B-"; } else if (a>=67){ gradereturn = "C+"; } else if (a>=63){ gradereturn = "C"; } else if (a>=60){ gradereturn = "C-"; } else if (a>=57){ gradereturn = "D+"; } else if (a>=53){ gradereturn = "D"; } else if (a>=50){ gradereturn = "D-"; } else{ gradereturn = "F"; } return gradereturn; } private class ActionHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ // action handler to find according letters int q = 0; String s = ""; String r = ""; r = input.getText(); q = Integer.parseInt(r); s = findGrade(q); gradeOut.setText(s); } }

public static void main(String[] args){ GradeInput app = new GradeInput(); app.addWindowListener(w); } private static class myWinHandler extends WindowAdapter{ public void windowClosing (WindowEvent event){ System.exit(0); } } }

package GUI; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author michaelrodda */ public class HarmonicNumber extends JFrame{ private JLabel text; private Container c; private JTextField input; public HarmonicNumber(){ super ("Harmonic Number"); text = new JLabel("Answer"); //text.setEditable(false); c = getContentPane(); TextFieldHandler h = new TextFieldHandler(); input = new JTextField("Enter Number Here."); input.addActionListener(h); //add objects c.add(text, BorderLayout.NORTH); c.add(input, BorderLayout.SOUTH);

setSize(200,100); setVisible(true); }

public static double findHarmonic(int input) { double result = 0; //finding harmonics for (int i = 1; i <= input; i++) { result += (1/(double) i); } return result; } private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ String s = ""; int q = 0; double r = 0;

//textfield handler for inputs if (event.getSource()==input){ s = input.getText(); q = Integer.parseInt(s); r = findHarmonic(q); s = String.valueOf(r); text.setText(s); } } } public static void main(String[] args){ HarmonicNumber app = new HarmonicNumber(); windowHandler wl = new windowHandler(); app.addWindowListener(wl); } private static class windowHandler extends WindowAdapter{ public void windowClosing(WindowEvent event){ System.exit(0); } } }

package GUI; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author RaveenVasudeva */ public class Gui_MouseDraw2 extends JFrame{ private JLabel lbl_Instructions; private Gui_MousePanel MousePanel; public Gui_MouseDraw2(){ super ("Drawing with a mouse"); Container c = getContentPane(); c.setLayout(new BorderLayout()); lbl_Instructions=new JLabel("Drag mouse to draw"); lbl_Instructions.setFont(new Font("Serif", Font.BOLD, 20)); c.add(lbl_Instructions,BorderLayout.SOUTH); MousePanel = new Gui_MousePanel(); c.add(MousePanel, BorderLayout.CENTER); setSize(300,300); setVisible(true); } public static void main(String[] args){ Gui_MouseDraw2 mouse_app1 = new Gui_MouseDraw2(); // mouse_app1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyWinHandler mWinH1 = new MyWinHandler(); mouse_app1.addWindowListener(mWinH1); } private static class MyWinHandler extends WindowAdapter{ public void windowClosing (WindowEvent w_event){ System.exit(0); } } }

package GUI; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author jonathanwu */ public class ReverseString extends JFrame { JTextField txt1, txt2; String h; ReverseString() { super("Reverse String"); setLayout(new FlowLayout()); txt1 = new JTextField("Enter String here"); add(txt1); txt2= new JTextField("Reversed String here"); add(txt2); txt2.setEditable(false); Action a= new Action(); txt1.addActionListener(a); setVisible(true); setSize(500,500); } class Action implements ActionListener { public void actionPerformed(ActionEvent e) { h=reverseString(txt1.getText()); txt2.setText(h); } } String reverseString(String s) { String b=""; try{ for(int i=s.length()-1; i>=0; i--) { b+=s.charAt(i); }} catch(StringIndexOutOfBoundsException e){} return b; }

package GUI; import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; /** * * @author pratanabanthoon */ public class SwapString extends JFrame{ public static JTextField textField1, textField2 ; public static String a, b ; public SwapString(){ super("swapping"); //create container and add both textfields for input and output Container container = getContentPane(); container.setLayout(new FlowLayout()); //textfield for input textField1 = new JTextField(10); container.add(textField1); //textfield for output textField2 = new JTextField(10); container.add(textField2); //call on event handler TextFieldHandler handler = new TextFieldHandler(); textField1.addActionListener(handler); textField2.addActionListener(handler); setSize(500, 100); setVisible(true); } public static void main(String[] args){ String i ; String j ; SwapString application = new SwapString(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void swapping(String i, String j ){ //swapping Strings i and j String t=i; i=j; j=t; //instantiate i and j to a and b respectively Character a = i.charAt(0) ; Character b = j.charAt(0) ; textField1.setText(a+""); textField2.setText(b+"");

} private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ //handle the swapping of Strings swapping(textField1.getText(), textField2.getText()); } } }

package GUI; import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; /** * * @author pratanabanthoon */ public class SwapInt extends JFrame{ public static JTextField textField1, textField2 ; public SwapInt(){ super("swapping integer"); //create container and add both textfields for input and output Container container = getContentPane(); container.setLayout(new FlowLayout()); //textfield for input textField1 = new JTextField(10); container.add(textField1); //textfield for output textField2 = new JTextField(10); container.add(textField2); //call on event handler TextFieldHandler handler = new TextFieldHandler(); textField1.addActionListener(handler); textField2.addActionListener(handler); setSize(500, 100); setVisible(true); } public static void main(String[] args){ String i ; String j ; SwapInt application = new SwapInt(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void swapping(int i, int j ){ //swapping integer i and j int t=i; i=j; j=t; //instantiate i and j to a and b respectively textField1.setText(i+"") ; textField2.setText(j+"") ;

} private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ //handle the swapping of integers swapping(Integer.parseInt(textField1.getText()), Integer.parseInt(textField2.getText())); } } }

You might also like