0% found this document useful (0 votes)
6 views23 pages

Java Assignment Revised

The document outlines a course on Internet Application Using Java Programming for BCA II Year students at Acropolis Institute of Management Studies & Research, Indore. It includes various programming assignments covering topics such as number conversion, pass/fail checks, odd/even determination, array operations, and object-oriented programming concepts. Each assignment is accompanied by example Java code demonstrating the required functionality.
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)
6 views23 pages

Java Assignment Revised

The document outlines a course on Internet Application Using Java Programming for BCA II Year students at Acropolis Institute of Management Studies & Research, Indore. It includes various programming assignments covering topics such as number conversion, pass/fail checks, odd/even determination, array operations, and object-oriented programming concepts. Each assignment is accompanied by example Java code demonstrating the required functionality.
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/ 23

Acropolis Institute of Management Studies &

Research, Indore

Bachelors Of Computer Application

Subject:- Internet Application Using Java Programming


Course code:- S2-BCAB2T
Class:- BCA II Year
Department:- FCA
Session:- 2022-23

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.

4. write a program to find sum and average of


10 no. using array

5. Write a program to display reverse of a digit


no.

6. write a program to display grade according to


the marks obtained by the student.

7. find the factorial of number if


number is given by user using
command line argument.
8. write a program to print Fibonacci series.
9. write a program to display table s from 2 to
10.
10 write a program to take an input from user
. and check given number is prime or not.

11 write a program to implement method


. overriding.

12 Write a program to convert given string into


. uppercase and lowercase and get the length
of string using array .
13 Write a program to overload volume method
. to find out volume of cube and cuboid.

14 write a program to design a class using


. abstract methods and class.

15 write a program to implement multiple


. inheritance using interface.

Q1. Write a program to print numbers into words


using Nested if and Switch case.
import java.util.Scanner;

public class PrintNumberInWord {

public static void main(String[] args)


{
int num;
Scanner reader = new Scanner(System.in);
System.out.println("Enter Number: ");
num = reader.nextInt();

useNestedIf(num);
useSwitchCase(num);
}

private static void useNestedIf(int number)


{
String numberStr = null;
if (0 == number) {
numberStr = "ZERO";
} else if (1 == number) {
numberStr = "ONE";
} else if (2 == number) {
numberStr = "TWO";
} else if (3 == number) {
numberStr = "THREE";
} else if (4 == number) {
numberStr = "FOUR";
} else if (5 == number) {
numberStr = "FIVE";
} else if (6 == number) {
numberStr = "SEX";
} else if (7 == number) {
numberStr = "SEVEN";
} else if (8 == number) {
numberStr = "EIGHT";
} else if (9 == number) {
numberStr = "NINE";
} else {
numberStr = "OTHER";
}
System.out.println("(a) Use a \"nested-if\" statement: " +
numberStr);
}

private static void useSwitchCase(int number)


{
String numberStr = null;
switch (number) {
case 0: numberStr = "ZERO"; break;
case 1: numberStr = "ONE"; break;
case 2: numberStr = "TWO"; break;
case 3: numberStr = "THREE"; break;
case 4: numberStr = "FOUR"; break;
case 5: numberStr = "FIVE"; break;
case 6: numberStr = "SEX"; break;
case 7: numberStr = "SEVEN"; break;
case 8: numberStr = "EIGHT"; break;
case 9: numberStr = "NINE"; break;
default: numberStr = "OTHER"; break;
}
System.out.println("(b) Use a \"switch-case\" statement: " +
numberStr);
}
}
Q2. 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.
import java.util.Scanner;
public class PassFail
{
public static void main(String[] args)
{
int num;
Scanner reader = new Scanner(System.in);
System.out.println("Enter score: ");
num = reader.nextInt();

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;

public class OddEven {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

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


int num = reader.nextInt();

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);

int n; //Declare array size


System.out.println("Enter the total number of elements in the array ");
n=sc.nextInt(); //Initialize the array size

int arr[] = new int[n]; //Declare array


System.out.println("Enter the array elements ");
for(int i=0 ; i<n ; i++) //Initialize the array
{
arr[i]=sc.nextInt();
}

System.out.println("The average of all the elements in an array is


"+averageCalculate(arr, n));
}
}
Q5. Write a program to display reverse of a digit no.
public class ReverseArray {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("Array in reverse order: ");
//Loop through the array in reverse order
for (int i = arr.length-1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}

}
Q6. write a program to display grade according to the marks obtained by the
student.
import java.util.Scanner;

public class GradeCalculator {


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

System.out.print("Enter marks obtained: ");


double marks = input.nextDouble();

String grade = calculateGrade(marks);

System.out.println("Grade: " + grade);


}

public static String calculateGrade(double marks) {


String grade;
if (marks >= 90) {
grade = "A+";
} else if (marks >= 80) {
grade = "A";
} else if (marks >= 70) {
grade = "B";
} else if (marks >= 60) {
grade = "C";
} else if (marks >= 50) {
grade = "D";
} else if (marks >= 40) {
grade = "E";
} else {
grade = "F";
}
return grade;
}
}
Q7. find the factorial of number if number is given by user using command
line argument.
public class FactorialCalculator {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java FactorialCalculator <number>");
System.exit(1);
}
int number = Integer.parseInt(args[0]);
int factorial = 1;
if (number < 0) {
System.out.println(" Enter a positive integer:”);
System.exit(1);
} else if (number == 0) {
System.out.println("Factorial of 0 is 1.");
System.exit(0);
}
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("Factorial of " + number + " is: " + factorial);
}
}
Q8. write a program to print Fibonacci series.
//Java Program to print Fibonacci series
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt(); //Declare and Initialize the number of terms
System.out.println("First " + n + " terms of fibonnaci series: ");
//Print the fibonacci series
for (int i = 1; i <= n; ++i)
{ System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
Q9. write a program to display table s from 2 to 10.
public class MultiplicationTables {
public static void main(String[] args) {
for (int i = 2; i <= 10; i++) {
System.out.println("Table of " + i + ":");
for (int j = 1; j <= 10; j++) {
int result = i * j;
System.out.println(i + " x " + j + " = " + result);
}
System.out.println();
}
}

}
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 Dog extends Animal {


public void displayInfo() {
System.out.println("I am a dog.");
}
}

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);
}

public static double calculateVolume(double side) {


return side * side * side;
}
public static double calculateVolume(double length, double breadth, double height) {
return length * breadth * height;
}
}
Q14. write a program to design a class using abstract methods and class.
abstract class Demo {

// method of abstract class


public void display() {
System.out.println("This is Java Programming");
}
}
class Main extends Demo {

public static void main(String[] args) {

// create an object of Main


Main obj = new Main();

// access method of abstract class


// using object of Main class
obj.display();
}
}
Q15. write a program to implement multiple inheritance using interface.

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");}

public static void main(String args[]){


A7 obj = new A7();
obj.print();
obj.show();
}
}
}
}

You might also like