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

Java User-Defined Packages and GUI Apps

The document outlines the creation of Java programs utilizing user-defined packages, a GUI displaying text and images, and a tip calculator application using JavaFX. It provides step-by-step instructions for creating and importing packages, as well as building a simple GUI with Swing and JavaFX components. The document includes source code examples and expected output for each program, demonstrating successful execution and functionality.

Uploaded by

drajud431
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)
5 views6 pages

Java User-Defined Packages and GUI Apps

The document outlines the creation of Java programs utilizing user-defined packages, a GUI displaying text and images, and a tip calculator application using JavaFX. It provides step-by-step instructions for creating and importing packages, as well as building a simple GUI with Swing and JavaFX components. The document includes source code examples and expected output for each program, demonstrating successful execution and functionality.

Uploaded by

drajud431
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

Exercise-8

User defined packages


(a)Write a java program that import and use the user defined packages.

AIM: To create a Java program that imports and utilizes user-defined packages for organized code
structure.
Description:
A package is a directory (or folder) that organizes related classes, interfaces, and subpackages in Java.
Packages are divided into two categories:
 Built-in Packages (packages from the Java API)
 User-defined Packages (create your own packages)
(i) Creating a package:
To create a package, you choose a name for the package and put a package statement with that name at
the top of every source file that contains the types (classes, interfaces, enumerations, and annotation
types) that you want to include in the package.
The package statement (for example, package graphics;) must be the first line in the source file. There can
be only one package statement in each source file, and it applies to all types in the file.
Steps:
1. First declare the name of the package using package keyword
Example: package mypack;
2. Type the following SOURCE-CODE under this package statement. In package : class ,data, methods
all are public

package mypack;
public class box
{
public int l=10,b=20;
public void display()
{
[Link](l);
[Link](b);
}
}

To compile a Java file using the javac command, you use:


javac -d . [Link]

 Javac : The Java compiler command.


 -d . : This option specifies the destination directory for the compiled class files. The . indicates
the current directory, maintaining the package structure.
 [Link]: The name of the Java file you want to compile.

(ii) importing a package:


Java has an import statement that allows you to import an entire package (as in earlier examples), or use
only certain classes and interfaces defined in the package.
Steps:
1. packages can be accessed by using the import statement
General form: import pack1[.pack2].(classname/*);
Example: import [Link].*;
Here pack1 is name of top level package and pack2 is name of sub package
2. Type the following SOURCE-CODE under the current working directory and save the
SOURCE-CODE with a file [Link].

import [Link];
class packagedemo
{
public static void main(String args[])
{
box b1=new box();
[Link]();
}
}
3. Now compile the above SOURCE-CODE in the current working directory
javac [Link]
4. Execute the above SOURCE-CODE in current working directory
java packagedemo

OUT-PUT:

Result:
The program successfully imported a user-defined package and displayed the message
b) Without writing any code, build a GUI that display text in label and image in an image view.

AIM:
To create a JavaFX GUI that displays text in a label and an image in an image view.
Description:
This Java program demonstrates the use of Swing, a part of Java's standard library used to build graphical
user interfaces (GUIs). The goal of this program is to create a simple window that displays a text
message and an image, providing a basic understanding of how visual elements can be arranged and
displayed using Java.
The program uses a JFrame as the main application window, which acts as a container for all GUI
components. A JLabel is used to display a line of text such as "Welcome to Java GUI". Another JLabel is
used to show an image, which is loaded using the ImageIcon class. Both labels are arranged vertically
using a layout manager like BoxLayout to maintain a clean and simple interface.
Source Code:
import [Link].*;
public class GUIExample {
public static void main(String[] args) {
// Create a new JFrame (main window)
JFrame frame = new JFrame("Java GUI Example");
// Set the layout manager to BoxLayout (vertical)
[Link](new BoxLayout([Link](), BoxLayout.Y_AXIS));
// Create a JLabel to display text
JLabel textLabel = new JLabel("Welcome to Java GUI!", [Link]);
[Link](JLabel.CENTER_ALIGNMENT);
// Load an image using ImageIcon
ImageIcon imageIcon = new ImageIcon("[Link]");
// Make sure [Link] is in the same folder
JLabel imageLabel = new JLabel(imageIcon);
[Link](JLabel.CENTER_ALIGNMENT);
// Add the labels to the frame
[Link](textLabel);
[Link](imageLabel);
// Set frame size and close operation
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true); // Show the frame
}
}

OUTPUT:
Result:
The program successfully displayed a welcome message in a label and an image in the image view.
c) Build a tip Calculator app using several javafx components and learn how to respond to user
interactions with the GUI.
AIM:
To create a JavaFX tip calculator app that responds to user interactions for calculating tip amounts based
on bill and tip percentage inputs.
Description:
JavaFX Tip Calculator application provides a simple interface for calculating tips based on a bill amount
and tip percentage entered by the user. The GUI consists of two text fields where users input the bill total
and desired tip percentage. Upon clicking the “Calculate Tip” button, the program calculates the tip by
multiplying the bill amount by the tip percentage and then displays the result formatted as currency. The
interface also handles invalid inputs gracefully by informing the user if non-numeric values are entered.
This application demonstrates the use of JavaFX components such as TextField, Label, Button, and layout
controls like VBox. It highlights event-driven programming by attaching an action listener to the button
to respond to user interactions.
SOURCE CODE:
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
public class TipCalculator extends Application {
public void start(Stage stage) {
// Input for bill amount
TextField billField = new TextField();
[Link]("Enter bill amount");
// Input for tip percentage
TextField tipField = new TextField();
[Link]("Enter tip %");
// Label for showing result
Label resultLabel = new Label("Tip amount: ");
// Button to calculate tip
Button calcBtn = new Button("Calculate Tip");
[Link](e -> {
try {
double bill = [Link]([Link]());
double tipPercent = [Link]([Link]());
double tip = bill * tipPercent / 100;
[Link]([Link]("Tip amount: $%.2f", tip));
} catch (NumberFormatException ex) {
[Link]("Please enter valid numbers!");
}
});
VBox root = new VBox(10, billField, tipField, calcBtn, resultLabel);
[Link](new Insets(15));
Scene scene = new Scene(root, 300, 200);
[Link]("Tip Calculator");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch();
}}

OUTPUT:
For Java 11 before versions
Compile: Javac [Link]
Run :java TipCalculator
For java 11 after versions
Compile: javac --module-path "C:\Program Files\Java\javafx-sdk-24.0.2\lib" --add-modules
[Link] [Link]
Run : java --module-path "C:\Program Files\Java\javafx-sdk-24.0.2\lib" --add-modules [Link]
TipCalculator

Result:
The Tip Calculator app successfully calculates and displays the tip amount based on user input for the bill
and tip percentage.

You might also like