0% found this document useful (0 votes)
89 views

Kolej Matrikulasi Kedah

The program receives 10 price inputs from the user and saves them to a double array. It then prompts the user to input a search price and checks the array to see if the price is found. If found, it displays the index and increments a counter. If not found, it displays a message. It finally displays the total number of times the search price was found. The program is tested with different scenarios such as finding the price twice, once, and not finding it.

Uploaded by

siti aisyah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Kolej Matrikulasi Kedah

The program receives 10 price inputs from the user and saves them to a double array. It then prompts the user to input a search price and checks the array to see if the price is found. If found, it displays the index and increments a counter. If not found, it displays a message. It finally displays the total number of times the search price was found. The program is tested with different scenarios such as finding the price twice, once, and not finding it.

Uploaded by

siti aisyah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1|Page

KOLEJ MATRIKULASI KEDAH

SC025 COMPUTER SCIENCE 2

TITLE : ARRAY

NAME :NUR ANIS IZZATY HIDAYAH BINTI MANSOR

MATRIC NO : MS2018175630

PRACTICUM : DFT34

LECTURER : MADAM ZAINI BINTI MD AKHIR

SUBMITTED ON : 7 APRIL 2022


2|Page

TABLE OF CONTENT

INTRODUCTION………………………..……………………………………………3

PROBLEM STATEMENT……………………………………………………………4

PROBLEM ANALYSIS……………………………………………………………….5

DESIGN A SOLUTION……………………………………………………………..6-7

Pseudocode……………………………………………………………………………6
Flowchart………………………………………………………………………………7

IMPLEMENTATION………………………………………………………………8-9

TESTING………………………………………………………………………….10-12

The search price found twice with index……………………………………………..10


The search price found once with index………………………………………….......11
The search price was not found…………………………………………………...….12

DOCUMENTATION…………………………………………………………………13

REFERENCES………………………………………………………………………..14
3|Page

1.0 Introduction

An Array is an ordered indexed collection of data values of the same type. An array consists
of five (5) components which is data type, name, size, index and element. Additionally, the
elements of an array are stored in a contiguous memory location. It is a data structure where
we store similar elements. We can store only a fixed set of elements in a Java array. Array in
Java is index-based, the first element of the array is stored at the 0th index, 2nd element is
stored on 1st index and so on. To use an array, it must first be declared and created. Then
operations on an array can access elements in the array to do some operations on the array
such as entering data into array, displaying elements in array, find the sum of all elements,
find the average of elements, find the largest or smallest element, find the frequency of
elements in an array and searching for specific value in an array.
4|Page

2.0 Problem Statement

A program will receive inputs from the user and save them in a double type array name
price[]. The minimum data to be saved is ten (10).

This program also allows user to retrieve the saved data. User will enter the keyword (price)
to be searched in the array.

The program then will display the following information:

- an appropriate message if the element is found


- the index of found element
- an appropriate message if the element is not found.
5|Page

3.0 Problem Analysis

Input 10 prices, search price

Process Declaring and create array price to enter the 10 prices entered by
the user . Repeating read the prices for 10 prices and stored the
prices at the index. The user will enter the search price that he/she
want to find at the index and the message will display if the search
price was found at the index and calculate the total of search price
was found at the index. If the search was not found at the index the
message will display .

Output The message “ The price is found at the index” + i and The Total of
the price found at the index or the message “ The price is not
found”.
6|Page

4.0 Design a solution

4.1 Pseudocode

Start
Declare and create array price
i=0

for ( i < 10 )
Read price[i]
i=i+1
endfor

initialize user = false


Foundprice=0
Read searchprice

for ( i < 10 )
if ( searchprice == price [i])
user = true
print message “ Price is found at index” ,i
Foundprice = Foundprice + 1
endif
i=i+1
endfor
if ( user == false )
print message “Price is not found”
else
print Foundprice

Stop
7|Page

4.2 Flowchart

Start

Declare and create array price

i =0

False
Initialize user = false
i < 10
Foundprice=0
True

Read price [i]


Read
searchprice

i=i+1

User ==
i < 10
false
True False

True

True
Searchprice=
= price [i]
True
Print message “ The
price is not found” False

user = true

Print message “ Price


Print Foundprice
is found at index” , i

Foundprice = Foundprice + 1

Stop
i=i+1
8|Page

5.0 Implementation

//NUR ANIS IZZATY HIDAYAH BINTI MANSOR


//DFT34 <3
// A program will receive 10 input from the user and it will
display message " The price is found at the index " or the
message " The Price is not found ".
// program also display the total of the price found
import java.util.Scanner;
class ArrayPrice{ //class name
public static void main( String[]args){ //method main
Scanner sc=new Scanner (System.in); //creating objects for
user entered the input ( sc as object )

//declaring variables
//size of array is 10
double[]price=new double[10];

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

//Input statements
System.out.print ("Please enter the price : RM ");
price[i] = sc.nextDouble();
}

//Input statement which will display the message


System.out.print (" \nPlease enter the price you want to
search : RM ");

boolean user = false;


9|Page

double searchprice = sc.nextDouble();int Foundprice=0;

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

if ( searchprice == price[i]){

//display the message the price was found and index


price
System.out.println ( " \nThe price is found at the
index " + i);

user = true;
//processing statement that will calculate the
total of search price
Foundprice = Foundprice + 1;

}
}

if (user == false )

//display the message that the price was not find the
index
System.out.println ( " \nThe price is not found at
the index");
else
//display message for total of price found
System.out.print ( " \nTotal of the price found is "
+ Foundprice);

}
}
10 | P a g e

6.0 Testing

6.1 The search price found twice with index

 ----jGRASP exec: java ArrayPrice


Please enter the price : RM 10
Please enter the price : RM 10
Please enter the price : RM 20
Please enter the price : RM 20
Please enter the price : RM 20
Please enter the price : RM 30
Please enter the price : RM 40
Please enter the price : RM 50
Please enter the price : RM 60
Please enter the price : RM 70

Please enter the price you want to search : RM 10

The price is found at the index 0

The price is found at the index 1

Total of the price found is 2
 ----jGRASP: operation complete.
11 | P a g e

6.2 The search price was found once with index



 ----jGRASP exec: java ArrayPrice


Please enter the price : RM 100
Please enter the price : RM 200
Please enter the price : RM 300
Please enter the price : RM 400
Please enter the price : RM 500
Please enter the price : RM 600
Please enter the price : RM 700
Please enter the price : RM 800
Please enter the price : RM 900
Please enter the price : RM 1000

Please enter the price you want to search : RM 400

The price is found at the index 3

Total of the price found is 1
 ----jGRASP: operation complete.
12 | P a g e

6.3 The price was not found

 ----jGRASP exec: java ArrayPrice


Please enter the price : RM 111
Please enter the price : RM 112
Please enter the price : RM 113
Please enter the price : RM 114
Please enter the price : RM 115
Please enter the price : RM 116
Please enter the price : RM 117
Please enter the price : RM 118
Please enter the price : RM 119
Please enter the price : RM 120

Please enter the price you want to search : RM 200

Total of the price found is 0
The price is not found at the index

 ----jGRASP: operation complete.

13 | P a g e

7.0 Documentation – Comments

A program will receive 10 input from the user and the user will enter the search price that to
be searched at the index. It will display message "The price is found at the index" or the
message "The Price is not found ". The program also display the frequency of the price found
based on the search price. The class name is ArrayPrice. The method main in a Java Program
marks entry point of the program. The program execution begins with the first statement in
the method main, and ends with the last statement in it. Creating objects for user entered the
input (sc as object). Declaring variables because it can be used to store the prices that entered
by the user. The size of array is 10 because to allocate 10 consecutive memory location to
store 10 prices that entered by the user. Input statements price[i] used to accept the 10 prices.
The input statement which will display the message to read the search price the user want to
search at the index. The program will display the message the price was found and index
price if the price was found at the index. Processing statement that will calculate the total of
search price in the index number of prices. It will display message for total of price found
from the loop. If the search prices was not found in the index prices it will display the
message that the price was not find the index.
14 | P a g e

8.0 References

Ali, M. A. (2022). Problem Solving using Java Learn by Examples. In M. M. Zain.


Arrays in Jave. (2022, march 31). Retrieved from https://www.geeksforgeeks.org/arrays-in-
java/
java arrays. (n.d.). Retrieved from https://www.javatpoint.com/array-in-java#:~:text=Java
%20array%20is%20an%20object,elements%20in%20a%20Java%20array.
15 | P a g e
16 | P a g e

You might also like