I'm currently working on an assignment about java interface and I'm in the last 2 steps stuck there without knowing the exact problem here is the code I wrote
#Override
public boolean isNonDescending() {
double smallestElement = data[0];
for (int i = 0; i < data.length;) {
if (data[i] >= smallestElement) {
i++;
}
}
return true;
}
but when I submit my work to WebCat, it says this specific method is not working as it suppose to do, the main thing it should do is:
• isNonDescending: Returns a boolean value indicating whether the set of elements
is organized from smallest to largest (with equal elements being adjacent). Put
another way, a set of elements is nondescending if there is no smaller element that
comes after a larger element. For example, (1, 2, 2, 3, 4, 5) is nondescending while
(1, 2, 2, 3, 1, 5) is not, because a smaller element (1) came after a larger element (3).
the other method which are mentioned smallestElement in the method IsNonDescending if you need it:
#Override
public double smallestElement() {
double minElement = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] < minElement) {
minElement = data[i];
}
}
return minElement;
}
My question is where is the line with a wrong code or am i missing something?
Thanks in advance
Let's imagine I run your isNonDescending method on the following array : [ 1,2,0]
The if test is going to be wrong for the first element, so i will never be incremented and you will have an infinite loop. Plus, you never return false in your method. You should do this to fix it :
#Override
public boolean isNonDescending() {
// starting at 1 since we will be looking at data[i-1], which
// must start at 0. Alternatively, you can write
// for (int i=0 ; i < data.length - 1 ; i++) and work with data[i+1]
for (int i = 1 ; i < data.length ; i++)
if (data[i] > data[i-1]) return true;
return false;
}
public static boolean isNonDescending() {
for (int i = 1; i < data.length-1; i++) {
if (data[i-1] > data[i]) {
return false;
}
}
return true;
}
but I would name it isAscending(), nonDescending() doesn't describe your example correctly.
I think this is what you're looking for:
#Override
public static boolean isNonDescending() {
boolean success = true;
for (int i = 1; i < data.length - 1; i++) {
if (data[i - 1] > data[i]) {
success = false;
}
}
return success;
}
Hope it helps.
Clemencio Morales Lucas.
Related
I'm trying to compare the last names of two students in a directory, I'm doing this by overriding the compareTo method. The following code does not reflect exactly what I'm wanting to do here, the return value is just a placeholder for now. It is saying it won't compile because it cannot find the symbol for subString(int) in class String.
for (int i = 0; i < this.getLastName().length(); i++) {
if (!this.getLastName().subString(i).equals(s.getLastName().subString(i))) {
return 1;
}
}
Update: I appreciate y'all pointing out my idiocy in case sensitivity (no really, thanks) here's an update to where I'm at in the code. I think I can take it from here.
#Override
public int compareTo(Student s) {
for (int i = 0; i < this.getLastName().length(); i++) {
if (!this.getLastName().equals(s.getLastName())) {
for(int j = 0; j < this.getLastName().length() || j < s.getLastName().length(); j++) {
if (this.getLastName().charAt(j) < s.getLastName().charAt(j)) {
return 1;
}
else {
return -1;
}
}
}
}
looking at it again, I don't even need that first for loop.
and here's the finished method.
#Override
public int compareTo(Student s) {
if (!this.getLastName().equals(s.getLastName())) {
for(int j = 0; j < this.getLastName().length() || j < s.getLastName().length(); j++) {
if (this.getLastName().charAt(j) < s.getLastName().charAt(j)) {
return 1;
}
else if (this.getLastName().charAt(j) > s.getLastName().charAt(j)) {
return -1;
}
}
}
if (!this.getFirstName().equals(s.getFirstName())) {
for (int i = 0; i < this.getLastName().length() || i < s.getLastName().length(); i++) {
if (this.getFirstName().charAt(i) < s.getFirstName().charAt(i)) {
return 1;
}
else if (this.getFirstName().charAt(i) > s.getFirstName().charAt(i)) {
return -1;
}
}
}
return 0;
}
The simple way to compare two Java strings is to use the String.compareTo(String). You don't need to implement that yourself using loops. (Or at least, not in a real world context.)
If you want to order by lastname then firstname:
call `compareTo` on the last name string
- if the result is non zero, return it
- if the result is zero, compare the first name string.
I'll leave you to figure out the rest ... since this is clearly a "learning exercise".
Not sure why you want to reinvent the wheel. Stephen C was trying to point out that you can achieve the same result like this:
#Override
public int compareTo(Student that) {
int compare = this.getLastName().compareTo(that.getLastName());
if (compare == 0) {
compare = this.getFirstName().compareTo(that.getFirstName());
}
return compare;
}
Assume that I have an array with the following values: 0,1,0,0,0,1,0,1,1
I am currently looping over my array and replacing 1's with 0's. However I would to break out of this loop if there are 2 1's left in my array. I don't really have much in terms of code but this is a stub of what I've been working on
if(//There are more than 2 1s ){
return true; //carry on looping
}
return false; //break the loop
I have no idea how to differentiate between the 0's and the 1's and so I am quite confused with how to get this to work. Any ideas would be appreciated.
One possible solution is to start by writing a utility method to test if a given value at a specific position is unique from every subsequent position in the array like,
private static boolean testUnique(int[] arr, int i) {
int t = arr[i];
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] == t) {
return false;
}
}
return true;
}
Then you can iterate the array from the left to the right, checking if every value is unique like
public static boolean hasDuplicate(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (!testUnique(arr, i)) {
return false;
}
}
return true;
}
Using your array,
public static void main(String[] args) {
int[] arr = { 0, 1, 0, 0, 0, 1, 0, 1, 1 };
System.out.println(hasDuplicate(arr));
}
That is false. Alternatively, you might find it easier if you first sort your array.
public int[] testDuplicatesofOne(int[] arr)
{
int count=0;
for(int i=0;i<arr.length-1;i++)
{
count+=(arr[i]>0?1:0);
}
if(count>=2)
{
for(int j=0;j<arr.length-1;j++)
{
if(count>2)
{
if(arr[j]==1)
{
arr[j]=0;
count--;
}
}else
{
break;
}
}
}
}
Hi Lukasz try this, Sorry if I have not understood your requirement properly.
I am trying to write a program using two methods that determines if a sub array is located within an array. subArray() is supposed to receive two arrays and return the index of the start of the sub array within the array. If the sub array is not located in the array it returns -1. subArray() then calls subArrayAppearsAt() and passes in the two arrays and a location. subArrayAppearsAt() is supposed to return true if the sub array is located in the array starting at the location passed in, false otherwise.
Currently if I pass in array {1,2,3} and sub array {2,3}, it returns 2 as the starting position but it should return 1.
If I pass in array {1,2,3,4,5} and sub array {4}, it returns -1, but it should return 3.
Does anyone see why this might be happening?
public static int subArray(int [ ] array, int [ ] subArray )
{
boolean result=true;
int subArrayLength = subArray.length;
if (subArrayLength == 0) {
return -1;
}
int limit = array.length - subArrayLength;
int i;
for ( i = 0; i <= limit; i++)
result = subArrayAppearsAt(array, subArray, i );
if (result==true)
return i;
else
return -1;
}
public static boolean subArrayAppearsAt(int[] largeArray, int[] subArray, int i) {
{
if (subArray[0] == largeArray[i])
{
boolean subArrayFound = true;
for (int j = 1; j < subArray.length; j++)
{
if (subArray[j] != largeArray[i+j])
{
subArrayFound = false;
j=subArray.length;
}
/* Sub array found - return its index */
if (subArrayFound==true)
{
return true;
}
}
}
}
/* Return default value */
return false;
}
Look at this part
for ( i = 0; i <= limit; i++)
result = subArrayAppearsAt(array, subArray, i );
it sets result every time it goes through the loop. If you test if {4} is conatined in {1, 2, 3, 4, 5} it will set result to the return value of subArrayAppearsAt(array, subArray, 4); which will return false
So for that problem you could do something like
for ( i = 0; i <= limit; i++) {
result = subArrayAppearsAt(array, subArray, i );
if (result==true)
return i;
}
return -1;
The other problem is, that i will be incremented after it goes into the for-loop the last time, and then you return that value. That problem should be solved with my code solution too.
I didn't test my solution but it should work ;)
EDIT
Sorry that wasn't all correct. Your subArrayAppearsAt() returns true too early. Edit your subArrayAppearsAt() function to this and it should work
public static boolean subArrayAppearsAt(int[] largeArray, int[] subArray, int i)
{
if (subArray[0] == largeArray[i])
{
for (int j = 1; j < subArray.length; j++)
{
if (subArray[j] != largeArray[i+j])
{
return false;
}
}
return true;
}
return false;
}
The problem is that if you want to know the start position you should put the if that checks the result inside de loop
public static int subArray(int [ ] array, int [ ] subArray )
{
boolean result=true;
int subArrayLength = subArray.length;
if (subArrayLength == 0) {
return -1;
}
int limit = array.length - subArrayLength;
int i;
for ( i = 0; i <= limit; i++){
result = subArrayAppearsAt(array, subArray, i );
if (result==true)
return i;
}
return -1;
}
public static void main(String[] args) {
int[] first = {1,2,4,5,3,2,1,3,4,5,6,33,432,21,5};
int[] second = {2,1};
System.out.println(findpos(first, second));
}
private static int findpos(int[] a, int[] b){
for(int i=0; i<a.length; i++){
if(a[i]!=b[0]){
continue;
}
if(a.length - i < b.length) return -1;
int itemp = i;
boolean found = true;
for(int j=0; j<b.length; j++){
if(itemp < a.length && a[itemp]!=b[j]){
found = false;
}
itemp++;
}
if(found){
return i;
}
}
return -1;
}
If I want to compare index 0 with index 1, 2 and 3 for instance, how is that possible?
boolean iftrue = false;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < i; j++) {
if (IntValue(array[j]) == IntValue(array[i + j])) {
iftrue = true;
}
}
}
return iftrue;
}
Just to put Sotirios' suggestion into code... Recall he suggested you save the first elem and compare other elements against it.
public boolean sameAsFirstElem(int[] array) {
boolean isEqual = false;
int firstElem = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] == firstElem) {
return true;
}
}
return isEqual;
}
Take 0th element is an variable and go on searching from 1st index onward. If you find the match stop else go all the way upto end of the array and report that match not found. This can be done in linear time O(N) where N is the size of the array. No need to have two loops and hence increase the time complexity to O(N^2)
boolean sameAsFirstElem(Object[] array) {
boolean isEqual = false; //Assume match will not be there, if we come across any,
// we will set it to true
int firstElement = IntValue(array[0]);
for (int i = 1; i < array.length; i++) {
if (IntValue(array[i]) == firstElement) {
isEqual = true; //Indicating that match has found
}
}
return isEqual;
}
I am assuming that IntValue(Object) return int, and hence the int firstElement, and taking Object as a parameter.
I'm working on a method that finds the first instance of a given value and returns its position. It works for some cases, but if I give it an array of [1,2,3], and set the value to 2, it returns 0, instead of 1. I'm not sure why, either. Here is the code:
int b = 0;
for(int a = 0; a < values.length; a++) {
if (values[a] == find){
b++;
}
}
return b-1;
Thanks in advance!
Its because you are returning b-1. In fact, if you need to find the same instance and return the index, you wont even need the variable b. You could achieve this with something like this:
for( int a = 0; a < values.length; a++) {
if (values[a] == find){
return a;
}
}
return -1 // Notfound
}
Add the return -1 line for when a value is not found, to use as a sentinel value.
Try
for( int a = 0; a<values.length; a++) {
if (values[a] == find){
return a;
}
}
Why not return a itself instead of doing b-1;
Maybe you can add a break statement too to stop iterating as you just need the position of first instance
int b=0,result;
for( int a = 0; a<values.length; a++)
{
if (values[a] == find)
{
result=a;
break;
}
}
return result;