Practical : 1
AIM :
Write a Program that displays Welcome to Java, Learning Java Now and Programming is fun.
public class PR_1{
public static void main(String[] args) {
System.out.println("Welcome To Java, Lerning Java Now and Progrmaing is fun");
}
}
Practical : 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 )
public class PR_2 {
public static void main(String[] args){
double a=3.4,b=50.2,c=2.1,d=0.55,e=44.5,f=5.9;
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);
}
}
Practical : 3
AIM :
Write a program that reads a number in meters, converts it to feet, and displays the result.
import java.util.Scanner;
public class PR_3 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Number in Meter: ");
double m = input.nextDouble();
double f=m*3.2808;
System.out.println("Result in feet: "+f);
}
}
Practical : 4
AIM :
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.
import java.util.Scanner;
public class PR_4 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Your Weight in pound : ");
float w = sc.nextFloat();
System.out.print("Enter Your Height in Inch : ");
float h = sc.nextFloat();
double BMI = (w*0.4535) / ((h*0.0254)*(h*0.0254));
System.out.println("Your BMI is: "+BMI);
}
}
Practical : 5
AIM :
Write a program that prompts the user to enter three integers and display the integers in decreasing order.
import java.util.Scanner;
public class PR_5 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter three Number: ");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int temp;
if(x<y){
temp = x;
x = y;
y = temp;
}
if(z>y){
if(z>x){
temp = z;
z = y;
y = x;
x = temp;
}
else{
temp = z;
z = y;
y = temp;
}
}
System.out.println("Number in Desending Order: \n"+x+"\n"+y+"\n"+z);
}
}
Practical : 6
AIM :
Write a program that prompts the user to enter a letter and check whether a letter is a vowel or constant.
import java.util.Scanner;
public class PR_6 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a char to check vowel : ");
char c = sc.next().charAt(0);
switch(Character.toLowerCase(c)){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println( c+ " is vowel char.");
break;
default:
System.out.println( c+" is not a vowel char.");
}
}
}
Practical : 7
AIM :
Assume a vehicle plate number consists of three uppercase letters followed by four digits. Write a program to generate a plate number.
public class PR_7 {
public static void main(String[] args){
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 dig1 = (int) (Math.random() * 10);
int dig2 = (int) (Math.random() * 10);
int dig3 = (int) (Math.random() * 10);
int dig4 = (int) (Math.random() * 10);
System.out.println("" + (char) (a1) + ((char) (a2)) + ((char) (a3)) + dig1 + dig2 + dig3 + dig4);
}
}
Practical : 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.
import java.util.Scanner;
public class PR_8 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int div = 2;
System.out.println("Enter a Number : ");
int number = sc.nextInt();
while(number > 1){
if(number % div == 0){
System.out.print(div+",");
number = number / div;
}else{
div++;
}
}
}
}
Practical : 9
AIM :
Write a method with following method header. public static int gcd(int num1, int num2) Write a program that prompts the user to enter two integers and compute the gcd of two integers.
import java.util.Scanner;
public class PR_9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
int num1 = sc.nextInt();
System.out.println("Enter Second Number: ");
int num2 = sc.nextInt();
System.out.println("GCD of "+num1+" and "+num2+" is "+gcd(num1,num2));
}
public static int gcd(int num1,int num2){
while(num1 != num2){
if(num1>num2){
num1 = num1 - num2;
}
else{
num2 = num2 - num1;
}
}
return num1;
}
}
Practical : 10
AIM :
Write a test program that prompts the user to enter ten numbers, invoke a method to reverse the numbers, display the numbers.
import java.util.Scanner;
public class PR_10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i=0;
int num_arr[] = new int[10];
for(i=0;i<10;i++){
System.out.print("Number at Position "+(i+1)+" :");
num_arr[i]=sc.nextInt();
}
reversenum(num_arr);
for(i=0;i<10;i++){
System.out.println("Value at Position "+(i+1)+":"+num_arr[i]);
}
}
public static void reversenum(int arr[]) {
int j=0,temp;
while(j<=arr.length/2){
temp = arr[j];
arr[j] = arr[arr.length-1-j];
arr[arr.length-1-j] = temp;
j++;
}
}
}
Practical : 12_1
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.
import java.util.Random;
public class PR_12_A {
public static void main(String[] args) {
Random random = new Random(1000);
for(int i = 0; i<100; i++){
System.out.format("%3s",random.nextInt(49));
if((i+1)%20==0){
System.out.println();
}
}
}
}
Practical : 12_2
AIM :
Write a program to create circle class with area function to find area of circle.
public class PR_12_B {
public static void main(String[] args) {
double r=2;
System.out.println(area(r));
}
public static double area(Double r){
double pi = 3.14;
return pi*r*r;
}
}
Practical : 12_3
AIM :
Write a Program using Constructor overloading in JAVA
public class PR_12_C {
public static void main(String[] args) {
// this parameter for circle area
double r=2;
System.out.println(area(r));
// this parameter fot ractengle area
double l=10,b=32.4;
System.out.println(area(l,b));
}
public static double area(double l,double b){
return l*b;
}
public static double area(Double r){
double pi = 3.14;
return pi*r*r;
}
}
Practical : 13
AIM :
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.
import java.util.Scanner;
public class PR_13 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter any String");
String s = sc.nextLine();
String p = s.replaceAll(" ", "");
System.out.println("After removing space:- "+p);
}
}
Practical : 14
AIM :
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.
import java.util.ArrayList;
import java.util.Date;
public class PR_14 {
public static void main(String[] args) {
ArrayList<Object> arr = new ArrayList<Object>();
arr.add(new Lone(120000));
arr.add(new Date());
arr.add(new Circle(7.5));
arr.add(new String("My name is Aditya"));
for (int i = 0; i < arr.size(); i++) {
System.out.println((arr.get(i)).toString());
}
}
}
class Circle {
double diameter;
Circle(double r) {
this.diameter = r * 2;
}
public String toString() {
return "Circle Diameter is:- " + this.diameter;
}
}
class Lone{
double intrest = 6.4, t;
Lone(double amount) {
this.t = (amount * intrest * 12) / (100*12);
this.t = t+amount;
}
public String toString() {
return "Total amount you pay:- " + this.t;
}
}
Practical : 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.
import java.util.Scanner;
class PR_11
{
public static int[][] create_fill_matrix()
{
int [][]matrix = new int[6][6];
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
matrix[i][j]=(int)((Math.random()*5)%2);
}
}
return matrix;
}
public static void displayMatrix(int [][]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+1)+" have ODD no of 1s");
}
}
System.out.println("\nColumns Having ODD no of 1s");
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 - "+(i+1)+" " +
"have ODD no of 1s");
}
}
}
}
Practical : 21
AIM :
Write a program to create a file name 123.txt, if it does not exist. Append a new data to it if it already exist. write 150 integers created randomly into the file using Text I/O. Integers are separated by space.
import java.io.*;
import java.util.Scanner;
class PR_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();
}
}
}
Practical : 19
AIM :
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.
package Practical;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Practical_19 extends Application{
@Override
public void start(Stage primaryStage) {
double width = 400;
double height = 400;
Circle c = new Circle(width / 2, height / 2, Math.min(width, height) / 10, Color.WHITE);
c.setStroke(Color.BLACK);
StackPane pane = new StackPane(c);
primaryStage.setScene(new Scene(pane, width, height));
pane.setOnMousePressed(e -> c.setFill(Color.RED));
pane.setOnMouseReleased(e -> c.setFill(Color.BLUE));
primaryStage.setTitle("Click circle..");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Practical : 20
AIM :
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 displaye.
package Practical;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
public class Practical_20 extends CheckBoxDemo {
@Override // Override the getPane() method in the super class
protected BorderPane getPane() {
BorderPane pane = super.getPane();
HBox paneForButtons = new HBox(20);
Button btLeft = new Button("Left",
new ImageView("left.png"));
Button btRight = new Button("Right",
new ImageView("right.png"));
paneForButtons.getChildren().addAll(btLeft, btRight);
paneForButtons.setAlignment(Pos.CENTER);
paneForButtons.setStyle("-fx-border-color: green");
BorderPane pane1 = new BorderPane();
pane.setBottom(paneForButtons);
Pane paneForText = new Pane();
paneForText.getChildren().add(text);
pane.setCenter(paneForText);
btLeft.setOnAction(e -> text.setX(text.getX() - 10));
btRight.setOnAction(e -> text.setX(text.getX() + 10));
Font fontBoldItalic = Font.font("Times New Roman",
FontWeight.BOLD, FontPosture.ITALIC, 20);
Font fontBold = Font.font("Times New Roman",
FontWeight.BOLD, FontPosture.REGULAR, 20);
Font fontItalic = Font.font("Times New Roman",
FontWeight.NORMAL, FontPosture.ITALIC, 20);
Font fontNormal = Font.font("Times New Roman",
FontWeight.NORMAL, FontPosture.REGULAR, 20);
text.setFont(fontNormal);
VBox paneForCheckBoxes = new VBox(20);
paneForCheckBoxes.setPadding(new Insets(5, 5, 5, 5));
paneForCheckBoxes.setStyle("-fx-border-color: green");
CheckBox chkBold = new CheckBox("Bold");
CheckBox chkItalic = new CheckBox("Italic");
paneForCheckBoxes.getChildren().addAll(chkBold, chkItalic);
pane.setRight(paneForCheckBoxes);
EventHandler handler = e -> {
if (chkBold.isSelected() && chkItalic.isSelected()) {
text.setFont(fontBoldItalic); // Both check boxes checked
}
else if (chkBold.isSelected()) {
text.setFont(fontBold); // The Bold check box checked
}
else if (chkItalic.isSelected()) {
text.setFont(fontItalic); // The Italic check box checked
}
else {
text.setFont(fontNormal); // Both check boxes unchecked
}
};
chkBold.setOnAction(handler);
chkItalic.setOnAction(handler);
VBox paneForRadioButtons = new VBox(20);
paneForRadioButtons.setPadding(new Insets(5, 5, 5, 5));
paneForRadioButtons.setStyle
("-fx-border-width: 2px; -fx-border-color: green");
RadioButton rbRed = new RadioButton("Red");
RadioButton rbGreen = new RadioButton("Green");
RadioButton rbBlue = new RadioButton("Blue");
paneForRadioButtons.getChildren().addAll(rbRed, rbGreen, rbBlue);
pane.setLeft(paneForRadioButtons);
ToggleGroup group = new ToggleGroup();
rbRed.setToggleGroup(group);
rbGreen.setToggleGroup(group);
rbBlue.setToggleGroup(group);
rbRed.setOnAction(e -> {
if (rbRed.isSelected()) {
text.setFill(Color.RED);
}
});
rbGreen.setOnAction(e -> {
if (rbGreen.isSelected()) {
text.setFill(Color.GREEN);
}
});
rbBlue.setOnAction(e -> {
if (rbBlue.isSelected()) {
text.setFill(Color.BLUE);
}
});
return pane;
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Practical : 22
AIM :
Write a recursive method that returns the smallest integer in an array. Write a test program that prompts the user to enter an integer and display its product.
package Practical;
//Recursive Java program to find minimum
import java.util.*;
class Practical_22{
// function to return minimum element using recursion
public static int findMinRec(int A[], int n)
{
// if size = 0 means whole array
// has been traversed
if(n == 1)
return A[0];
return Math.min(A[n-1], findMinRec(A, n-1));
}
// Driver code
public static void main(String args[])
{
int A[] = {1, 4, 45, 6, -50, 10, 2};
int n = A.length;
// Function calling
System.out.println(findMinRec(A, n));
}
}
Practical : 14
AIM :
Write a program to create a file name 123.txt, if it does not exist. Append a new data to it if it already exist. write 150 integers created randomly into the file using Text I/O. Integers are separated by space.