0% found this document useful (0 votes)
14 views7 pages

PF Assignment 3

The document contains example code for methods to check if an integer list is sorted, check if an integer array contains four consecutive equal numbers, and merge two sorted integer lists. It provides sample inputs and outputs for each method.

Uploaded by

Waleed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

PF Assignment 3

The document contains example code for methods to check if an integer list is sorted, check if an integer array contains four consecutive equal numbers, and merge two sorted integer lists. It provides sample inputs and outputs for each method.

Uploaded by

Waleed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

QUESTION 3:

Write the following method that returns true if the list is already sorted in increasing order.

public static boolean isSorted(int[] list)

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.

ANSWER:

import java.util.Scanner;

public class QUESTION 3 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter the numbers in array ");

int[] list = new int [10];

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

list[i]=input.nextInt();

int temp;

if (isSorted(list))

System.out.println("yes it is already sorted");

else

System.out.println("no it is not sorted");{

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

for (int j=i+1;j<10;j++)

if (list[i]>list[j]){

temp=list[i];

list[i]=list[j];

list[j]=temp;}

}}

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


{

System.out.println(list[i]) ;

public static boolean isSorted(int[] list){

boolean issorted=true;

for (int i=0;i<list.length-1;i++)

if (list[i]>list[i+1])

issorted=false;

break;

return issorted;

Output:

QUESTION 4
Write the following method that tests whether the array has four consecutive numbers with the
same value. public static boolean isConsecutiveFour(int[] values) 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.

ANSWER:

import java.util.Scanner;

public class Question4 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the array= ");

int size = scanner.nextInt();

int[] array = new int[size];

System.out.print("Enter the elements of the array= ");

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

array[i] = scanner.nextInt();

if (isConsecutiveFour(array)) {

System.out.println("The array contains four consecutive numbers with the same value.");

} else {

System.out.println("The array does not contain four consecutive numbers with the same
value.");

public static boolean isConsecutiveFour(int[] array) {

for (int i = 0; i <= array.length - 4; i++) {

if (array[i] == array[i + 1] &&

array[i] == array[i + 2] &&

array[i] == array[i + 3]) {

return true;
}

return false;

QUESTION 5:

Write the following method that merges two sorted lists into a new sorted list. Implement the
method in a way that takes at most list1.length + list2. length comparisons. Write a test program that
prompts the user to enter two sorted lists and displays the merged list. Here is a sample run. Note
that the first number in the input indicates the number of the elements in the list. This number is not
part of the list.

ANSWER:

import java.util.Scanner;

public class Mearge{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the first list: ");

int size1 = scanner.nextInt();

int[] list1 = new int[size1];

System.out.print("Enter the elements of the first sorted list: ");

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

list1[i] = scanner.nextInt();

System.out.print("Enter the size of the second list: ");

int size2 = scanner.nextInt();

int[] list2 = new int[size2];

System.out.print("Enter the elements of the second sorted list: ");


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

list2[i] = scanner.nextInt();

int[] mergedList = mergeSortedLists(list1, list2);

System.out.print("Merged Sorted List: ");

for (int value : mergedList) {

System.out.print(value + " ");

public static int[] mergeSortedLists(int[] list1, int[] list2) {

int[] mergedList = new int[list1.length + list2.length];

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

mergedList[i]=list1[i];}

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

mergedList[list1.length + i]=list2[i];}

int temp;

for (int i=0;i<mergedList.length-1;i++)

for (int j=i+1;j<mergedList.length;j++)

if (mergedList[i]>mergedList[j]){

temp=mergedList[i];

mergedList[i]=mergedList[j];

mergedList[j]=temp;}

}
return mergedList;

OUTPUT:

You might also like