Java All Question
Java All Question
import java.util.*;
int fact = 1;
for(int i = 1; i <= n; i++){
fact = fact * i;
}
System.out.println("Factorial = " + fact);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> s1 = new ArrayList<>();
s1.add("Devraj");
s1.add("Mayur");
s1.add("Dinesh");
s1.add("Het");
s1.add("Vashu");
}
}
// 9. Write a Java program that takes an integer input from the user and
determines whether the
// entered year is a leap year or not.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Any Year : ");
int n = sc.nextInt();
// 10. Write a Java program that prints the numbers from 1 to 10 using a
while loop.
public class Main {
public static void main(String[] args) {
int i = 1;
// 11. Write a Java program that takes an integer input from the user and
prints whether the number is
// positive, negative, or zero using an if-else statement.
import java.util.*;
if(n == 0){
System.out.println("Number Is Zero.");
} else if(n > 0){
System.out.println("Number Is Positive.");
} else {
System.out.println("Number Is Negitive.");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Your Age : ");
int age = sc.nextInt();
import java.util.*;
public class Main {
public static void main(String[] args) {
int i = 20;
import java.util.*;
public class Calculator {
static int max(int i, int j){
if(i > j) return i;
else return j;
}
public static void main(String[] args) {
int i = 20;
int j = 10;
import java.util.*;
public class Main {
static double convertTemperature(double c){
double f = (1.8 * c) + 32;
return f;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> num = new ArrayList<>();
num.add(1);
num.add(5);
num.add(7);
num.add(9);
num.add(10);
double sum = 0;
for(int i = 0; i < num.size(); i++){
sum = sum + num.get(i);
}
double avg = sum / num.size();
System.out.println("Average Of ArrayList = " + avg);
}
}
// 21. Write a Java program that takes a day of the week as input (1 for
Monday, 2 for Tuesday, etc.)
// and prints the name of the day using a switch statement.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number : ");
int n = sc.nextInt();
switch(n){
case 1:
System.out.println("MonDay");
break;
case 2:
System.out.println("TuesDay");
break;
case 3:
System.out.println("WednesDay");
break;
case 4:
System.out.println("ThrusDay");
break;
case 5:
System.out.println("FriDay");
break;
case 6:
System.out.println("SaturDay");
break;
case 7:
System.out.println("SunDay");
break;
default:
System.out.println("Please Enter Only 1 To 7 Number
Only.");
}
}
}
System.out.println(s1.toString());
System.out.println("Enter Name You Wan't to Remove :");
String removeItem = sc.nextLine();
sc.close();
}
}
// 24. Write a program that grades a student's score into letter grades
(A, B, C, D, F) using if-else else-if
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Marks : ");
int mark = sc.nextInt();
sc.close();
}
}
import java.util.*;
if(s1.contains(check) == true){
System.out.println(check + " Is Present In the ArrayList.");
}
if(s1.contains(check) == false){
System.out.println(check + " Is Not Present In the
ArrayList. ");
}
sc.close();
}
}
// 29. Write a program that prompts the user to input a number and prints
whether it is even or odd.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number : ");
int n = sc.nextInt();
sc.close();
}
}
// 30. Create a program that uses a for loop to calculate the sum of
numbers from 1 to 100.
import java.util.*;
public class Main {
static int addNumber(int a, int b){
return a + b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Num 1 :");
int num1 = sc.nextInt();
}
}
// 33. Create a program that uses a for-each loop to print all elements
in an ArrayList of Strings
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<String> s1 = new ArrayList<>();
s1.add("Devraj");
s1.add("Dinesh");
s1.add("Devang");
Collections.reverse(Arrays.asList(numbers));
// 38. Write a program that prompts the user to enter a number and checks
whether it is prime.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double result;
switch(ch){
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.println("Enter valid Operator...");
return;
}
System.out.println(num1 + " " + ch + " " + num2 + " = " +
result);
}
}
//40. Write a Java program that reads a string input from the user and
counts the number of vowels in
// it.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter A Word : ");
String str = sc.nextLine();
str = str.toUpperCase();
int count = 0;
import java.util.*;
public class Main {
static void printMatrix(int[][] arr){
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] arr = {{1,2},{3,4}};
printMatrix(arr);
}
}
// 46. Write a program that takes a number from the user and prints the
multiplication table for that
// number.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter No : ");
int n = sc.nextInt();
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int sum = 0;
for(int elements : arr){
sum += elements;
}
// 52. Write a Java program that takes a sentence as input from the user
and reverses the order of
// words in the sentence.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Sentance : ");
String str = sc.nextLine();
System.out.println(rev.trim());
}
}
import java.util.*;
public class Main {
static int quotient(int n, int d){
return n / d;
}
public static void main(String[] args) {
int n = 8;
int d = 2;
try{
int result = quotient(n, d);
System.out.println("Result : " + n + " / " + d + " = " +
result);
}
catch(ArithmeticException e){
System.out.println("Division By Zero");
}
System.out.println("BYE");
}
}
import java.util.*;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter No Of Element : ");
int n = sc.nextInt();
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter No of Elements : ");
int n = sc.nextInt();
package JavaQue;
// QUESTION 17. What is a method and what role does it play in a class?
Define a method in a class that returns a greeting message.
/*
A **method** in Java is a block of code within a class that performs a
specific task. It defines the behavior of objects created from that
class. Methods allow objects to perform actions, interact with other
objects, and encapsulate functionality.
package JavaQue;
// Making Constructor :
public C_Rectangle(double width, double height){
this.width = width;
this.height = height;
}
// 26. Define a class Student with a String field name and an int field
age. Write a method
// displayInfo() that prints the student's name and age. Create an object
of the Student class, assign
// values to the fields, and call the displayInfo() method.
package JavaQue;
stu1.displayInfo();
stu2.displayInfo();
}
}
// 34. Define a Book class with fields for title, author, and price.
Create a method that applies a
// discount to the price and prints the new price.
package JavaQue;
book1.discountPrice(20);
}
}
// 36. Create a Car class with attributes for model, year, and mileage.
Write a method to calculate the
// car’s depreciation and print the result.
package JavaQue;
car1.depreciation();
}
}
// QUESTION 41. Create a class Employee with fields for name, employeeID,
and salary. Write a method to give
// the employee a raise and print their new salary.
package JavaQue;
emp1.incriment(25);
}
}
// 50. Define a BankAccount class with fields for account number and
balance. Write a method to
// deposit money into the account and another method to withdraw money,
ensuring that the
// balance never goes negative.
package JavaQue;
// import java.util.Scanner;
acc1.depositMoney(20000);
acc1.withdwaw(1500);
}
}