Q1) (30 pts) a. (2 pts) Suppose the rule of the party is that the participants who arrive later will leave earlier. Which data structure is appropriate to store the participants? _________ Stack e. (3 pts) If xMethod() is invoked in the following constructor, xMethod() must be a(n) ___________ method. instance public MyClass() { xMethod(); } f. (3 pts) A property that is shared by all objects of the class is coded using a(n) _____________________. a static variable g. (2 pts) If a class named Student has no constructors defined explicitly, Java will _________________________________________________. no-arg constructor h. (4 pts) What is printed when the following program is executed? public class TestMT2 { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Exceptions"); } catch(Exception e){ System.out.println("What's wrong?"); } finally { System.out.println("The finally clause is executed"); } System.out.println("End of the block"); } } Welcome to Java What's wrong? The finally clause is executed End of the block i. (2 pts) Suppose charList is an object of class ArrayList<Character>. Give the code to display the elements of charList on the screen using a for-each loop. for(Character elem : charList) System.out.println(elem); j. (2 pts) Given ArrayList<String> aList = new ArrayList<String>(), give the statements to add String s = “This”, into the first position in the list and the last position in the list. First: ______________________________________ add(0,s) Last: ______________________________________ add(s) 2 Q2.(40 pts) Write a Java program that simulates a red ball (diameter 20 pixels) going up and down in a frame 300 x 200 whose background color is white. When it reaches the bottom or the top, it bounces back and continues its vertical motion. Use an instance of Timer class with a delay of 10 milliseconds. Sample statement for creating such an instance is given below: Timer timer = new Timer( 10, new TimerListener()); import import import public java.awt.*; java.awt.event.*; javax.swing.*; class UpDownBall extends JFrame{ private int xCoord = 140; private int yCoord = 20; private int diffy = 2; Timer timer = new Timer( 10, new TimerListener()); public static void main( String[] args) { UpDownBall di = new UpDownBall( ); di.setVisible( true); } public UpDownBall() { setSize( 300, 300); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); getContentPane().setBackground(Color.WHITE); timer.start(); } private class TimerListener implements ActionListener { public void actionPerformed( ActionEvent e) { repaint(); } } public void paint( Graphics g) { super.paint( g); g.setColor( Color.RED); g.fillOval( xCoord, yCoord, 20, 20); yCoord += diffy; if( yCoord > getHeight() - 20 || yCoord < 20) diffy = -diffy; } } 3 Q3-a) (15 points) Given the following code: public interface TestMt2Q3 { int x = 99; void X(); } abstract class Test0 implements TestMt2Q3{ public void X(){ System.out.println("X is " + x); } public abstract void X(Integer x); } class Test extends Test0{ public void X(Integer x){ x++; System.out.println("Result is " + x); } public static void main(String[] args) { new Test().X(); //line #1 new Test0().X(); //line #2 new Test().X(new Integer(33)); //line #3 new Test().X(55); //line #4 TestMt2Q3.x++; //line #5 } } Are the statements in lines #1 to #5 legal or not? If not legal state the reason why not, otherwise give the output: Legal or Not Output line #1 _________________ _____________________ X is 99 line #2 _________________ N _____________________ line #3 _________________ _____________________ Result is 34 line #4 _________________ _____________________ Result is 55 line #5 _________________ N x: __________________ 4 Q3-b) (15 pts) What is the output of the following program after 3 clicks on the button? import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestMt2Q3b extends JFrame { private JButton click = new JButton( "Click"); private int count = 0; public static void main(String[] args) { TestMt2Q3b b = new TestMt2Q3b(); b.setVisible( true); } public TestMt2Q3b() { super(); setSize( 200, 200); setLocationRelativeTo( null); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); BallPanel bp = new BallPanel(); JPanel buttonPanel = new JPanel(); buttonPanel.add( click); add( buttonPanel, BorderLayout.SOUTH); add( bp, BorderLayout.CENTER); click.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e){ count += 15; repaint(); } }); } private class BallPanel extends JPanel { public void paintComponent( Graphics g) { super.paintComponent( g); g.fillArc(50,50,100,100,0,count); count += 15; } } } Program starts Click 1 Click 2 Click3 5 Q4) (40 pts) Write a program that will allow text to be entered into a text area and then save it into a file whose name will be entered into the text field as shown below. It should have a menu with three items: Open, Save and Exit. Open will open and read the contents of a file and display them in the text area. If the file does not exist, a message to such effect should be displayed in the text area. Save will save the text in the text area into the file given in the text field and Exit will stop the application. import import import import import java.util.*; java.io.*; javax.swing.*; java.awt.*; java.awt.event.*; public class TextEditor extends JFrame implements ActionListener { public static void main( String[] args){ TextEditor s1 = new TextEditor( ); s1.setVisible( true); } JTextField fileNameField; JTextArea textArea; public TextEditor( ) { super( ); setSize( 400, 600); setTitle( "Welcome to Swing"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setLayout( new BorderLayout( )); JPanel fileNamePanel = new JPanel( ); fileNamePanel.setLayout( new FlowLayout()); JLabel fileLabel = new JLabel( "Enter File Name"); fileNamePanel.add( fileLabel); 6 fileNameField = new JTextField( 20); fileNamePanel.add( fileNameField); add( fileNamePanel, BorderLayout.NORTH); textArea = new JTextArea( 5, 20); add( textArea, BorderLayout.CENTER); JMenu fileMenu = new JMenu( "File"); JMenuItem openFile = new JMenuItem( "Open"); fileMenu.add( openFile); openFile.addActionListener( this); JMenuItem saveFile = new JMenuItem( "Save"); fileMenu.add( saveFile); saveFile.addActionListener( this); JMenuItem exit = new JMenuItem( "Exit"); //exit.setFont( new Font( "Courier", 12, 20)); fileMenu.add( exit); exit.addActionListener( this); JMenuBar bar = new JMenuBar( ); bar.add( fileMenu); setJMenuBar( bar); } public void actionPerformed( ActionEvent e) { String s = e.getActionCommand(); String fn = fileNameField.getText(); try { if( s.equals( "Open")) { Scanner scan = new Scanner( new FileInputStream( fn)); textArea.setBackground( Color.YELLOW); while( scan.hasNextLine()) { String t = scan.nextLine(); textArea.append( t + "\n"); } } else if( s.equals( "Save")) { PrintWriter pw = new PrintWriter( new FileOutputStream( fn)); String t = textArea.getText( ); pw.println( t); pw.close(); } else { System.exit( 0); } } catch( Exception exc){ } } } 7