Question
Stack Pgm Attached Files at bottom: Properly Nested Delimiters: A Java program can have the following type of delimiters: {, }, (, ), [, and
Stack Pgm
Attached Files at bottom:
Properly Nested Delimiters:
A Java program can have the following type of delimiters: {, }, (, ), [, and ]. In a correct Java program, these delimiters must be properly nested. Think of each left delimiter {, (, [ as opening a scope, and think of each right delimiter }, ), ] as closing a scope opened by a corresponding left delimiter. A string of characters containing these delimiters has proper nesting of delimiters if each scope this is opened is eventually closed, and the scopes are opened and closed in a last-opened-first-closed fashion.
Write a program that reads a file containing Java source code and checks it for proper nesting of delimiters. Your program should read the source code from the file and print it to the screen. If the file is properly nested, all of it is printed to the screen and a message is printed that the file is properly nested, if the file is not properly nested, then copying of the file to the screen stops as soon as the improper nesting is detected, and your program prints a message that the file has errors. To simplify your task, you may assume these delimiters do not appear inside of comments and string literals, and that they do not appear in the program as character literals. Obviously, this program should use a stack :)
Test your source code with the 4 input files provided (attached).
/** * Activity 6.1.3: Adding to the face printing program. * * */ public class FacePrinterSimple { public static void printFaceB() { // FIXME: FINISH return; } public static void printFaceA() { char faceChar = 'o'; System.out.println(" " + faceChar + " " + faceChar ); // Eyes System.out.println(" " + faceChar); // Nose System.out.println(" " + faceChar + faceChar + faceChar); // Mouth return; } public static void main (String [] args) { printFaceA(); return; } } /////
//Welcome program with check boxes, radio buttons, and combo box
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GrandWelcomeFinal extends JApplet implements ItemListener { private int intBold = Font.PLAIN; private int intItalic = Font.PLAIN; private Color currentColor = Color.black; private String currentFontName ="Courier"; private JCheckBox boldCB, italicCB; private JRadioButton redRB, greenRB, blueRB; private ButtonGroup ColorSelectBGroup; private JComboBox fontFaceDD; private String[] fontNames = {"Dialog", "Century Gothic", "Courier", "Serif"}; public void init() { Container c = getContentPane(); c.setLayout(null); boldCB = new JCheckBox("Bold"); italicCB = new JCheckBox("Italic"); redRB = new JRadioButton("Red"); greenRB = new JRadioButton("Green"); blueRB = new JRadioButton("Blue"); fontFaceDD = new JComboBox(fontNames); fontFaceDD.setMaximumRowCount(3); boldCB.setSize(80, 30); italicCB.setSize(80, 30); redRB.setSize(80, 30); greenRB.setSize(80, 30); blueRB.setSize(80, 30); fontFaceDD.setSize(80, 30); boldCB.setLocation(100, 70); italicCB.setLocation(100, 150); redRB.setLocation(300, 70); greenRB.setLocation(300, 110); blueRB.setLocation(300, 150); fontFaceDD.setLocation(200, 70); boldCB.addItemListener(this); italicCB.addItemListener(this); redRB.addItemListener(this); greenRB.addItemListener(this); blueRB.addItemListener(this); fontFaceDD.addItemListener(this); c.add(boldCB); c.add(italicCB); c.add(redRB); c.add(greenRB); c.add(blueRB); c.add(fontFaceDD); ColorSelectBGroup = new ButtonGroup(); ColorSelectBGroup.add(redRB); ColorSelectBGroup.add(greenRB); ColorSelectBGroup.add(blueRB); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.orange); g.drawRoundRect(75, 50, 324, 140, 10, 10); g.drawLine(183, 50, 183, 190); g.drawLine(291, 50, 291, 190); g.setColor(currentColor); g.setFont(new Font(currentFontName, intBold + intItalic, 24)); g.drawString("Welcome to Java Programming", 30, 30); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == boldCB) { if (e.getStateChange() == ItemEvent.SELECTED) intBold = Font.BOLD; if (e.getStateChange() == ItemEvent.DESELECTED) intBold = Font.PLAIN; } if (e.getSource() == italicCB) { if (e.getStateChange() == ItemEvent.SELECTED) intItalic = Font.ITALIC; if (e.getStateChange() == ItemEvent.DESELECTED) intItalic = Font.PLAIN; } if (e.getSource() == redRB) currentColor = Color.red; else if (e.getSource() == greenRB) currentColor = Color.green; else if (e.getSource() == blueRB) currentColor = Color.blue; if (e.getSource() == fontFaceDD) currentFontName = fontNames[fontFaceDD.getSelectedIndex()]; repaint(); } } ////
/** * */ /** * * */ import java.util.Scanner; import java.io.FileInputStream; import java.io.IOException; public class FileReadNums { public static void main(String[] args) throws IOException { FileInputStream fileByteStream = null; // File input stream Scanner inFS = null; // Scanner object int fileNum1 = 0; // Data value from file int fileNum2 = 0; // Data value from file // Check number of arguments if (args.length != 1) { System.out.println("Usage: java FileReadNums inputFileName"); return; } // Try to open the file System.out.println("Opening file " + args[0] + "."); fileByteStream = new FileInputStream(args[0]); inFS = new Scanner(fileByteStream); // File is open and valid if we got this far // myfile.txt should contain two integers, else problems System.out.println("Reading two integers."); fileNum1 = inFS.nextInt()); //**here fileNum2 = inFS.nextInt(); // Done with file, so try to close it System.out.println("Closing file " + args[0] + " "); fileByteStream.close(); // close() may throw IOException if fails // Output values read from file System.out.println("num1: " + fileNum1); System.out.println("num2: " + fileNum2); System.out.println("num1 + num2: " + (fileNum1 + fileNum2)); return; } } ////
/** * */ /** * * */ public class FirstProgram { /** * @param args */ public static void main(String[] args) { System.out.print("Hello World "); String dogNames[] = {"Fido","Buster","Fifi","Patches","Gromit","Toodles"}; for (String temp:dogNames) // **here System.out.print(temp + " "); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started