Finalf 10
Finalf 10
Finalf 10
Example Questions
Part A Properties
Translate the following Java class plus test driver into C# with correct use of properties:
Translate the following Java code into C#. Follow the C# rules regarding exact naming conventions and
keywords as much as possible.
class Record {
public String toString() {
String result = “abc”;
return result;
}
}
class NewRecord extends Record {
public String toString() {
String result = “def”;
String oldresult = super.toString();
return result+oldresult;
}
}
Problem 1 (Continued)
Part C Class Vs Struct
In C#, what is the significance of the difference between the following two definitions?
class Point {
public int X;
public int Y;
}
and
struct Point {
public int X;
public int Y;
}
Problem 2 Java and C# Menus Comparison
Part A
Write code to build a small Java Swing menu system containing a File menu with New, Open, and Save
menu items. Start with the JMenuBar. Don’t worry about defining keyboard equivalents. For the New
menu item only, add event handling code. For purposes of this simple example, the event handler
response is limited to displaying a simple confirmation dialog box (JOptionPane.ShowConfirmDialog(…)).
Problem 2 (Continued)
Part B
Write the equivalent code for C#/WPF. In place of a JOptionPane for a simple confirm dialog, use
MessageBox.Show().
Problem 3 Java Swing Misc
Part A
In Java Swing, the JPanel class is heavily used as an intermediate container. When subclassing it to
define custom behaviors, certain methods are the most commonly overridden. What are these
methods and for what purpose would you override them?
Problem 3 (Continued)
Part B
Imagine a group of three JRadioButton components in a Java Swing GUI. The implied behavior is that
only one button in the group should be selected at a time. This can be implemented by adding the
buttons to a ButtonGroup object. In this exercise, code an event listener that implements this logic
directly, without the ButtonGroup. There are several ways, but an ActionListener is probably the
easiest. Use the following methods to read/modify the selected state of a button.
boolean isSelected();
void setSelected(boolean value);
Problem 4
Part A
A WPF Button responds to the “Click” event, with delegate RoutedEventHandler.
Suppose you add code to handle the Click event for a Button subclass (derived class) that you cannot
directly modify.
…
CustomButton b = new CustomButton();
…
Part B
In contrast to Part A, suppose that you control the code for class CustomButton and can add methods to
its definition as needed. Show a second way to implement a response to the Click event available in this
instance.
Part A
Write Java Swing code to introduce event handlers for mouse up and down events. Define a listener,
then register listener with a generic JPanel. Bodies of the listener methods can be left empty.
Part B
Write the equivalent code for C#/WPF. Instead of a JPanel, assume a control of type Canvas. You may
subclass Canvas (with internal event handlers) or modify a generic Canvas (with external event
handlers).
Problem 6 WPF Code and Markup
Look at the following XAML-based GUI. Some tag attributes such as xmlns are omitted. The GUI defines
a Grid with one row and three columns, and adds 3 buttons with various properties to the grid. When
executed, a top-level container is provided implicitly.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
Rewrite the XAML to build the equivalent GUI in code only. Use the skeleton provided. You do not need
to show namespace using directives.
Hints: Explicitly code the top-level container (class Window) and Application object for the GUI. Use the
Grid control as an intermediate container. If a XAML tag sets an attribute value, assume there is a
similar object property to assign in code. Use the “Add()” method to add an item to a collection
property like g.ColumnDefinition.Add() or g.Children.Add().
Problem 6 (Continued)
Problem 7 WPF Code and Markup
In Java Swing, a tree component with expandable/collapsible nodes is called a JTree, with individual
nodes built from instances of class DefaultMutableTreeNode.
In C#/WPF, the equivalent classes are TreeView and TreeViewItem. Here’s a screen capture for a simple
tree built from TreeView and TreeViewItem objects:
Part A
Write a xaml definition for this GUI. Use a TreeView tag for the top level, with nested TreeViewItem
tags for the items. Use the Header tag attribute to assign a display string to a TreeView Item. You do
not need to show definitions for namespace attributes such as xmlns.
Problem 7 (Continued)
Part B
Write a code equivalent for this app. Create a top-level container and add the TreeView component as
its Content property. Use the Add() method to add items to the Items collection property of both
TreeView and TreeViewItem (view.Items.Add(…)). Show the construction for the first TreeViewItem
node in detail, you may abbreviate when constructing the second node. Use the skeleton provided.
public static void BuildGUI() { // instantiate window, app, and run them
}
[STAThread]
public static void Main() {
TreeViewDemo.BuildGUI();
}
}