import java.util.*;
public class tryAgain {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
//Creating array with Children object
Children[] ages = new Children[5];
//Creating new Children ages and adding em to counter
ages[0] = new Children(12);
ages[1] = new Children(10);
ages[2] = new Children (13);
ages[3] = new Children(9);
ages[4] = new Children(15);
sortArray(ages);
System.out.print("Enter the age that you want to search: ");
int val = sc.nextInt();
int res = binarySearch(ages, val);
System.out.println("Value found at index " res);
}
public static int binarySearch (Children[] ages, int value) {
int[] A = ages;
int n = A.length - 1; // size of an arraylist
int x = value; // value to be searched
boolean isFound = false;
int lowerBound = 1;
int upperBound = n;
int midPoint = 0;
while (isFound = true) {
if (upperBound < lowerBound) {
System.out.println("Searched value doesn't exists.");
}
midPoint = lowerBound ( upperBound - lowerBound) / 2;
if (lessThan(A[midPoint], x)) {
lowerBound = midPoint 1;
}
if (lessThan(x, A[midPoint])) {
lowerBound = midPoint - 1;
}
if (A[midPoint] == x) {
isFound = true;
return midPoint;
}
}
return midPoint;
}
public static void sortArray (Children[] ages) {
int n = ages.length;
for (int i = 1; i < n; i) {
int key = ages[i];
int j = i - 1;
while (j >= 0 && lessThan(key, ages[i])) {
ages[j 1] = ages[j];
j = j - 1;
}
ages[j 1] = key;
}
}
public static boolean lessThan(int x, int y) {
if (y == 0) {
return false;
}
if (x == 0) {
return true;
}
return lessThan(x - 1, y - 1);
}
static class Children {
int age = 0;
//constructor for setting Children ages
public Children (int age) {
this.age = age;
}
//getting ages of Children from constructor
public int getAge() {
return this.age;
}
//setting new age to children
public void setAge(int age) {
this.age = age;
}
}
}
It giving me an error like: Type mismatch: cannot convert from tryAgain.Children to int The method lessThan(int, int) in the type tryAgain is not applicable for the arguments (int, tryAgain.Children) Type mismatch: cannot convert from int to tryAgain.Children
at tryAgain.sortArray(tryAgain.java:63)
at tryAgain.main(tryAgain.java:19)
The aim is to add some values to the object array and then sort them with sorting method, and after that find the needed value with binary search.
CodePudding user response:
When you call this method, what are you passing to it?
lessThan(key, ages[i])
key
is an int
, and ages[i]
is a Children
(which is unnecessarily misleading, we'll get to that below). But what does the method expect?:
public static boolean lessThan(int x, int y)
An int
and an int
. So just as the error states, a Children
can not be converted to an int
. Presumably you meant to get the age from the Children
object:
lessThan(key, ages[i].getAge())
As an aside... Names are important. You've essentially confused yourself by giving your classes/variables/etc. misleading names.
What is a Children
? Is it a single instance of a "child"? Then why it is plural?
What is ages
? Is it a collection of child objects? Then why does it imply that it's just a collection of numeric values? (Which is exactly the bug you're trying to fix right now.)
Rename your Children
class to Child
, because it represents a child.
Rename your ages
variable to children
, because it represents a collection of children.
Name things by what they semantically represent and your code will be much easier for you to read and understand.