Q1) (30 pts)

paltryboarpigSoftware and s/w Development

Nov 3, 2013 (3 years and 1 month ago)

78 views


Q1)
(30 pts)

a
.
(2 pts)
S
uppose 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");



}

}










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.


Welcome to Java

What's wrong?

The finally clause is executed

End o
f the block


2

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)




3

Q
2
.
(
40

pts)

Write a Java
program that simulates a red ball (diameter 20 pixels) going
up and down in a frame

300 x 2
00

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 java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public 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);




getContentPa
ne().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.setC
olor( Color.RED);



g.fillOval( xCoord, yCoord, 20, 20);






yCoord += diffy;




if( yCoord > getHeight()
-

20 || yCoord < 20)




diffy =
-
diffy;


}

}

4

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
line
s

#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:
__________________



5

Q
3
-
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 b
p = new BallPanel();




JPanel buttonPanel = new JPanel();







buttonPanel.add( click);







add( buttonPanel, BorderLayout.SOUTH);




add( bp, BorderLayout.CENTER);







click.addActionListener( new ActionListener() {




pub
lic 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







6

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 b
e 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 disp
layed in the text area. Save will save the text in
the text area into t
he file given in the text field and Exit will stop the application.







import java.util.*;

import java.io.*;

import javax.swing.*;

import java.awt.*;

import 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 T
extEditor( ) {



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);


7






fileNameField = new JTextField( 20);



fileNamePanel.add( fileNameField);






add( fileNamePa
nel, 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.a
ddActionListener( 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()) {






Str
ing 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){







}


}

}