0% found this document useful (0 votes)
57 views

Java Assignment

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Java Assignment

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 java.awt.*;
import java.awt.event.*;
class StudentGradingSystem extends Frame
{
TextField name,pass;
Button b1,b2;
StudentGradingSystem()
{
setLayout(new FlowLayout());
this.setLayout(null);
Label p=new Label("Student Name:",Label.CENTER);
Label p=new Label("Subject One:",Label.CENTER);
Label p=new Label("Subject Two:",Label.CENTER);
Label p=new Label("Average Marks:",Label.CENTER);
name=new TextField(20);
pass=new TextField(20);
pass.setEchoChar('#');
b1=new Button("submit");
b2=new Button("cancel");
this.add(n);
this.add(name);
this.add(p);
this.add(pass);
this.add(b1);
this.add(b2);
n.setBounds(70,90,90,60);
p.setBounds(70,130,90,60);
name.setBounds(200,100,90,20);
pass.setBounds(200,140,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
}
public static void main(String args[])
{
MyLoginWindow ml=new StudentGradingSystem();
ml.setVisible(true);
ml.setSize(400,400);
ml.setTitle("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);
System.out.println("Length = " + rect.length);
System.out.println("Breadth = " + rect.breadth);
System.out.println("Area = " + rect.getArea());
System.out.println("Perimeter = " + rect.getPerimeter());
}
}
class Rectangle
{
double length;
double breadth;
Rectangle(double length, double breadth)
{
this.length = length;
this.breadth = 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 javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class ACheckBox {
public static void main(String args[]) {
String title = (args.length == 0 ? "CheckBox Sample" : args[0]);
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Pizza Toppings");
panel.setBorder(border);
JCheckBox check = new JCheckBox("Anchovies");
panel.add(check);
check = new JCheckBox("SHOES");
panel.add(check);
check = new JCheckBox("Socks");
panel.add(check);
check = new JCheckBox("Pants");
panel.add(check);
check = new JCheckBox("vest");
panel.add(check);
JButton button = new JButton("Shirts");
Container contentPane = frame.getContentPane();
contentPane.add(panel,
frame.setVisible(true);
}
}

You might also like