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

Code linearsearch

The document contains Java code for a simple console application that validates user input, generates a random array, and performs a linear search for a specified key. It includes classes for validation, array handling, and searching, with error handling for input and array generation. The main method orchestrates the user interaction and displays the results of the search.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Code linearsearch

The document contains Java code for a simple console application that validates user input, generates a random array, and performs a linear search for a specified key. It includes classes for validation, array handling, and searching, with error handling for input and array generation. The main method orchestrates the user interaction and displays the results of the search.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Validator

package utils;

import java.util.Scanner;
import java.net.Socket;

public class Validator {

private static final Scanner SCANNER = new Scanner(System.in);

private Validator() {//constructer ngan ko cho khoi tao doi tuong

/**
*
* @param messageInfo message info want to enter number
* @param messageErrorOutOfRange range of enter number
* @param messageErrorNumber data type of enter number
* @param min min value of enter number
* @param max max value of enter number
* @return
*/
public static int getInt(String messageInfo, String messageErrorOutOfRange,
String messageErrorNumber, int min, int max) {
do {
try {//de bat loi nhap sai dinh dang
System.out.println(messageInfo);
int number = Integer.parseInt(SCANNER.nextLine());// chuyen chuoi so
"12345"thanh so 12345
if (number >= min && number <= max) {
return number;
} else {
System.out.println(messageErrorOutOfRange);
}
} catch (Exception e) {
System.out.println(messageErrorNumber);
}
} while (true);
}

}
package entity;
/**
*
* @author Dell
*/
public class LinearSearch {

private int[] array;


public LinearSearch(int[] array) {
this.array = array;
}

public int searchFirstIndex(int key) {


for (int i = 0; i < array.length; i++) {
if (array[i] == key) {
return i;
}
}
return -1;
}
}package ui;

import entity.Array;
import entity.LinearSearch;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.Validator;

public class Main {

public static void main(String[] args) {


int number = Validator.getInt("Enter number of array", "Please enter number
> 0",
"Please enter integer number", 1, Integer.MAX_VALUE);
int key = Validator.getInt("Enter search value", "Error range!",
"Please enter integer number", Integer.MIN_VALUE,
Integer.MAX_VALUE);

Array array = new Array(number);


int[] randomArray = null;
try {
randomArray = array.getRandomArray(number);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}

System.out.println("The array:" + array.toString());


LinearSearch linear = new LinearSearch(randomArray);
int index = linear.searchFirstIndex(key);
if (index == -1) {
System.out.println("Can not found key!");
} else {
System.out.println("Found " + key + " at index: " + index);
}

}
}package entity;

import java.util.Random;
public class Array {

private int[] array;


//constructer

public Array(int number) {


array = new int[number];
}

/*
if enter a number<0 throw excep
get a random array = integer < array.length
*/
public int[] getRandomArray(int number) throws Exception {
if (number <= 0) {
throw new Exception("number parameter must be >0");
}
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(array.length);
}
return array;
}
/*
toString to print out string start with digit" [ "and add number, if number -> add"
,"
when i = arraylength-1 add "]" when done return str.
*/
@Override
public String toString() {
String str = "[";
for (int i = 0; i < array.length; i++) {
str += array[i];
if (array.length - 1 == i) {
str += "]";
} else {
str += ", ";
}
}
return str;

You might also like