0% found this document useful (0 votes)
3 views2 pages

Laboratory Manual ITE4

The document outlines two programming tasks focused on handling distinct numbers from user input. The first task requires creating a program that counts and displays distinct numbers from ten user inputs, while the second task involves writing a method to eliminate duplicates from an array and return a new array of distinct values. Both tasks include steps for understanding the problem, planning logic, writing Java code, and running sample programs.

Uploaded by

mabutijayboy
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)
3 views2 pages

Laboratory Manual ITE4

The document outlines two programming tasks focused on handling distinct numbers from user input. The first task requires creating a program that counts and displays distinct numbers from ten user inputs, while the second task involves writing a method to eliminate duplicates from an array and return a new array of distinct values. Both tasks include steps for understanding the problem, planning logic, writing Java code, and running sample programs.

Uploaded by

mabutijayboy
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/ 2

Program Title: Print distinct numbers

Problem:
Create a program that takes ten numbers, shows how many distinct numbers there are, and
arranges the numbers in the order they were entered, with exactly one space between each
number (such that if a number appears more than once, it is shown only once). (Hint: If a
number is new, read it and store it to an array; if it's already in the array, disregard it.) The array
then contains the different numbers.

Solution:
Step 1: Understand the Problem
You need to input 10 numbers from the user.
You must store only distinct numbers in an array.
After input, display:
Count of distinct numbers
The distinct numbers in input order (duplicates should not be stored).

Step 2: Plan the Logic


Create an array to store the distinct numbers.
Read 10 numbers from the user.
Check if each number is already in the array:
If not in the array, add it.
If already in the array, ignore it.
After input, count and display the distinct numbers.

Step 3: Write the Java Code

Step 4: Explanation of the Code


Scanner for user input → Scanner scanner = new Scanner(System.in);
Array to store distinct numbers → int[] distinctNumbers = new int[10];
Loop 10 times to read numbers:
Calls isInArray() to check if the number is already stored.
If it's new, store it and increase the count.
After input, print:
The count of distinct numbers.
The distinct numbers in input order.

Step 5: Run the Program

Sample Run:
Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct numbers is 6
The distinct numbers are: 1 2 3 6 4 5

Rueda Street, Calbayog City


063 055 5339857
Samar, Philippines 6710

[email protected] www.nwssu.edu.ph
Program Title: Eliminate duplicates

Problem:
Write a method that returns a new array by eliminating the duplicate values in the array using
the following method header:
public static int[] eliminateDuplicates(int[] list)
Write a test program that reads in 10 integers, invokes the method, and displays the distinct
numbers separated by exactly one space.

Solution:
Step 1: Understand the Problem
We need to read 10 numbers from the user.
We must create a method eliminateDuplicates(int[] list) that:
Takes an integer array as input.
Returns a new array with only distinct values (preserving input order).
The main program should:
Call eliminateDuplicates().
Display the distinct numbers.

Step 2: Plan the Logic


Create an eliminateDuplicates() method:
Store distinct numbers in a new array or ArrayList.
Check if a number is already stored; if not, add it.
Return the new array.
Read 10 numbers into an array.
Call the method to get the distinct numbers.
Print the distinct numbers.

Step 3: Write the Java Code

Step 4: Explanation of the Code


Read 10 numbers into an array:
int[] numbers = new int[10];
Use a loop to fill it.
Call eliminateDuplicates():
Uses an ArrayList<Integer> to store distinct values.
Iterates through list and adds only new values.
Converts ArrayList to an int[] and returns it.
Print the distinct numbers.

Step 5: Run the Program

Sample Run:
Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 6 4 5

Rueda Street, Calbayog City


063 055 5339857
Samar, Philippines 6710

[email protected] www.nwssu.edu.ph

You might also like