OOP Practical
OOP Practical
Home
Write a Program that displays Welcome to Java, Learning Java Now and
Programming is fun.
class Main {
OUTPUT
Program - 02
Write a program that solves the following equation and displays the value x and
y: 1) 3.4 x + 50.2 y = 44.5 2) 2.1 x + .55 y = 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)
import java.util.Scanner;
class Main {
double a = input.nextDouble();
double b = input.nextDouble();
double e = input.nextDouble();
double c = input.nextDouble();
double f = input.nextDouble();
OUTPUT
Program - 03
import java.util.Scanner;
class Main {
OUTPUT
Program - 04
import java.util.Scanner;
class Main {
OUTPUT
Program - 05
Write a program that prompts the user to enter three integers and display the
integers in decreasing order.
import java.util.Scanner;
class Main {
int temp;
int a = input.nextInt();
int b = input.nextInt();
if (a < b) {
temp = a;
a = b;
b = temp;
}
int c = input.nextInt();
if (c > b) {
if (c > a) {
temp = c;
c = b;
b = a;
a = temp;
} else {
temp = c;
c = b;
b = temp;
OUTPUT
Program - 06
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;
class Main {
char ch = input.next().charAt(0);
switch (Character.toLowerCase(ch)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
break;
default:
}
OUTPUT
Program - 07
class Main {
OUTPUT
Program - 08
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;
class Main {
int div = 2;
if (number % div == 0) {
System.out.print(div + ",");
} else {
div++;
OUTPUT
Program - 09
Write a method with the 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;
class Main {
} else {
}
}
return num1;
System.out.print("GCD of " + number1 + " and " + number2 + " = " + gcd(number1, number2));
OUTPUT
Program - 10
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;
class Main {
int j = 0, temp;
temp = numbers[j];
numbers[numbers.length - 1 - j] = temp;
j++;
int i = 0;
num_array[i] = input.nextInt();
reverse(num_array);
OUTPUT
Program - 11
import java.util.Scanner;
class Main {
return matrix;
}
System.out.println();
int my_matrix[][];
int i, j, cnt;
my_matrix = create_fill_matrix();
displayMatrix(my_matrix);
cnt = 0;
if (my_matrix[i][j] == 1) {
cnt++;
if (cnt % 2 != 0) {
cnt = 0;
if (my_matrix[j][i] == 1) {
cnt++;
if (cnt % 2 != 0) {
}
}
OUTPUT
Program - 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
import java.util.Random;
class Main {
System.out.format("%3d", rand.nextInt(49));
if ((i + 1) % 20 == 0) {
System.out.println();
OUTPUT
Program - 13
acceptable expressions.
import java.util.Scanner;
class Main {
if (a.length() < 3) {
System.out.println(
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)) {
case '+':
break;
case '-':
case '*':
break;
case '/':
break;
OUTPUT
Program - 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 toString() method.
import java.util.ArrayList;
import java.util.Date;
class Main {
ArrayList < Object > arr_list = new ArrayList < Object > ();
arr_list.add(new Loan(5000.50));
arr_list.add(new Date());
arr_list.add(new Circle(3.45));
for (int i = 0; i < arr_list.size(); i++) {
System.out.println((arr_list.get(i)).toString());
class Circle {
double radius;
Circle(double r) {
this.radius = r;
class Loan {
double amount;
Loan(double amt) {
this.amount = amt;
OUTPUT
Program - 15
import java.util.Scanner;
class Main {
int decimal = 0;
int strLength = binaryString.length();
return decimal;
try {
} catch (NumberFormatException e) {
System.out.println(e);
OUTPUT
Program - 16
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.
import java.util.Scanner;
import java.math.BigInteger;
class Main {
Double d;
if (decimal[0].charAt(0) == '-') {
} else {
Program - 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.
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;
Program - 18
Write a program that moves a circle up, down, left or right using arrow keys.
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;
class Main {
@Override
pane.getChildren().add(circle);
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());
});
primaryStage.show();
pane.requestFocus();
Program - 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.
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;
class Main {
@Override
c.setStroke(Color.WHITE);
primaryStage.setTitle("Click circle..");
primaryStage.show();
Application.launch(args);
Program - 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.
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;
class Main {
@Override
paneForButtons.getChildren().addAll(btLeft, btRight);
paneForButtons.setAlignment(Pos.CENTER);
pane.setBottom(paneForButtons);
paneForRadioButtons.getChildren().addAll(rbRed, rbYellow,
rbRed.setToggleGroup(group);
rbYellow.setToggleGroup(group);
rbBlack.setToggleGroup(group);
rbOrange.setToggleGroup(group);
rbGreen.setToggleGroup(group);
paneForText.setStyle("-fx-border-color: black");
paneForText.getChildren().add(text);
pane.setCenter(paneForText);
pane.setTop(paneForRadioButtons);
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);
});
Scene scene = new Scene(pane, 450, 150);
primaryStage.setTitle("GUI program");
primaryStage.setScene(scene);
primaryStage.show();
Program - 21
Write a program to create a file name 123. txt, if it does not exist.Append a
new data to it if it already exists.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 Main {
try (
){
fnfe.printStackTrace();
Program - 22
import java.util.Scanner;
class Main {
list[i] = input.nextInt();
}
if (index < 0) {
return min;
} else {
Write a test program that prompts the user to enter an integer and display
its product.
import java.util.Scanner;
class Main {
int product = 1;
list[i] = input.nextInt();
product *= list[i];
OUTPUT
Program - 23
Write a generic method that returns the minimum elements in a two dimensional
array.
int value = 0;
if (element.compareTo(max) > 0) {
max = element;
}
}
}
return max;
}
}
Program - 24
MyPriorityQueue < String > queue1 = new MyPriorityQueue < > ();
queue1.offer("Oklahoma");
queue1.offer("Indiana");
queue1.offer("Georgia");
queue1.offer("Texas");
queue1.offer("New York");
queue1.offer("New Jersey");
queue2.remove();
// Display queues
System.out.print("Queue1: ");
System.out.println();
System.out.print("Queue2: ");
System.out.println();
Program - 25
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.import com.sun.javaws.exceptions.InvalidArgumentException;
import java.io.*;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeSet;
if (args.length != 1)
if (!file.isFile())
sb.append(inputS);
TreeSet < String > ndWords = new TreeSet < > (Arrays.asList(words));
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
G-MAIL: [email protected]
0 Add a comment