0% found this document useful (0 votes)
2 views18 pages

Java Detailed Practical for Execution

The document provides troubleshooting steps for the 'javac' not recognized error, detailing how to check if Java is installed, install the JDK, and set the JAVA PATH. It also includes a practical guide for creating a Java applet that demonstrates graphics, font, and color classes, along with a calculator application using AWT with event handling. The document contains code examples, explanations for each step, and execution instructions for both the applet and calculator.

Uploaded by

sahilshukla0506
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)
2 views18 pages

Java Detailed Practical for Execution

The document provides troubleshooting steps for the 'javac' not recognized error, detailing how to check if Java is installed, install the JDK, and set the JAVA PATH. It also includes a practical guide for creating a Java applet that demonstrates graphics, font, and color classes, along with a calculator application using AWT with event handling. The document contains code examples, explanations for each step, and execution instructions for both the applet and calculator.

Uploaded by

sahilshukla0506
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

🔴 PROBLEM 2: 'javac' is not recognized

This error means:

❌ Java is NOT installed


OR
❌ Java is installed but PATH is not set

Without fixing this, NO Java program will compile or run.

✅ SOLUTION PART A: Check if Java is


installed
Type:
java -version

Case 1: If you see version info ✅

Java is installed → PATH issue only

Case 2: If you see:


'java' is not recognized

Java is NOT installed → you must install JDK

SOLUTION PART B: Install JDK


(MANDATORY)
Step 1: Download JDK

 Download JDK 8 (best for applet syllabus)


 Install it normally (Next → Next → Finish)
After installation, note this path (example):
C:\Program Files\Java\jdk1.8.0_361

✅ SOLUTION PART C: Set JAVA PATH


(MOST IMPORTANT STEP)
Step 1: Open Environment Variables

1. Press Windows key


2. Search: Environment Variables
3. Click Edit the system environment variables
4. Click Environment Variables

Step 2: Edit PATH

1. Under System Variables, find Path


2. Click Edit
3. Click New
4. Add this path (example):
C:\Program Files\Java\jdk1.8.0_361\bin

5. Click OK → OK → OK

Step 3: Restart Command Prompt

❗ Close CMD
❗ Open CMD again (important!)

✅ VERIFY FIX
Now type:
javac -version

You should see something like:


javac 1.8.0_361

Then test:
appletviewer

If no error → ✅ SUCCESS

▶ NOW RUN YOUR APPLET (FINAL


STEPS)
Step 1: Go to Desktop
cd Desktop

(or cd OneDrive\Desktop if needed)

Step 2: Compile
javac [Link]

(no output = success)

Step 3: Run Applet


appletviewer [Link]

🎉 Your graphics applet window will open!


PRACTICAL 2
Java Applet to Demonstrate Graphics, Font, and Color Classes

Aim
To write a Java Applet that demonstrates the use of:

 Graphics class
 Font class
 Color class

by drawing text, shapes, and using different fonts and colors.

Basic Concept (Before Code)


1⃣ Applet

 An applet is a Java program that runs inside a browser or applet viewer.


 It does not have main() method.
 Graphics are drawn using the paint(Graphics g) method.

⚠ Note (for exams):


Applet is deprecated in modern Java, but still included in syllabus for understanding
graphics.

2⃣ Graphics Class

Used to draw:

 Text
 Lines
 Rectangles
 Ovals
 Polygons
 Arcs

3⃣ Font Class
Used to define:

 Font name
 Font style (BOLD, ITALIC)
 Font size

4⃣ Color Class

Used to apply colors like:

 [Link]
 [Link]
 [Link]
 etc.

Code
import [Link];
import [Link].*;

public class GraphicsMethods extends Applet {

String s = new String();


String s1 = new String(), s2 = new String();

Font f1 = new Font("Courier New", [Link], 20);

public void paint(Graphics ga) {

[Link](f1);
[Link]([Link]);
[Link]("Illustration of methods of Graphics Class", 200,
520);

Font f2 = [Link]();
s = [Link]();
[Link](s, 5, 540);

[Link]([Link]);
Color col = [Link]();
s2 = [Link]();
[Link](s2, 5, 560);

[Link](160, 500, 70, 90);


[Link](160, 5, 60, 60);
[Link](10, 120, 155, 95);

[Link]([Link]);
[Link](700, 140, 50, 150);

[Link]([Link]);
[Link](380, 100, 200, 180);
[Link](400, 150, 180, 280, 90, 70);

int[] x2 = {200, 120, 280, 240};


int[] y2 = {260, 370, 370, 270};

[Link]([Link]);
[Link](x2, y2, 4);

[Link]([Link]);
[Link](15, 15, 30, 50);

FontMetrics f3 = [Link]();
s1 = [Link]();
[Link](s1, 5, 580);

[Link]([Link]);
[Link](510, 400, 90, 80, 20, 20);
}
}

Step-by-Step Code Explanation

🔹 Step 1: Import Statements


import [Link];
import [Link].*;

 [Link] → Needed to create an applet


 [Link].* → Provides Graphics, Font, Color classes

🔹 Step 2: Class Declaration


public class GraphicsMethods extends Applet

 Class inherits Applet


 Makes it a Java Applet program

🔹 Step 3: Variable Declaration


String s, s1, s2;
Font f1 = new Font("Courier New", [Link], 20);

 String variables store font & color details


 Font object created with:
o Font Name: Courier New
o Style: Bold
o Size: 20

🔹 Step 4: paint() Method


public void paint(Graphics ga)

 Called automatically whenever applet is displayed


 Graphics ga is used to draw shapes and text

🔹 Step 5: Setting Font and Color


[Link](f1);
[Link]([Link]);

 Applies font and blue color


 Affects all following drawings until changed

🔹 Step 6: Drawing Text


[Link]("Illustration of methods of Graphics Class", 200, 520);

 Displays text at position (x=200, y=520)

🔹 Step 7: Getting Current Font & Color


Font f2 = [Link]();
Color col = [Link]();

 Retrieves current font and color


 Converts them to string and displays

🔹 Step 8: Drawing Rectangles


[Link](160, 500, 70, 90);
[Link](160, 5, 60, 60);

 fillRect() → Filled rectangle


 drawRect() → Outline rectangle
🔹 Step 9: Drawing Ovals
[Link](10, 120, 155, 95);
[Link](700, 140, 50, 150);

 drawOval() → Hollow oval


 fillOval() → Filled oval

🔹 Step 10: Drawing Line & Arc


[Link](380, 100, 200, 180);
[Link](400, 150, 180, 280, 90, 70);

 Line between two points


 Arc with start angle & sweep angle

🔹 Step 11: Drawing Polygon


int[] x2 = {200, 120, 280, 240};
int[] y2 = {260, 370, 370, 270};
[Link](x2, y2, 4);

 Creates a filled polygon using coordinate arrays

🔹 Step 12: Font Metrics


FontMetrics f3 = [Link]();

 Gives font measurement details


 Displayed using drawString()

🔹 Step 13: Rounded Rectangle


[Link](510, 400, 90, 80, 20, 20);

 Draws rectangle with rounded corners

Applet HTML Code


<applet code="[Link]" width="800" height="600">
</applet>

▶ Execution Steps (Exam-Ready)


Step 1:

Save file as:

[Link]

Step 2:

Compile:

javac [Link]

Step 3:

Run using Applet Viewer:

appletviewer [Link]

🖥 Output Description (Important for Viva)


 Blue heading text using Courier New font
 Multiple shapes:
o Rectangles
o Ovals
o Line
o Arc
o Polygon
o Rounded rectangle
 Different colors applied to each shape
 Font and color information printed as text

📷 Expected Output (Visual Idea)


PRACTICAL 3
Design a Calculator using AWT (with Event Handling)

Aim
To design a simple calculator using AWT/Swing components that performs:

 Addition
 Subtraction
 Multiplication
 Division
 Clear operation

using ActionListener for event handling.

Concepts Used (Before Code)


1⃣ GUI Components

 JFrame → Main window


 JLabel → Text labels
 JTextField → Input/output fields
 JButton → Operation buttons

2⃣ Event Handling

 ActionListener interface
 actionPerformed() method
 Button click detection using getSource()

3⃣ Layout

 null layout → Manual positioning using setBounds()


Given Code (As Provided)
import [Link].*;
import [Link].*;
import [Link].*;

public class Calculator extends JFrame implements ActionListener {

private JTextField num1Field, num2Field, resultField;


private JButton addButton, subtractButton, multiplyButton,
divideButton, clearButton;

public Calculator() {
setTitle("Simple Calculator");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);

JLabel num1Label = new JLabel("First Number:");


[Link](20, 20, 100, 20);
num1Field = new JTextField();
[Link](130, 20, 100, 20);

JLabel num2Label = new JLabel("Second Number:");


[Link](20, 60, 100, 20);
num2Field = new JTextField();
[Link](130, 60, 100, 20);

JLabel resultLabel = new JLabel("Result:");


[Link](20, 100, 100, 20);
resultField = new JTextField();
[Link](130, 100, 100, 20);

[Link](false);

addButton = new JButton("Add");


[Link](20, 140, 80, 30);
subtractButton = new JButton("Subtract");
[Link](110, 140, 80, 30);
multiplyButton = new JButton("Multiply");
[Link](200, 140, 80, 30);
divideButton = new JButton("Divide");
[Link](290, 140, 80, 30);
clearButton = new JButton("Clear");
[Link](20, 180, 80, 30);

[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);

add(num1Label);
add(num1Field);
add(num2Label);
add(num2Field);
add(resultLabel);
add(resultField);
add(addButton);
add(subtractButton);
add(multiplyButton);
add(divideButton);
add(clearButton);

setVisible(true);
}

public void actionPerformed(ActionEvent e) {


try {
double num1 = [Link]([Link]());
double num2 = [Link]([Link]());
double result = 0;

if ([Link]() == addButton) {
result = num1 + num2;
} else if ([Link]() == subtractButton) {
result = num1 - num2;
} else if ([Link]() == multiplyButton) {
result = num1 * num2;
} else if ([Link]() == divideButton) {
if (num2 == 0) {
[Link]("Cannot divide by zero");
return;
}
result = num1 / num2;
} else if ([Link]() == clearButton) {
[Link]("");
[Link]("");
[Link]("");
return;
}

[Link]([Link](result));

} catch (NumberFormatException ex) {


[Link]("Invalid input");
}
}

public static void main(String[] args) {


new Calculator();
}
}

Step-by-Step Code Explanation

🔹 Step 1: Import Statements


import [Link].*;
import [Link].*;
import [Link].*;

 [Link].* → GUI basics


 [Link].* → Event handling
 [Link].* → Advanced GUI components

🔹 Step 2: Class Declaration


public class Calculator extends JFrame implements ActionListener

 JFrame → Creates application window


 ActionListener → Handles button click events

🔹 Step 3: Declaring Components


private JTextField num1Field, num2Field, resultField;
private JButton addButton, subtractButton, multiplyButton, divideButton,
clearButton;

 Text fields → Input and output


 Buttons → Arithmetic operations

🔹 Step 4: Constructor (GUI Design)


public Calculator() {

This is where the entire GUI is created.

Window Settings
setTitle("Simple Calculator");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);

 null layout → manual positioning

Labels & Text Fields


JLabel num1Label = new JLabel("First Number:");
num1Field = new JTextField();

 Label shows text


 TextField takes user input
Result Field
[Link](false);

 Prevents manual editing of result

Buttons Creation
addButton = new JButton("Add");
subtractButton = new JButton("Subtract");

 Creates calculator buttons

Event Registration
[Link](this);

 Registers button click events

🔹 Step 5: Adding Components to Frame


add(num1Label);
add(num1Field);
...

 Makes components visible in window

🔹 Step 6: actionPerformed() Method


public void actionPerformed(ActionEvent e)

 Automatically executed when any button is clicked

Input Conversion
double num1 = [Link]([Link]());

 Converts text input to numbers

Operation Selection
if ([Link]() == addButton)

 Detects which button is clicked


Division by Zero Handling
if (num2 == 0) {
[Link]("Cannot divide by zero");
}

 Prevents runtime error

Exception Handling
catch (NumberFormatException ex)

 Handles invalid input like letters or symbols

🔹 Step 7: main() Method


public static void main(String[] args) {
new Calculator();
}

 Starts the application

▶ Execution Steps (Very Important for Exams)


Step 1:

Save file as:

[Link]

Step 2:

Compile:

javac [Link]

Step 3:

Run:

java Calculator
Output Description
 Window titled Simple Calculator
 Two input fields
 Result display field
 Buttons for:
o Add
o Subtract
o Multiply
o Divide
o Clear
 Displays result or error message

You might also like