Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

16 MQB

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.*;

public class DrawTest

public static void main(String[] args)

DrawFrame frame = new DrawFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

/**

* A frame that contains a panel with drawings

*/

class DrawFrame extends JFrame

public DrawFrame()

setTitle("DrawTest");

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// add panel to frame

DrawComponent component = new DrawComponent();

add(component);
}

public static final int DEFAULT_WIDTH = 400;

public static final int DEFAULT_HEIGHT = 400;

/**

* A component that displays rectangles and ellipses.

13

*/

class DrawComponent extends JComponent

public void paintComponent(Graphics g)

Graphics2D g2 = (Graphics2D) g;

// draw a rectangle

double leftX = 100;

double topY = 100;

double width = 200;

double height = 150;

Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width,

height);

g2.draw(rect);

// draw the enclosed ellipse

Ellipse2D ellipse = new Ellipse2D.Double();

ellipse.setFrame(rect);

g2.draw(ellipse);

// draw a diagonal line

g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY +


height));

// draw a circle with the same center

double centerX = rect.getCenterX();

double centerY = rect.getCenterY();

double radius = 150;

Ellipse2D circle = new Ellipse2D.Double();

circle.setFrameFromCenter(centerX, centerY, centerX + radius,

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

queue for what operating system reporting.

* The information about the events is encapsulated in the

event object.

* All event object will be inherited from java.util.EventObject.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ButtonTest

public static void main(String[] args)

ButtonFrame frame = new ButtonFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

class ButtonFrame extends JFrame

public ButtonFrame()

setTitle("ButtonTest");

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

JButton yellowButton = new JButton("Yellow");

JButton blueButton = new JButton("Blue");

JButton redButton = new JButton("Red");

buttonPanel = new JPanel();

// add buttons to panel

buttonPanel.add(yellowButton);

buttonPanel.add(blueButton);

buttonPanel.add(redButton);

add(buttonPanel);

27
ColorAction yellowAction = new ColorAction(Color.YELLOW);

ColorAction blueAction = new ColorAction(Color.BLUE);

ColorAction redAction = new ColorAction(Color.RED);

yellowButton.addActionListener(yellowAction);

blueButton.addActionListener(blueAction);

redButton.addActionListener(redAction);

private class ColorAction implements ActionListener

public ColorAction(Color c)

backgroundColor = c;

public void actionPerformed(ActionEvent event)

buttonPanel.setBackground(backgroundColor);

private Color backgroundColor;

private JPanel buttonPanel;

public static final int DEFAULT_WIDTH = 300;

public static final int DEFAULT_HEIGHT = 200;

}
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.*;

public class Test {


public static void main(String[] args)
throws IOException
{
File file = new File("C:\\Users\\hp\\Desktop\\TextReader.txt");
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new
InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new
BufferedReader(inputStreamReader);

String line;
int wordCount = 0;
int characterCount = 0;
int paraCount = 0;
int whiteSpaceCount = 0;
int sentenceCount = 0;

while ((line = bufferedReader.readLine()) != null) {


if (line.equals("")) {
paraCount += 1;
}
else {
characterCount += line.length();
String words[] = line.split("\\s+");
wordCount += words.length;
whiteSpaceCount += wordCount - 1;
String sentence[] = line.split("[!?.:]+");
sentenceCount += sentence.length;
}
}
if (sentenceCount >= 1) {
paraCount++;
}
System.out.println("Total word count = "+ wordCount);
System.out.println("Total number of sentences = "+
sentenceCount);
System.out.println("Total number of characters = "+
characterCount);
System.out.println("Number of paragraphs = "+ paraCount);
System.out.println("Total number of whitespaces = "+
whiteSpaceCount);
}
}
OUTPUT:
13. Write a Java program to count the number of vowels,consonants,letters and digits in a
string?

public class CountVowelConsonant {

public static void main(String[] args) {

//Counter variable to store the count of vowels and consonant

int vCount = 0, cCount = 0;

//Declare a string

String str = "This is a really simple sentence";

//Converting entire string to lower case to reduce the comparisons

str = str.toLowerCase();

for(int i = 0; i < str.length(); i++) {

//Checks whether a character is a vowel

if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' ||


str.charAt(i) == 'u') {

//Increments the vowel counter

vCount++;

}
//Checks whether a character is a consonant

else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {

//Increments the consonant counter

cCount++;

System.out.println("Number of vowels: " + vCount);

System.out.println("Number of consonants: " + 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:

You might also like