Professional Documents
Culture Documents
Final QB Java N-T
Final QB Java N-T
1. Develop a Java program to display the following picture as Output using Applet viewer and
discuss its coordinates ?
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
/**
*/
public DrawFrame()
setTitle("DrawTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
add(component);
}
/**
13
*/
Graphics2D g2 = (Graphics2D) g;
// draw a rectangle
height);
g2.draw(rect);
ellipse.setFrame(rect);
g2.draw(ellipse);
centerY + radius);
g2.draw(circle);
OUTPUT:
15. Write a java program to implement event handling by implementing Action Listener?
In event handling programming we need to write the code that constantly checks the event
event object.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
public ButtonFrame()
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
27
ColorAction yellowAction = new ColorAction(Color.YELLOW);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
public ColorAction(Color c)
backgroundColor = c;
buttonPanel.setBackground(backgroundColor);
}
12 Write a Java program that reads a .txt file from user and display number of words and lines
in the file
import java.io.*;
String line;
int wordCount = 0;
int characterCount = 0;
int paraCount = 0;
int whiteSpaceCount = 0;
int sentenceCount = 0;
//Declare a string
str = str.toLowerCase();
vCount++;
}
//Checks whether a character is a consonant
cCount++;
Output:
Number of vowels: 10
Number of consonants: 17
11. Develop an application to show multithreaded program to show hours, minutes and
seconds on separate threads.
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MultithreadedClock extends JFrame {
private JLabel hoursLabel, minutesLabel, secondsLabel;
public MultithreadedClock() {
setTitle("Multithreaded Clock");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLayout(new FlowLayout());
hoursLabel = new JLabel("Hours: ");
minutesLabel = new JLabel("Minutes: ");
secondsLabel = new JLabel("Seconds: ");
add(hoursLabel);
add(minutesLabel);
add(secondsLabel);
// Create and start threads
Thread hoursThread = new TimeThread(Calendar.HOUR_OF_DAY, "HH");
Thread minutesThread = new TimeThread(Calendar.MINUTE, "mm");
Thread secondsThread = new TimeThread(Calendar.SECOND, "ss");
hoursThread.start();
minutesThread.start();
secondsThread.start();
}
private class TimeThread extends Thread {
private int calendarField;
private String format;
public TimeThread(int calendarField, String format) {
this.calendarField = calendarField;
this.format = format;
}
@Override
public void run() {
try {
while (true) {
Calendar calendar = Calendar.getInstance();
OUTPUT: