0% found this document useful (0 votes)
43 views4 pages

NAME-Megha Saxena Registration Number - 20BIT0366 Java Programming Lab - 33+34 Assignment 3

The document describes two Java classes - MathProcess containing methods to check if a number is a perfect number and calculate factorials, and StatProcess containing methods to input array values, calculate the mean, and import and test the classes; it also provides code to validate student registration numbers and phone numbers by throwing specific exception types for invalid inputs.

Uploaded by

Megha Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

NAME-Megha Saxena Registration Number - 20BIT0366 Java Programming Lab - 33+34 Assignment 3

The document describes two Java classes - MathProcess containing methods to check if a number is a perfect number and calculate factorials, and StatProcess containing methods to input array values, calculate the mean, and import and test the classes; it also provides code to validate student registration numbers and phone numbers by throwing specific exception types for invalid inputs.

Uploaded by

Megha Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

NAME- Megha Saxena

Registration Number- 20BIT0366


Java Programming
Lab- 33+34
Assignment 3
1. Develop a package called CSE1007. Design two classes MathProcess and StatProcess 3M

a. MathProcess

i. Contains an integer member and a constructor to initiate the integer member.

ii. Design a method isProductPerfectNumber which returns Boolean. It verifies the member is a
product perfect number or not. A number is a product perfect number if the product of all its
divisors, other than the number itself, is equal to the number. For example, 10 and 21 are product
perfect numbers since 1*2*5 = 10 and 1*3*7 = 21. Finally returns the result.

iii. Design another method to find the factorial of the member and returns the factorial value.

b. StatProcess

i. Contains an array of integers and its size n as members. Create a constructor to initiate the size
of array. Design a method getInput which gets ‘n’ integer numbers. Design another method
findMean which finds mean value of the array and return it.

Design the test class which imports this package and demonstrate these classes and their
methods through few objects.

CODE:

package CSE1007;
public class MathProcess {
int member1;
public void member(){
member1=10;
}
static boolean isPerfect(int n)
{
// To store sum of divisors
int sum = 1;

// Find all divisors and add them


for (int i = 2; i * i <= n; i++)
{
if (n % i==0)
{
if(i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
// If sum of divisors is equal to
// n, then n is a perfect number
if (sum == n && n != 1)
return true;

return false;
}

// Driver program

{
System.out.println("Below are all perfect" +
"numbers till 10000");
for (int n = 2; n < 10000; n++)
if (isPerfect(n))
System.out.println( n +
" is a perfect number");
}
static int factorial(int n)
{
if (n == 0)
return 1;

return n * factorial(n - 1);


}

// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num
+ " is " + factorial(5));
}
}

package CSE1007;
import java.util.Scanner;
public class JStatProcess {

public static void main(String[] args) {


System.out.println("How many numbers you want to enter?");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
/* Declaring array of n elements, the value
* of n is provided by the user
*/
double[] arr = new double[n];
double total = 0;
for(int i=0; i<arr.length; i++){
System.out.print("Enter Element No."+(i+1)+": ");
arr[i] = scanner.nextDouble();
}
scanner.close();
for(int i=0; i<arr.length; i++){
total = total + arr[i];
}

double average = total / arr.length;

System.out.format("The average is: %.3f", average);


}
}
Import CSE1007;
OUTPUT:

2. Write a program in Java to raise exception for data validation and typo error.

a. Read the Register Number and Mobile Number of a student. If the Register Number does not
contain exactly 9 characters or if the Mobile Number does not contain exactly 10 characters,
throw an IllegalArgumentException.
b. If the Mobile Number contains any character other than a digit, raise a
NumberFormatException. c. If the Register Number contains any character other than digits and
alphabets, throw a NoSuchEl Get the input and print it if no error in the input. Otherwise raise the
corresponding error

CODE:

import java.io.*;
public class error{
public static void main(String args[]){
try{
int registrationnumber[]=new int[9];
int mobilenumber[]= new int[10] ;
if(registrationnumber.length<9 )
{
System.out.println("no error");
}
if(mobilenumber.length<10)
{
System.out.println("no error");
}
}catch(IllegalArgumentException e)
{
System.out.println("Error throw:" +e);
}
try{
if(registrationnumber != [a-z,A-Z])
{
System.out.println("no error");
}catch(NoSuchElementException e1)
{
System.out.println("Error throw:" +e1);
}
}
}
}
OUTPUT:

You might also like