(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96
Greatest path is of length 10.
Now, the code that I have works, kind of. Instead of recurring several times at each point, it only recurs once.
So say I'm looking at (1,1). With (1,1) being 57. The area around it looks like this.
97 47 56
35 57 41
89 36 98
Now when I look at it, there are several paths it can take. It can go 57, 97 or 57, 89 or 57, 98. However, I'm pretty sure that it just uses the first one that corresponds with the first if statement that is valid. So I start checking north of the value, then northeast, then east, then southeast, which at southeast is where I find my first greater than value. After it finds it's first valid number, it then continues from that number, instead of checking if there are other longer paths stemming from the original value.
In conjunction with that, you can see that the printout just returns all paths from each value. Which isn't what I want. I need a way to store the longest current path, then check each path after to see if it's longer. If it is, it's replaced, if not, it stays the same.
I've also attached Ass1Q2_test4.txt
import java.util.*;
import java.io.*;
public class MaxIncreasingSub {
I am trying to find the longest descending sequence without arrays. So 65124976231 would output 9762.
import java.util.*; public class HelloWorld { public static void main(String[] args){ String num = ""; int longestLen = 0; int currLen = 0; String max = "";
[Code]...
I keep getting: The longest descending sequence is: 6512 In an infinite loop.
Consider the sequence of digits from 1 through N (N<=9) in increasing order: 1 2 3 4 N
Insert either a +(for addition) or a - (for subtraction) between each of the digits so that the resultant sum is zero. Print all possible combinations that sum to zero.
Example: Enter a number: 7 1+2-3+4-5-6+7=0 1+2-3-4+5+6-7=0 1-2+3+4-5+6-7=0 1-2-3-4-5+6+7=0
My problem is that I can't even run the program, because it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13 at domashno.ten.longestSortedSequence(ten.java:37) at domashno.ten.main(ten.java:17)
Code :
public static void main(String[] args) { int[] arr = {3, 8, 10, 1,9, 14, -3, 0, 14, 207, 56, 98, 12}; longestSortedSequence(arr); System.out.println(longestSortedSequence(arr)); } public static int longestSortedSequence(int[] arr) {
Below is a method that is suppose to insert a value inside a stack based on the index. I thought it would be easier to copy the value, if any, that was in the index into the variable holder and replace it with the input value. After in which it would copy the holder value and place it at the top of the stack. (topOfStack variable is what the holder is copying too.)
public void pushExact (int index, String input) { String holder = ""; if (maxSize == 0) { theStack[topOfStack] = input; topOfStack++; } else if (topOfStack + 1 < maxSize) { for (int n= maxSize - 1;n >= 0;n--) {
I am having problems with writing a simple program to see if a number is divisible by 6.
public void run() { println("This program will display all numbers divisible by"); println(" 6 between 1 and 100."); int x =1; boolean divide =(x%6==0);
[Code] ....
It is telling that every number is not divisable by 6?
Why this extremely simple program seems to be giving me a negative value for amount of calculations done within one minute ( Just using it as a bit of fun to see how different computers in the office perform).
Java Code:
class Benchmark { public static void main(String[] args) { long endTime = System.currentTimeMillis() + 60000; int count = 0; for (int i = 0; System.currentTimeMillis() < endTime; i++) { double x = Math.sqrt(System.currentTimeMillis()); count = i; } System.out.print(count + " calculations per minute"); } } mh_sh_highlight_all('java');
I am getting results between -2.1billion and -3.4billion which to me would make sense since they are not the best computers in the world but I would be expecting a positive value?
I have to find where in the fibonacci sequence a at number belongs, using a if or while loop.
Example
>55 is a Fibonacci number whose order in the sequence is 11 >35 is not a Fibonacci number. However, it lies between Fibonacci numbers 34 (order: 10) and 55 (order: 11)
import java.util.Scanner; public class While { public static void main(String[] args) { System.out.println("Welcome to the Fibonacci Sequence Detector"); Scanner in = new Scanner(System.in);
I have to find where in the fibonacci sequence a at number belongs, using a while loop.
Example
>55 is a Fibonacci number whose order in the sequence is 11 >35 is not a Fibonacci number. However, it lies between Fibonacci numbers 34 (order: 10) and 55 (order: 11)
import java.util.Scanner; public class While { public static void main(String[] args) { System.out.println("Welcome to the Fibonacci Sequence Detector");
I have some N*M matrix or N*N matrx , and there's a "worm" that can start from any index in the first column, or, any index in the first row. i need to choose the longest worm that satisfying this :
The index that comes after the index before him must be greater then 1. and i must use recursion, also for helper methods. no loops at all. that's it. i'm having a problem to find the longest worm in the matrix.
My idea was to create an helper array and assign to the array indexes a variable that will count the steps of the worm and finally assigns in to the helper array index. then i used findMax method to find the max value in an index. and thats will be the longest worm. i wrote a lot of code so i wont put it here. i will say that i'm close. if the longest worm is 6 i get in my output 7.
I have a given array of numbers, and I have to find the longest decreasing sub-array, but the thing is that the elements don't have to be next to each other!
For example, the longest decreasing sub-array for this array : 546 -156 165 -156 -56 -13 5
is 3 (check the bold numbers)
Until now, I have nearly finished my code, but I have one problem...
private static int decreasing(int[] a) { int result=1, temp=0, br=1; //Checking all the elements one by one: for(int i=0;i<a.length;i++){ temp=a[i]; //placing the element on a temp value
[Code] ....
The problem with this code is that it's not smart.. let's say I have : 100 -500 90 80 70
Once it hits -500, none of the other if's with pass....
i want a simple,beginner program to accept a string from the user and display the longest word in the string.i thought of taking the length of the string and choosing the longest string.but if the user enters a big paragraph,how can i store each word in a variable??
The array size is fixed improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array into it .I am using a course class and here is the code for the class
public class Course { private String courseName; private int numberOfStudents; private String[] students = new String[100]; public Course(String courseName)
[Code] ....
As you can see I have the array set to size 100, how do i make so it increments each time the user adds a student.
I'm working on an assignment that says the following.
" The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it.Implement the dropStudent method.Add a new method named clear() that removes all students from the course.
Write a test program that creates a course, adds three students, removes one, and displays the students in the course."
10.6 Listing
public class Course { private String courseName; private String[] students = new String[100]; private int numberOfStudents; } public Course(String courseName) { this.courseName= courseName;
[Code]...
My Test Code based off of book
public static void main(String[] args) { Course course1= new Course("Business"); course1.addStudent("Jay"); course1.addStudent("Silent Bob"); course1.addStudent("Dante"); course1.dropStudent("Jay");
[Code]....
My adjusted 10.6
public class Course { private String courseName; private String[] students = new String[100]; private int numberOfStudents; } public Course(String courseName) { this.courseName= courseName;
[Code]...
The problem I'm having is, for the first part of the question where I need to automatically increase the array size. I'm really not great at this stuff. I have tried breaking it down, but can't "get it", I guess.
I assume, it'd be a loop that checks to see if the student array is full and if so, do the increaseArray() part, by maybe multiplying the student array and then assigning it. I just don't know how to do it haha.
My *best* attempt at the loop so far has been
if (students == students.length){ int bigArray = 2*students.length; String increaseArray()= new String[students]; System.arraycopy(students, 0, increaseArray, 0, students.length); students= increaseArray;
I am trying to make a code that copies the users String to a char array. However, I am in a predicament: since I would not know the exact size of the users String I am left with the options of either making my array large by default, filled in with, lets say 25, empty spaces per index OR starting out with a default size of 1, with an empty space, and then some how increase the size from there.
At this moment I am leaning on the first option, creating a large default array and then deleting the unused values. However, this brings me to my actual question: if I set the non used indexes to null, if that wont give me an error, would that change the size of my array?
Ex: //lets say i finally copied all of the values and this is the result char[] word = {'b', 'o', 'b', ' ', ' '}; for(int i = word.length(); i > 0; i--){ if(word[i] == ' ')//delete the value so the size decreases word[i] = null;//if possible }
We are writing our own classes and methods. My instructor has provide the code
/*------------------------------------------------------------------------- import java.util.*; public class Assignment5 { public static void main (String[] args) {
[Code].....
I am having trouble with the question counter. I need the counter to increase when c, d or e are entered. I think I need to set up a if or while loop but I'm not sure how to setup the variables. This is what I have for the counter so far.
I am using a 64 bit Win 7 Pc with 64-bit JVM and we get the error: Java heap space. So we want to increase the Java heap size but not for one application but for every application or in general.
We tried with the java -xmx command but it didn't work...
We tried setting the system variable JAVA_OPTS but again it didn't work...
why am I not getting a proper shape of "Leftarrow"? The result should look like the following (the system draws the arrow abnormally, so the beginning should a real ARROW:
*
* *
* *
* * * * * * * * *
* *
* *
*
here is the class:
Java Code: public class LeftArrow extends ShapeBase { private int lengthOfTail; private int widthOfArrowHead;
So I need to print out the table of conversions from kilogram to pound and from pounds to kilograms. I think I have done a while loop correctly, but it is hard to actually check it since I do not have proper output format. I have tried also %4.2f format option however could not find the proper position in the print.
public static void main (String[] args){ System.out.printf("%10s%10s | %10s%10s ", "Kilograms", "Pounds", "Kilograms", "Pounds"); System.out.println("---------------------------------------------");
I have a signed certificate from Entrust which I used to sign a DeploymentRuleSet.xml file. I placed the DeploymentRuleSet.jar in the proper location C:WindowsSunJavaDeployment, afterward the java control panel's security tab shows a link to "show the active deployment rule set" which did not exist prior to coping the file to the directory. When I click on the link a new window opens and says "Rule Set not found" ....
Write a program that asks the user for an integer andthen prints out all its factors in increasing order. Use a class FactorGenerator with a constructor FactorGenerator(int numberToFactor) and methods nextFactor and hasMoreFactors. Supply a class FactorTester whose main methods reads a user input, constructs a FactorGenerator object and prints the factors.
Here is what I have so far for my main class and tester class
public class FactorGenerator { int factor; int number; int x; FactorGenerator(int numberToFactor) { number = numberToFactor; }