Professional Documents
Culture Documents
Practical – 1
Q-1. WAP to print “Welcome to Java, Learning Java Now and Programming is Fun.”
ANS:-
class p1
{
public static void main(String[] args)
{
System.out.println("Welcome to Java, Learning Java Now and Programming is Fun");
}
}
Output:-
1|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical - 2
Q-2. Write a program that solves the following equation and displays the value x and y:
1) 3.4x+50.2y=44.5 2) 2.1x+.55y=5.9 (Assume Cramer’s rule to solve equation .
ANS:-
import java.util.*;
class p2
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.println("Values for equation:-1");
System.out.println("Enter value of a:");
double a = input.nextDouble();
System.out.println("Enter value of b:");
double b = input.nextDouble();
System.out.println("Enter value of e:");
double e = input.nextDouble();
System.out.println("Values for equation:-2");
System.out.println("Enter value of c:");
double c = input.nextDouble();
System.out.println("Enter value of d : ");
double d=input.nextDouble();
System.out.println("Enter value of f : ");
double f=input.nextDouble();
double x=((e*d)-(b*f))/((a*d)-(b*c));
double y=((a*f)-(e*c))/((a*d)-(b*c));
System.out.println("X = "+x+" Y = "+y);
2|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
}
}
Output:-
Practical – 3
Q-3 Write a program that reads a number in meters, converts it to feet, and displays the
result.
ANS:-
import java.util.*;
class p3
{
public static void main(String []args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter the value in meters : ");
3|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
double m=input.nextDouble();
Double f=m*3.28084;
System.out.println(m+" METERS = "+f+" FEETS");
}
}
Output:-
Practical – 4
Q-4. Body Mass Index (BMI) is a measure of health on weight. It can be calculated by
taking your weight in kilograms and dividing by the square of your height in meters.
Write a program that prompts the user to enter a weight in pounds and height in inches
and displays the BMI. Note:- 1 pound=.45359237 Kg and 1 inch=.0254 meters.
ANS:-
import java.util.*;
class p4
4|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
double w=input.nextDouble();
double h=input.nextDouble();
double b=(w*0.45359237)/((h*0.0254)*(h*0.0254));
Output:
5|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical – 5
Q-5. Write a program that prompts the user to enter three integers and display the integers
in decreasing order.
ANS:-
import java.util.*;
class p5
{
public static void main(String args[])
{
int temp;
Scanner input=new Scanner(System.in);
System.out.println("Enter num1: ");
int a=input.nextInt();
System.out.println("Enter num2: ");
int b=input.nextInt();
if(a<b)
{
temp=a;
a=b;
b=temp;
}
System.out.println("Enter num3: ");
int c=input.nextInt();
if(c>b)
{
if(c>a)
{
temp=c;
c=b;
b=a;
6|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
a=temp;
}
else
{
temp=c;
c=b;
b=temp;
}
}
System.out.println("Decreasing order :"+a+" >"+b+" >"+c);
}
}
Output:-
7|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical – 6
Q-6. Write a program that prompts the user to enter a letter and check whether a letter is a
vowel or constant.
ANS:-
import java.util.*;
class p6
int i=0;
char ch=input.next().charAt(0);
switch(ch)
case 'a':
case 'e':
case 'i':
case '0':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':i++;
if(i==1)
8|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
else
if((ch>='a'&&ch<='z')||(ch>='A'&& ch<='Z'))
else
System.out.println("NOT AN ALPHABET");
Output:-
9|Page
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical – 7
Q-7. Assume a vehicle plate number consists of three uppercase letters followed by four
digits. Write a program to generate a plate number.
ANS:-
import java.util.*;
class p7
int a1='A'+(int)(Math.random()*('Z'-'A'));
int a2='A'+(int)(Math.random()*('Z'-'A'));
int a3='A'+(int)(Math.random()*('Z'-'A'));
int digit1=(int)(Math.random()*10);
int digit2=(int)(Math.random()*10);
int digit3=(int)(Math.random()*10);
int digit4=(int)(Math.random()*10);
System.out.println(""+(char)(a1)+(char)(a2)+(char)(a3)+digit1+digit2+digit3+digit4);
Output :-
10 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical – 8
Q-8. Write a program that reads an integer and displays all its smallest factors in increasing
order. For example if input number is 120, the output should be as follows:2,2,2,3,5.
ANS:-
import java.util.*;
class p8
int d=2;
int n=input.nextInt();
while(n>1)
if(n%d==0)
System.out.println(d+",");
n=n/d;
else
d++;
11 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Output:-
12 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical-9
Q-9. Write a java program that prompts the user to enter two integers and compute the
gcd of two integers.
ANS:-
import java.util.Scanner;
class p9
int a= input.nextInt();
int b= input.nextInt();
int ans=gcd(a,b);
int gcd=1;
int max=0;
if(a>b)
max=a;
else
max=b;
13 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
for(int i=1;i<=max;i++)
gcd=i;
return gcd;
Output:-
14 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical - 10
Q-10. Write a test program that prompts the user to enter ten number , invoke a method
to reverse the number, display the numbers.
ANS:-
import java.util.Scanner;
import java.util.Arrays;
class p10
int i=0;
for(i=0;i<10;i++)
a[i] = input.nextInt();
reverse(a);
for(i=0;i<10;i++)
int j=0,temp;
while(j<=numbers.length/2)
15 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
temp=numbers[j];
numbers[j]=numbers[numbers.length-1-j];
numbers[numbers.length-1-j]=temp;
j++;
Output:-
Practical - 11
Q-11. Write a program that generate 6*6 two-dimensional matrix, filled with 0’s and 1’s , display
the matrix, check every raw and column have an odd number’s of 1’s.
ANS:-
import java.util.Scanner;
16 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
import java.util.Arrays;
class p11
System.out.println();
System.out.println("Envery row" + (isRowsOdd1s(a) ? " " : " does not " + "have an odd
number of 1s"));
System.out.println("Envery row" + (isColumnOdd1s(a) ? " " : " does not " + "have an odd
number of 1s"));
int count = 0;
if (m[i][j] == 1)
count++;
17 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
if (count % 2 == 0)
return false;
return true;
int count = 0;
if (m[j][i] == 1)
count++;
if (count % 2 == 0)
return false;
return true;
18 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Output:-
Practical – 12
Q-12. Write a program that creates a Random object with seed 1000 and displays the
first 100 random integers between 1 and 49 using the NextInt (49) method.
ANS:-
import java.util.Random;
class p12
System.out.format("%5d",a.nextInt(49));
19 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
if((i+1)%20==0)
System.out.println();
Output:-
20 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical - 13
Q-13. Write a program for calculator to accept an expression as a string in which the
operands and operator are separated by zero or more spaces. For ex: 3+4 and 3 + 4 are
acceptable expressions.
ANS:-
import java.util.Scanner;
class p13
String a=string.trim();
a=a.replaceAll(" ","");
if (a.length() < 3)
System.exit(0);
int result = 0;
int i = 0;
while (a.charAt(i) != '+' && a.charAt(i) != '-' && a.charAt(i) != '*' && a.charAt(i) != '/')
i++;
switch (a.charAt(i))
21 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
Output:-
22 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical - 14
Q-14. Write a program that creates an Array List and adds a Loan object , a Date object , a
string, and a Circle object to the list, and use a loop to display all elements in the list by
invoking the object’s to String() method.
ANS:-
import java.util.ArrayList;
import java.util.Date;
class p14
a.add(new Loan(10000));
a.add(new Date());
a.add(new Circle(3.45));
System.out.println(( a.get(i)).toString());
class Circle
double radius;
Circle(double r)
this.radius=r;
23 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
class Loan
double amount;
Loan(double amt)
this.amount=amt;
Output:-
24 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical – 15
Q-15. Write the bin2Dec (string binary String) method to convert a binary string into a
decimal number. Implement the bin2Dec method to throw a NumberFormatException if
the string is not a binary string.
ANS:-
import java.util.Scanner;
class p15
strLength=binaryString.length();
return decimal;
try
25 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
catch(NumberFormatException e)
System.out.println(e);
Output:-
26 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
Practical - 16
Q-16. Write a program that prompts the user to enter a decimal number and displays
the number in a fraction.
ANS:-
import java.util.Scanner;
class Fraction
{
private int real;
private int imaginary;
Fraction(int r,int img)
{
real=r;
imaginary=img;
}
public long gcm(long a, long b)
{
return b == 0 ? a : gcm(b, a % b);
}
public String toString()
{
long gcm = gcm(real, imaginary);
return real/gcm+"/"+imaginary/gcm;
}
}
public class p16
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a Decimal Number: ");
String decimal=sc.nextLine();
int indexOfDecimal = decimal.indexOf(".");
int len=decimal.substring(indexOfDecimal).length();
int imag_part=(int) Math.pow(10,len-1);
int real_part=(int)(Double.parseDouble(decimal)*imag_part);
Fraction fr=new Fraction(real_part,imag_part);
System.out.println("The Fraction Number is "+fr);
System.out.print("Enter a Decimal Number: ");
decimal=sc.nextLine();
27 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
indexOfDecimal = decimal.indexOf(".");
len=decimal.substring(indexOfDecimal).length();
imag_part=(int) Math.pow(10,len-1);
real_part=(int)(Double.parseDouble(decimal)*imag_part);
fr=new Fraction(real_part,imag_part);
System.out.println("The Fraction Number is "+fr);
}
}
Output:-
28 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI
Ritik P Patel 201100107038
29 | P a g e
GIDC DEGREE ENGINEERING COLLEGE NAVSARI