If a complete answer is given to this question, I will create a new question and
ID: 3554516 • Letter: I
Question
If a complete answer is given to this question, I will create a new question and link you to it in the comments for an extra 1500 points. Chegg won't let me assign 3,000 for some stupid reason.
Given the following grammar:
gui ::=
Window STRING '(' NUMBER ',' NUMBER ')' layout widgets End '.'
layout ::=
Layout layout_type ':'
layout_type ::=
Flow |
Grid '(' NUMBER ',' NUMBER [',' NUMBER ',' NUMBER] ')'
widgets ::=
widget widgets |
widget
widget ::=
Button STRING ';' |
Group radio_buttons End ';' |
Label STRING ';' |
Panel layout widgets End ';' |
Textfield NUMBER ';'
radio_buttons ::=
radio_button radio_buttons |
radio_button
radio_button ::=
Radio STRING ';'
Using recursive descent, create a Java program that will parse the following input file
input.txt:
Into a program with the below user-interface:
The text field nor any of the buttons need to function - the interface simply needs to display properly after parsing the input file and the simpler the program is, the better. Really in a bind with this one, so any help would be appreciated.
Explanation / Answer
package chegg;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
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.JPanel;
import javax.swing.JTextField;
public class GUICreator {
private static Scanner sc;
private static File file;
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;
try {
file = new File("Input.txt");
sc = new Scanner(file);
if (sc.hasNextLine()) {
str = sc.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) {
// TODO Auto-generated catch block
System.out.println("File not found");
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 (sc.hasNextLine()) {
str = sc.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 (sc.hasNextLine()) {
str = sc.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("Window cant be nested inside");
} else {
panel.add(tempComponent);
}
}
}
} else {
System.out.println("Error in nesting");
break;
}
}
return panel;
}
if (label.equalsIgnoreCase("button")) {
str = str.trim();
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");
}
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));
return field;
}
return null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.