Java Assignment Revised
Java Assignment Revised
Research, Indore
Submitted To Submitted By
prof.Anil Kumar Tanish Shrotriya
S.N Topic Date Remarks
o
1. Write a program to print
numbers into words using
Nested if and Switch case.
2. Write a program called pass
fail which prints 'pass' if the
int variable 'mark' is more
than or equal to 50; or prints
'fail' otherwise.
3. write a program called odd even
which prints 'odd number' if
the int variable 'number'
is odd, or 'even number'
otherwise.
useNestedIf(num);
useSwitchCase(num);
}
if (num>=37)
{
System.out.println("Pass!");
}
else
System.out.println("Fail!");
}
}
Q3 write a program called odd even which prints 'odd
number' if the int variable 'number'
is odd, or 'even number' otherwise.
import java.util.Scanner;
if(num % 2 == 0)
System.out.println(num + " is Even Number ");
else
System.out.println(num + " is Odd Number ");
}
}
Q4. write a program to find sum and average of 10 no. using array
//Java program to calculate the average of array elements
import java.util.Scanner;
public class Main
{
// Function that returns the average of an array.
static double averageCalculate(int a[], int n)
{
// Find sum of array element
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += a[i];
}
System.out.println("The total sum of all the elements in the array is "+sum);
return (double)sum / n;
}
//driver code
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
}
Q6. write a program to display grade according to the marks obtained by the
student.
import java.util.Scanner;
}
Q10. write a program to take an input from user and check given number is
prime or not.
import java.util.Scanner;
public class PrimeNumberChecker {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int num = input.nextInt();
boolean isPrime = checkPrime(num);
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
} }
public static boolean checkPrime(int num) {
if (num <= 1) {
return false; // Numbers less than or equal to 1 are not prime
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false; // If the number is divisible by any number from 2 to sqrt(num), it's
not prime
}
}
return true; // Otherwise, it's prime }}
Q11. write a program to implement method overriding.
import java.util.*;
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Q12. Write a program to convert given string into uppercase and lowercase
and get the length of string using array .
import java.util.*;
public class StringConverter {
public static void main(String[] args) {
String input = "Hello, World!";
char[] charArray = input.toCharArray();
int length = charArray.length;
System.out.println("Original String: " + input);
System.out.println("Length of String: " + length);
System.out.print("Uppercase String: ");
for (int i = 0; i < length; i++) {
char c = Character.toUpperCase(charArray[i]);
System.out.print(c);
charArray[i] = c;
}
System.out.println();
System.out.print("Lowercase String: ");
for (int i = 0; i < length; i++) {
char c = Character.toLowerCase(charArray[i]);
System.out.print(c);
charArray[i] = c;
}
System.out.println();}
Q13. Write a program to overload volume method to find out volume of cube
and cuboid.
import java.util.*;
public class VolumeCalculator {
public static void main(String[] args) {
double side = 5.0;
double length = 10.0;
double breadth = 7.0;
double height = 3.0;
double volumeCube = calculateVolume(side);
double volumeCuboid = calculateVolume(length, breadth, height);
System.out.println("Volume of Cube: " + volumeCube);
System.out.println("Volume of Cuboid: " + volumeCuboid);
}
interface Printable {
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}