0% found this document useful (0 votes)
5 views7 pages

U4 Oops Sem

Uploaded by

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

U4 Oops Sem

Uploaded by

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

Develop a java program that have 11 text fields one submit button.

When you
press the button first 10 text field’s average has to be displayed in the 11th text
field.

📘 Title:
Program to calculate the average of 10 numbers using Java AWT.

📋 Algorithm:
1. Start the program.
2. Create a frame using AWT and set its size and layout.
3. Add 10 input text fields and 1 result text field to the frame.
4. Add a button named "Calculate Average".
5. On button click, read numbers from the input fields.
6. Handle any invalid inputs using try-catch.
7. Calculate the average and display it in the result text field.
8. End the program.

💻 Java Code:
import [Link].*;

import [Link].*;

public class SimpleAverageCalculator {

public static void main(String[] args) {

Frame frame = new Frame("Average Calculator");

[Link](400, 300);

[Link](new FlowLayout());

TextField[] textFields = new TextField[11];


for (int i = 0; i < 11; i++) {

textFields[i] = new TextField(5);

[Link](textFields[i]);

// Make result field read-only

textFields[10].setEditable(false);

Button submitButton = new Button("Calculate Average");

[Link](submitButton);

// Action on button click

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

double sum = 0;

int count = 0;

for (int i = 0; i < 10; i++) {

try {

sum += [Link](textFields[i].getText());

count++;

} catch (NumberFormatException ex) {

textFields[10].setText("Error");

return;

}
}

double average = sum / count;

textFields[10].setText([Link]("%.2f", average));

});

[Link](true);

🖼 Sample Output Layout (Rough GUI Box):


-----------------------------------------

✅ Conclusion:
The average of 10 input numbers is calculated and displayed using Java AWT components
such as TextField, Button, and ActionListener.
✅ PART 1: Line-by-line Explanation of the Java
Program to Display an Image
Here’s the corrected and properly structured version of your code, followed by line-by-line
explanation and then how to write it in the exam.

✅ Corrected Code:
java
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];

public class ImageDisplay extends Frame {


BufferedImage img;

// Constructor
public ImageDisplay() {
try {
img = [Link](new File("[Link]")); // Load image
} catch (IOException e) {
[Link]("Image not found!");
}

setSize(500, 500); // Set frame size


setVisible(true); // Make frame visible
}

// paint() method to draw image on the frame


public void paint(Graphics g) {
if (img != null) {
[Link](img, 50, 50, this); // Draw image at (x=50, y=50)
}
}

public static void main(String[] args) {


new ImageDisplay(); // Create frame object
}
}

📘 Line-by-Line Explanation:
Line Code Explanation

Imports AWT classes for GUI like Frame,


1 import [Link].*;
Graphics.

Imports image-related classes like


2 import [Link].*;
BufferedImage.

3 import [Link].*; Needed for file reading.


Line Code Explanation

Contains [Link]() for reading


4 import [Link];
image files.

public class ImageDisplay extends


6 Frame {
Creates a class that is a GUI window.

7 BufferedImage img; Variable to store the image after loading.

10– Constructor to load the image and set up


public ImageDisplay()
15 the window.

img = [Link](new
11 File("[Link]"));
Reads the image file named [Link].

12 catch (IOException e) Catches error if the image is missing.

16 setSize(500, 500); Sets window size to 500x500 pixels.

17 setVisible(true); Makes the window appear.

20– Draws the image when the window is


public void paint(Graphics g)
24 refreshed.

Displays the image on frame starting at


22 [Link](img, 50, 50, this);
(50,50).

public static void main(String[]


26 args)
Main method to run the program.

27 new ImageDisplay(); Creates an object of the frame class.

✅ PART 2: How to Write This in Exam for Full Marks


1. 📘 Title
Title: Program to display an image on a Frame using Java AWT and ImageIO.

2. 📋 Algorithm

1. Start the program.


2. Create a Frame class extending Frame.
3. Use [Link]() to load the image.
4. Override paint() method to draw the image using Graphics.
5. Display the image using drawImage().
6. Make the frame visible.
7. Stop.
3. 💻 Code
java
CopyEdit
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];

public class ImageDisplay extends Frame {


BufferedImage img;

public ImageDisplay() {
try {
img = [Link](new File("[Link]")); // Load image
} catch (IOException e) {
[Link]("Image not found!");
}

setSize(500, 500);
setVisible(true);
}

public void paint(Graphics g) {


if (img != null) {
[Link](img, 50, 50, this); // Draw image
}
}

public static void main(String[] args) {


new ImageDisplay(); // Run program
}
}

4. 🖼 Sample Output Diagram (Rough Sketch)


---------------------------------
|

---------------------------------
5. ✅ Conclusion
Conclusion: The program loads an image using ImageIO and displays it on a
Frame using AWT in Java.

You might also like