SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 2 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 19 Nov 2017 My Price 10.00

development on the program to make it functional.

Hi, you have helped me on a project recently. I have uploaded the code and would like your help in further development on the program to make it functional. + button to add two numbers together, - button to perform subtraction, x button for multiplication function, / for division so two number could be divided and could also tell the user that they cannot divide by zero if they try to input that. Whole number is fine, I don't need to make this work for decimals. Also, I have a couple of question for you to answer.

 

 

  1.  Write up, or explanation on your approach. This could be a paragraph, or two on this. 
  2. What are the lesson learned on this
  3. Write up on ways to improve your code. Improvement could be something you would have done different, but was not able to do because the scope, or time did not permit, functions that you were not able to implement and the reason for not doing, a different approach that you would have taken a different way of solving the problem etc. This could be a paragraph, or two on this. import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class GenerateGUI {

       private static Scanner scanner;
       private static File txtfile;
       private static JTextField textField;

       private static String getLabel(String line) {

           int counter;

           line.trim();
           for (counter = 0; counter < line.length(); counter++) {
               if (!Character.isAlphabetic(line.charAt(counter))) {
                   break;
               }
           }

           return line.substring(0, counter);

       }

       public static void main(String[] args) {

           String str    , label;
           String fileName ="ProperInput.txt";
           try {

               txtfile = new File(fileName);
               scanner = new Scanner(txtfile);

               if (scanner.hasNextLine()) {
                   str = scanner.nextLine().trim();
                   label = getLabel(str);
                   if (!label.equalsIgnoreCase("Window")) {
                       System.out.println("First label should be WINDOW");
                       return;
                   }
                   str = str.substring(label.length()).trim();
                   JFrame frame = (JFrame) addComponentRecursively(str, label);
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setVisible(true);
               } else {
                   System.out.println("Unknown Error");

               }

           } catch (FileNotFoundException e) {
            
             
               JOptionPane.showMessageDialog(null, fileName + " file not found");
               return;
              // e.printStackTrace();
           } catch (Exception exception) {
               System.out.println("Unknown Error");
               exception.printStackTrace();
           }
       }

       private static ArrayList<Integer> getIntegerArray(String str)
               throws Exception {
           int i    , j;
           ArrayList<Integer> result = new ArrayList<>();
           for (i = 0; i < str.length(); i++) {
               for (j = i; j < str.length() && Character.isDigit(str.charAt(j)); j++)
                   ;
               if (i != j) {
                   result.add(Integer.parseInt(str.substring(i, j)));
               }
               i = j;
           }

           return result;
       }

       private static Component addComponentRecursively(String str, String label)
               throws Exception {
           String temp;
           if (label.equalsIgnoreCase("Window")) {
               str = str.trim();
               JFrame frame;
               if (str.charAt(0) == '"') {
                   str = str.substring(1);
                   temp = str.substring(0, str.indexOf('\"'));
                   frame = new JFrame(temp);
                   str = str.substring(str.indexOf('"') + 1).trim();
               } else {
                   frame = new JFrame("Default title");
               }

               if (str.charAt(0) == '(') {

                   temp = str.substring(0, str.indexOf(')') + 1);
                   str = str.substring(temp.length()).trim();
                   ArrayList<Integer> ints = getIntegerArray(temp);
                   if (ints.size() == 2) {
                       frame.setSize(ints.get(0), ints.get(1));
                   }

               }
               temp = getLabel(str);
               str = str.substring(temp.length()).trim();
               JPanel p = new JPanel();
               if (temp.equalsIgnoreCase("Layout")) {
                   temp = getLabel(str);
                   str = str.substring(temp.length()).trim();
                   if (temp.equalsIgnoreCase("flow")) {
                       FlowLayout fl = new FlowLayout();
                       p.setLayout(fl);
                   }
                   if (temp.equalsIgnoreCase("grid")) {
                       if (str.charAt(0) == '(') {

                           temp = str.substring(0, str.indexOf(')') + 1);
                           str = str.substring(temp.length()).trim();
                           ArrayList<Integer> ints = getIntegerArray(temp);
                           GridLayout tempLayout;
                           if (ints.size() == 2) {
                               tempLayout = new GridLayout(ints.get(0),
                                       ints.get(1));
                               p.setLayout(tempLayout);
                           } else if (ints.size() == 4) {
                               tempLayout = new GridLayout(ints.get(0),
                                       ints.get(1), ints.get(2), ints.get(3));
                               p.setLayout(tempLayout);
                           }

                       }

                   }

               }

               while (true) {
                   if (scanner.hasNextLine()) {
                       str = scanner.nextLine().trim();
                       temp = getLabel(str);
                       if (temp.equalsIgnoreCase("end")) {
                           break;
                       } else {
                           Component tempComponent = addComponentRecursively(
                                   str.substring(temp.length()), temp);
                           if (tempComponent != null) {
                               if (tempComponent.getClass() == frame.getClass()) {
                                   System.out
                                           .println("Window cant be nested inside");
                               } else {
                                   p.add(tempComponent);
                               }
                           }
                       }
                   } else {
                       System.out.println("Error in nesting");
                       break;
                   }
               }
               frame.add(p);
               return frame;
           }

           if (label.equalsIgnoreCase("panel")) {
               str = str.trim();
               JPanel panel = new JPanel();
               temp = getLabel(str);
               str = str.substring(temp.length()).trim();
               if (temp.equalsIgnoreCase("Layout")) {
                   temp = getLabel(str);
                   str = str.substring(temp.length()).trim();
                   if (temp.equalsIgnoreCase("flow")) {
                       FlowLayout fl = new FlowLayout();
                       panel.setLayout(fl);
                   }
                   if (temp.equalsIgnoreCase("grid")) {
                       if (str.charAt(0) == '(') {

                           temp = str.substring(0, str.indexOf(')') + 1);
                           str = str.substring(temp.length()).trim();
                           ArrayList<Integer> ints = getIntegerArray(temp);
                           GridLayout tempLayout;
                           if (ints.size() == 2) {
                               tempLayout = new GridLayout(ints.get(0),
                                       ints.get(1));
                               panel.setLayout(tempLayout);
                           } else if (ints.size() == 4) {
                               tempLayout = new GridLayout(ints.get(0),
                                       ints.get(1), ints.get(2), ints.get(3));
                               panel.setLayout(tempLayout);
                           }

                       }

                   }

               }

               while (true) {
                   if (scanner.hasNextLine()) {
                       str = scanner.nextLine().trim();
                       temp = getLabel(str);
                       if (temp.equalsIgnoreCase("end")) {
                           break;
                       } else {
                           Component tempComponent = addComponentRecursively(
                                   str.substring(temp.length()), temp);
                           if (tempComponent != null) {
                               if (tempComponent.getClass() == new JFrame()
                                       .getClass()) {
                                   System.out
                                           .println("");
                               } else {
                                   panel.add(tempComponent);
                               }
                           }
                       }
                   } else {
                       System.out.println("Error in nesting");
                       break;
                   }
               }

               return panel;
           }

           if (label.equalsIgnoreCase("button")) {
               str = str.trim();
               final JButton button;
               if (str.charAt(0) == '"') {
                   str = str.substring(1);
                   temp = str.substring(0, str.indexOf('\"'));
                   button = new JButton(temp);
                   str = str.substring(str.indexOf('"') + 1).trim();
               } else {
                   button = new JButton("Default title");
               }
               button.addActionListener(new ActionListener() {
                   
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(textField!=null){
                            textField.setText(textField.getText()+button.getText());
                        }
                       
                    }
                });
               return button;
           }
         
           if (label.equalsIgnoreCase("label")) {
               str = str.trim();
               JLabel Label;
               if (str.charAt(0) == '"') {
                   str = str.substring(1);
                   temp = str.substring(0, str.indexOf('\"'));
                   Label = new JLabel(temp);
                   str = str.substring(str.indexOf('"') + 1).trim();
               } else {
                   Label = new JLabel("Default title");
               }
               return Label;
           }
         
           if (label.equalsIgnoreCase("textfield")) {
               str =str.trim();
               ArrayList< Integer> i = getIntegerArray(str);
               JTextField field = new JTextField(i.get(0));
               textField = field;
               return field;
           }

           return null;
       }
    }


Attachments:

Answers

(5)
Status NEW Posted 19 Nov 2017 08:11 AM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)