PDA

View Full Version : Java help (homework question)




prostuff1
Apr 25, 2006, 02:03 PM
This is the exact question that i need to complete, but i can sem to get all of it.

Write a program segment that reads a sequence of integers until a 0 is encountered. For each number read, output the number of times the number can be divided by 2 before you get 0. For example, if the input is:

2
9
-7
4
-3
0

The code will read in all the integers, and produce the following output:

2
4
3
3
2


I have gotten to the part were i can put in a string of numbers until I enter a 0 but as soon as that is done i hae problems. I think i need to do a nested while loop thing but cant figure out how to set the stuff all up.

If anyone can point me in the right direction and help me out it would be greatly appreciated!!



Doctor Q
Apr 25, 2006, 02:38 PM
Please post the code you have and we'll help you with it. We shouldn't simply do your assignment for you by writing it and posting it ourselves.

prostuff1
Apr 25, 2006, 03:08 PM
Please post the code you have and we'll help you with it. We shouldn't simply do your assignment for you by writing it and posting it ourselves.

Sorry about that. When i posted that first one i was away form the comptuer that had the code on it.

Here is the code i have:
import java.util.Scanner;

public class HW4 {

/**
* @param args
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean Integer = true;

while (Integer == true)
{
System.out.print("Enter an Integer: ");
int character = keyboard.nextInt();
int quotient = character/2;
int counter = 0;

if (character == 0)
{
Integer = false;
}
while (quotient > 0)
{
quotient = quotient/2;
counter = counter + 1;

if (quotient == 0)
{
quotient = 0;
}
}
System.out.println(counter);
}
}
}

Now this seems to work OK but if i read the direction correctly then i should be entering integer until i enter a zero. As it is now i can enter and it counts just fine but it prints the results right after i enter the number. I thnk i need to be able to enter numbers until i enter a 0 then it stops and computers the number.

SilentPanda
Apr 25, 2006, 03:18 PM
You need to store your initial numbers somewhere OR store the counters somewhere. For instance:

Please enter an Integer:
2
9
-7
4
-3
0

store these numbers in an Object of some sort then when 0 is encountered, calculate your "counter" on each of them and display it. I can't tell you for sure which Object to use (or even if an Object is a good choice for you) since I'm not sure what you've learned so far.

OR instead of storing the numbers above, calculate the "counter" and store those to be displayed once 0 is entered.

SIDE NOTE(s):
I would avoid using the variables "Integer" and "character". Integer is an Object in Java typically. Also while "character" is valid "Character" (with an uppercase) would be the same as "Integer". Use something like "IsInteger" and "LastNumber". I would personally have used something like "bIsInteger" and "cLastInput" but that's just preference. Declaring a boolean variable with the name "Integer" and an int variable with the name "character" could be confusing in a larger project.

Also look at this code again:
if (quotient == 0)
{
quotient = 0;
}

Doesn't really need to be there. :)

Another thing I realized is that "Integer" is not very representative. You want it to be true when the input is not 0, not when the input is an Integer. So you might even rename it further to "IsNotZero".

Also with booleans you don't really need to say "while (Integer == true)". You can just say "while (Integer)". If you change the variable name it would read "while (isNotZero)".

Sorry... not trying to be mean or anything just trying to point out some things to help you out in the long run! Good luck! :)

SC68Cal
Apr 25, 2006, 03:50 PM
My suggestion is going to depend on how strict your teacher is. If your teacher wants your code output to look EXACTLY like that question, meaning you wait untill all text has been put into the program before execution, instead of entering one input and getting one output, in your position I would have used an ArrayList, or an Array. Depending on which you feel more comfortable with. I just like Array Lists :-\

Pseudo-code:

if (userinput !=0)
{
ArrayList.add(userinput);
}
else
<ALGORITHM>

That above is a bit sketchy, it's been a bit since I've coded java and I'm just getting back into the swing of things, but it should send you on the right track

Doctor Q
Apr 25, 2006, 04:20 PM
I think that you already have the mostly correct program logic, prostuff1. It reads a number, computes a value (with a loop), and then outputs it.

There are two logic bugs that I see. First, your assignment is to count how many times you can divide by two before reaching zero, and they count the final division. For example, the input 9 should produce output 4, computed like this:

9 / 2 = 4 (count is 1)
4 / 2 = 2 (count is 2)
2 / 2 = 1 (count is 3)
1 / 2 = 0 (count is 4)

Compare with what your code does with the value 9:

quotient = 9/2 = 4
counter = 0

while (4 > 0) : TRUE
quotient = 4/2 = 2
counter = 1

while (2 > 0) : TRUE
quotient = 2/2 = 1
counter = 2

while (1 > 0) : TRUE
quotient = 1/2 = 0
counter = 3

while (0 > 0) : FALSE

So your answers come out one too small. There are a few ways to adjust your code to fix this.

The other bug is that you are to count until you get to zero, but your code is counting until it gets to less-than-or-equal-to zero. That gives you the wrong answers for negative input values.

Speak up if you have trouble fixing either of these problems.

prostuff1
Apr 25, 2006, 09:58 PM
OK thanks for the help so far!! I have fixed some of the stuff but i am still having trouble figuring out how to get it to work the way it is supposed to. I know what i need it to do but actualy making it do that is confusing me.

The only problem i have now is when i enter 0 it gives me 1 as the value back. I know why that is but i cant figure out how to fix it.

We have not learned about arrays or anything like that yet. So i dont think the professor wants us to use them. About the farest we have gone is these while loops and we just started learing about creating our own methods.

Here is what i changed int he code (not all that much):

import java.util.Scanner;
public class HW {

/**
* @param args
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean isNotZero = true;

while (isNotZero == true)
{
System.out.print("Enter an Integer: ");
int isInteger = keyboard.nextInt();
int quotient = isInteger/2;
int counter = 1;

if (isInteger == 0)
{
isNotZero = false;
}
while (quotient != 0)
{
quotient = quotient/2;
counter = counter + 1;
}
System.out.println(counter);
}
}
}

Doctor Q
Apr 26, 2006, 12:39 AM
Look at these two consecutive sections of code. First this:if (isInteger == 0)
{
isNotZero = false;
}and then this:while (quotient != 0)
{
quotient = quotient/2;
counter = counter + 1;
}
System.out.println(counter);The problem is that the second section of code is being executed all the time, and you want it to execute only if the if statement in the first section of code is false.

To fix that, you could turn the first if into an if-else. In other words, when isInteger is 0, you want to set isNotZero to false. Otherwise, you want to compute the proper counter and output it.

prostuff1
Apr 26, 2006, 02:15 AM
import java.util.Scanner;
public class HW {

/**
* @param args
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean isNotZero = true;
String s = "0 ";

while (isNotZero == true)
{
System.out.print("Enter an Integer: ");
int isInteger = keyboard.nextInt();

if (isInteger == 0)
{
isNotZero = false;
}
else
{
int counter = 0;

while (isInteger != 0)
{
isInteger = isInteger/2;
counter = counter + 1;
s = s + counter;
}
}
}
System.out.println(s + "\n");
}
}

I am getting closer but i am still not quite there. Now this code does almost exactly what i need. The only problem i have now is that the final line that prints out is not quite correct. It prints a whole string of numbers and the onlynumbers i need are before the number 1.

Basically this program asks for and integer and until i enter a 0 it keeps running. Of the non zero numbers i enter it is supposed to compute the number of times it can be divided by 2 then output that later. It needs to remember the previous calculations which is what i was having trouble making it do. well now i seem to have that but the output is not quite right.

If anyone could help me out and finish this up that would be great. I am so close but i have been looking at this so long that it is starting to hurt my head.

Thanks for the pushes and suggestions in the right direction so far it has help a lot.

Eraserhead
Apr 26, 2006, 06:54 AM
Try this

import java.util.Scanner;
public class HW {

/**
* @param args
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean isNotZero = true;
String s = "";

while (isNotZero == true)
{
System.out.print("Enter an Integer: ");
int isInteger = keyboard.nextInt();

if (isInteger == 0)
{
isNotZero = false;
}
else
{
int counter = 0;

while (isInteger != 0)
{
isInteger = isInteger/2;
counter = counter + 1;
}
s = s + "\n"+counter;
}
}
System.out.println(s);
}
}

I have added the "\n" to the s when it is made and taken it outside the second while loop so it only adds the answer to the string.

prostuff1
Apr 26, 2006, 09:09 AM
Try this

import java.util.Scanner;
public class HW {

/**
* @param args
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean isNotZero = true;
String s = "";

while (isNotZero == true)
{
System.out.print("Enter an Integer: ");
int isInteger = keyboard.nextInt();

if (isInteger == 0)
{
isNotZero = false;
}
else
{
int counter = 0;

while (isInteger != 0)
{
isInteger = isInteger/2;
counter = counter + 1;
}
s = s + "\n"+counter;
}
}
System.out.println(s);
}
}

I have added the "\n" to the s when it is made and taken it outside the second while loop so it only adds the answer to the string.

Thanks!!

I was actually thinking about moving that part outside the second while loop and what it would do. Again thanks for the help!! With it not being to far into the class it is soemtimes difficult to figure things out with only what you know. Especially when there are probably easier ways.

Eraserhead
Apr 26, 2006, 12:32 PM
Thanks!!

I was actually thinking about moving that part outside the second while loop and what it would do. Again thanks for the help!! With it not being to far into the class it is soemtimes difficult to figure things out with only what you know. Especially when there are probably easier ways.

Java's a bi*ch to learn to start with, I found objects EVIL to start with, though they're really useful later... I found a good book (Java 2 for Dummies) to be extremely useful for the basic's then you can work out most advanced stuff from the API (google <java class>+java to get it) or by asking on MacRumors.