- Problem 1:
- Part (a) For the following pairs of classes, identify the superclass and the subclass.
Employee, Manager
GraduateStudent, Student
Person, Student
BankAccount, CheckingAccount
Vehicle, Minivan
Number, Integer
Triangle, Shape
Part (b) Draw an inheritance diagram to show what relationships you would establish among the following classes.
Student
Professor
TeachingAssistant
Employee
Secretary
DepartmentChair
Person
Course
Seminar
Lecture
ComputerLab
Part (c) For the class LibraryBook list several reasonable properties and methods that could be in the class definition.
- Problem 2:
- Consider the following two class definitions.
class Thing1
{ public static int y;
public int x;
public void setX(int x){ this.x = x; }
public int getX(){ return x; }
}//Thing1
class Thing2 extends Thing1
{ private int x;
public void setX(int x){ this.x = x; }
public int getX(){ return x; }
}//Thing2
Suppose that we have the following declarations.
Thing1 a = new Thing1();
Thing2 b = new Thing2();
Explain why each of the following assignment statements is either OK or not OK?
Part (a)
a.x = 5;
Part (b)
b.x = 5;
Part (c)
a = b;
Part (d)
b = a;
Part (e)
Thing1.x = 2;
Part (f)
Thing2.y = -2;
Part (g) Consider the following code fragment.
Thing1 c = new Thing2();
c.x = 5;
c.setX(7);
System.out.println( c.x );
System.out.println( c.getX() );
Explain why the output of this fragment is
5
7
- Problem 3:
- Part (a) What are a couple of the principle differences between String objects and StringBuffer objects?
Part (b) Explain the difference between the capacity and the length of a StringBuffer object.
- Problem 4:
- Consider the following program.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Question4 extends JFrame implements ActionListener
{
JTextField field1, field2;
JButton button1, button2;
public Question4()
{ super("Question4");
Container c = getContentPane();
c.setLayout( new FlowLayout() );
field1 = new JTextField(10);
c.add(field1);
button1 = new JButton("Button1");
button1.addActionListener(this);
c.add(button1);
field2 = new JTextField(10);
c.add(field2);
button2 = new JButton("Button2");
button2.addActionListener(this);
c.add(button2);
setSize(300,300);
show();
}
public void actionPerformed(ActionEvent e)
{ if (e.getSource() == button1 )
{ field2.setText( field1.getText() ); }
else
{ if ( field2.getText().equals("done") ) System.exit(0);
else field2.setText("");
}
}
public static void main( String args[] )
{ Question4 app = new Question4(); }
}//Question4
Part (a) Briefly describe what the graphical user interface looks like.
Part (b) What happens when a user clicks on Button1?
Part (c) What happens when a user clicks on Button2?
Part (d) What happens when a user enters the string "Done" into field2 and then presses the Enter key? Briefly explain why?
- Problem 5:
- Consider the following program.
import javax.swing.JOptionPane;
public class Question5
{
public static void main( String args[] )
{ while(true)
{ try
{ String input = JOptionPane.showInputDialog("Input 1");
int number = Integer.parseInt(input);
if (number < 0) throw new Exception();
}
catch(NumberFormatException e)
{ JOptionPane.showMessageDialog(null, "Opps"); }
catch(Exception e)
{ String input = JOptionPane.showInputDialog("Input 2");
if (input.equals("yes")) System.exit(0);
}
}
}//main
}//Question5
Part (a) What happens if a user types the string "yes" into the dialog box labelled "Input 1" and clicks on the "OK" button? Briefly explain why.
Part (b) What happens if a user types the string "123" into the dialog box labelled "Input 1" and clicks on the "OK" button? Briefly explain why.
Part (c) What happens if a user types the string "-123" into the dialog box labelled "Input 1" and clicks on the "OK" button? Briefly explain why.