CORRECTED 3rd Time

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

Question 1

Write a program to declare a square matrix A[ ][ ] of order


‘n’. Allow the user to input positive
integers into this matrix. Perform the following tasks on the
matrix:
(i) Output the original matrix.
(ii) Find the SADDLE POINT for the matrix. If the matrix has
no saddle point,

output the message “NO SADDLE POINT”.

[Note: A saddle point is an element of the matrix such that it


is the minimum element for the
row to which it belongs and the maximum element for the
column to which it belongs. Saddle
point for a given matrix is always unique.]
Example: In the Matrix
456
789
513

Algorithm

Step 1 - Start
Step 2 –Take a Square matrix a[][] of order n
Step 3 - Accept variables r and c as number of rows and
columns
Step 4 – Take the elements in the array
Step 5 – Find the minimum of a row and return the index
position
Step 6 - Display the saddle point
Step 7 - Create a main function
Step 8 – Create an object and calling the parent class method
Step 9 – Stop

Program

VARIABLE DESCRIPTION
Variable Datatype Description
a[][] int To store the array
elements
r int To enter the
number of rows
c int To store the
number of
columns
a[][] int Array to store the
elements
min int To store the
minimum value
max int To store the
maximum value
p int To store loop
variable (i)
i int Loop variable
j int Loop variable
mp int To store the value
of min.

OUTPUT
Question 2

WRITE A PROGRAM IN Java to fill a square matrix of size


“n*n” in a circular fashion(clockwise) with natural numbers
from 1 to n*n, taking ‘n’ as input.
For example : if n=4, then n*n=16. Hence the array will be
filled as given below.

Algorithm

Step 1 - Start
Step 2 - Taking a square matrix a[][] of size n
Step 3 - Initialize value to be filled in matrix
Step 4 - accepting k , m , l and n
Step 5 - Print the first row from the remaining rows
Step 6 - Print the last column from the remaining column
Step 7 - Print the first column from the remaining columns
Step 8 - Driver program to test above function
Step 9 - Stop

Program
import java.util.*;
public class Spiral
{
void spiralFill(int m, int n, int a[][])
{
// Initialize value to be filled in matrix
int val = 1;

/* k - starting row index


m - ending row index
l - starting column index
n - ending column index */
int k = 0, l = 0;
while (k < m && l < n)
{
/* Print the first row from the remaining
rows */
for (int i = l; i < n; i++)
a[k][i] = val++;

k++;

/* Print the last column from the remaining


columns */
for (int i = k; i < m; ++i)
a[i][n-1] = val++;
n--;

/* Print the last row from the remaining


rows */
if (k < m)
{
for (int i = n-1; i >= l; --i)
a[m-1][i] = val++;
m--;
}

/* Print the first column from the remaining


columns */
if (l < n)
{
for (int i = m-1; i >= k; --i)
a[i][l] = val++;
l++;
}
}
}
/* Driver program to test above functions */
public static void main()
{
Scanner in= new Scanner(System.in);
System.out.println("Enter the number of rowns (n) :");
int n=in.nextInt();
System.out.println();
int m=n;
int a[][]=new int[m][n];
Spiral ob=new Spiral();
ob.spiralFill(m, n, a);
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
VARIABLE DESCRIPTION
Variable Data Type Description
val int To initialize value
to be filled in the
matrix.
k int Starting row index
m int Ending row index
n int Ending column
index
I int Starting column
index
a[][] int To store the array
elements
i int As loop variable

OUTPUT
Question 3

A class Collection contains an array of 100 integers. Using


the following class description, create an array with common
elements from two integer arrays. The members of the class
are given below:
Class name: Collection
Data members/ Instance variables:
arr[] : integer array
len : length of array
Member functions:
Collection() : default constructor
Collection(int) : parameterized constructor to assign the
length of the array.
void inparr() : to accept the array elements.
Collection common(Collection) : returns a Collection
containing the common elements of current Collection object
and the Collection object passed as a parameter.
void arrange() : sort the array element of the object
containing common elements in ascending order using any
sorting technique.
void display() : displays the array element.
Specify the class Collection giving the details of the
constructors, void inparr() and void arrange(), Collection
common(Collection). You need to write the main().

Algorithm

Step 1- Start
Step 2- Accepting an integer array arr[] and length of an
array len
Step 3- Accepting the memeber methods
Step 4- Accepting an integer element l as parameterized
constructor to assign
the length of an array
Step 5- A collection containing the common elements of
current collection
object and the collection object passed as a parameter
Step 6- Sorting the array elements of the object containing
common elements is
ascending order
Step 7- Displaying the array elements
Step 8- A main function is created
Step 9- Two objects c1 and c2 has been created for calling
the member methods
from the parent class.
Step 10- Stop
Program

import java.util.*;
public class Collection
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[100];
int len;
public Collection()
{
len=0;
for(int i=0;i<100;i++)
arr[i]=0;
}
public Collection(int l)
{
len=l;
for(int i=0;i<len;i++)
arr[i]=0;
}
public void inpar()
{
for(int i=0;i<len;i++)
{
System.out.println("Enter the element:");
arr[i]=sc.nextInt();
}
}
Collection common(Collection c)
{
Collection c1=new Collection(len);
int i,j,f,k=0;
for(i=0;i<len;i++)
{
f=0;
for(j=0;j<c.len;j++)
{
if(arr[i]==c.arr[j])
f=1;
}
if(f==1)
{
c1.arr[k]=arr[i];
k++;
}
}
c1.len=k;
return c1;
}
public void arrange()
{
int i,j,t;
for(i=0;i<len;i++)
{
for(j=0;j<len-i-1;j++)
{
if(arr[j]>arr[j+1])
{
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
}
public void display()
{
System.out.println("The elements:");
for(int i=0;i<len;i++)
{
System.out.print(arr[i]+" ");
}
}
public static void main()
{
Collection c1=new Collection(5);
Collection c2=new Collection(4);
Collection c3=new Collection(4);
System.out.println("First array:");
c1.inpar();
System.out.println("Second array:");
c2.inpar();
c3=c1.common(c2);
System.out.println("\nCommon elelments:");
c3.display();
System.out.println("\nCommon elelments in sorted
order:");
c3.arrange();
c3.display();
}
}
Output-

(MISSING BECAUSE ERROR IN PROGRAM)

Varaiable description
Variable Datatype Purpose
len int To accept Length
Arr[][] int Array element
I int Loop variable
j int Loop variable
f int To check the
variable
k int To show length of
c1
t int To assign the
array

Question 4

A class Distance is designed to calculate the sum of two


distances. The description of the
class is as follows:
Class name: Distance
Data members/ instance variables:
M :to store the distance in meters.
N :to store the distance in centimeter
Member functions/methods:
Distance( double X, double Y) : parameterized constructor to
initialize the X to M and Y to N.
void display( ) : to print the distance in the given format
12km 56m.
Distance Add_Dis( Distance a, Distance b) : to add the
distances passed as object and return the
result.

Specify the class Distance with the details of the data


members and member functions. Also write
the main( ) and create the object to call the methods
accordingly to fulfil the above task.

Algorithm
Step 1 - Start
Step 2 - Accept the distances as kilometer and meter
Step 3 - Assign kilometer into x and meter in y
Step 4 - Take input as distance a and b
Step 5 - Now calculate the distance.
Step 6 - Check conditions of dN
Step 7- Enter distance
Step 8 - Calculate d1,d2,d3
Step 9 - Print d3 and sum distance
Step 10 - Stop

Program
import java.util.*;
public class Distance
{
double M,N;
public Distance(double x,double y)
{
M=x;
N=y;
}
public void display()
{
System.out.println(M+"km"+" "+N+"m");
}
public Distance Add_Dis(Distance a,Distance b)
{
Distance d=new Distance(0,0);
d.M=a.M+b.M;
d.N=a.N+b.N;
if(d.N>=1000)
{
d.M+=1;
d.N-=1000;
}
return d;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
double k,m,k1,m1;
System.out.println("Enter first distance");
k=sc.nextDouble();
m=sc.nextDouble();
System.out.println("Enter 2nd distance:");
k1=sc.nextDouble();
m1=sc.nextDouble();
Distance d1=new Distance(k,m);
Distance d2=new Distance(k1,m1);
Distance d3=new Distance(0,0);
d3=d1.Add_Dis(d1,d2);
System.out.println("The sum distance:");
d3.display();
}
}

Variable Description

Data Type Purpose


Variable
x double To input the
parameterized
constructor to
initialize the
kilometer to x
y double To input the
parameterized
constructor to
initialize the meter
to n
M double To store the
distance in
kilometers.
N double To store the
distance in meters.
km double Distance M is
taken as km.
m double Distance N is taken
as m.
a distance Parameter
variable
b distance Parameter
variable
d distance Parameter
variable
k double To store 1st
distance in km
m double To store 1st
distance in m
k1 double To store 2st
distance in km
m1 double To store 2st
distance in m
d1 distance To display
distance 1
d2 distance To display
distance 2
d3 distance To display
distance 3

Output
Question 5

Using the concept of inheritance. The superclass, subclass


and main function need to be written.
Algorithm
Step 1 - Start
Step 2 - accept variables name , code and amount
Step 3 - assign n into name, c into code, and amount into p
Step 4 - Print name of the product
Step 5 - Print product code
Step 6 - Print the amount of the product
Step 7 - Subclass Sales has been created
Step 8 - Parameterized constructor to assign values to data
members of both the classes
Step 9 - Calculate the total amount with tax and fine
Step 10 – Stop

Program

import java. util.*;


import java.io. *;
class Bank
{
String name; long accno;double p;
Bank(String n, long ac, double ammt)
{
name=n;
accno=ac;
p=ammt;
}

public void display()


{
System.out.println("Name of the customer:"+name);
System.out.println("Account number:"+accno);
System.out.println("Principal Amount:"+p);
}

}
public class Account extends Bank
{
double am;
Account(String n, long ac, double ammt, double amt)
{
super(n,ac,ammt);
amt=am;
}

void deposit()throws IOException


{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the amount:");
am=Double.parseDouble(br.readLine());
p=p+am;
}

void withdraw()throws IOException


{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the amount:");
am=Double.parseDouble(br.readLine());
p=p-am;
}

public static void main()throws IOException


{
Scanner sc=new Scanner(System.in);
String n;
long acno;
double a;
System.out.print("Enter name:");
n=sc.nextLine();
System.out.print("Enter account number:");
acno=sc.nextLong();
System.out.print("Enter balance amount:");
a=sc.nextDouble();
Account a1=new Account(n,acno,a,0);
System.out.print("Deposit :");
a1.deposit();
a1.display();
System.out.print("Withdraw :");
a1.withdraw();
a1.display();
}
}

Output
Variable description

Variable Datatype Description


n String To assign
name and
enter name
ac Long To assign acc
and enter acc
no
Ammt Double To assign p
am double To assign amt
p int To calculate p
– am
a double To enter
balance.

Question6
A superclass Product has been defined to store the details of
a product sold by a wholesaler to a retailer. Define a subclass
Sales to compute the total amount paid by the retailer with
or without fine along with service tax. Some of the members
of both classes are given below:

Algorithm

Step 1 - Start
Step 2 - accpet variables name , code and amount
Step 3 - assign n into name , c into code and amount into p
Step 4 - Print name of the product
Step 5 - Print product code
Step 6 - Print amount of the product
Step 7 - Subclass Sales has been created
Step 8 - Parameterized constructor to assign values to data
members of both the classes
Step 9 - Calculate the total amount with tax and fine
Step 10 - Stop

Program

import java.util.*;
class Product
{
String name; int code;double amount;
Product( String n,int c, double p)
{
name=n;
code=c;
amount=p;
}

void show()
{
System.out.println("Name of the product:"+name);
System.out.println("Product Code:"+code);
System.out.println("Total sale amount of the
product:"+amount);
}
}
class Sales extends Product
{
int day;double tax;double totamt;double fine;
Sales(String n,int c, double p,int d,double ta,double tot)
{
super(n,c,p);
day=d;
tax=ta;
totamt=tot;
}

void compute()
{
tax=12.4/100.0*amount;
if(day>30)
fine=2.5/100.0*amount;
totamt=amount+tax+fine;
}

void show()
{
super.show();
System.out.println("Total amount to be paid:"+totamt);
}
}

Output
(missing because error in the program)
Variable description

Variable Datatype Description


n String To assign name
c Int To assign code
P double To assign amount
d double To assign day and
check conditions
ta double To assign tax and
calculate
tot double To assign totamt
and the sum
totamt double Display the total
amount

You might also like