Java Practical
Java Practical
Faisal khan
Telegram : GUBCA Instagram mr_faisal_607
UNIT: 1
class Pro1
{
public static void main(String args[])
{
double p=100,r=5.0,n=2;
double si=(p*r*n)/100;
System.out.println("Simple Interset : "+si);
}
}
OUTPUT
principal = Double.parseDouble(args[0]);
rate=rate/(12*100);
OUTPUT
do
{
System.out.println("ITEM Price");
System.out.println("1.Seat cover 6000");
System.out.println("Enter Choice");
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println("Seat cover"+"\n");
salestax = seatcover*2/100;
seatcover= seatcover+salestax;
System.out.println("Bill for seat cover is "+
seatcover);
name[i]="seat cover";
amt[i]=seatcover;
i++;
break;
case 2:
System.out.println("Steering wheel"+"\n");
salestax = 0.0;
salestax = str_wheel*3/100;
str_wheel= str_wheel+salestax;
System.out.println("Bill for seat cover is "+
str_wheel);
name[i]="steering wheel";
amt[i]=str_wheel;
i++;
break;
case 3:
System.out.println("Car Ligthining "+"\n");
salestax =0.0;
salestax = ligths *15/100;
case 4:
System.out.println("Air Purifiers"+"\n");
salestax = 0.0;
salestax = airpurifiers*2.5/ 100;
airpurifiers= airpurifiers+salestax;
System.out.println("Bill for seat cover is "+
airpurifiers);
name[i]="Air purifiers";
amt[i]=airpurifiers;
i++;
break;
case 5:
System.out.println("Other Items"+"\n");
salestax = 0.0;
salestax = others*2.5/ 100;
others= others+salestax;
System.out.println("Bill for seat cover is "+ others);
name[i]="Other Items";
amt[i]=others;
i++;
break;
case 6:
quit=true;
break;
default:
System.out.println("wrong input"+"\n");
}
}
class Pro4
{
public static void main(String args[])
{
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
int num3=Integer.parseInt(args[2]);
}
}
OUTPUT
class Pro5
{
public static void main(String args[])
{
double x=3.0,y=4.0;
System.out.println("x= "+x);
System.out.println("y= "+y);
double sum=(x*x)+(y*y);
System.out.println("Sum of square of x and y is : "+sum);
double hypotense=Math.sqrt(sum);
System.out.println("Hypotenuse of right angled triangle is :
"+hypotense);
}
}
OUTPUT
OUTPUT
class discount
{
int pv;
discount(int val)
{
pv=val;
}
double cal_dis()
{
double disc;
if(pv<=1000)
{
disc=pv*0.10;
}
else if(pv>1000 && pv<=1500)
{
disc= pv * 0.12;
}
else
{
disc= pv * 0.15;
}
class pro8
{
public static void main(String args[])
{
int n;
Scanner in= new Scanner(System.in);
System.out.println("Enter purchase value: ");
n = in.nextInt();
OUTPUT
}
}
class Pro10
{
public static void main(String args[])
{
int p=1;
for(int i=0;i<10;i++)
{
p=p<<1;
System.out.println(p);
}
}
}
OUTPUT
Output
Output
for(c=0;c<row;c++)
{
for(d=0;d<col;d++)
{
sum[c][d]=first[c][d]+second[c][d];
}
}
System.out.println("Sum of matrix: ");
for(c=0;c<row;c++)
{
for(d=0;d<col;d++)
{
System.out.print(sum[c][d]+"\t");
}
}
Output
Output
import java.util.*;
class NumberData
{
int arr[] = new int[5];
NumberData()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter 5 elements");
for(int i=0;i<5;i++)
{
arr[i]=s.nextInt();
}
}
}
class NumPlay extends NumberData
{
NumPlay()
{
super();
}
void display()
{
System.out.println("Elemetns are :");
for(int i=0;i<5;i++)
{
System.out.println(super.arr[i]);
}
}
void sum_arr()
{
int sum=0;
for(int i=0;i<5;i++)
{
sum=sum+super.arr[i];
}
System.out.println("Sum of numbers is : " + sum);
default:
System.out.println("Invalid choice");
}
}
}
Output
interface Exam {
boolean pass(int mark);
}
interface Classify {
String division(int avg);
}
class Pro8 {
public static void main(String args[]) {
boolean pass;
int mark, avg;
String division;
Result res = new Result();
Scanner s = new Scanner(System.in);
if (pass)
System.out.println("Passed : " + division); // Corrected variable name
else
System.out.println("Failed : " + division); // Corrected variable name
}
}
Output
pakclass.java
package P;
public class packclass
{
public int findMax(int x,int y,int z)
{
if(x>y && x>z)
return x;
else if(y>x && y>z)
return y;
else
return z;
}
}
//javac -d . packclass.java
demo.java
import P.packclass;
public class demo
{
public static void main(String args[])
{
int a=6,b=29,c=30;
packclass ps = new packclass();
int max=ps.findMax(a,b,c);
System.out.println(max+ " is the greatest number");
}
}
Output
}
}
Output
class pro4 {
public static void revStr(String str) {
char[] try1 = str.toCharArray();
for (int i = try1.length - 1; i >= 0; i--) {
System.out.println(try1[i]);
}
}
do {
System.out.println("1.Display each character on separate line in reverse order
");
System.out.println("2.Count total number characters and display each
character's position too. ");
System.out.println("3.Identify whether the string is palindrome or not");
System.out.println("4.Count total number of uppercase and lowercase characters
in it. ");
System.out.println("5. Quit");
System.out.println("Enter your choice :");
int ch = in.nextInt();
switch (ch) {
case 1:
revStr(myStr);
break;
case 2:
countChar(myStr);
break;
case 3:
palindrome(myStr);
break;
case 4:
countCase(myStr);
break;
case 5:
quit = true;
break;
default:
System.out.println("Wrong input");
}
} while (!quit);
}
}
class pro7
{
public static void main(String args[])
{
try
{
int m1,m2,m3;
m1=Integer.parseInt(args[0]);
m2=Integer.parseInt(args[1]);
m3=Integer.parseInt(args[2]);
8. Write a program which takes the age of 5 persons from command line and find the
average age of all persons. The program should handle exception if the argument is
not correctly formatted and custom exception if the age is not between 1 to 100.
class AgeException extends Exception {
public AgeException(String str) {
System.out.println(str);
}
}
try {
for (int i = 0; i < n; i++) {
age[i] = Integer.parseInt(args[i]);
sumage = sumage + age[i];
}
aveage = sumage / n;
System.out.println("Average age of person is: " + aveage);
Output
9. Write an application that converts between meters and feet. Its first commandline
argument is a number and second command line argument is either "centimeter" or
"meter". If the argument equals "centimeter" displays a string reporting the
equivalent number of meters. If this argument equals "meters", display a string
reporting the equivalent number of centimeter. If unit is not given properly then
generate custom exception Unitformatexception. If first argument is not proper format
then generate numberformatexception. Generate other exception as per
requirements. (1 meter=100 centimeter)
class UnitFormateException extends Exception
{
UnitFormateException(String str)
{
super(str);
}
}
public class pro9
{
public static void main(String args[])
{
int num,value;
String str;
try
{
num = Integer.parseInt(args[0]);
str = args[1];
Output
10.Write a program that accepts 5 even numbers from command line , if any of the
numbers is odd then throw custom exception OddException and count such invalid
numbers.
import java.io.*;
class OddException extends Exception
{
OddException(String str)
{
super(str);
}