0% found this document useful (0 votes)
6 views13 pages

Java Programming Practical Exercises

The document contains a series of practical programming exercises in Java, covering various concepts such as printing output, calculating percentages, string concatenation, conditional statements, loops, arrays, user-defined methods, data input, string manipulation, exception handling, assertions, and threading. Each practical includes a code snippet, its output, and a note indicating successful execution. The exercises are designed to help learners understand fundamental programming constructs and Java syntax.

Uploaded by

cewapoh100
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)
6 views13 pages

Java Programming Practical Exercises

The document contains a series of practical programming exercises in Java, covering various concepts such as printing output, calculating percentages, string concatenation, conditional statements, loops, arrays, user-defined methods, data input, string manipulation, exception handling, assertions, and threading. Each practical includes a code snippet, its output, and a note indicating successful execution. The exercises are designed to help learners understand fundamental programming constructs and Java syntax.

Uploaded by

cewapoh100
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

PRACTICAL NO.

1
//HELLO AURANGABAD//
PROGRAM
package HelloAurangabad;
public class HelloAurangabad {
public static void main(String[] args) {
[Link]("Hello , Aurangabad");
}
}
OUTPUT
run:
Hello , Aurangabad
BUILD SUCCESSFUL (total time: 3 seconds)

PRACTICAL NO. 2
PERCENTAGE CALCULATOR
PROGRAM
package HelloAurangabad;
public class PercentageCalculator {
public static void main(String[] args) {
int total_marks=40;

double marks_obtained=346;
double percentage=0.0;
percentage=(marks_obtained/total_marks)//100;
[Link]("Studen1's Percentage = "+percentage);
marks_obtained = 144;
percentage=(marks_obtained/total_marks)//100;
[Link]("Studen2's Percentage = "+percentage);
}
}
OUTPUT
run:
Studen1's Percentage = 865.0
Studen2's Percentage = 360.0
BUILD SUCCESSFUL (total time: 1 second)

PRACTICAL NO. 3
//STRING VARIABLE CONCATE//
PROGRAM
public class StringConcate {
public static void main(String[] args){
String s1="yash";
String s2="mayank";
[Link](s1+" "+s2);
}}
OUTPUT
run:
yash mayank
BUILD SUCCESSFUL (total time: 0 seconds)

PRACTICAL NO. 4
//THE IF ELSE STATEMENT//
PROGRAM
PROGRAM
public class NewPercentageCalculator {
public static void main(String [] args){
int total_marks=400;
double marks_obtained=346;
double percentage=0.0;
percentage=(marks_obtained/total_marks)//100;
[Link]("Student's Percentage= "+percentage);
if (percentage >=40) {
[Link]("PASSED");
if (percentage >=75) {
[Link]("With Distination");
}
}
else {
[Link]("FAILED");
}
marks_obtained =144;
percentage=(marks_obtained/total_marks)//100;
[Link]("Student's Percentage = "+percentage);
if (percentage >=40) {
[Link]("PASSED");
if (percentage >=75) {
[Link]("With Distination!");
}
}
else {
[Link]("FAILED");
}
}}
OUTPUT
run:
Student's Percentage= 86.5
PASSED
With Distination
Student's Percentage = 36.0
FAILED
BUILD SUCCESSFUL (total time: 0 seconds)

PRACTICAL NO. 5
//SWITCH STATEMENT//
PROGRAM
public class SwitchDemo {
public static void main(String[] args) {
int today=5;
String day="";
switch(today) {
case 1:
[Link]("Monday");
case2:
[Link]("Tuesday");
case 3:
[Link]("Wednesday");
case 4:
[Link]("Thursday");
case 5:
[Link]("Friday");
case 6:
[Link]("Saturday");
case 7:
[Link]("Sunday");
default:
[Link]("Incorrect Day");
}
}
}
OUTPUT
run:
Friday
Incorrect Day
BUILD SUCCESSFUL (total time: 0 seconds)

PRACTICAL NO.6
//WHILE STATEMENT//
PROGRAM
public class WhileDemo {
public static void main(String[] args) {
int number=1;
while(number<=5) {
[Link]("Square of"+number);

[Link]("="+number//number);
++number;
}
}
OUTPUT
run:
Square of1
=1
Square of2
=4
Square of3
=9
Square of4
=16
Square of5
=25
BUILD SUCCESSFUL (total time: 0 seconds)
[Link]("="+number//number);
++number;
}
}
OUTPUT
run:
Square of1
=1
Square of2
=4
Square of3
=9
Square of4
=16
Square of5
=25
BUILD SUCCESSFUL (total time: 0 seconds)
PRACTICAL NO. 7
//DO WHILE STATEMENT//
public class DoWhileDemo {
public static void main(String[] args) {
int number=1;
do {
[Link]("Square of"+number);
[Link]("="+ number+number);
++number;

} while (number<=5);
}
}
OUTPUT
run:
Square of1
=11
Square of2
=22
Square of3
=33
Square of4
=44
Square of5
=55
BUILD SUCCESSFUL (total time: 0 seconds)

PRACTICAL NO. 8
//FOR STATEMENT//
PROGRAM:
public class NewClass {
public static void main(String[] args) {
for(int times = 0;times<10;times++) {
[Link]("Your code has run "+times);
}

}
}
OUTPUT
run:
Your code has run 0
Your code has run 1
Your code has run 2
Your code has run 3
Your code has run 4
Your code has run 5
Your code has run 6
Your code has run 7
Your code has run 8
Your code has run 9
BUILD SUCCESSFUL (total time: 0 seconds)

PRACTICAL NO. 9
//ARRAY STATEMENT//
PROGRAM:
public class ArraySum {
public static void main(String[] args) {
int a;
int[] array = new int[10];

for(a=0;a<10;a++)
array[a]=a;
for(a=0;a<10;a++)
[Link](array[a]);
}
}
OUTPUT
run:
0
1
2
3
4
5
6
7
8
9
BUILD SUCCESSFUL (total time: 1 second)BUILD SUCCESSFUL (total time: 0 seconds)

PRACTICAL NO. 10
//USER DEFINED METHOD//
PROGRAM:
public class MethodDemo {
static double rectangle_area(double length, double breadth) {
return(length//breadth);
}
public static void main(String[] args) {
double a=0;
a = rectangle_area(30.5, 25.5);
[Link]("Area of the rectangle ="+a);
}}
}
OUTPUT:
Area of the rectangle =777.75
BUILD SUCCESSFUL (total time: 1 second)
PRACTICAL NO. 11
//DATA INPUT METHOD//
PROGRAM:
Package javaprogram;
import [Link].//;
public class DataInputMethod {
public static void main(String[] args) {
[Link]("Enter the value =");
Scanner kcc=new Scanner([Link]);
int a=[Link] ();
[Link]("Value of a="+a);
}
}
}
OUTPUT
run:
Enter the value =
12
Value of a=12
BUILD SUCCESSFUL (total time: 10 seconds)

PRACTICAL NO. 12
//STRING MANIPULATION//
PROGRAM:
package javaprogram1;
import [Link]; public class NewClass {
public static void main(String[] args) {
String[] names={"Rossy","Dossy","Cossy","luzzy"};
[Link]("Names Array before sorting:");
for(int i=0;i<[Link];i++)
[Link](names[i]+",");
[Link]();
[Link](names);
[Link]("Names Array after sorting:");
for(int i=0;i<[Link];i++)
[Link](names[i]+",");
[Link]();
}
}
OUTPUT:
run:
Names Array before sorting:
Rossy,
Dossy,
Cossy,
luzzy,
Names Array after sorting:
Cossy,Dossy,Rossy,luzzy,
BUILD SUCCESSFUL (total time: 2 seconds)
PRACTICAL NO. 13
//EXCEPTION HANDLING//
PROGRAM:
package [Link];
public class ExceptionDemo {
static int divide(int divident, int divisior) {

return divident/divisior;
}
public static void main(String[] args) {
try {
int quotient = divide(12, 0);
[Link](quotient);
}
catch (Exception e) {
[Link]([Link]());
}
}
}
OUTPUT:
run:
/ by zero
BUILD SUCCESSFUL (total time: 1 second)

PRACTICAL NO. 14
//ASSERTION//
PROGRAM
Package javaprogram;
import [Link];
public class AssertionDemo {
public static void main(String args[] ) {
Scanner scanner =new Scanner ([Link]);
[Link]("Enter the age of the client: ");
int age=[Link]();
assert age>= 18:"Age not Valid";
[Link]("Age is "+age);
}
}
OUTPUT
run:
Enter the age of the client: 9
Exception in thread "main" [Link]: Age not Valid
at [Link]([Link])
C:\Users\Lenovo\AppData\Local\NetBeans\Cache\18\executor-
snippets\[Link]: The following error occurred while executing this line:
C:\Users\Lenovo\AppData\Local\NetBeans\Cache\18\executor-
snippets\[Link]: Java returned: 1
BUILD FAILED (total time: 5 seconds)

PRACTICAL NO.15
//THREAD//
PROGRAM
package HeloWorld;

public class ExtendThread extends Thread {


public void run() {
[Link]("Created a Thread");
for (int count=1;count<=3;count++)
[Link]("Count="+count);
}
public static void main(String args[]) {
ExtendThread t1=new ExtendThread();
ExtendThread t2=new ExtendThread();
[Link]();
[Link]();
}
}
OUTPUT
run:
Created a Thread
Created a Thread
Count=1
Count=2
Count=3
Count=1
Count=2
Count=3
BUILD SUCCESSFUL (total time: 1 second)

You might also like