0% found this document useful (0 votes)
24 views5 pages

Assignment Java

Assignment of java contain problem solving skills and solutions of problems regarding java

Uploaded by

yapagod112
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
24 views5 pages

Assignment Java

Assignment of java contain problem solving skills and solutions of problems regarding java

Uploaded by

yapagod112
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Name: Bhushan Mule

1.Write a Java method to compute the sum of digits in an integer.


import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter 1st: ");


int num1 = scanner.nextInt();
System.out.print("Enter 2nd: ");
int num2 = scanner.nextInt();
System.out.print("Enter 3rd: ");
int num3 = scanner.nextInt();

int ans = product(num1, num2, num3);


System.out.println("Answer: " + ans);

scanner.close();
}

public static int product(int a, int b, int c) {


return a*b*c;
}
}
2.Write a Java method to compute the average of three numbers.
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter Three Numbers: ");


int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();

float ans = avg(num1, num2, num3);


System.out.println("Answer: " + ans);

scanner.close();
}

public static int avg(int a, int b, int c) {


int sum=a+b+c;
return sum/3;
}
}

3.Write a Java method to find the smallest number among three numbers.
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter Three Numbers: ");


int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();

float ans = check(num1, num2, num3);


System.out.println("Answer: " + ans);

scanner.close();
}
public static int check(int a, int b, int c) {
int smallest = c < (a < b ? a : b) ? c : ((a < b) ? a : b);
return smallest;
}
}

4.Write a Java method to display the middle character of a string.


import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a String: ");


String str = scanner.nextLine();
System.out.print("The middle character in the string: " +
middle(str)+"\n");

scanner.close();
}
public static String middle(String str)
{
int pos, len;
if (str.length() % 2 == 0)
{
pos = str.length() / 2 - 1;
len = 2;
}
else
{
pos = str.length() / 2;
len = 1;
}
return str.substring(pos, pos + len);
}
}

5.Vowel
import java.util.Scanner;

public class Main {


public static int countVowels(String str) {
int count = 0;
str = str.toLowerCase();
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;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Input the string: ");
String str = scanner.nextLine();
scanner.close();

int vowelCount = countVowels(str);


System.out.println("Number of vowels in the string: " +
vowelCount);
}
}

6.Write a Java method to count all the words in a string.


import java.util.Scanner;
public class Main {
public static int countWords(String str) {
String[] words = str.split("\\s+");
return words.length;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Input the string: ");
String str = scanner.nextLine();
scanner.close();

int wordCount = countWords(str);


System.out.println("Number of words in the string: " +
wordCount);
}
}
7.Write a Java method to check whether a string is a valid password.
public class Main
{
public static boolean isValidPassword(String password)
{
if (password.length() < 10 || password.replaceAll("\\D",
"").length() < 2)
{
return false;
}
if (!password.matches("[a-zA-Z0-9]+"))
{
return false;
}
return true;
}
public static void main(String[] args)
{
String password = "Pass123";
if (isValidPassword(password))
{
System.out.println("The password is valid.");
}
else
{
System.out.println("The password is not valid.");
}
}
}

8.Write a Java method to display the current date and time.


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {


public static void displayCurrentDateTime() {
// Get the current date and time
LocalDateTime currentDateTime = LocalDateTime.now();

// Define the desired format


DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE
MMMM dd, yyyy HH:mm:ss");

// Format the current date and time using the defined format
String formattedDateTime = currentDateTime.format(formatter);

// Display the formatted current date and time


System.out.println("Current date and time: " +
formattedDateTime);
}

public static void main(String[] args) {


displayCurrentDateTime();
}
}
9.Write a Java method to display the factors of 3 in a given integer.

public class DisplayFactorsOfThree {


public static void displayFactorsOfThree(int number) {
System.out.print("Factors of 3 in " + number + " are: ");
for (int i = 1; i <= number; i++) {
if (i % 3 == 0) {
System.out.print(i + " ");
}
}
System.out.println();
}

public static void main(String[] args) {


int number = 30;
displayFactorsOfThree(number);
}
}
public static void main(String[] args) {
int number = 30;
displayFactorsOfThree(number);
}
}

10.Write a Java methods to calculate area of triangle.

public class Main {


public static double calculateTriangleArea(double base, double
height) {
return 0.5 * base * height;
}

public static void main(String[] args) {


double base = 5.0;
double height = 8.0;
double area = calculateTriangleArea(base, height);
System.out.println("Area of triangle with base " + base + " and
height " + height + " is " + area);
}
}

11. Write a Java method to create a pentagon's area.

public class PentagonArea {


public static double calculatePentagonArea(double side) {
return 0.25 * Math.sqrt(5 * (5 + 2 * Math.sqrt(5))) * side * side;
}

public static void main(String[] args) {


double side = 5.0;
double area = calculatePentagonArea(side);
System.out.println("Area of pentagon with side " + side + " is "
+ area);
}

You might also like