Return True If Array Contains 3 Adjacent Scores That Differ From Each Other

Mar 12, 2014

Assignment: Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}.

scoresClump({3, 4, 5}) → true
scoresClump({3, 4, 6}) → false
scoresClump({1, 3, 5, 5}) → true

Codingbat AP-1 3rd question

public boolean scoresClump(int[] scores) { 
for (int i = 0; i < scores.length; i++) {
int a = scores[i];
int b = scores[i + 1];
int c = scores[i + 2];
if (Math.abs(b-a) + Math.abs (c-b) <=2)
return true;
}
return false;
}

I got it right for some of the input, but not one of them. I tried to use the for loop and if statement on a specific input that I got wrong:

{4, 5, 8}
scores[1] - scores[0] = 1
scores[2] - scores[1] = 3

I suspect it has something to do with the for loop, but I don't see the problem with it. It should work, shouldn't it?
But anyway, here is the error for {4,5,8} :

Exception:java.lang.ArrayIndexOutOfBoundsException : 3 (line number:7)

View Replies


ADVERTISEMENT

Array Of Scores - Return True If Each Score Is Equal Or Greater Than One Before

Mar 8, 2014

Given an array of scores, return true if each score is equal or greater than the one before. The array will be length 2 or more.

scoresIncreasing({1, 3, 4}) → true
scoresIncreasing({1, 3, 2}) → false
scoresIncreasing({1, 1, 4}) → true

Java Code:

public boolean scoresIncreasing(int[] scores)
{
for (int i = 0; i< scores.length-1; i++) {
if (scores[i] < scores[i+1] || scores[i] == scores [i+1]) {
return true;
} else {
return false;
}
}
return false;
}

Unsure what the problem is, it will always return True for some reason...

View Replies View Related

Java Method - Return True If String Can Be Found As Element Of Array And False Otherwise

Mar 17, 2014

I am new to Java and would like to ask how to do this question specifically:

Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.

View Replies View Related

How To Return 2 Lowest Scores

Mar 19, 2015

If this method returns the lowest score, how do I return the 2 lowest scores.

For example If I input 1, 2, 3, 4, 5 this code will return 1.

How do i return the two lowest scores, for example if I input 1, 2, 3, 4, 5, I want to return 1, 2

public double getLowestScore() {
smallest = testScores[0];
double sum = 0.0;
double total = 0.0;
double Average = 0.0;

[Code] ....

View Replies View Related

How To Return Alert True If Server Is Functioning Normally

Apr 29, 2014

So, I don't know how to start off this program. I'm not looking for the final answer, but I don't know two things: 1. the objective, 2. what type of thing do I start it off with? A method? I'm pretty sure it's not a class right because it already has a class?

public class WebFarm
{
private ArrayList<Server> servers;
/* constructors and other methods and instance variables not shown */
}
 public class Server

[Code] ....

the Instructions:

The ping() method of the Server class returns true if the server is currently functioning normally; otherwise it returns false. Write a definition for the needsAttention() method of WebFarm that returns an ArrayList containing the servers that are not functioning normally:
 
public ArrayList<Server> needsAttention()
{
//I can put code here
}

View Replies View Related

2D Boolean Array - Gain A Value When All Elements Have Been Turned True

Feb 2, 2014

I'm creating a 4x4 2D array. I need to be notified, or gain a value when all elements have been turned true. Is this as simple as

"If the2DArray[] = true { blah blah }" ?

View Replies View Related

Program To Find Adjacent Value For -1 - No Output

Apr 25, 2015

I wrote a program that find adjacent value for -1 .ex:

input
1
3 3
1 2 2
1 1 -1
5 5 0

output
1

code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.util.Scanner;

[Code] ....

View Replies View Related

High Scores On Table - Shifting Elements In Array To The Right

Dec 7, 2014

I'm trying to write a program that asks a user how many high scores they want on a table, then the users types the inital highscores and is repeatedly asked to place more high scores on the table, which if larger than any existing high score, will take its place and shift the other scores down.

Although for the shifting and inserting of that next score the code just doesnt seem to be working, the insertScore function is where im getting the main exception, and im not sure why?

import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class HighScores {
public static void main(String[] args) {

[Code] .....

View Replies View Related

Button Colors Differ In XP Verses Windows 8

Apr 21, 2014

I created a GUI with NetBeans. When I run the program in XP and the program changes the button colors, the whole button changes color. In this case, from "background" to "Green".When I run the program under Windows 8 only the border of the button becomes green.

NetBeans seems to use the Look and Feel "nimbus". I single stepped through the program on both computers and it looks like a request for nimbus does not cause an exception on either computer.

View Replies View Related

Equals Method - Take Object Reference And Return True If Given Object Equals This Object

Mar 28, 2014

Create an equals method that takes an object reference and returns true if the given object equals this object.

Hint: You'll need 'instanceof' and cast to a (Geocache)

So far I have:

public boolean equals(Object O){
if(O instanceof Geocache){
Geocache j=(Geocache) O;
if (this.equals(j)) //I know this is wrong... but I can't figure it out
return true;
}

else return false;
}

I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?

View Replies View Related

Method Must Return Int Type - If Given Integer Is Strong Return A / If Not Return B

Sep 7, 2014

I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?

I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.

mlong should return an int depending on the X.moth. at the moment my code looks like this:

// File1:
public class date {
public int day;
public int month;
public int year;
}

// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}

View Replies View Related

Return Lowest Value From Array

Aug 12, 2014

I am trying to return the lowest value from the array. having trouble trying to capture the return value by placing it into the variable ..

int myLowest = getLowest(yourNumbers); and then printing out myLowest

import java.util.*;
public class numArrays
{
public static void main(String[] args){
int numbers;
int[]yourNumbers;

[code]....

View Replies View Related

How To Return Sum Of Multidimensional Array

Nov 21, 2014

what would the return be for adding two array's together,

public static int sum (int[][] a, int[][] b) {

int[] [] c = new int[a[0].length][b[0].length];
for (int i=0; i< a.length; i++){
for (int j=0; j<a[i].length; j++){
c[i][j] = a[i][j] + b[i][j];
}
}
return c[i][j];
}

My return is wrong...... I know, Any hints?

View Replies View Related

Return Value From Array To Main

Mar 11, 2014

I want to return values from arrays to the main and the problem is i cant use my variables from my constructor, I use new variables in my functions and i know this is no good, when I used the variables from the constructor in my function. I have a compilations errors,also i want to create un object in main and with this object i want to call the functions.

Java Code:

package javaapplication4;
import java.util.Scanner;
public class JavaApplication4 {
public static int[] MyInt;
public static double[] MyDouble;
public static String[] MyString;
public static char[] MyChar;

[Code] .....

The code is working when i run it and i have the right result in my screen but i know this is all wrong with the variables.

View Replies View Related

Error When Trying To Return Array Via Method

Jan 14, 2014

I am getting these errors with this code, I can not figure why this error is occuring.

what is the issue with this code?

Java Code: package ABC;
import java.util.ArrayList;
import java.util.Scanner;
public class GetInputFromUser {
private ArrayList<String> name = new ArrayList<String>();

[code]....

View Replies View Related

Return Array Contain All Object Name Of A Class

Mar 16, 2014

I need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retrieve them?

View Replies View Related

Return Array Containing All The Object Name Of A Class

Mar 16, 2014

I need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retrieve them?

View Replies View Related

Return Location And Value To Two-dimensional Array

Jan 14, 2015

I need to design a class named Location for locating a maximal value and its location in a two-dimensional array. The class should contain public data fields row, column, and maxValue that store the maximal value and its indices in a two dimensional array with row and column as int type and maxValue as double type.

I need to write the following method that returns the location of the largest element in a two-dimensional array:
public static location locateLargest(double[][] a)

The return value is an instance of Location. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array.

Here is what i get when I run

Enter the number of rows and columns of the array:
3 4
Enter the array: 23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at 00
The location of the largest element is at 01

But I need it to display this instead.

Enter the number of rows and columns of the array:
3 4
Enter the array: 23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is 45 at (1,2)

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Number of rows and columns: ");
int row = input.nextInt();
int col = input.nextInt();

[Code] ....

Enter the number of rows and columns of the array:
3 4
Enter the array: 23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at 00
The location of the largest element is at 01

I need it to display this instead.

Enter the number of rows and columns of the array:
3 4
Enter the array: 23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is 45 at (1,2)

View Replies View Related

Incrementing Array Index During Return Statement

Feb 14, 2015

I am trying to understand the following code.This return statement should actually return the char at myArray[index] first, then increments the index afterwords correct?

Public char next(){
return myArray[index++];
}

View Replies View Related

Neighbors Of 2D Array - Return SmartArray With Values

Feb 2, 2015

I have attempted on my own many times but I am not getting any closer to a solution.

/**

* Returns a SmartArray with eight values. The values are the values stored in the 8 neighbors of the array cell at the given location in the Smart2DArray.

* Start with the neighbor to the "north" of the given cell and proceed clockwise, using -1 as the value if the neighboring cell is outside the Smart2DArray.

For example, if the array is:
1 2 3
4 5 6
7 8 9

neighbors(1, 1) should return a SmartArray with the values:
2 3 6 9 8 7 4 1
in that order.

neighbors(2,1) should return a SmartArray with the values:
3 -1 -1 -1 9 8 5 2
in that order.

*/
public SmartArray neighbors (int col, int row) {
}

View Replies View Related

Return Array Containing All Strings That Start With Letter A

May 13, 2014

Write a method that accepts an array of Strings.  The method must return an array containing all strings that start with the letter 'A'.  

There must be no 'null' values in the resulting array.  The resulting array may be of size zero.

I don't think I am returning ALL the strings that start with A, just the last one of the for loop. Does this look somewhat correct?

public String[] startsWithA(String[] strings) {
String startsWithA = strings[0];
for(int i = 0; i < strings.length; i++) {
if(strings[i].charAt(0) == 'A') {
startsWithA = strings[i];
}
return startsWithA;
}
}

View Replies View Related

JSP :: How To Differ Between Fields That Not Exist To Fields That Are Null

Dec 7, 2014

how to differ between fields that are not exists to fields that are null? because in my api when someone wants to delete a field he sends null instead of a value. and if he doesnt want to effect this feild he doesnt send it.

{
"a" : {"1","2"},
"b" : "hello"
}
{
"a" : null,
"b" : "hello"
}
{
"b" : "hello"
}

View Replies View Related

Parallel Array Recall - Return Value Based On Input

Mar 8, 2015

I am having an issue with a parallel array that I created that returns array values based on an input. The code only works for the first four array locations just fine. The remaining values are not found. I thought it might be a memory allocation issue with the array size, I tried to dimension the array, however, using the Eclipse editor get errors.
 
import java.util.*;
import javax.swing.JOptionPane;
public class StudentIDArray {
static String[] studentNum = new String[]
{"1234", "2345", "3456", "4567", "5678", "6789", "7890", "8901", "9012", "0123"};
static String[] studentName = new String[]

[Code] .....

View Replies View Related

Return Array Data From A Method Back To Main

Mar 16, 2014

I'm trying to return an array back to main. The array returned to main should contain the reversed random numbers array. I believe I have the array correctly reversed within the 'reverseArray' method. I'm trying to send this array back to main, but it appears to contain empty data (a bunch of zeros).

Java Code:

class ArrToMain {
public static void main(String[] args) {
final int NUMBER_OF_ELEMENTS = 1000;
double[] numbers = new double [NUMBER_OF_ELEMENTS];
//Invoke initialize method
initialize(numbers);

[Code] ......

View Replies View Related

Unable To Return Array And It Is Printing Out Random Characters?

Dec 21, 2014

This is my code:

public class rotate
{
public static void main(String[] args)
{
int[] arrayInput = {1, 7, 8, 6, 2};
for(int i = 0; i<arrayInput.length; i++)

[Code]...

It's printing out this (the second line):

View Replies View Related

Find And Remove All Occurrences Of Given Integer And Return Amended Array

Jan 12, 2014

Question - Given an specific integer and an array of integers, find and remove all occurrences of the given integer and return an amended array. I solved it. Here is my solution -

public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 8, 8, 8, 7, 7, 9};
int input = 8;
int newLen = nums.length;
for(int i=0; i<newLen; i++){
if(nums[i] == input){

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved