Sunteți pe pagina 1din 28

The project was designed to showcase different simple java programs in one frame.

The main frame is made with menu panels to show the options for the different programs stored. There are three main categories- Math, Algorithms, and Drawings. The Math category stores the factorial program, harmonic number calculation program, x to the power y program, and the grade calculation program. The Algorithms category stores the Binary to Decimal, Bubble Sort, and the Reverse String programs. The Drawing category stores the bouncing ball and the mouse drawing programs. Each of the programs run from a different window even though they are run from the same window.

Jonathan Wu Project: Showcase of Basic Java Programs 1. Introduction to your first project Word 1 page 50 marks a. System GUI architecture overview a small diagram will also help b. Math briefly list and explain the programs briefly c. Algorithms list and explain d. Drawing explain mouse draw 2. Visio system drawings for above 3 categories should be finalized and ready 100 marks 3. Source code copy-pasted in correct sequence.

Link to the .jar file here Link to the Source Code here

package JavaBasicGUI;

import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; /** * * @author jonathanwu */ // Creates the Main Frame for the ShowCase public class MainFrame extends JFrame{ JMenuBar programMenu; JMenu mathMenu, algorithmMenu, graphicMenu; JMenuItem mouseDraw, bouncingBall, factorial, nHarmonic, bubbleSort, xpowery, letterGrade, reverseString, bin2Dec; //--------- Initial Classes--------// BouncingBall b; MouseDrawing mdraw; Grade g; menuHandler h; Factorial f; BubbleSort_GUI bs; HarmonicNumber hn; power p; ReverseString rs; BinaryToDecimal B; //---------------------------------// public MainFrame() { super("learning programs in sem1"); programMenu= new JMenuBar(); // Main Menu Bar add(programMenu, BorderLayout.NORTH); addMenuItems(); addAction(); setSize(500,500); setVisible(true); } void addMenuItems() { //----------3 Core Menu Items-------------//

mathMenu= new JMenu("Math"); programMenu.add(mathMenu); algorithmMenu= new JMenu("Algorythms"); programMenu.add(algorithmMenu); graphicMenu= new JMenu("Graphics"); programMenu.add(graphicMenu); //--------------------------------------//

//*************Math Menu Items****************// factorial= new JMenuItem("Factorial"); mathMenu.add(factorial); nHarmonic= new JMenuItem("N Harmonic"); mathMenu.add(nHarmonic); xpowery= new JMenuItem("X^Y"); mathMenu.add(xpowery); bin2Dec= new JMenuItem("Binary to Decimal"); mathMenu.add(bin2Dec); //*********************************************//

//***************Algorithm Menu Items*****************//

//----------Bubble Sort--------------// bubbleSort= new JMenuItem("Bubble Sort"); algorithmMenu.add(bubbleSort);

//-------------------------------------------// //-----------------Other options--------------// letterGrade= new JMenuItem("Display Letter Grade"); algorithmMenu.add(letterGrade); reverseString= new JMenuItem("Reverse a String"); algorithmMenu.add(reverseString); //--------------------------------------------// //**************************************************//

//*************Graphics Menu***********************// mouseDraw = new JMenuItem("Mouse Drawing"); graphicMenu.add(mouseDraw); bouncingBall= new JMenuItem("Bouncing Ball"); graphicMenu.add(bouncingBall); //*************************************************//

} void addAction() { h= new menuHandler(); //*************Add Action Listeners***************// mouseDraw.addActionListener(h); xpowery.addActionListener(h); letterGrade.addActionListener(h); bouncingBall.addActionListener(h); factorial.addActionListener(h); nHarmonic.addActionListener(h); bubbleSort.addActionListener(h); reverseString.addActionListener(h); bin2Dec.addActionListener(h); //************************************************// } class menuHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==mouseDraw) { mdraw= new MouseDrawing(); } else if(e.getSource()== xpowery) { power p= new power(); } else if(e.getSource()== letterGrade) { g= new Grade(); } else if(e.getSource()== factorial) { f= new Factorial(); }

else if(e.getSource()== nHarmonic) { hn= new HarmonicNumber(); } else if(e.getSource()== xpowery) { p= new power(); } else if(e.getSource()== bin2Dec) { B= new BinaryToDecimal(); } else if(e.getSource()== reverseString) { rs= new ReverseString(); } else if(e.getSource()== mouseDraw) { mdraw= new MouseDrawing(); } else if(e.getSource()== bouncingBall) { b= new BouncingBall(); } else if(e.getSource()== bubbleSort) { bs= new BubbleSort_GUI(); } } } public static void main(String args[]) { MainFrame M= new MainFrame(); M.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

package JavaBasicGUI; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.FlowLayout; import javax.swing.JOptionPane; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** * * @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("Decimal --> Binary"); btn2= new JButton("Binary --> Decimal"); add(btn1); add(btn2); //------------------------------------------------------// //-----------Adding Action Listeners-------------// Action a= new Action(); btn1.addActionListener(a); btn2.addActionListener(a); //-----------------------------------------------// setSize(500,500); setVisible(true); } public String IntToBin(int i)

{ String result; result = Integer.toBinaryString(i);// method in the Integer class that converts Integer values to Binary return result; } public String BinToInt(String str) { long num = Long.parseLong(str); long rem; while(num > 0)// find the decimal value of the binary number { 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(IntToBin(Integer.parseInt(txt1.getText()))); } else if(e.getSource()==btn2) { txt2.setText(BinToInt(txt1.getText())); } } } }

package JavaBasicGUI; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Graphics; import java.awt.Color; /** * * @author jonathanwu */ public class BouncingBall extends JFrame{ double delta_x=5,delta_y=5,gravity=0.7; JButton b; Ball B; Button btn; int ball_x=30, ball_y=300, radius=20, speed=20, start_x=30, start_y=30, width=500, height=500 ; BouncingBall() { super("Bouncing Ball"); b= new JButton("Bounce"); add(b,BorderLayout.SOUTH); B= new Ball(); add(B, BorderLayout.CENTER); setSize(612,612); setVisible(true); btn= new Button(); b.addActionListener(btn); }

class Ball extends JPanel { public void paintComponent(Graphics g) {

g.setColor(Color.RED); g.drawRect(start_x, start_y, width, height); g.setColor(Color.RED); g.fillOval(ball_x, ball_y, radius, radius); }

} public void bounceBall() { while(true) { try { Thread.sleep(speed); } catch(InterruptedException e) {} if(ball_x>= (start_x+ width-10)) { delta_x=-delta_x; } else if(ball_x<=(start_x-10)) { delta_x=-delta_x; } if(ball_y>= (start_y+ height-10)) { delta_y=-delta_y; } else if(ball_y<=(start_y-10)) { delta_y=-delta_y; } ball_x+=delta_x; ball_y+=delta_y; if(delta_y<20) { delta_y+=gravity; gravity+=0.001; }

repaint(); } } public class Button implements ActionListener { public void actionPerformed(ActionEvent e) { bounceBall(); } } }

Bubble Sort Algorithm


package JavaBasicGUI; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * * @author jonathanwu */ public class BubbleSort { public void bubbleSort(int[] a){ int n=a.length; int temp=a[0]; 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]; 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; 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]; 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; } } } } }

JFrame Class
class BubbleSort_GUI extends JFrame { JButton Integer, Character, string, Double; JTextField Input; JLabel Output; BubbleSort b= new BubbleSort(); Parser p= new Parser(); String out="",s=""; BubbleSort_GUI() { super("Bubble Sort"); setLayout(new FlowLayout()); Input= new JTextField("Input List here"); Output= new JLabel("Sorted List will be displayed here"); 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(Input); add(Integer); add(Character);

add(string); add(Double); add(Output); Action a= new Action(); 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) { 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); } 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); } else if(e.getSource()==string) { s=Input.getText(); p.parseIn(s); for (int i=0; i<p.values.length; i++){ p.values[i].trim(); } b.bubbleSort(p.values); out= p.convertStringArray_String(p.values); Output.setText(out); } 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); } } } }

Parser
package JavaBasicGUI; /** * * @author jonathanwu */ public class Parser { public int count=1; public String[] values; public int[] int_values; public double[] Double_values; public char[] char_values; public void parseIn(String s){ int element=0; for (int i=0; i<s.length(); i++)// counts number of commas { if (s.charAt(i) == ',') count++; } values = new String[count];// makes a new string array for(int i=0; i<count; i++){ String number_string = new String(); try{ while (s.charAt(element) !=','){ number_string +=s.charAt(element); if(element>=s.length()-1) break; element++; }} catch(StringIndexOutOfBoundsException e){}

values[i]=number_string.toString(); for(int j=0; j<values.length;j++) element++; } } public void convertInt(String[] ss){ int_values = new int[count]; for(int i=0; i<count; i++){ int_values[i] = Integer.parseInt(values[i]); } } public void convertDouble(String[] ss){ Double_values = new double[count]; for(int i=0; i<count; i++){ Double_values[i] = Double.parseDouble(values[i]); } } public void convertChar(String[] ss){ char_values = new char[count]; for(int i=0; i<count; i++){ char_values[i] = values[i].charAt(0); } } public String convertInt_String(int[] int_vals){ String output=""; for(int i=0; i<count; i++){ output+=int_vals[i]+","; } return output; } public String convertDouble_String(double[] Double_vals){ String output=""; for(int i=0; i<count; i++){ output+=Double_vals[i]+","; } return output; } public String convertChar_String(char[] char_vals){ String output=""; for(int i=0; i<count; i++){ output+=char_vals[i]+","; } return output; } public String convertStringArray_String(String[] str_vals){ String output=""; for(int i=0; i<count; i++){

output+=str_vals[i]+","; } return output; } }// end class

package JavaBasicGUI;

import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /* * * @author michaelrodda */ public class Factorial extends JFrame{ // declared variables for GUI private JLabel text; private JTextField input; //constuctor is made public Factorial(){ super ("Factorial of a Number"); text = new JLabel("Answer");

TextFieldHandler h = new TextFieldHandler(); input = new JTextField("Enter Number Here."); input.addActionListener(h); add(text, BorderLayout.NORTH); 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); } } } }

package JavaBasicGUI; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JLabel; /** * * @author jonathanwu */ public class Grade extends JFrame{ JTextField input; JLabel output; JButton btn; int grade; String s= "The letter grade is: ", g; Grade() { super("Display Letter Grade"); setLayout(new FlowLayout()); input= new JTextField("Enter Percentage"); add(input); output= new JLabel("Letter Grade"); add(output); btn= new JButton("Calculate"); add(btn); LetterGrade G= new LetterGrade(); btn.addActionListener(G); setSize(300,200); setVisible(true); } void findGrade(int x) { if(x>=90) g="A"; else if(x>=80 && x<=90) g="B"; else if(x>=70 && x<=80) g="B"; else if(x>=60 && x<=70) g="B"; else if(x>=50 && x<=60) g="B"; else if(x<50)

g="F"; } class LetterGrade implements ActionListener { public void actionPerformed(ActionEvent e) { grade=Integer.parseInt(input.getText()); findGrade(grade); output.setText(s+g); } } }

package JavaBasicGUI; 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); c.add(text, BorderLayout.NORTH); c.add(input, BorderLayout.SOUTH);

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

public static double findHarmonic(int input) { double result = 0; 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;

if (event.getSource()==input){ s = input.getText(); q = Integer.parseInt(s); r = findHarmonic(q); s = String.valueOf(r); text.setText(s); } } } private static class windowHandler extends WindowAdapter{ public void windowClosing(WindowEvent event){ System.exit(0); } } }

package JavaBasicGUI; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author raveenvasudeva */ public class MouseDrawing extends JFrame{ private int xval=-10,yval=-10; int brushSize; JTextField txt; public MouseDrawing(){ super("Mouse Drawing");

txt= new JTextField("Enter Brush Size here"); add(txt, BorderLayout.NORTH); BrushSize b= new BrushSize(); txt.addActionListener(b);

MousePainter m = new MousePainter(); addMouseMotionListener(m);

setSize(500,500); setVisible(true); }

class MousePainter extends MouseMotionAdapter{ public void mouseDragged(MouseEvent m_event){ xval=m_event.getX(); yval=m_event.getY(); repaint(); } } private class BrushSize implements ActionListener { public void actionPerformed(ActionEvent e) {

brushSize=Integer.parseInt(txt.getText()); } } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(xval, yval, brushSize, brushSize); }

package JavaBasicGUI; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ 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 JavaBasicGUI; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author jonathanwu */ public class power extends JFrame{ JTextField X, Y; JLabel answer; double ans; JButton calculate; power() { super("Calculate Power"); setLayout(new FlowLayout()); X= new JTextField("Enter X value"); Y= new JTextField("Enter Y value"); answer= new JLabel("Answer"); calculate= new JButton("Calculate"); add(X); add(Y); add(calculate); add(answer); Power p= new Power(); calculate.addActionListener(p); setSize(300,300); setVisible(true); } public void paintComponent(Graphics g){ setBackground(Color.RED); } double calc(double x,double y) { double ans= Math.pow(x,y); return ans;

} class Power implements ActionListener { public void actionPerformed(ActionEvent e) { ans=calc(Double.parseDouble(X.getText()),Double.parseDouble(Y.getText())); answer.setText("The answer is: "+ans); } } }

S-ar putea să vă placă și