...

View Full Version : Homework help desperate!!!!



xellos16
12-04-2004, 02:25 AM
I am trying to get this project done for my CS class but I don't know if I am anywhere in the ball park. THis is what I have so far. I need to have a toDecimal, toBinary, and isBinary, that return true for a legal binary number and false other wise. Could someone take a look at this and see if they could help?



import java.io.*;
public class BinConverter
{
public static void main(String[] args)
{

System.out.println("Welcome to your third java program!!");
//next 2 lines are the setup for reading stuff in from the user
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(input);
String line = "s";
if (isBinary (line))
{
int num= toDecimal(s);
System.out.println(num);
String answer=toBinary (num);
System.out.println (answer);


}
else
{
System.out.println("Not Binary");
}
public static boolean isBinary (String s)
{
int l = s.length();
int check = 1;
for (int i = 0; i < l; i++)
{
if (s.charAt(i) == '1' || s.charAt(i) == '0')
{
else
} check = 0;
}
if (check == 0)
return false;

else
return true;
}
public static double toDecimal (String s)
{
int l = s.length();
double result = 0;
System.out.println("s=" + s);
System.out.println("l=" + l);
for (int i = 0; i < l; i++)
{
System.out.println("result before iteration=" + result);
System.out.println("s.charAt(i)=" + (double)s.charAt(i));
System.out.println("s.length() - i - 1=" + (s.length() - i - 1));
System.out.println("Math.pow(2, (s.length() - i - 1))=" + Math.pow(2, (s.length() - i - 1)));
System.out.println("adding " + (s.charAt(i) * Math.pow(2, (s.length() - i - 1))) + " to result");
result = result + s.charAt(i) * Math.pow(2, (s.length() - i - 1));
System.out.println("result after iteration=" + result);
}
System.out.println("final result=" + result);
return result;

}


public static String toBinary (int n)
{
if (n % 2==0)
return 0;
n--;
}
else
{
return 1;
n--;
n % n;
}
}
}




I have two errors: 25 : illegal start of expression static boolean isBinary (String s)
78 : ';' expected }

KeZZeR
12-04-2004, 02:48 AM
Personally i wouldn't give a variable a value of 0 if it was false. I'd just use a bool and say that once it has reached a character which isn't a 0 or a 1 then change the boolean value to false so it would return false. That should cut down the program quite a lot.

I'm not sure but i think you may have to add another modifier to your isBinary method, public static instead of just static. Not sure on that one though.



public static boolean isBinary(String s)
{
boolean check = true;
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) != '1' && s.charAt(i) != '0')
{
check = false;
}
}
return check;
}


I haven't got a clue if that's right, i'm first year CS as well but that's just an idea as it cuts down the code.

EDIT: It's just a way to cut down your code, if the if statement fails each time then it won't change the value of the variable check to false meaning that the value IS binary. Flame me if you will, it was just an idea :)

EDIT EDIT: Also, you shouldn't have to ask a forum how to do your homework. I do all my java programming for my CS course all by myself, i haven't asked one question about it on these forums yet, i class it as cheating, if you turn up to lectures and go to tutorials then you should know everything you need to know to complete the coursework. Also, the java API is a seriously handy resource, just a little imagination is needed (and read your java books!) :)

turbowrx
12-04-2004, 03:46 AM
A hint to help you with your problem. You have methods inside methods.

liorean
12-04-2004, 04:16 AM
EDIT EDIT: Also, you shouldn't have to ask a forum how to do your homework. I do all my java programming for my CS course all by myself, i haven't asked one question about it on these forums yet, i class it as cheating, if you turn up to lectures and go to tutorials then you should know everything you need to know to complete the coursework. Also, the java API is a seriously handy resource, just a little imagination is needed (and read your java books!) :)
Well, there's a difference: The unwritten rule is: We don't WRITE code for homework assignments. However, this is quite another case. He has evidently tried to make it work, he gets an error, and wants to find out what he's doing wrong. Telling him what he should think of and what mistakes he's making will help him become a better programmer. It's those that expect us to write the program for them that we don't help. Those that are really trying, those we do help.

xellos16
12-04-2004, 04:21 AM
methods inside methods? How?

turbowrx
12-04-2004, 04:27 AM
Well, your main method seems to encompass your other methods, for example the isBinary method.

turbowrx
12-04-2004, 04:29 AM
There seems to be other brackets in need of fixing as well.

turbowrx
12-04-2004, 04:31 AM
And what is that while loop doing at the end of your code?

xellos16
12-04-2004, 04:31 AM
Do you think that that my be due to a missed bracket somewhere, or did I just screw up something?

turbowrx
12-04-2004, 04:33 AM
You also have a problem in your main method with the variable s. You never create it.

You also have a duplicate num variable.

Well, it looks like you may have misplaced the bracket at the end. Just create and delete brackets where you need them.

There are more too. You'll come up on them as you get the bracket situation fixed. Just work your way through it and I'll help you if you need it.

Looks like you're going to need some work on your toDecimal method as well.

xellos16
12-04-2004, 04:51 AM
I deleted the num varible at the top because I don't think I need it :confused: For the toBinary statement. The while loop at the bottom goes to the string toBinary statement as well. Do I need to change the variable 's' from a 2 of the statments. And I think I called the 'S' statment at the top, I am probably wrong though.

turbowrx
12-04-2004, 05:02 AM
I'm thinking maybe your s variable in main should be line, and you just put s because that is what you set line to. You are trying to send s to the isBinary method, but there is no s to send.

I don't see where the while loop at the bottom does anything.

xellos16
12-04-2004, 05:16 AM
I updated the file. How do I get rid of that illegal start? ANd a tthe buttom of the page, it says that I am missing a semi-colon. How do I check for that?

turbowrx
12-04-2004, 05:29 AM
I think your original 2 errors had to do with the fact that your main method was bracketed correctly, which is what the illegal start is. You can't call the isBinary method if it's not outside the main method. Your main method needs to end.

Why don't you repost your code as well.

xellos16
12-04-2004, 05:32 AM
Ok, I did that. This is where I am at now.


import java.io.*;
public class BinConverter
{
public static void main(String[] args)
{

System.out.println("Welcome to your third java program!!");
//next 2 lines are the setup for reading stuff in from the user
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(input);
String line = "s";
if (isBinary (line))
{
int num= toDecimal(s);
System.out.println(num);
String answer=toBinary (num);
System.out.println (answer);


}
else
{
System.out.println("Not Binary");
}
}
public static boolean isBinary (String s)
{
int l = s.length();
int check = 1;
for (int i = 0; i < l; i++)
{
if (s.charAt(i) == '1' || s.charAt(i) == '0')
{}
else
check = 0;
}
if (check == 0)
return false;

else
return true;
}
public static double toDecimal (String s)
{
int l = s.length();
double result = 0;
System.out.println("s=" + s);
System.out.println("l=" + l);
for (int i = 0; i < l; i++)
{
System.out.println("result before iteration=" + result);
System.out.println("s.charAt(i)=" + (double)s.charAt(i));
System.out.println("s.length() - i - 1=" + (s.length() - i - 1));
System.out.println("Math.pow(2, (s.length() - i - 1))=" + Math.pow(2, (s.length() - i - 1)));
System.out.println("adding " + (s.charAt(i) * Math.pow(2, (s.length() - i - 1))) + " to result");
result = result + s.charAt(i) * Math.pow(2, (s.length() - i - 1));
System.out.println("result after iteration=" + result);
}
System.out.println("final result=" + result);
return result;

}


public static String toBinary (int n)
{
if (s % 2==0)
return 0;
s--;
}
else
{
return 1;
s--;
s % 2;
}
}





With these two errors
C:\Program Files\Xinox Software\JCreator LE\MyProjects\dd\BinConverter.java:71: illegal start of type
else
^
C:\Program Files\Xinox Software\JCreator LE\MyProjects\dd\BinConverter.java:76: <identifier> expected
}

turbowrx
12-04-2004, 05:39 AM
You need 2 more brackets in your toBinary method. Your if has nothing on it and it needs one because you have multiple statements after it. Also, you cannot have more statements after you return, so you will see that error as well.

Also, you are going to want to do an s= s% 2. I believe that is the other error.

xellos16
12-04-2004, 05:43 AM
Like this?


public static String toBinary (int s)
{
if (s % 2==0){
return 0;
s--;
}

}
else
{
return 1;
s--;
s % 2;

}
}

turbowrx
12-04-2004, 05:47 AM
A little more like this. The other problem here that I changed is that you are returning a string, yet in your code you were returning an int.



public static String toBinary (int s)
{
if (s % 2==0){
s--;
return "0";
}
else
{
s--;
s = s % 2;
return "1";
}
}

xellos16
12-04-2004, 05:53 AM
A couple of more errors.... :confused: :(

C:\Program Files\Xinox Software\JCreator LE\MyProjects\dd\BinConverter.java:16: cannot resolve symbol
symbol : variable s
location: class BinConverter
int num= toDecimal(s);
^
C:\Program Files\Xinox Software\JCreator LE\MyProjects\dd\BinConverter.java:16: possible loss of precision
found : double
required: int
int num= toDecimal(s);
^
2 errors

Process completed.

turbowrx
12-04-2004, 05:58 AM
Just change the s to line. It looks like you only changed it in the one spot. All references to s in the main method should be changed to line.

The other problem you are having has to do with the fact that you are returning a double to an int. Can't do that.

You have
int blah = some double being returned ----- can't do it

xellos16
12-04-2004, 06:11 AM
So basically this:


{

System.out.println("Welcome to your third java program!!");
//next 2 lines are the setup for reading stuff in from the user
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(input);
String line = "s";
if (isBinary (line))
{
int num= toDecimal(line);
System.out.println(num);
String answer=toBinary (num);
System.out.println (answer);


}
else
{
System.out.println("Not Binary");
}

and what int am I supposed to change? the one in the main and thats it?

turbowrx
12-04-2004, 06:17 AM
You could cast, but I would suggest just changing the double values in toDecimal. You need to return an int, which requires 2 changes to the toDecimal method.

xellos16
12-04-2004, 06:23 AM
I know I'ma pain, if I had any money I would pay you!!! :D

So this is what ou are talkinging about....


public static double toDecimal (String s)
{
int l = s.length();
double result = 0;
System.out.println("s=" + s);
System.out.println("l=" + l);
for (int i = 0; i < l; i++)

and doing this?


public static int toDecimal (String s)
{
int l = s.length();
num result = 0;
System.out.println("s=" + s);
System.out.println("l=" + l);
for (int i = 0; i < l; i++)

turbowrx
12-04-2004, 06:26 AM
Changing the return type to int -> correct.

Changing the double to num, -> bad.

You now have num result, which is nothing and will cause errors. result is a new variable which means you should assign it to be an int. Get it now?

xellos16
12-04-2004, 06:31 AM
public static int toDecimal (String s)
{
int l = s.length();
int result = 0;
System.out.println("s=" + s);
System.out.println("l=" + l);
for (int i = 0; i < l; i++)

turbowrx
12-04-2004, 06:33 AM
Correct. I believe you are now good with your syntax, but you need some work on your algorithms. Run it and see what happens.

xellos16
12-04-2004, 06:35 AM
like always you are correct:

C:\Documents and Settings\lab\Desktop\Mary.java:58: possible loss of precision
found : double
required: int
result = result + s.charAt(i) * Math.pow(2, (s.length() - i - 1));
^
should I change result to answer?

turbowrx
12-04-2004, 06:43 AM
This goes back to us changing the doubles to ints. Those doubles gave you more precision, but were hurting you elsewhere. If everything can go back to a double, that would be best.

This means that you need to take toBinarys input and change it to double, change the return and result in toDecimal to double, and change num to a double in main. This should fix everything, but you'll still have algorithm work because i don't think you'll get the answers you want.

xellos16
12-04-2004, 06:54 AM
now I'm getting this C:\Documents and Settings\lab\Desktop\Mary.java:18: ')' expected
String answer=toBinary (double);

turbowrx
12-04-2004, 07:00 AM
Remember, you cannot send double somewhere. You have to send the variable. You did change num to a double, correct? num is the variable you want to send. You cannot send double, int, string, or etc. You have to send the object that you created of that type. Follow.

xellos16
12-04-2004, 07:04 AM
No I don't follow. :confused: I changed the toBinary to a num and double but still got the same error.



public static String toBinary (num s)
{
if (s % 2==0){
s--;
return "0";
}
else
{
s--;
s = s % 2;
return "1";
}
}
}



public static String toBinary (double s)
{
if (s % 2==0){
s--;
return "0";
}
else
{
s--;
s = s % 2;
return "1";
}
}
}

turbowrx
12-04-2004, 07:19 AM
Yeah, that's all fine.

Here is your problem.
String answer=toBinary (double);

You are trying to pass double into the toBinary method. You need to pass the variable num, which is of type double.

you have a method signature like
public String returnName( String name ){
return name;
}
You are specifying this as a public method, saying it returns a string, and accepts a string. Somewhere else i may call this method like this - >

String me = "John";
String name = returnName(me);
name in this case is ready to accept the return string, which is why i typed it string...i pass the returnName method the variable me I can't pass return name a type like int, string, double...I need to pass an object of that type, which me is.

Hope this helped.

turbowrx
12-04-2004, 07:28 AM
Well, going to bed now. Check back in the morning. Later.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum