0% found this document useful (0 votes)
71 views

Assignment PGD-PR-2105

This document contains a Java programming assignment submitted by Gurpreet Singh Bedi, roll number 56463, for the 2nd semester of PGDCA in 2019-2020. It includes 6 questions asking to write Java code to calculate a factorial using a while loop, convert a numeric day to a named day using a switch statement, calculate circle properties like diameter and area from a radius, find the minimum value in an array, print the initial characters of command line arguments, and calculate exponents recursively. For each question, the code provided is included along with sample output.

Uploaded by

Gurpreet Singh
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)
71 views

Assignment PGD-PR-2105

This document contains a Java programming assignment submitted by Gurpreet Singh Bedi, roll number 56463, for the 2nd semester of PGDCA in 2019-2020. It includes 6 questions asking to write Java code to calculate a factorial using a while loop, convert a numeric day to a named day using a switch statement, calculate circle properties like diameter and area from a radius, find the minimum value in an array, print the initial characters of command line arguments, and calculate exponents recursively. For each question, the code provided is included along with sample output.

Uploaded by

Gurpreet Singh
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/ 22

Assignment PGD-PR-2105

JAVA Programming Assignment

USOL-PGDCA

GURPREET SINGH BEDI


ROLL NO-56463
PGDCA- 2nd SEMESTER (2019-2020)

GURPREET SINGH BEDI ROLL NO-56463


Q1 Write a Java program to calculate the factorial for a number. Use a while
loop to perform the calculation.

import java.util.Scanner;
public class CalcFactorial {

public static void main(String[] args)


{

int number;
System.out.println("Enter the number: ");
Scanner scanner = new Scanner(System.in);
number = scanner.nextInt();
scanner.close();
long fact = 1;
int i = 1;
while(i<=number)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is:
"+fact);
}
}

Output:
Enter​ the number:
6
Factorial​ of ​6​ ​is​: ​720

GURPREET SINGH BEDI ROLL NO-56463


Q2 Develop code to convert a numeric day to a named day of the week
(1=Sunday, 2=Monday, etc.). Write code that will take an int variable named
day and print the corresponding named day to the standard output. Use a
switch statement.

import java.util.Scanner;

public class CalcWeekkDay


{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int day = in.nextInt();
System.out.println(getDayName(day));
}

// Get the name for the Week


public static String getDayName(int day) {
String dayName = "";
switch (day) {
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
case 7: dayName = "Sunday"; break;
default:dayName = "Invalid day range";
}
return dayName;
}
}

Output:
Enter​ the number:
2
Tuesday

GURPREET SINGH BEDI ROLL NO-56463


Q3 Given the following detailed description, identify the objects, data
members, and operations that can be used to solve the problem. “Write a
program to input the radius of a circle and calculate and print the diameter,
circumference, and area of the circle.”

import java.util.*;
public class Circle
{
float radius;
float diameter;
float area;
float circumference;

double calcDiameter(float r)
{
return (2*r);
}
double calcArea(float r)
{
return 3.14*r*r ;
}
double calcCircumference(float r)
{
return 2* 3.14 * r ;
}
public static void main(String args[])
{
Circle ob = new Circle();
Scanner in = new Scanner(System.in);
System.out.print("Enter the radius: ");
ob.radius = in.nextLine().valueOf();
System.out.println("Area of circle is" +
ob.calcArea(ob.radius));
System.out.println("Diameter of circle is" +
ob.calcDiameter(ob.radius));
System.out.println("Circumference of circle is" +
ob.calcCircumference(ob.radius));
}
}

Output:
Enter the radius: Area of circle is78.5
Diameter of circle is10.0
Circumference of circle is31.400000000000002

GURPREET SINGH BEDI ROLL NO-56463


Q4 Create a class with a single method that will find the minimum value in a
one-dimensional array. The method should return the value once it has
processed the array. The main method of your class should declare and
initialize a one-dimensional array, call the min method, and print out the
minimum value.

public class CalcMin{


public static int getSmallest(int[] a){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < 6; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[0];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
System.out.println("Smallest: "+getSmallest(a));
}}

Output:
1

GURPREET SINGH BEDI ROLL NO-56463


Q5 Write a Java program that takes a few strings using command line
arguments and displays the initial character of each string.

class Commandstr
{

public static void main(String args[])


{

int len=args.length;

if(len==0)
{
System.out.println("No arguments are given ! ");
return;
}
for(int i=0; i<len; i++){
System.out.println(args[i].charAt(0));
}
}
}

Q6 Create a Java class containing a recursive method that calculates the


result of raising an int to an int power. For example, 2 to the 3rd is 8. The class
should contain the pow function that takes two int parameters - the number
and the exponent, and returns the number raised to the exponent power. The
class should also include a main method that calls the pow method and prints
the results.

public class CalcRecurs{


public static int pow(int num, int expo){
if(expo==1)
{
return num;
}
else
{
return num*pow(num, expo-1);
}
}
public static void main(String args[]){
Scanner in = new Scanner(System.in);

GURPREET SINGH BEDI ROLL NO-56463


System.out.print("Enter the number ");
int num= in.nextInt();
System.out.print("Enter the exponent ");
int expo= in.nextInt();
int result = pow(num,expo);
System.out.println(result);
}}

Output:
Enter the number
2
Enter the exponent
3
8

Q7 Implement Bubble Sort algorithm using Java. Write a class Bubblesort


having methods – swap, sort, print etc.

public class MyBubbleSort {

// logic to sort the elements


public static void bubble_srt(int array[]) {
int n = array.length;
int k;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
k = i + 1;
if (array[i] > array[k]) {
swapNumbers(i, k, array);
}
}
printNumbers(array);
}
}
private static void swapNumbers(int i, int j, int[]
array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}

private static void printNumbers(int[] input) {


for (int i = 0; i < input.length; i++) {

GURPREET SINGH BEDI ROLL NO-56463


System.out.print(input[i] + ", ");
}
System.out.println("\n");
}
public static void main(String[] args) {
int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
bubble_srt(input);
}
}

Output:

2, 4, 6, 9, 12, 23, 0, 1, 34,

2, 4, 6, 9, 12, 0, 1, 23, 34,

2, 4, 6, 9, 0, 1, 12, 23, 34,

2, 4, 6, 0, 1, 9, 12, 23, 34,

2, 4, 0, 1, 6, 9, 12, 23, 34,

2, 0, 1, 4, 6, 9, 12, 23, 34,

0, 1, 2, 4, 6, 9, 12, 23, 34,

0, 1, 2, 4, 6, 9, 12, 23, 34,

0, 1, 2, 4, 6, 9, 12, 23, 34,

0, 1, 2, 4, 6, 9, 12, 23, 34,

GURPREET SINGH BEDI ROLL NO-56463


Q8 Create a class Account that stores the customer name, account number ant
type of account. From this derive the classes Curr_acc and Sav_acc to make
them more specific. Include the necessary methods to accept deposit, allow
withdrawal, compute interest, display balance etc.

import java.io.*;
class Curr_acct //CURRENT ACCOUNT CLASS
{
final int max_limit = 20;
final int min_limit = 1;
final double min_bal = 500;
private String name[] = new String[20];
private int accNo[] = new int[20];
private String accType[] = new String[20];
private double balAmt[] = new double[20];
static int totRec = 0;
//Intializing Method
public void initialize() {
for (int i = 0; i < max_limit; i++) {
name[i] = "";
accNo[i] = 0;
accType[i] = "";
balAmt[i] = 0.0;
}
}
//TO ADD NEW RECORD
public void newEntry() {
String str;
int acno;
double amt;
boolean permit;
permit = true;

if (totRec > max_limit) {


System.out.println("\n\n\nSorry we cannot admit
you in our bank...\n\n\n");
permit = false;
}

if (permit = true) //Allows to create new entry


{
totRec++; // Incrementing Total Record

System.out.println("\n\n\n=====RECORDING NEW
ENTRY=====");

GURPREET SINGH BEDI ROLL NO-56463


try {
accNo[totRec] = totRec; //Created AutoNumber
to accNo so no invalid id occurs
System.out.println("Account Number : " +
accNo[totRec]);

BufferedReader obj = new BufferedReader(new


InputStreamReader(System.in));
System.out.print("Enter Name : ");
System.out.flush();
name[totRec] = obj.readLine();

accType[totRec] = "Current Account";


System.out.println("Account Type : " +
accType[totRec]);

do {
System.out.print("Enter Initial Amount
to be deposited : ");
System.out.flush();
str = obj.readLine();
balAmt[totRec] = Double.parseDouble(str);
} while (balAmt[totRec] < min_bal);
//Validation that minimun amount must be 500

System.out.println("\n\n\n");
} catch (Exception e) {}
}
}

//TO DISPLAY DETAILS OF RECORD


public void display() {
String str;
int acno = 0;
boolean valid = true;

System.out.println("\n\n=====DISPLAYING DETAILS OF
CUSTOMER=====\n");
try {
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter Account number : ");
System.out.flush();
str = obj.readLine();
acno = Integer.parseInt(str);
if (acno < min_limit || acno > totRec) //To check
whether accNo is valid or Not
{

GURPREET SINGH BEDI ROLL NO-56463


System.out.println("\n\n\nInvalid Account
Number \n\n");
valid = false;
}

if (valid == true) {
System.out.println("\n\nAccount Number : " +
accNo[acno]);
System.out.println("Name : " + name[acno]);
System.out.println("Account Type : " +
accType[acno]);
System.out.println("Balance Amount : " +
balAmt[acno] + "\n\n\n");
}
} catch (Exception e) {}
}

//TO DEPOSIT AN AMOUNT


public void deposit() {
String str;
double amt;
int acno;
boolean valid = true;
System.out.println("\n\n\n=====DEPOSIT AMOUNT=====");

try {
//Reading deposit value
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));

System.out.print("Enter Account No : ");


System.out.flush();
str = obj.readLine();
acno = Integer.parseInt(str);
if (acno < min_limit || acno > totRec) //To check
whether accNo is valid or Not
{
System.out.println("\n\n\nInvalid Account
Number \n\n");
valid = false;
}

if (valid == true) {
System.out.print("Enter Amount you want to
Deposit : ");
System.out.flush();
str = obj.readLine();
amt = Double.parseDouble(str);

GURPREET SINGH BEDI ROLL NO-56463


balAmt[acno] = balAmt[acno] + amt;

//Displaying Depsit Details


System.out.println("\nAfter Updation...");
System.out.println("Account Number : " +
acno);
System.out.println("Balance Amount : " +
balAmt[acno] + "\n\n\n");
}
} catch (Exception e) {}
}
//TO WITHDRAW BALANCE
public void withdraw() {
String str;
double amt, checkamt, penalty;
int acno;
boolean valid = true;
System.out.println("\n\n\n=====WITHDRAW
AMOUNT=====");

try {
//Reading deposit value
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));

System.out.print("Enter Account No : ");


System.out.flush();
str = obj.readLine();
acno = Integer.parseInt(str);

if (acno < min_limit || acno > totRec) //To check


whether accNo is valid or Not
{
System.out.println("\n\n\nInvalid Account
Number \n\n");
valid = false;
}

if (valid == true) {
System.out.println("Balance is : " +
balAmt[acno]);
System.out.print("Enter Amount you want to
withdraw : ");
System.out.flush();
str = obj.readLine();
amt = Double.parseDouble(str);

GURPREET SINGH BEDI ROLL NO-56463


checkamt = balAmt[acno] - amt;

if (checkamt >= min_bal) {


balAmt[acno] = checkamt;
//Displaying Depsit Details
System.out.println("\nAfter
Updation...");
System.out.println("Account Number : " +
acno);
System.out.println("Balance Amount : " +
balAmt[acno] + "\n\n\n");
} else {
System.out.println("\n\nYour Balance has
gone down and so penalty is calculated");
//Bank policy is to charge 20% on total
difference of balAmt and min_bal to be maintain
penalty = ((min_bal - checkamt) * 20) /
100;
balAmt[acno] = balAmt[acno] - (amt +
penalty);
System.out.println("Now your balance
revels : " + balAmt[acno] + "\n\n\n");
}
}
} catch (Exception e) {}
}

}
class Sav_acct //SAVING ACCOUNT CLASS
{
final int max_limit = 20;
final int min_limit = 1;
final double min_bal = 500;
private String name[] = new String[20];
private int accNo[] = new int[20];
private String accType[] = new String[20];
private double balAmt[] = new double[20];
static int totRec = 0;

//Intializing Method
public void initialize() {
for (int i = 0; i < max_limit; i++) {
name[i] = "";
accNo[i] = 0;
accType[i] = "";
balAmt[i] = 0.0;
}
}

GURPREET SINGH BEDI ROLL NO-56463


//TO ADD NEW RECORD
public void newEntry() {
String str;
int acno;
double amt;
boolean permit;
permit = true;

if (totRec > max_limit) {


System.out.println("\n\n\nSorry we cannot admit
you in our bank...\n\n\n");
permit = false;
}

if (permit = true) //Allows to create new entry


{
totRec++; // Incrementing Total Record

System.out.println("\n\n\n=====RECORDING NEW
ENTRY=====");
try {
accNo[totRec] = totRec; //Created AutoNumber
to accNo so no invalid id occurs
System.out.println("Account Number : " +
accNo[totRec]);

BufferedReader obj = new BufferedReader(new


InputStreamReader(System.in));
System.out.print("Enter Name : ");
System.out.flush();
name[totRec] = obj.readLine();

accType[totRec] = "Saving Account";


System.out.println("Account Type : " +
accType[totRec]);

do {
System.out.print("Enter Initial Amount
to be deposited : ");
System.out.flush();
str = obj.readLine();
balAmt[totRec] = Double.parseDouble(str);
} while (balAmt[totRec] < min_bal);
//Validation that minimun amount must be 500

System.out.println("\n\n\n");
} catch (Exception e) {}
}

GURPREET SINGH BEDI ROLL NO-56463


}
//TO DISPLAY DETAILS OF RECORD
public void display() {
String str;
int acno = 0;
boolean valid = true;

System.out.println("\n\n=====DISPLAYING DETAILS OF
CUSTOMER=====\n");
try {
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter Account number : ");
System.out.flush();
str = obj.readLine();
acno = Integer.parseInt(str);
if (acno < min_limit || acno > totRec) //To check
whether accNo is valid or Not
{
System.out.println("\n\n\nInvalid Account
Number \n\n");
valid = false;
}

if (valid == true) {
System.out.println("\n\nAccount Number : " +
accNo[acno]);
System.out.println("Name : " + name[acno]);
System.out.println("Account Type : " +
accType[acno]);

//Bank policy is to give 10% interest on Net


balance amt
balAmt[acno] = balAmt[acno] + (balAmt[acno] /
10);
System.out.println("Balance Amount : " +
balAmt[acno] + "\n\n\n");
}
} catch (Exception e) {}
}
//TO DEPOSIT AN AMOUNT
public void deposit() {
String str;
double amt;
int acno;
boolean valid = true;
System.out.println("\n\n\n=====DEPOSIT AMOUNT=====");

GURPREET SINGH BEDI ROLL NO-56463


try {
//Reading deposit value
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));

System.out.print("Enter Account No : ");


System.out.flush();
str = obj.readLine();
acno = Integer.parseInt(str);
if (acno < min_limit || acno > totRec) //To check
whether accNo is valid or Not
{
System.out.println("\n\n\nInvalid Account
Number \n\n");
valid = false;
}

if (valid == true) {
System.out.print("Enter Amount you want to
Deposit : ");
System.out.flush();
str = obj.readLine();
amt = Double.parseDouble(str);

balAmt[acno] = balAmt[acno] + amt;

//Displaying Depsit Details


System.out.println("\nAfter Updation...");
System.out.println("Account Number : " +
acno);
System.out.println("Balance Amount : " +
balAmt[acno] + "\n\n\n");
}
} catch (Exception e) {}
}

//TO WITHDRAW BALANCE


public void withdraw() {
String str;
double amt, checkamt;
int acno;
boolean valid = true;
System.out.println("\n\n\n=====WITHDRAW
AMOUNT=====");

try {
//Reading deposit value
BufferedReader obj = new BufferedReader(new

GURPREET SINGH BEDI ROLL NO-56463


InputStreamReader(System.in));

System.out.print("Enter Account No : ");


System.out.flush();
str = obj.readLine();
acno = Integer.parseInt(str);

if (acno < min_limit || acno > totRec) //To check


whether accNo is valid or Not
{
System.out.println("\n\n\nInvalid Account
Number \n\n");
valid = false;
}

if (valid == true) {
System.out.println("Balance is : " +
balAmt[acno]);
System.out.print("Enter Amount you want to
withdraw : ");
System.out.flush();
str = obj.readLine();
amt = Double.parseDouble(str);

checkamt = balAmt[acno] - amt;

if (checkamt >= min_bal) {


balAmt[acno] = checkamt;
//Displaying Depsit Details
System.out.println("\nAfter
Updation...");
System.out.println("Account Number : " +
acno);
System.out.println("Balance Amount : " +
balAmt[acno] + "\n\n\n");
} else {
System.out.println("\n\nAs per Bank Rule
you should maintain minimum balance of Rs 500\n\n\n");
}
}
} catch (Exception e) {}
}
}

class Account {
public static void main(String args[]) {
String str;
int choice, check_acct = 1, quit = 0;

GURPREET SINGH BEDI ROLL NO-56463


choice = 0;

Curr_acct curr_obj = new Curr_acct();


Sav_acct sav_obj = new Sav_acct();

System.out.println("\n=====WELLCOME TO BANK DEMO


PROJECT=====\n");
while (quit != 1) {
try {
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Type 1 for Current Account
and Any no for Saving Account : ");
System.out.flush();
str = obj.readLine();
check_acct = Integer.parseInt(str);
} catch (Exception e) {}

if (check_acct == 1) {
do //For Current Account
{
System.out.println("\n\nChoose Your
Choices ...");
System.out.println("1) New Record Entry
");
System.out.println("2) Display Record
Details ");
System.out.println("3) Deposit...");
System.out.println("4) Withdraw...");
System.out.println("5) Quit");
System.out.print("Enter your choice :
");
System.out.flush();
try {
BufferedReader obj = new
BufferedReader(new InputStreamReader(System.in));
str = obj.readLine();
choice = Integer.parseInt(str);

switch (choice) {
case 1: //New Record Entry
curr_obj.newEntry();
break;
case 2: //Displaying Record
Details
curr_obj.display();
break;
case 3: //Deposit...

GURPREET SINGH BEDI ROLL NO-56463


curr_obj.deposit();
break;
case 4: //Withdraw...
curr_obj.withdraw();
break;
case 5:

System.out.println("\n\n.....Closing Current Account.....");


break;
default:
System.out.println("\nInvalid
Choice \n\n");
}
} catch (Exception e) {}
} while (choice != 5);
} else {
do //For Saving Account
{
System.out.println("Choose Your Choices
...");
System.out.println("1) New Record Entry
");
System.out.println("2) Display Record
Details ");
System.out.println("3) Deposit...");
System.out.println("4) Withdraw...");
System.out.println("5) Quit");
System.out.print("Enter your choice :
");
System.out.flush();
try {
BufferedReader obj = new
BufferedReader(new InputStreamReader(System.in));
str = obj.readLine();
choice = Integer.parseInt(str);

switch (choice) {
case 1: //New Record Entry
sav_obj.newEntry();
break;
case 2: //Displaying Record
Details
sav_obj.display();
break;
case 3: //Deposit...
sav_obj.deposit();
break;
case 4: //Withdraw...

GURPREET SINGH BEDI ROLL NO-56463


sav_obj.withdraw();
break;
case 5:

System.out.println("\n\n.....Closing Saving Account.....");


break;
default:
System.out.println("\nInvalid
Choice \n\n");
}
} catch (Exception e) {}
} while (choice != 5);
}

try {
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("\nEnter 1 for Exit : ");
System.out.flush();
str = obj.readLine();
quit = Integer.parseInt(str);
} catch (Exception e) {}
}
}
}

Q9 Write a java program to show usage of exceptions like


ArithmeticException, ArrayIndexOutOfBoundException,
NumberFormatException.

class Exception_Demo
{
public static void main(String args[])
{
try {
int a = 30, b = 0;
int c = a/b; // cannot divide by zero
System.out.println ("Result = " + c);

}
catch(ArithmeticException e) {
System.out.println ("Can't divide a number by
0");

GURPREET SINGH BEDI ROLL NO-56463


}
try{
int a[] = new int[5];
a[6] = 9; // accessing 7th element in an
array of
// size 5
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out
Of Bounds");
}
try {
// "akki" is not a number
int num = Integer.parseInt ("akki") ;

System.out.println(num);
} catch(NumberFormatException e) {
System.out.println("Number format
exception");
}
}
}

Output:
Can't divide a number by 0

GURPREET SINGH BEDI ROLL NO-56463


Q10 Write an applet to draw a square inside a circle and circle inside a square.

import java.awt.*;
import java.applet.*;
/*
*/
public class Shapes extends Applet
{
public void paint(Graphics g)
{
/*Squar Inside A Circle*/
g.drawString("(c).Square Inside A
Circle",150,110);
g.drawOval(180,10,80,80);
g.drawRect(192,22,55,55);
/*Circle Inside a Squar*/
g.drawString("(d).Circle Inside a
Squar",290,110);
g.drawRect(290,10,80,80);
g.drawOval(290,10,80,80);
}
}

GURPREET SINGH BEDI ROLL NO-56463

You might also like