Laboratory Manual: Government Engineering College, Modasa
Laboratory Manual: Government Engineering College, Modasa
Laboratory Manual: Government Engineering College, Modasa
LABORATORY MANUAL
Information Technology Department
Semester IV
Year 2020-21
NAME: Jethava Deven Sureshbhai
EnrollmentNumber : 190160116030
Government Engineering College, Modasa
Date:
PROGRAM:
class pra1
{
public static void main(String[] args)
{
System.out.println("Wellcome to java"+","+"Learning java Now"+"
and"+" Programming is fun");
}
}
OUTPUT:
Experiment 2
AIM: 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
ax+by=e x=ed-bf/ad-bc
cx+dy=f y=af-ec/ad-bc )
PROGRAM:
import java.util.Scanner;
class ready
{
public static void main(String[] args)
{
double a,b,c,d,e,f;
double x,y;
Scanner i=new Scanner(System.in);
System.out.println("enter the value of a=");
a=i.nextDouble();
System.out.println("enter the value of b=");
b=i.nextDouble();
System.out.println("enter the value of c=");
c=i.nextDouble();
System.out.println("enter the value of d=");
d=i.nextDouble();
System.out.println("enter the value of e=");
e=i.nextDouble();
System.out.println("enter the value of f=");
f=i.nextDouble();
x=((e*d)-(b*f))/((a*d)-(b*c));
y=((a*f)-(e*c))/((a*d)-(b*c));
System.out.println("x="+x);
System.out.println("y="+y);
}
}
OUTPUT:
EXPERMENT 3
AIM: Write a program that reads a number in meters,
converts it to feet, and displays the result.
PROGRAM:
import java.util.Scanner;
class prg2
double m,f;
m=i.nextDouble();
System.out.println("meter="+m);
f=3.28*m;
System.out.println("feet="+f);
}
OUTPUT:
EXPERMENT 4
PROGRAM:
import java.util.*;
class prg5
double h,w,bmi;
System.out.println("hight in inch=");
h=m.nextDouble();
System.out.println("weight in pound=");
w=m.nextDouble();
h=h*0.0254;
System.out.println("hight in meters="+h);
w=w*0.45359237;
System.out.println("weight in kg="+w);
bmi=w/(h*h);
System.out.println("BMI="+bmi);
OUTPUT:
EXPERMENT 5
PROGRAM:
import java.util.*;
class prg3
int temp,a,b,c;
a=i.nextInt();
b=i.nextInt();
c=i.nextInt();
if(a<b)
temp=a;
a=b;
b=temp;
else
if(c>b)
if(c>a)
temp=c;
c=b;
b=a;
a=temp;
else
temp=c;
c=b;
b=temp;
}
}
}
OUTPUT:
EXPERMENT 6
PROGRAM:
import java.util.Scanner;
class prg4
char ch;
System.out.println("add char=");
ch=i.next().charAt(0);
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=
='u'||ch=='U')
System.out.println("char is vovel");
else
System.out.println("char is consonants");
}
OUTPUT:
EXPERMENT 7
PROGRAM:
args)
10);
10);
10);
System.out.println("" + (char)
(alpha1) + ((char)(alpha2)) +
+ digit3 + digit4);
}
OUTPUT:
EXPERMENT 8
AIM: 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.
PROGRAM:
import java.util.Scanner;
args)
int div=2;
(System.in);
System.out.print("Enter Integer
Value : ");
while(number > 1)
{
if(number%div==0)
System.out.print(div+",");
number=number/div;
else
div++;
}}
OUTPUT:
EXPERMENT 9
PROGRAM:
import java.util.Scanner;
args) {
Scanner(System.in);
System.out.print("Enter first
number:");
num1 = (int)scanner.nextInt
();
System.out.print("Enter
second number:");
num2 = (int)scanner.nextInt
();
scanner.close();
else
}
//displaying the result
System.out.printf("GCD of
OUTPUT:
EXPERMENT 10
AIM: Write a test program that prompts the user to enter ten
numbers, invoke a method to reverse the numbers, display
the numbers.
PROGRAM:
import java.util.Scanner;
(String[] args) {
Scanner(System.in); // Create a
Scanner
length ten
// Prompt the user to
System.out.print
numbers.length; i++)
numbers[i] =
input.nextInt();
reverse(numbers);
// Display the
numbers
System.out.println();
(int[] list) {
int temp;
for (int i = 0, j =
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
OUTPUT:
EXPERMENT 11
AIM: 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.
PROGRAM:
import java.util.Scanner;
create_fill_matrix()
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
matrix[i][j]=(int)((Math.random
()*5)%2);
}
}
return matrix;
[][]matrix)
System.out.print("\nMatrix Values
\n");
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
System.out.print(matrix[i][j]+ "
");
System.out.println();
}
public static void main(String[]
args)
int my_matrix[][];
int i,j,cnt;
my_matrix=create_fill_matrix();
displayMatrix(my_matrix);
System.out.println("\nRows Having
ODD no of 1s");
for(i=0;i<6;i++)
cnt=0;
for(j=0;j<6;j++)
if(my_matrix[i][j]==1)
cnt++;
}
if(cnt%2!=0)
System.out.println("Row - "+(i
System.out.println("\nColumns
for(i=0;i<6;i++)
cnt=0;
for(j=0;j<6;j++)
if(my_matrix[j][i]==1)
cnt++;
if(cnt%2!=0)
{
System.out.println("Column -
}
}OUTPUT:
EXPERMENT 12
AIM: 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 .
PROGRAM:
import java.util.Random;
args)
(1000);
+)
System.out.format
("%3d",rand.nextInt(49));
if((i+1)%20==0)
System.out.println();
}
OUTPUT:
EXPERMENT 13
PROGRAM:
import java.util.Scanner;
if (a.length() < 3) {
System.out.println(
Required");
System.exit(0);
int result = 0;
int i = 0;
i++;
switch (a.charAt(i)) {
case '+' :
result = Integer.parseInt(a.substring
(0,i))+Integer.parseInt(a.substring(i+1,a.length
()));
break;
case '-' :
result = Integer.parseInt(a.substring(0,i))-
Integer.parseInt(a.substring(i+1,a.length()));
break;
case '*' :
result = Integer.parseInt(a.substring
(0,i))*Integer.parseInt(a.substring(i+1,a.length
()));
break;
case '/' :
result = Integer.parseInt(a.substring
(0,i))/Integer.parseInt(a.substring(i+1,a.length
()));
break;
OUTPUT:
EXPERMENT 14
PROGRAM:
import java.util.ArrayList;
import java.util.Date;
args)
ArrayList<Object>();
arr_list.add(new Loan(5000.50));
arr_list.add(new Date());
arr_list.add(new String("String
class"));
arr_list.add(new Circle(3.45));
(); i++)
System.out.println((arr_list.get
(i)).toString());
class Circle
double radius;
Circle(double r)
this.radius=r;
}
public String toString()
"+this.radius;
class Loan
double amount;
Loan(double amt)
this.amount=amt;
"+this.amount;
}
}
OUTPUT:
PRACTICAL-15
Aim: 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.
PROGRAM:
import java.util.Scanner;
class Practical_15
throws NumberFormatException
int decimal = 0;
int strLength=binaryString.length();
{
throw new NumberFormatException("The Input String is not
Binary");
decimal += (binaryString.charAt(i)-'0') *
Math.pow(2, strLength-1-i);
return decimal;
try
catch(NumberFormatException e)
{
System.out.println(e);
OUTPUT:
PRACTICAL-16
Aim: Write a program that prompts the user to enter a
decimal number and displays the number in a fraction.
Hint: Read the decimal number as a string, extract the integer
part and fractional part from the string.
PROGRAM:
java.util.Scanner;
class Fraction{
real=r;
imaginary=img;
}
public String toString() {
return real/gcm+"/"+imaginary/gcm;
String decimal=sc.nextLine();
int len=decimal.substring(indexOfDecimal).length();
int real_part=(int)(Double.parseDouble(decimal)*imag_part);
decimal=sc.nextLine();
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);
OUTPUT:
Practical-17
Write a program that displays a tic-tac-toe board. A cell may be X, O, or empty. What to
display at each cell is randomly decided. The X and O are images in the files X.gif and
O.gif.
program
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
output:
Practical-18
Write a program that moves a circle up, down, left or right using arrow keys.
program:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
import javafx.stage.Stage;
pane.setOnKeyPressed(e -> {
switch (e.getCode()) {
case UP : circle.setCenterY(circle.getCenterY() >
circle.getRadius() ? circle.getCenterY() - 15 :
circle.getCenterY()); break;
case DOWN : circle.setCenterY(circle.getCenterY() <
pane.getHeight() - circle.getRadius() ?
circle.getCenterY() + 15 : circle.getCenterY());
break;
case LEFT : circle.setCenterX(circle.getCenterX() >
circle.getRadius() ? circle.getCenterX() - 15 :
circle.getCenterX()); break;
case RIGHT : circle.setCenterX(circle.getCenterX() <
pane.getWidth() - circle.getRadius() ?
circle.getCenterX() + 15: circle.getCenterX());
}
});
output:
Practical-19
Write a program that displays the color of a circle as red when the mouse button is pressed
and as blue when the mouse button is released.
program:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.layout.StackPane;
circlePane.setOnMousePressed(e -> {
circlePane.paintBlack();
});
circlePane.setOnMouseReleased(e -> {
circlePane.paintWhite();
});
circlePane.requestFocus();
output:
Practical-20
Write a GUI program that use button to move the message to the left and right and use the radio
button to change the color for the message displayed.
program:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.paint.Color;
rbRed.setOnAction(e -> {
if (rbRed.isSelected()) {
text.setFill(Color.RED);
}
});
rbYellow.setOnAction(e -> {
if (rbYellow.isSelected()) {
text.setFill(Color.YELLOW);
}
});
rbBlack.setOnAction(e -> {
if (rbBlack.isSelected()) {
text.setFill(Color.BLACK);
}
});
rbOrange.setOnAction(e -> {
if (rbOrange.isSelected()) {
text.setFill(Color.ORANGE);
}
});
rbGreen.setOnAction(e -> {
if (rbGreen.isSelected()) {
text.setFill(Color.GREEN);
}
});
output:
Practical – 21
Program :-
import java.io.*;
import java.util.Scanner;
class Practical_21
{
public static void main(String[] args)
{
try (
PrintWriter pw = new PrintWriter(new FileOutputStream(new File("123.txt"),
true));
){
for (int i = 0; i < 150; i++)
{
pw.print((int)(Math.random() * 150) + " ");
}
}
catch (FileNotFoundException fnfe)
{
System.out.println("Cannot create the file.");
fnfe.printStackTrace();
}
}
}
Output :-
Practical – 22
Program :-
import java.util.Scanner;
class Practical_22
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int product=1;
System.out.print("Enter five integers: ");
int[] list = new int[5];
for (int i = 0; i < list.length; i++)
{
list[i] = input.nextInt();
product *= list[i];
}
System.out.println("The Product of elements is " + product);
}
}
Output :-
Practical – 23
Program :-
import java.util.Scanner;
Output :-
Practical – 24
Program :-
import java.util.PriorityQueue;
}
}
Output :-
Practical – 25
Aim :- Write a program that reads words from a text file and displays
all the nonduplicate words in descending order.The text file is passed as
a command-line argument.
Program :-
import java.io.*;
import java.util.*;
class Practical_25 {
public static void main(String[] args) {
if(args.length==1){
String fileName = args[0];
TreeSet<String> set = new TreeSet<>();
File file = new File(fileName);
try {
Scanner s = new Scanner(file);
while (s.hasNext()){
set.add(s.next());
}
System.out.println(set);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else{
System.out.println("Please, Pass the File Name as Command Line
Argument");
}
}
}
OUTPUT: