Assignment 3
Assignment 3
FUNDAMENTALS
Reg.no. FA23-BSE-015
Assignment 3
Date 11-5-24
Moderator:
SIR MUZAFFAR IQBAL
Fa23-bse-015
ASSIGNMENT 3:
QUESTION 1:
Write a program that reads in ten numbers and displays the number of
distinct numbers and the distinct numbers separated by exactly one space
(i.e., if a number appears multiple times, it is displayed only once). (Hint:
Read a number and store it to an array if it is new. If the number is
already in the array, ignore it.) After the input, the array contains the
distinct numbers.
CODE:
package assignment3;
import java.util.Scanner;
int counter=0;
for(int i=0;i<10;i++){
int num=input.nextInt();
if(!contain(n,num)){
n[counter]=num;
2
Fa23-bse-015
counter++;
for(int i=0;i<counter;i++){
if (array[i]==num){
return true;
return false;
3
Fa23-bse-015
OUTPUT:
4
Fa23-bse-015
QUESTION 2:
Write a method that returns a new array by eliminating the duplicate
values in the array using the following method header:
Write a test program that reads in ten integers, invokes the method, and
displays the result.
CODE:
package assignment3;
import java.util.Scanner;
int counter=0;
for(int i=0;i<10;i++){
int num=input.nextInt();
if(!contains(n,num)){
n[counter]=num;
counter++;
5
Fa23-bse-015
for(int i=0;i<counter;i++){
for(int i=0;i<array.length;i++){
if(array[i]==num){
return true;
return false;
6
Fa23-bse-015
OUTPUT:
7
Fa23-bse-015
QUESTION 3:
Write the following method that returns true if the list is already sorted in
increasing order.
Write a test program that prompts the user to enter a list and displays
whether the list is sorted or not. If the list is not in sorted order then sort
the list and display.
CODE:
import java.util.Arrays;
import java.util.Scanner;
for(int i=0;i<n.length;i++){
n[i]=input.nextInt();
if(!isSorted(n)){
Arrays.sort(n);
display(n);
8
Fa23-bse-015
else {
for(int i=0;i<list.length-1;i++){
for(int j=i+1;j<list.length;j++){
if(list[i]>list[j]){
return false;
return true;
for(int x:array)
9
Fa23-bse-015
OUTPUT:
10
Fa23-bse-015
QUESTION 4:
Write the following method that tests whether the array has four
consecutive numbers with the same value.
Write a test program that prompts the user to enter a series of integers
and dis plays if the series contains four consecutive numbers with the
same value. Your program should first prompt the user to enter the input
size—i.e., the number of values in the series
CODE:
package assignment2;
import java.util.Scanner;
int size=input.nextInt();
for(int i=0;i<n.length;i++){
n[i]=input.nextInt();
if(size>=4){
if(isConsecutiveFour(n)){
11
Fa23-bse-015
else{
else
for(int i=0;i<values.length;i++){
return true;
return false;
OUTPUT:
12
Fa23-bse-015
QUESTION 5:
Write the following method that merges two sorted lists into a new sorted
list.
Write a test program that prompts the user to enter two sorted lists and
displays the merged list.
Note that the first number in the input indicates the number of the
elements in the list.
CODE:
package assignment3;
13
Fa23-bse-015
import java.util.Arrays;
import java.util.Scanner;
int s1=input.nextInt();
for(int i=0;i<n1.length;i++){
n1[i]=input.nextInt();
int s2=input.nextInt();
for(int i=0;i<n2.length;i++){
n2[i]=input.nextInt();
14
Fa23-bse-015
int[] merged=isSortedMerged(n1,n2);
int[] m=sortedList(merged);
for(int x:m){
int i = 0, j = 0, k = 0;
mergedList[k++] = arr1[i++];
} else {
mergedList[k++] = arr2[j++];
mergedList[k++] = arr1[i++];
15
Fa23-bse-015
mergedList[k++] = arr2[j++];
mergedList=sortedList(mergedList);
return mergedList;
for(int j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
16
Fa23-bse-015
return arr;
OUTPUT:
QUESTION 6:
CODE:
package assignment3;
import java.util.Scanner;
17
Fa23-bse-015
int r1 = input.nextInt();
int c1 = input.nextInt();
int r2 = input.nextInt();
int c2 = input.nextInt();
if (c1 != r2) {
} else {
enterMatrixElements(input, matrix1);
18
Fa23-bse-015
enterMatrixElements(input, matrix2);
displayMatrix(result);
input.close();
matrix[i][j] = input.nextInt();
int r1 = matrix1.length;
int c1 = matrix1[0].length;
19
Fa23-bse-015
int c2 = matrix2[0].length;
return result;
System.out.print(num + "\t");
System.out.println();
20
Fa23-bse-015
OUTPUT:
21
Fa23-bse-015
QUESTION 7:
Write a program that prompts the user to enter the length of a square
matrix, randomly fills in 0s and 1s into the matrix, prints the matrix, and
finds the rows, columns, and diagonals with all 0s or 1s.
CODE:
package assignment3;
import java.util.Random;
import java.util.Scanner;
matrix[i][j] = random.nextInt(2);
22
Fa23-bse-015
display(matrix);
checkRowsColumnsDiagonals(matrix);
System.out.println();
if (matrix[i][j] != 0) {
allZeros = false;
23
Fa23-bse-015
if (matrix[i][j] != 1) {
allOnes = false;
if (allZeros) {
if (allOnes) {
if (matrix[i][j] != 0) {
allZeros = false;
if (matrix[i][j] != 1) {
allOnes = false;
24
Fa23-bse-015
if (allZeros) {
if (allOnes) {
if (matrix[i][i] != 0) {
allZerosDiagonal1 = false;
if (matrix[i][i] != 1) {
allOnesDiagonal1 = false;
if (matrix[i][length - i - 1] != 0) {
25
Fa23-bse-015
allZerosDiagonal2 = false;
if (matrix[i][length - i - 1] != 1) {
allOnesDiagonal2 = false;
if (allZerosDiagonal1) {
if (allOnesDiagonal1) {
if (allZerosDiagonal2) {
if (allOnesDiagonal2) {
26
Fa23-bse-015
OUTPUT:
QUESTION 8:
Write a Java program to accept roll numbers, names and marks of 10
students. Sort this data on the basis of marks in descending order such
that data of student having maximum marks should come at the top and
the data of student having minimum marks should come at the bottom.
27
Fa23-bse-015
CODE:
package assignment3;
import java.util.Scanner;
for(int i=0;i<5;i++){
rollNum[i]=input.nextInt();
names[i]=input.next();
marks[i]=input.nextDouble();
descendingSort(rollNum,names,marks);
for(int i=0;i<5;i++){
28
Fa23-bse-015
for(int i=0;i<4;i++){
for(int j=i+1;j<5;j++){
if(z[i]<z[j]){
double temp=z[i];
z[i]=z[j];
z[j]=temp;
int temp1=x[i];
x[i]=x[j];
x[j]=temp1;
String temp2=y[i];
y[i]=y[j];
y[j]=temp2;
29
Fa23-bse-015
OUTPUT:
30
Fa23-bse-015
QUESTION 9:
Write a Menu Driven JAVA program that creates one-dimensional array
arr[] and initialize it with user. The program should do following Tasks
using Menu, the menu operations are implemented using methods:
2. Write a method partition(), that take the first element of the array x and
put x in a position such that all smaller elements (smaller than x) are
before x, and put all greater elements (greater than x) after x.
CODE:
package assignment3;
import java.util.Scanner;
31
Fa23-bse-015
int size=input.nextInt();
int option=input.nextInt();
for(int i=0;i<size;i++){
array[i]=input.nextInt();
switch(option){
case 1:{
int num=input.nextInt();
break;
case 2:{
32
Fa23-bse-015
int pivot=input.nextInt();
partition(array,pivot);
break;
case 3:{
duplicates(array);
break;
case 4:{
circular(array);
break;
case 5:{
shiftCircular(array);
break;
default:
System.out.println("invalid choice");
33
Fa23-bse-015
int counter=0;
for(int i=0;i<arr.length;i++){
if(arr[i]==num){
counter++;
return counter;
int x = arr[0];
int i = 1;
if (arr[j] <= x) {
i++;
swap(arr, i - 1, j);
swap(arr, 0, i - 1);
34
Fa23-bse-015
arr[i] = arr[j];
arr[j] = temp;
freq[i] = 1;
if (arr[i] == arr[j]) {
freq[i]++;
arr[j] = Integer.MAX_VALUE;
System.out.println("Frequencies of elements:");
if (arr[i] != Integer.MAX_VALUE) {
35
Fa23-bse-015
duplicate = arr[i];
} else {
arr[arr.length - 2] = temp1;
arr[arr.length - 1] = temp2;
if (duplicate != -1) {
36
Fa23-bse-015
arr[arr.length - 2] = temp1;
arr[arr.length - 1] = temp2;
OUTPUT:
37
Fa23-bse-015
38
Fa23-bse-015
39