Stack Pgm Attached Files at bottom: Properly Nested Delimiters: A Java program c
ID: 3606457 • Letter: S
Question
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).
Explanation / Answer
public class FacePrinterSimple {
public static void printFaceB() {
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;
}
}
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;
Scanner inFS = null;
int fileNum1 = 0;
int fileNum2 = 0;
if (args.length != 1) {
System.out.println("Usage: java FileReadNums inputFileName");
return;
}
System.out.println("Opening file " + args[0] + ".");
fileByteStream = new FileInputStream(args[0]);
inFS = new Scanner(fileByteStream);
System.out.println("Reading two integers.");
fileNum1 = inFS.nextInt()); //**here
fileNum2 = inFS.nextInt();
System.out.println("Closing file " + args[0] + " ");
fileByteStream.close(); // close() may throw IOException if fails
System.out.println("num1: " + fileNum1);
System.out.println("num2: " + fileNum2);
System.out.println("num1 + num2: " + (fileNum1 + fileNum2));
return;
}
}
public class FirstProgram {
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 + " ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.