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

Java PRC 02

The document outlines a series of practical exercises for Java programming, including installation of the JDK, setting up the environment, and writing various Java programs. Each practical exercise includes specific aims, code examples, and explanations, covering topics like basic syntax, debugging, and XML file creation. The exercises range from simple 'Hello World' programs to more complex tasks such as calculating percentages and handling exceptions.

Uploaded by

shrutidarbar001
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)
2 views15 pages

Java PRC 02

The document outlines a series of practical exercises for Java programming, including installation of the JDK, setting up the environment, and writing various Java programs. Each practical exercise includes specific aims, code examples, and explanations, covering topics like basic syntax, debugging, and XML file creation. The exercises range from simple 'Hello World' programs to more complex tasks such as calculating percentages and handling exceptions.

Uploaded by

shrutidarbar001
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

Java programming (C01209)

240101116015

PRACTICAL-1
AIM:
• Install the JDK (Download the JDK and install it.)
• Set path of the jdk/bin directory.
• Create Java ggram
• Compile & Run the java program
• Write a simple “Hello World” java program, compilation, debugging, executing using
java compiler and interpreter.

[Link] the JDK (Download and Install)

JDK (Java Development Kit) is required to write, compile, and run Java programs.

Steps to Install JDK:

1. Open a web browser


2. Go to Oracle JDK or OpenJDK website
3. Download the JDK according to your operating system (Windows / Linux / Mac)
4. Run the installer
5. Follow on-screen instructions
6. JDK will be installed in:
7. C:\Program Files\Java\jdk

[Link] Path of the JDK / bin Directory (Windows)

Setting the PATH allows Java commands (javac, java) to run from any folder.

Steps to Set Path:

8. Right-click This PC → Properties


9. Click Advanced system settings
10. Click Environment Variables
11. Under System Variables, select Path 12. Click Edit → New
13. Add:
14. C:\Program Files\Java\jdk\bin
15. Click OK and restart Command Prompt

Verify Installation:

java -version javac version

[Link] a Java Program

Steps:

16. Open Notepad


17. Type the Java code

1
Java programming (C01209)
240101116015

18. Save the file as:


19. [Link]

[Link] “Hello World” Java Program

class HelloWorld
{ public static void main(String[] args)
{
[Link]("Hello
World"); }
}

Explanation:

• class HelloWorld → Class name


• main() → Program execution starts here
• [Link]() → Prints output

[Link] the Java Program (Using Java Compiler)

What is Compilation?

Compilation checks syntax errors and converts .java file into .class file (bytecode).

Command:

javac [Link]

Result:

• If no error → [Link] file created


• If error → Error message shown

[Link] the Java Program

Example Error:

[Link]("Hello World")

Missing semicolon

Error Message:

';' expected

Debugging:

• Identify the error

2
Java programming (C01209)
240101116015

• Correct the mistake


• Recompile the program

[Link]("Hello World");

[Link] the Java Program (Using Java Interpreter)

What is Execution?

The Java Interpreter runs the bytecode using JVM.

Command:

java HelloWorld

Do not use .class

Output:

PRACTICAL-2

AIM: Write a program to convert rupees to dollar. 60 rupees=1 dollar.

Program:

import [Link];

public class Main { public static void


main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter amount in Rupees: ");


double rupees = [Link]();

double dollars = rupees / 60;

3
Java programming (C01209)
240101116015

[Link]("Amount in Dollars = " +


dollars); }
}

OUTPUT:

PRACTICAL-3

AIM: Write a program that calculate percentage marks of the student if marks
of 6 subjects are given.

Program:

import [Link]; public class


Main { public static void main(String[]
args) {
Scanner sc = new Scanner([Link]);

double s1, s2, s3, s4, s5, s6;


double total, percentage;

[Link]("Enter marks of Subject 1: ");


s1 = [Link]();

4
Java programming (C01209)
240101116015

[Link]("Enter marks of Subject 2: ");


s2 = [Link]();

[Link]("Enter marks of Subject 3: ");


s3 = [Link]();

[Link]("Enter marks of Subject 4: ");


s4 = [Link]();

[Link]("Enter marks of Subject 5: ");


s5 = [Link]();

[Link]("Enter marks of Subject 6: ");


s6 = [Link]();

total = s1 + s2 + s3 + s4 + s5 + s6;
percentage = (total / 600) * 100;

[Link]("Total Marks = " + total);


[Link]("Percentage = " + percentage +
"%"); }
}
OUTPUT:

5
Java programming (C01209)
240101116015

PRACTICAL-4
AIM: Write a program to find length of string and print second half of the
string.

Program:

import [Link];

public class Main


{ public static void main(String[] args)
{

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");


String str = [Link]();

int length = [Link]();


[Link]("Length of the string: " + length);

int mid = length / 2;


[Link]("Second half of the string: " + [Link](mid));
}
}

OUTPUT:

6
Java programming (C01209)
240101116015

PRACTICAL-5
AIM: Write a java program which should display maximum number of given 4
numbers.

Program:

import [Link];

public class Main { public static void

main(String[] args) { Scanner sc = new

Scanner([Link]); int a, b, c, d, max;

[Link]("Enter first number: ");


a = [Link]();

[Link]("Enter second number: ");


b = [Link]();

[Link]("Enter third number: ");


c = [Link]();

[Link]("Enter fourth number: ");


d = [Link]();

max = a;

if (b >
max) max
= b; if (c >
max) max
= c;
if (d > max)
max = d;

[Link]("Maximum number is: " +


max); }
}

OUTPUT:

7
Java programming (C01209)
240101116015

8
Java programming (C01209)
240101116015

PRACTICAL-6
AIM: Write a program to accept a line and check how many consonants and
vowels are there in line.
Program:
import [Link]; public class
Main { public static void main(String[]
args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a line: "); String


line = [Link]().toLowerCase(); int
vowels = 0, consonants = 0;

for (int i = 0; i < [Link](); i++)


{ char ch = [Link](i);

if (ch >= 'a' && ch <= 'z') { if (ch


== 'a' || ch == 'e' || ch == 'i' || ch
== 'o' || ch == 'u') { vowels++;
} else { consonants+
+;
}
}
}

[Link]("Number of vowels: " + vowels);


[Link]("Number of consonants: " + consonants);
}
}

OUTPUT:

PRACTICAL-7
AIM: Write a program to count the number of words that start with capital
letters.

Program:

9
Java programming (C01209)
240101116015

import [Link]; public class


Main { public static void main(String[]
args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a sentence: ");


String sentence = [Link]();

String[] words = [Link](" ");


int count = 0;

for (String word : words) {


if ([Link]() > 0 && [Link]([Link](0))) {
count++;
}
}

[Link]("Number of words starting with capital letters: " + count);


}
}

OUTPUT:

PRACTICAL-8
AIM: Write a program to find that given number or string is palindrome or not.

Program:

import [Link];

public class Main { public static void


main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string or number: ");


String input = [Link]();

10
Java programming (C01209)
240101116015

String reverse = "";

for (int i = [Link]() - 1; i >= 0; i--)


{ reverse = reverse + [Link](i); }

if ([Link](reverse)) {
[Link]("It is a Palindrome.");
} else {
[Link]("It is NOT a
Palindrome."); }
}
}

OUTPUT:

PRACTICAL-9
AIM: Write a program in Java to demonstrate throw, throws, finally, multiple
try blocks and multiple catch exception.

Program:

public class Main {

// Method demonstrating 'throws' static void checkAge(int


age) throws ArithmeticException { if (age < 18) { // throw
keyword throw new ArithmeticException("Not eligible to
vote");
} else {
[Link]("Eligible to
vote"); }
}

public static void main(String[] args) {

// First try-catch-finally
block try { int a = 10, b = 0;
int c = a / b; // ArithmeticException [Link](c);
}

11
Java programming (C01209)
240101116015

catch (ArithmeticException e) {
[Link]("Arithmetic Exception caught");
}
finally
{
[Link]("Finally block executed (First
try)"); }

// Second try block with multiple catch try { int arr[]


= new int[3]; arr[5] = 100; //
ArrayIndexOutOfBoundsException
}
catch (ArithmeticException e) {
[Link]("Arithmetic Exception caught");
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array Index Out Of Bounds Exception caught");
}
catch (Exception e) {
[Link]("General Exception caught");
}
finally
{
[Link]("Finally block executed (Second
try)"); }

// Third try block demonstrating throw & throws


try {
checkAge(16);
}
catch (ArithmeticException e) {
[Link]("Exception caught from method: " + [Link]());
}
finally
{
[Link]("Finally block executed (Third
try)"); }
}
}

OUTPUT:

12
Java programming (C01209)
240101116015

PRACTICAL-10
AIM: Write a program of XML using java framework.

Program:

import [Link];
import
[Link];
import [Link];import
[Link]; import [Link];
import [Link];

import [Link]; import


[Link];

import [Link];

public class Main {

public static void main(String[] args)


{ try {
// Create DocumentBuilder
DocumentBuilderFactory factory = [Link]();
DocumentBuilder builder = [Link]();

// Create XML Document


Document document = [Link]();

13
Java programming (C01209)
240101116015

// Root element
Element root = [Link]("student");
[Link](root);

// Child elements
Element id = [Link]("id");
[Link]([Link]("101")); [Link](id);

Element name = [Link]("name");


[Link]([Link]("Bhoomika"));
[Link](name);
Element marks = [Link]("marks");
[Link]([Link]("85"));
[Link](marks);
// Write XML to file
TransformerFactory tf = [Link]();
Transformer transformer = [Link]();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("[Link]"));

[Link](source, result);

[Link]("XML file created successfully");

} catch (Exception e) {
[Link]();
}
}
}
OUTPUT:

14
Java programming (C01209)
240101116015

15

You might also like