java file (1)
java file (1)
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int first, second, add, subtract, multiply;
float devide;
Scanner scanner = new Scanner(System.in);
1
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
Output :-
2
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
3
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
import java.util.Scanner;
// Circle
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
double areaCircle = Math.PI * radius * radius;
System.out.println("Area of the circle: " + areaCircle);
// Rectangle
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
double areaRectangle = length * width;
System.out.println("Area of the rectangle: " + areaRectangle);
// Triangle
System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
double areaTriangle = 0.5 * base * height;
System.out.println("Area of the triangle: " + areaTriangle);
// Square
System.out.print("Enter the side of the square: ");
double side = scanner.nextDouble();
double areaSquare = side * side;
System.out.println("Area of the square: " + areaSquare);
scanner.close();
4
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
}
}
Output :-
5
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
import java.util.Scanner;
Output :-
6
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
7
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
import java.util.Scanner;
public class MinutesToYearsDays {
public static void main(String[] args) {
double minutesInYear = 60 * 24 * 365;
Scanner input = new Scanner(System.in);
System.out.print("Input the number of minutes: ");
double minutes = input.nextDouble();
8
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
9
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
10
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:
Enter coefficient a: 2
Enter coefficient b: 5
Enter coefficient c: 3
The roots are real and distinct.
Root 1: -1.0
Root 2: -1.5
11
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
12
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
10. Write program that gets a number from the user and generates an integer
between 1 and 7 subsequently should display the name of the weekday as
per that number.
import java.util.Scanner;
public class WeekdayGenerator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number between 1 and 7: ");
int number = scanner.nextInt();
if (number < 1 || number > 7) {
System.out.println("Please enter a valid number between 1 and 7.");
} else {
String weekday = "";
switch (number) {
case 1:
weekday = "Monday";
break;
case 2:
weekday = "Tuesday";
break;
case 3:
weekday = "Wednesday";
break;
case 4:
weekday = "Thursday";
break;
case 5:
13
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
weekday = "Friday";
break;
case 6:
weekday = "Saturday";
break;
case 7:
weekday = "Sunday";
break;
}
System.out.println("The day of the week is: " + weekday);
}
scanner.close();
}
}
OUTPUT:
14
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
15
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
scanner.close();
}
}
OUTPUT:-
16
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
17
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
13. Design & execute a program in Java to sort a numeric array and a string array.
import java.util.Arrays;
public class SortArrays {
public static void main(String[] args) {
int[] numericArray = {5, 2, 8, 1, 3};
String[] stringArray = {"Banana", "Apple", "Cherry", "Mango",
"Blueberry"};
Arrays.sort(numericArray);
// Sorting string array
Arrays.sort(stringArray);
System.out.println("Sorted numeric array: " +
Arrays.toString(numericArray));
System.out.println("Sorted string array: " +
Arrays.toString(stringArray));
}
}
OUTPUT:-
18
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
14. Calculate the average value of array elements through Java Program.
import java.util.Scanner;
public class AverageArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
double[] array = new double[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextDouble();
}
double sum = 0;
for (int i = 0; i < size; i++) {
sum += array[i];
}
double average = sum / size;
System.out.println("The average value of the array elements is: " +
average);
scanner.close();
}
Enter the size of the array: 3
}
Enter the elements of the array:
OUTPUT:-
5
5
10
The average value of the array elements is: 6.666666666666667
19
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
20
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
}
scanner.close();
}
}
OUTPUT:-
21
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
22
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
}
scanner.close();
}
}
OUTPUT:-
23
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:-
25
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
} 1
2
OUTPUT:-
3
19. Write a Java program to insert an element (on a specific position) into
Multidimensional array.
import java.util.Scanner;
public class InsertElementIn2DArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
int[][] array = new int[rows][columns];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = scanner.nextInt();
}
}
System.out.print("Enter the row position to insert the element: ");
int rowPos = scanner.nextInt();
System.out.print("Enter the column position to insert the element: ");
int colPos = scanner.nextInt();
System.out.print("Enter the element to insert: ");
int element = scanner.nextInt();
if (rowPos >= 0 && rowPos < rows && colPos >= 0 && colPos <
columns) {
array[rowPos][colPos] = element;
} else {
27
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:-
28
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
29
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
30
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
21. Developed Program & design a method to find the smallest number
among three numbers.
import java.util.Scanner;
public class SmallestNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter the third number: ");
double num3 = scanner.nextDouble();
double smallest = findSmallest(num1, num2, num3);
System.out.println("The smallest number is: " + smallest);
scanner.close();
}
public static double findSmallest(double a, double b, double c) {
if (a <= b && a <= c) {
return a;
} else if (b <= a && b <= c) {
return b;
} else {
return c;
}
}
}
31
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:-
32
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
33
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
23. Write a Program & design a method to count all vowels in a string.
import java.util.Scanner;
public class VowelCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
int vowelCount = countVowels(input);
System.out.println("The number of vowels in the string is: " +
vowelCount);
scanner.close();
}
public static int countVowels(String str) {
int count = 0;
str = str.toLowerCase(); // Convert the string to lowercase for easier
comparison
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
return count;
}
}
Enter a string: junaid
OUTPUT:-
The number of vowels in the string is: 3
34
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
35
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
36
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
37
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:-
38
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
OUTPUT:-
Thread-2: 1
Thread-1: 1
Thread-2: 2
Thread-1: 2
Thread-2: 3
Thread-1: 3
Thread-2: 4
Thread-1: 4
Thread-2: 5
Thread-1: 5
Thread-2 exiting.
Thread-1 exiting.
Main thread exiting.
40
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
41
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
}
class Labrador extends Dog implements CanRun, CanSwim {
public void run() {
System.out.println("The Labrador runs fast.");
}
public void swim() {
System.out.println("The Labrador swims well.");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
Puppy puppy = new Puppy();
puppy.eat();
puppy.bark();
puppy.weep();
Cat cat = new Cat();
cat.eat();
cat.meow();
Labrador labrador = new Labrador();
labrador.eat();
labrador.bark();
labrador.run();
labrador.swim();
}
42
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:-
43
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
44
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
greetings.sayGreetings();
}
}
OUTPUT:-
javac com/example/HelloWorld.java
javac com/example/Greetings.java
javac Main.java
java Main
Hello, World!
Greetings from the package!
46
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
31. To write and read a plain text file, write a Java program.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReadWrite {
public static void main(String[] args) {
String filePath = "example.txt";
String contentToWrite = "Hello, this is a sample text.";
try (BufferedWriter writer = new BufferedWriter(new
FileWriter(filePath))) {
writer.write(contentToWrite);
System.out.println("Content written to file successfully.");
} catch (IOException e) {
System.out.println("An error occurred while writing to the file.");
e.printStackTrace();
}
try (BufferedReader reader = new BufferedReader(new
FileReader(filePath))) {
String line;
System.out.println("Reading content from file:");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("An error occurred while reading from the
file.");
47
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
e.printStackTrace();
}
}
}
OUTPUT:-
48
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:-
49
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
33. Design a program in Java to get a list of all file/directory names from
the given.
import java.io.File;
public class ListFilesAndDirectories {
public static void main(String[] args) {
String directoryPath = "path/to/your/directory";
File directory = new File(directoryPath);
String[] fileList = directory.list();
if (fileList != null) {
System.out.println("Files and directories in " + directoryPath + ":");
for (String fileName : fileList) {
System.out.println(fileName);
}
} else {
System.out.println("The specified path is not a directory or an error
occurred.");
}
}
}
OUTPUT:-
50
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:-
51
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
35. Write a Java program to check if a file or directory has read and write
permission.
import java.io.File;
import java.util.Scanner;
public class FilePermissionsCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the pathname of the file or directory: ");
String pathname = scanner.nextLine();
File file = new File(pathname);
if (file.exists()) {
if (file.canRead()) {
System.out.println("The file or directory has read permission.");
} else {
System.out.println("The file or directory does not have read
permission.");
}
if (file.canWrite()) {
System.out.println("The file or directory has write permission.");
} else {
System.out.println("The file or directory does not have write
permission.");
}
} else {
System.out.println("The file or directory does not exist.");
}
scanner.close();
}
52
Name:-Bhavika Subject :-Programming in Java Laboratory
Roll No :-3133/22 Course Code: UGCA1938
OUTPUT:-
53