Consider Sequence Of Digits From 1 Through N In Increasing Order
Sep 3, 2014
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
View Replies
ADVERTISEMENT
Jan 28, 2015
Okay so I need to be able to read a file and then be able to find the longest sequence of increasing numbers.
So if the file was this, (this is Ass1Q2_test4.txt)
97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24
the output should be,
(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 {
[Code].....
View Replies
View Related
Nov 18, 2014
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;
}
[Code]....
View Replies
View Related
Mar 21, 2014
For example, if i am given 9864
Imust output 4689
Without use of arrays.
View Replies
View Related
Feb 2, 2015
Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.
import javax.swing.JOptionPane;
public class DigitsAndSum {
public static void main (String[] args) {
String str1;
String str2;
int int1 = 0;
int int2 = 0;
[Code] ....
View Replies
View Related
Mar 1, 2014
One of the random number generators in Java extract the higher-order bits of the random number in order to get a longer period.
I'm not sure if I understand how this is done. Suppose that the random number r = 0000 1100 1000 1101. If we extract the 16 most significant bits from r; is the new number r = 0000 1100 or r = 0000 1100 0000 0000?
View Replies
View Related
Jan 26, 2014
Any link to the accurate explanation of binary trees in java?
View Replies
View Related
Dec 8, 2014
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.
View Replies
View Related
Feb 5, 2014
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;
but,yeah... Doesn't seem to be right.
View Replies
View Related
Jul 11, 2014
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
}
View Replies
View Related
Feb 15, 2015
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.
[public int getNumberOfQuestions(){
numQuestions = 0;
numQuestions ++;
return numQuestions;
}
View Replies
View Related
Nov 21, 2013
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...
View Replies
View Related
Aug 29, 2014
In class we were give the algorithm for a non-decreasing algorithm here:
This is pseudo code given for the non-decreasing.
for j = 2 to A.length
key = A[j]
i = j - 1
while i > 0 and A[i] > key
A[i+1] = A[i]
i = i -1
A[i = 1] = key
//I was asked to make it non-increasing, so this is what I have.
for j = A.length - 1 to 1
key = A[j]
i = j - 1
while i > 0 and A[i] < key
A[i+1] = A[i]
i = i + 1
A[i-1] = key
Is there anything wrong with this algorithm? Will it give me the non-increasing sort?
View Replies
View Related
Jan 30, 2015
I have to seperate a number 9876 to 6 7 8 9, to 9 8 7 6. I need to know how to sum the digits. I have gotten to this point ::
import java.util.*;
public class week4program
{
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
int num1;
int digit;
int sum = 0;
[code]....
To this point its gives me the seperate integers. OK but how do I get the variable integers into seperate containers and then add them up?My assignment is to do that, and what I have above gets me to where I have seperate digits, but how do I catch them into seperate entities to be added to a sum?
View Replies
View Related
Oct 14, 2014
I have it so it gives me the sum of any digit, i just cant figure out how to get only the odd digits to add up. for example if I were to type 123. The odd numbers = 4 but using this program i get 6
class SumOfDigits {
public static void main(String args[]) {
int sum = 0;
System.out.println("Enter multi digit number:");
[Code] ....
View Replies
View Related
Feb 3, 2014
import java.util.Scanner;
public class CubesSum {
public static void main (String [] args){
int input;
System.out.println("Enter a positive integer:");
Scanner in = new Scanner(System.in);
input = in.nextInt();
As you can see I'm using a while loop. For part B which is to modify to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits. So for example, 371 = 3³+7³+1³. How to do it? I need to wrap a for loop around my while loop...
View Replies
View Related
Mar 28, 2014
I know my problem is my lack of understanding of OOP but I have searched for a solution and can't even seem to get close.This is a very simplified version of what I will need to do. I have a button and six textboxes. When the button is pressed I want to take the text from textbox6 and place it in textbox1, textbox2, textbox3, textbox4, and textbox5.
I need sometthing better then this brut force method.Java Code:
package textboxes;
public class TextBoxesGUI extends javax.swing.JFrame {
public String NewText;
public TextBoxesGUI() {
initComponents();
[code]....
View Replies
View Related
Oct 13, 2014
Digit sum of an integer is the sum of all its digits. For example, the digit sum of 123 is 1+2+3 = 6
and the digit sum of 99 is 9 + 9 = 18.
But how to write the programm that will count those numbers?
View Replies
View Related
Aug 29, 2014
Make a program which takes input from the user (6-digit number) and check how many times each digit gets repeated in the number. Java GUI was used (Netbeans 6.5.1).This is my code. I took the input via a JTextField named tf and displayed the output on JTextArea ta.
- arrays not used as we haven't studied them yet
int num= Integer.parseInt(tf.getText());
int no=num;
if(num<100000)
ta.setText("Invalid Input. Please Enter 6 digit number");
int n1= num%10;
num=num/10; //stores last digit
[code]....
How do I omit the last 5 lines? This problem occurs whenever I input a digit with any recurring digit.What changes/additions should I make?
View Replies
View Related
Feb 4, 2015
The following code splits an integer into separate digits. It then adds 7 to each of those digits, and then finds the remainder when the result is divided by ten. To do this it uses the Modulus operator.
public class manipulateIntegers {
public manipulateIntegers() {
System.out.println("Enter a four digit Integer");
String y = System.console().readLine();
int[] list = new int[5];
[code].....
I understand completely about what modulus does vis-à-vis fetching the remainder, but I don't understand why using the modulus operator gets you each digit individually?
for example, this snippet splits the digits of an integer - but how??
int number; // = some int
while (number > 0) {
print( number % 10);
number = number / 10;
}
and why does number have to be reassigned as number / 10?
View Replies
View Related
Jan 14, 2014
How can i make a method that makes sure that a String only contains digits and "-" (dash) ... (I want to make sure that the method can see if the user has written in an okay telephone number...)
This is what I've done this far.. But the method retuns false if you type - or +
static public boolean telephoneValidation(JTextField tf) {
boolean svar;
try {
Integer.parseInt(tf.getText());
svar = true;
}
catch(NumberFormatException fel)
[code]...
View Replies
View Related
Apr 25, 2014
why exactly this isn't printing out the correct way.I'm supposed to print out 100 elements from an array but assign them random integers between 0 and 9.I've tried troubleshooting the issue and when I do it seems i'm only getting one element assigned.
public class Exercise_6_7{
public static void main(String[] args){
int[] counts = new int[100];
for(int i=0; i<counts.length; i++){
counts[i] = (int)(Math.random()*10);
[code]....
View Replies
View Related
Jun 12, 2014
I what to check if value is not equal to CharSequence value
I try this but getting error:
Java Code:
private CharSequence testvalue = "";
if (!isEqualTo("BCDEF".contains(testvalue))){
}
public boolean isEqualTo(String s_src, String s_compareTo) {
boolean flag = false;
[Code] .....
View Replies
View Related
Mar 23, 2014
Fibonacci Sequence Code:
public class FibSeqByIanNeumann {
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int ctr, num1, num2, fib, maxTimes;
System.out.print("How many sequences do you want?: ");
maxTimes = get.nextInt(); //inputs the maxium limit of the fib sequence
[code]....
how to do a simple YES/NO Loop so I can try to have it work on my code if I want to try to do the Fibonacci Sequence again.Now I think it might have something to do with a do/while loop.
View Replies
View Related
Sep 17, 2014
I want to declare integers, while the program is running.
I run the program and then I give it via System.in.println an integer and repeat this as long as I want. I want the program to give those integers a name of a certain type for, for example a(i) or a[i], dunno, (it should be handy) and then a(i) represents the the i'th integer I gave the program. My idea is then that I can use those elements by their name just like, if I had declared them in the first place. For example add two integers together. For example I defined a method add+, which waits for 2 integer and then adds them. For example I write:
add
a(2)
a(47)
(then I would get here the result.)
However I don't know, how to let the program count the number of inputs or how to let it declare and use variables.
View Replies
View Related
Sep 17, 2014
I want to declare integers, while the program is running.
I run the program and then I give it via System.in.println an integer and repeat this as long as I want. I want the program to give those integers a name of a certain type for, for example a(i) or a[i], dunno, (it should be handy) and then a(i) represents the the i'th integer I gave the program. My idea is then that I can use those elements by their name just like, if I had declared them in the first place. For example add two integers together. For example I defined a method add+, which waits for 2 integer and then adds them. For example I write:
add
a(2)
a(47)
(then I would get here the result.)
I don't think implementing the add function is difficult. However I don't know, how to let the program count the number of inputs or how to let it declare and use variables.
View Replies
View Related