You are on page 1of 19

Java Assignment

Name :Vaishnavi Rajesh Nagarkar

PRN :10303320181124510060

Q1.Write a program to input n numbers on command line argument and


calculate maximum of them

Ans:

public class Max{

public static void main(String[] args) {

if (args.length == 0){

System.out.println("You must enter at least 1 integer. Please try again.");

System.exit(0);

int totalInput = args.length;

int commandLineNumber[] = new int[totalInput];

//convert string into integer.

for(int i = 0; i < totalInput; i++){

commandLineNumber[i] = Integer.parseInt(args[i]);

//find maximum number


int maxNumber = commandLineNumber[0];

for(int i = 1; i < totalInput; i++){

if(commandLineNumber[i] > maxNumber){

maxNumber = commandLineNumber[i];

System.out.println("Maximum number is "+maxNumber);

Output :
$javac Max.java

$java Max 5 6 7 8 20

Maximum number is 20

Q2.Write a program to print the sum and average of the even and odd numbers
of first ten natural numbers.

Ans:

//program to print the sum and average of the even and odd numbers of first ten
natural numbers.
import java.io.*;

import java.util.*;

public class SumAverage {

static void sum_avg_of_even_num(int n)

// sum of first n even numbers

int sum = 0;

for (int i = 1; i <= n; i++)

sum += 2*i;

// calculating Average

System.out.println("Sum of first " +n+ " even natural numbers is " +sum);

System.out.println("Average of first " +n+ " even natural numbars is " +(sum
/ n));

static void sum_avg_of_odd_num(int n)

// sum of first n odd numbers

int sum = 0;

for (int i = 0; i < n; i++)

sum += ( 1 + (2*i));
// calculating Average

System.out.println("Sum of first " +n+" odd natural numbers is " +sum);

System.out.println("Average of first " +n+ " odd natural numbars is " + (sum
/ n));

public static void main (String[] args) {

int n = 10;

sum_avg_of_even_num(n);

sum_avg_of_odd_num(n);

Output :
Sum of first 10 even natural numbers is 110

Average of first 10 even natural numbars is 11

Sum of first 10 odd natural numbers is 100

Average of first 10 odd natural numbars is 10

Q3.Design a class for a bank database the database should support the following operations.
1. Deposit a certain amount into an account,
2. Withdrawing a certain amount from an account,
3. Return a value specifying the amount (i.e. balance) in an amount.

Ans :

import java.util.*;

import java.text.DecimalFormat;

public class BankAccount {

public static void main(String[] args) {

double startBalance = 0;

double deposite;

double withdraw;

double totalwithdraw = 0;

double totaldeposite = 0;

int count =0;

Scanner key = new Scanner(System.in);

System.out.println("Please enter the starting balance");

startBalance=key.nextDouble();

System.out.println("Please enter number of transaction you wnat to do");


int b=key.nextInt();

while(count < b){

System.out.println("Enter the method number to execute. \n 1.View Balance\n


2.Deposite \n 3.Withdraw \n");

int a=key.nextInt();

switch(a)

case 1:

System.out.println("Account Balance Rs."+startBalance);

break;

case 2:

System.out.println("Please enter the amount to be withdraw");

withdraw=key.nextDouble();

if(withdraw > startBalance){

System.out.println("\nThere is less amount in balance than withdraw amount\n You


are not able to withdraw");

else{

totalwithdraw += withdraw;

System.out.println("Total withdraws Rs."+totalwithdraw);

startBalance -= withdraw;
System.out.println("Account Balance Rs."+startBalance);

break;

case 3:

System.out.println("Please enter the amount deposited");

deposite = key.nextDouble();

totaldeposite += deposite;

startBalance += deposite;

System.out.println("Total deposits Rs."+totaldeposite);

System.out.println("Account Balance Rs."+startBalance);

break;

default:

System.out.println("Method not found");

break;

count += 1;

}
Output :

Please enter the starting balance

400

Please enter number of transaction you want to do

Enter the method number to execute.

1.View Balance

2.Deposite

3.Withdraw

Please enter the amount to be withdraw

50

Total withdraws Rs.50.0

Account Balance Rs.350.0

Enter the method number to execute.

1.View Balance

2.Deposite

3.Withdraw

Please enter the amount deposited

600
Total deposits Rs.600.0

Account Balance Rs.950.0

Q4.write a program to perform operations on given string


1.convert any string into upper case to lower case and vice versa.
2.calculate length of a string
3.match the substring
4.find out location of certain letter.
5.compare two strings
6.concate any two strings.

Ans :

import java.util.Scanner;

import java.io.*;

public class TwoString

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

String[] input = new String[2];

String a, b ,c;

int temp;

System.out.print("Please enter two Strings: ");


input = sc.nextLine().split(" ");

a= input[0];

b= input[1];

System.out.println("You input: " + a + " and " + b);

//convert any string into upper case to lower case and vice versa

System.out.println("Lowercase of " + a + " is " +a.toLowerCase() + " and Lowercase of " +


b + " is " + b.toLowerCase());

System.out.println("Uppercase of " + a + " is " +a.toUpperCase() + " and Uppercase of " +


b + " is " + b.toUpperCase() + "\n\n");

//calculate length of a string

System.out.println("String length is "+a.length());

System.out.println("String length is "+b.length()+"\n\n");

//find out location of certain letter.

System.out.println("Enter Substring: ");

c=sc.nextLine();

System.out.println("Position of substring " + c + " in string " + a + " is " +a.indexOf(c) +


"\n\n");

//match the substring

int n = a.length();

int m = c.length();

int index=a.indexOf(c), i=index+1, count=(index>=0)?1:0;


while(index!=-1 && i<=(n-m))

index=a.substring(i, n).indexOf(c);

count=(index>=0)?count+1:count;

i=i+index+1;

System.out.println("Total Number Of Substring "+ c +" Appears: "+count+ "\n\n");

//compare two strings

temp=a.compareTo(b);

if(temp==0)

System.out.println("The Strings are Equal\n ");

else if(temp>0)

System.out.println("The String "+b+" is greater than the string "+a + "\n\n");

else

System.out.println("The String "+a+" is greater than the string "+b + "\n\n");

//concate any two strings.

System.out.println("Entered strings after concatenation is " +a.concat(b));

}
Output :
Please enter two Strings:vaishnavi nagarkar

You input: vaishnavi and nagarkar

Lowercase of vaishnavi is vaishnavi and Lowercase of nagarkar is nagarkar

Uppercase of vaishnavi is VAISHNAVI and Uppercase of nagarkar is NAGARKAR

String length is 9

String length is 8

Enter Substring: ai

Position of substring ai in string vaishnavi is 2

Total Number Of Substring ai Appears: 1

The String vaishnavi is greater than the string nagarkar

Entered strings after concatenation is vaishnavinagarkar


Q5.write a program to perform array addition and matrix multiplication.

Ans:

import java.util.Scanner;

public class Matrix

public static void main(String args[])

Scanner in = new Scanner(System.in);

int m, n, c, d, a, p, q, k;

System.out.println("Choose the Operation You want to do on Matrix:\n 1.Addition \n


2.Multiplication");

a = in.nextInt();

switch(a){

case 1:

System.out.println("Enter the number of rows and columns of matrix");

m = in.nextInt();

n = in.nextInt();
int first[][] = new int[m][n];

int second[][] = new int[m][n];

int add[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

first[c][d] = in.nextInt();

System.out.println("Enter the elements of second matrix");

for (c = 0 ; c < m; c++)

for (d = 0 ; d < n; d++)

second[c][d] = in.nextInt();

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

add[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices

System.out.println("Sum of the matrices:");

for (c = 0; c < m; c++)

{
for (d = 0; d < n; d++)

System.out.print(add[c][d] + "\t");

System.out.println();

break;

case 2:

int sum = 0;

System.out.println("Enter the number of rows and columns of first matrix");

m = in.nextInt();

n = in.nextInt();

int first_m[][] = new int[m][n];

System.out.println("Enter elements of first matrix");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

first_m[c][d] = in.nextInt();

System.out.println("Enter the number of rows and columns of second matrix");

p = in.nextInt();
q = in.nextInt();

if (n != p)

System.out.println("The matrices can't be multiplied with each other.");

else

int second_m[][] = new int[p][q];

int multiply[][] = new int[m][q];

System.out.println("Enter elements of second matrix");

for (c = 0; c < p; c++)

for (d = 0; d < q; d++)

second_m[c][d] = in.nextInt();

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++) {

for (k = 0; k < p; k++)

sum = sum + first_m[c][k]*second_m[k][d];

multiply[c][d] = sum;

sum = 0;

}
System.out.println("Product of the matrices:");

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++)

System.out.print(multiply[c][d]+"\t");

System.out.print("\n");

break;

default :

System.out.println("Invalid Operation:");

Output :
(base) ┌─[untold@parrot]─[~/Desktop/Assignment]

└──╼ $javac Matrix.java

(base) ┌─[untold@parrot]─[~/Desktop/Assignment]
└──╼ $java Matrix

Choose the Operation You want to do on Matrix:

1.Addition

2.Multiplication

Enter the number of rows and columns of matrix

2 2

Enter the elements of first matrix

1 0

0 1

Enter the elements of second matrix

1 2

2 1

Sum of the matrices:

2 2

2 2

Choose the Operation You want to do on Matrix:

1.Addition

2.Multiplication

Enter the number of rows and columns of first matrix

22

Enter elements of first matrix


12

45

Enter the number of rows and columns of second matrix

22

Enter elements of second matrix

13

46

Product of the matrices:

9 15

24 42

You might also like