0% found this document useful (0 votes)
8 views4 pages

Java Object-Oriented Programming Exam

This document contains a student grading system program written in Java with classes, methods, and user interface components like labels, text fields, and buttons to accept student name and subject marks and display average marks. It also contains questions on JScrollPane, polymorphism vs method overriding, computing area and perimeter of a rectangle class, Java events and event classes, and using checkboxes.

Uploaded by

Jafan Mulama
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Java Object-Oriented Programming Exam

This document contains a student grading system program written in Java with classes, methods, and user interface components like labels, text fields, and buttons to accept student name and subject marks and display average marks. It also contains questions on JScrollPane, polymorphism vs method overriding, computing area and perimeter of a rectangle class, Java events and event classes, and using checkboxes.

Uploaded by

Jafan Mulama
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

verage

ubject
et
udent
Grade
Two
One
Grading
Name
Marks System

MT KENYA
School UNIVERSITY
of Computing
Department of Information Technology
Virtual Exam
UNIT TITLE: OBJECT ORIENTED PROGRAAMING II
UNIT CODE: BIT2205
Instructions: Answer All Questions
QUESTION ONE
Explain JScrollPane container as one of the components and its conditions for displaying both horizontal and vertical scroll.
[4 marks]
A JScrollPane container can hold one component. To put it another way, a JScrollPane encompasses another component. If the wrapped component is
larger than the JScrollPane, scrollbars are automatically provided. JScrollPane, which controls scrollbar events, displays the relevant portion of the
contained component.
An optional row and column header viewport, as well as optional vertical and horizontal scroll bars, are all controlled by JScrollPane. For JScrollPane, a
row header and a column header are both possible. Which of these JViewport objects you want to use can be specified using setRowHeaderView and
setColumnHeaderView. The column header viewport scrolls left to right automatically in sync with the main viewport.
Write an applet program that can be used to produce the following user interface.
[6 marks]
2

import [Link].*;
import [Link].*;
class StudentGradingSystem extends Frame
{
TextField name,pass;
Button b1,b2;
StudentGradingSystem()
{
setLayout(new FlowLayout());
[Link](null);
Label p=new Label("Student Name:",[Link]);
Label p=new Label("Subject One:",[Link]);
Label p=new Label("Subject Two:",[Link]);
Label p=new Label("Average Marks:",[Link]);
name=new TextField(20);
pass=new TextField(20);
[Link]('#');
b1=new Button("submit");
b2=new Button("cancel");
[Link](n);
[Link](name);
[Link](p);
[Link](pass);
[Link](b1);
[Link](b2);
[Link](70,90,90,60);
[Link](70,130,90,60);
[Link](200,100,90,20);
[Link](200,140,90,20);
[Link](100,260,70,40);
[Link](180,260,70,40);
}
public static void main(String args[])
{
MyLoginWindow ml=new StudentGradingSystem();
[Link](true);
[Link](400,400);
[Link]("StudentGradingSystem ");
}
}
Differentiate between polymorphism and method overriding in java programs.
[1 mark]
In overloading, the method call is determined at compile time, but in overriding, the method call is determined at runtime based on the object type.
QUESTION TWO
3

Write a java program that can be used to compute the area of rectangle given the length and width. The program should have a class named Rectangle with
instance variables length and width, methods compute Area and getPerimeter,two constructors and destructors. Define objects so that to use object
reference to call the methods and print the results in appropriate dialog boxes. [5 marks].
class Rectangle{
public static void main(String arg[])
{
Rectangle rect = new Rectangle(10, 5);
[Link]("Length = " + [Link]);
[Link]("Breadth = " + [Link]);
[Link]("Area = " + [Link]());
[Link]("Perimeter = " + [Link]());
}
}
class Rectangle
{
double length;
double breadth;
Rectangle(double length, double breadth)
{
[Link] = length;
[Link] = breadth;
}
double getArea()
{
return length * breadth;
}
double getPerimeter()
{
return 2 * (length + breadth);
}
}
Explain Events in java programming, briefly explain any three event class types in Java language.
[6marks]
In Java, an event is any occurrence that changes the behavior or state of an entity. Actions include pressing a key on the keyboard, moving the cursor,
clicking a button, or scrolling the page.
ActionEvent
Is a semantic event that denotes the execution of a component-defined action.
Event of Class Adjustment
Event of Class Adjustment When the user changes its value, the scrolling component gets an instance of an AdjustmentEvent.
Event Component.
This low-level event is triggered whenever an element of an object (such as a List) is moved, resized, rendered invisible, or rendered visible once more.
The add method of the component sends the event to every ComponentListener or ComponentAdapter object that registers to receive such events.

Write a snippet to demonstrate the use of the following checkboxes. [ 8 marks]


4

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ACheckBox {
public static void main(String args[]) {
String title = ([Link] == 0 ? "CheckBox Sample" : args[0]);
JFrame frame = new JFrame(title);
[Link](JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = [Link]("Pizza Toppings");
[Link](border);
JCheckBox check = new JCheckBox("Anchovies");
[Link](check);
check = new JCheckBox("SHOES");
[Link](check);
check = new JCheckBox("Socks");
[Link](check);
check = new JCheckBox("Pants");
[Link](check);
check = new JCheckBox("vest");
[Link](check);
JButton button = new JButton("Shirts");
Container contentPane = [Link]();
[Link](panel,
[Link](true);
}
}

Common questions

Powered by AI

Event-driven programming facilitates interactivity in Java applications by enabling applications to respond dynamically to user inputs, such as clicks or keystrokes. It relies on the continuous monitoring of events and executing designated event handlers, thus making applications responsive and capable of handling asynchronous user interactions efficiently .

Inheritance in Java allows a class to use fields and methods of another class, promoting code reusability. In the Rectangle class example, defining methods like getArea() and getPerimeter() within the class allows any subclass to inherit these methods without redefining them, enabling a structured and efficient use of code across different parts of the program .

In Java, events are used to handle user interactions such as button clicks and mouse movements. Three event types are: 1) ActionEvent, generated by user actions like pressing a button; 2) AdjustmentEvent, triggered when a user changes the value of a scrollbar or other adjustable component; 3) ComponentEvent, generated when a component is hidden, shown, moved, or resized. ActionEvents are common in GUI button operations, AdjustmentEvents are used for scrollbars, and ComponentEvents are applicable when handling window changes .

Polymorphism in Java allows for methods to perform differently based on the object they are invoked upon, enhancing flexibility and enabling a single interface to control access to a general class of actions. For instance, using polymorphism, different object types can be processed in a generalized way, which is resolved at runtime, allowing for more dynamic and scalable code development within object-oriented programs .

Titles and borders like those used in JPanel components serve to enhance the visual clarity and user experience of the UI. In a JPanel containing JCheckBoxes, a titled border can categorize checkboxes, indicating what group of options they belong to, such as 'Pizza Toppings.' This structuring aids in organizing information and improves user navigation and interaction with the application .

In Java, method overloading occurs when multiple methods have the same name but different parameter lists within the same class, and is resolved at compile time. Method overriding, however, involves defining a method in a subclass that already exists in its superclass with the same signature, and is resolved at runtime based on the object's type .

Setting components with a null layout in Java GUI applications can lead to issues such as poor layout adaptability across different screen sizes, difficulty in maintaining and modifying the UI, and complications in aligning components precisely. A null layout means manual management of component positions, which is error-prone and often inefficient for dynamic interfaces .

Java does not use destructors like some other programming languages (e.g., C++); instead, it relies on garbage collection for memory management. This automatic process helps in managing heap memory by reclaiming memory allocated to objects that are no longer reachable, thus preventing memory leaks and optimizing performance in object-oriented design .

A JScrollPane in Java is used to provide a scrollable view of another component. It automatically adds horizontal and vertical scroll bars if the wrapped component is larger than the JScrollPane itself. The JScrollPane manages various optional viewports, including row and column headers, which can be specified using setRowHeaderView and setColumnHeaderView methods. This allows synchronized scrolling of the header with the main component .

In the JCheckBox example, the JFrame constructor is used to create a window that hosts UI components like JPanel and JCheckBoxes. By initializing JFrame with a title, setting the default close operation, adjusting its visibility, and adding JPanel with JCheckBoxes to its content pane, it establishes the foundation for a graphical user interface .

You might also like