Java Assignment 1 - New
Java Assignment 1 - New
Java Assignment 1 - New
OBJECTIVE: To learn
how to define multiple class in an application.
How to use real numbers properly
How to handle very large numbers in your application
1. Write a java program that finds the factorial value, check the prime number, and calculate
the gcd value of two numbers.
Consider the following implementation code/ driver code. Then complete the application
by defining the class and functions properly.
You should follow the following class hierarchy.
The Calculation class contains the code for calculating factorial, prime number and gcd.
This class contains the code for normal range integer value and also for very large Integer
value.
[Note: A prime number is a positive integer greater than 1 that has no positive integer
divisors other than 1 and itself. In other words, a prime number is a number that is only
divisible by 1 and itself.
For example, the first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, and so on. ]
2. The implementation class (Driver code, i.e. main function) is shown below:
1|PRADOS H BANDYOPADHYAY
public class CaclculationExample {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number = ");
int val = sc.nextInt();
System.out.println("Factorial value for "+val +" = "+ Calculation.factorial(val));
}//end of MAIN
}//end of CLASS
Take a good look at the following case studies before designing the class. Your code
should satisfy all the given cases below
Case 1:
run:
Enter the number = 5
Factorial value for 5 = 120
Case 2:
run:
Enter the number = 15
Factorial value for 15 = 1307674368000
BUILD SUCCESSFUL (total time: 27 seconds)
Case 3:
run:
Enter the number = 100
Factorial value for 100 =
933262154439441526816992388562667004907159682643816214685929638952175999
932299156089414639761565182862536979208272237582511852109168640000000000
00000000000000
BUILD SUCCESSFUL (total time: 30 seconds)
3. Write a Java Application that creates a student class and have the following class
hierarchy.
2|PRADOS H BANDYOPADHYAY
Create a class Student. Student class has the following attributes:
Assign the values into the attributes using the constructor (use the constructor
overloading). You can also use the getter/ setter methods. But before assigning the value
your application should check the student roll number must belong between 1 to 60. The
marks value should be less than 100.
The following attributes are initialized by the initializer list with following value:
Name and year is initialized with NULL. Roll and Marks is initialized by 0 and -1.
Create Three students ( [ “Sutapa Sen”, 25, 76, “2010”] , [ “Amal Basu”, 15, 85, “2010”],
[ “Hitesh Bagchi”, 31, 66,”2010”] ).
Print the student information.
Now print the student name and roll number who scored highest marks.
4. Consider the following implementation class or driver class, write a Java application that
defines the class Bank properly.
public class StudentImp {
public static void main(String[] args) {
Bank B1 = new Bank("01634001300000452","Biman Sen", 'S', "55000.67");
B1.display();
System.out.println("After Deposit ...");
B1.deposit("45987.59");
B1.display();
System.out.println("After With draw ...");
B1.withDraw("10000");
B1.display();
}//end of main
}//end of class
The Bank constructor takes account number, Customer name, account type (S for savings
or C for current), and amount. All these parameters are string type.
The display function displays the customer details.
The deposit function takes the amount in the string format and updates the balance
amount.
Similarly, the withdraw function takes the withdrawal amount in string format. Before
withdrawal the system checks the balance amount should be larger than 5000.
5. Write a Java class Complex for dealing with complex number. Your class must have the
following features:
Instance variables :
realPart for the real part of type double
imaginaryPart for imaginary part of type double.
4|PRADOS H BANDYOPADHYAY
Constructor:
public Complex (): A default constructor, it should initialize the number to 0, 0)
public Complex (double realPart, double imaginaryPart): A constructor with
parameters, it creates the complex object by setting the two fields to the passed
values.
Instance methods:
public Complex add (Complex otherNumber): This method will find the sum of
the current complex number and the passed complex number. The method returns
a new Complex number which is the sum of the two.
public Complex subtract (Complex otherNumber): This method will find the
difference of the current complex number and the passed complex number. The
method returns a new Complex number which is the difference of the two.
public Complex multiply (Complex otherNumber): This method will find the
product of the current complex number and the passed complex number. The
method returns a new Complex number which is the product of the two.
public void setRealPart (double realPart): Used to set the real part of this
complex number.
public void setImaginaryPart (double realPart): Used to set the imaginary part
of this complex number.
public double getRealPart(): This method returns the real part of the complex
number
public double getImaginaryPart(): This method returns the imaginary part of the
complex number
public String toString(): This method allows the complex number to be easily
printed out to the screen
Write a separate class ComplexDemo with a main() method and test the Complex class
methods.
6. a) Write a Java program that declares and initializes the array at the line of declaration in
the main function. The array elements are 1,2,3,5,4. Now write a print function that takes
the array as argument and print the value in the following fashion.
Arr = [1, 2, 3, 5, 4]
arr [0] =1
arr [1] =2
arr [2] =3
arr [3] =5
arr [4] =4
5|PRADOS H BANDYOPADHYAY
b) Now create an array and initialize it with the following values and then Print the array.
10 20 30
40 50
60
7. Write a Java program to find the k largest elements in a given array. Elements in the array
can be in any order. (assume that k = 3)
Complete the following code
return kLargest;
} //end of findKLargest
}//end of class
6|PRADOS H BANDYOPADHYAY